Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heinrich Schuchardt | 68e7f2d | 2018-02-07 22:14:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * efi_selftest_bitblt |
| 4 | * |
| 5 | * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | 68e7f2d | 2018-02-07 22:14:23 +0100 | [diff] [blame] | 7 | * Test the block image transfer in the graphical output protocol. |
| 8 | * An animated submarine is shown. |
| 9 | */ |
| 10 | |
| 11 | #include <efi_selftest.h> |
| 12 | |
| 13 | #define WIDTH 200 |
| 14 | #define HEIGHT 120 |
| 15 | #define DEPTH 60 |
| 16 | |
| 17 | static const struct efi_gop_pixel BLACK = { 0, 0, 0, 0}; |
| 18 | static const struct efi_gop_pixel RED = { 0, 0, 255, 0}; |
| 19 | static const struct efi_gop_pixel ORANGE = { 0, 128, 255, 0}; |
| 20 | static const struct efi_gop_pixel YELLOW = { 0, 255, 255, 0}; |
| 21 | static const struct efi_gop_pixel GREEN = { 0, 255, 0, 0}; |
| 22 | static const struct efi_gop_pixel DARK_BLUE = {128, 0, 0, 0}; |
| 23 | static const struct efi_gop_pixel LIGHT_BLUE = {255, 192, 192, 0}; |
| 24 | |
| 25 | static struct efi_boot_services *boottime; |
| 26 | static efi_guid_t efi_gop_guid = EFI_GOP_GUID; |
| 27 | static struct efi_gop *gop; |
| 28 | static struct efi_gop_pixel *bitmap; |
| 29 | static struct efi_event *event; |
| 30 | static efi_uintn_t xpos; |
| 31 | |
| 32 | static void ellipse(efi_uintn_t x, efi_uintn_t y, |
| 33 | efi_uintn_t x0, efi_uintn_t y0, |
| 34 | efi_uintn_t x1, efi_uintn_t y1, |
| 35 | const struct efi_gop_pixel col, struct efi_gop_pixel *pix) |
| 36 | { |
| 37 | efi_uintn_t xm = x0 + x1; |
| 38 | efi_uintn_t ym = y0 + y1; |
| 39 | efi_uintn_t dx = x1 - x0 + 1; |
| 40 | efi_uintn_t dy = y1 - y0 + 1; |
| 41 | |
| 42 | if (dy * dy * (2 * x - xm) * (2 * x - xm) + |
| 43 | dx * dx * (2 * y - ym) * (2 * y - ym) <= dx * dx * dy * dy) |
| 44 | *pix = col; |
| 45 | } |
| 46 | |
| 47 | static void rectangle(efi_uintn_t x, efi_uintn_t y, |
| 48 | efi_uintn_t x0, efi_uintn_t y0, |
| 49 | efi_uintn_t x1, efi_uintn_t y1, |
| 50 | const struct efi_gop_pixel col, struct efi_gop_pixel *pix) |
| 51 | { |
| 52 | if (x >= x0 && y >= y0 && x <= x1 && y <= y1) |
| 53 | *pix = col; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Notification function, copies image to video. |
| 58 | * The position is incremented in each call. |
| 59 | * |
| 60 | * @event notified event |
| 61 | * @context pointer to the notification count |
| 62 | */ |
| 63 | static void EFIAPI notify(struct efi_event *event, void *context) |
| 64 | { |
| 65 | efi_uintn_t *pos = context; |
| 66 | efi_uintn_t dx, sx, width; |
| 67 | |
| 68 | if (!pos) |
| 69 | return; |
| 70 | |
| 71 | /* Increment position */ |
| 72 | *pos += 5; |
| 73 | if (*pos >= WIDTH + gop->mode->info->width) |
| 74 | *pos = 0; |
| 75 | |
| 76 | width = WIDTH; |
| 77 | dx = *pos - WIDTH; |
| 78 | sx = 0; |
| 79 | if (*pos >= gop->mode->info->width) { |
| 80 | width = WIDTH + gop->mode->info->width - *pos; |
| 81 | } else if (*pos < WIDTH) { |
| 82 | dx = 0; |
| 83 | sx = WIDTH - *pos; |
| 84 | width = *pos; |
| 85 | } |
| 86 | |
| 87 | /* Copy image to video */ |
| 88 | gop->blt(gop, bitmap, EFI_BLT_BUFFER_TO_VIDEO, sx, 0, dx, DEPTH, |
Heinrich Schuchardt | 5f86d33 | 2018-03-14 19:57:02 +0100 | [diff] [blame] | 89 | width, HEIGHT, WIDTH * sizeof(struct efi_gop_pixel)); |
Heinrich Schuchardt | 68e7f2d | 2018-02-07 22:14:23 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Setup unit test. |
| 94 | * |
| 95 | * @handle: handle of the loaded image |
| 96 | * @systable: system table |
| 97 | * @return: EFI_ST_SUCCESS for success |
| 98 | */ |
| 99 | static int setup(const efi_handle_t handle, |
| 100 | const struct efi_system_table *systable) |
| 101 | { |
| 102 | efi_status_t ret; |
| 103 | struct efi_gop_pixel pix; |
| 104 | efi_uintn_t x, y; |
| 105 | |
| 106 | boottime = systable->boottime; |
| 107 | |
| 108 | /* Create event */ |
| 109 | ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, |
| 110 | TPL_CALLBACK, notify, (void *)&xpos, |
| 111 | &event); |
| 112 | if (ret != EFI_SUCCESS) { |
| 113 | efi_st_error("could not create event\n"); |
| 114 | return EFI_ST_FAILURE; |
| 115 | } |
| 116 | |
| 117 | /* Get graphical output protocol */ |
| 118 | ret = boottime->locate_protocol(&efi_gop_guid, NULL, (void **)&gop); |
| 119 | if (ret != EFI_SUCCESS) { |
| 120 | gop = NULL; |
| 121 | efi_st_printf("Graphical output protocol is not available.\n"); |
| 122 | return EFI_ST_SUCCESS; |
| 123 | } |
| 124 | |
| 125 | /* Prepare image of submarine */ |
| 126 | ret = boottime->allocate_pool(EFI_LOADER_DATA, |
| 127 | sizeof(struct efi_gop_pixel) * |
| 128 | WIDTH * HEIGHT, (void **)&bitmap); |
| 129 | if (ret != EFI_SUCCESS) { |
| 130 | efi_st_error("Out of memory\n"); |
| 131 | return EFI_ST_FAILURE; |
| 132 | } |
| 133 | for (y = 0; y < HEIGHT; ++y) { |
| 134 | for (x = 0; x < WIDTH; ++x) { |
| 135 | pix = DARK_BLUE; |
| 136 | |
| 137 | /* Propeller */ |
| 138 | ellipse(x, y, 35, 55, 43, 75, BLACK, &pix); |
| 139 | ellipse(x, y, 36, 56, 42, 74, LIGHT_BLUE, &pix); |
| 140 | |
| 141 | ellipse(x, y, 35, 75, 43, 95, BLACK, &pix); |
| 142 | ellipse(x, y, 36, 76, 42, 94, LIGHT_BLUE, &pix); |
| 143 | |
| 144 | /* Shaft */ |
| 145 | rectangle(x, y, 35, 73, 100, 77, BLACK, &pix); |
| 146 | |
| 147 | /* Periscope */ |
| 148 | ellipse(x, y, 120, 10, 160, 50, BLACK, &pix); |
| 149 | ellipse(x, y, 121, 11, 159, 59, YELLOW, &pix); |
| 150 | ellipse(x, y, 130, 20, 150, 40, BLACK, &pix); |
| 151 | ellipse(x, y, 131, 21, 149, 49, DARK_BLUE, &pix); |
| 152 | rectangle(x, y, 135, 10, 160, 50, DARK_BLUE, &pix); |
| 153 | ellipse(x, y, 132, 10, 138, 20, BLACK, &pix); |
| 154 | ellipse(x, y, 133, 11, 139, 19, RED, &pix); |
| 155 | |
| 156 | /* Rudder */ |
| 157 | ellipse(x, y, 45, 40, 75, 70, BLACK, &pix); |
| 158 | ellipse(x, y, 46, 41, 74, 69, ORANGE, &pix); |
| 159 | ellipse(x, y, 45, 80, 75, 109, BLACK, &pix); |
| 160 | ellipse(x, y, 46, 81, 74, 108, RED, &pix); |
| 161 | |
| 162 | /* Bridge */ |
| 163 | ellipse(x, y, 100, 30, 120, 50, BLACK, &pix); |
| 164 | ellipse(x, y, 101, 31, 119, 49, GREEN, &pix); |
| 165 | ellipse(x, y, 140, 30, 160, 50, BLACK, &pix); |
| 166 | ellipse(x, y, 141, 31, 159, 49, GREEN, &pix); |
| 167 | rectangle(x, y, 110, 30, 150, 50, BLACK, &pix); |
| 168 | rectangle(x, y, 110, 31, 150, 50, GREEN, &pix); |
| 169 | |
| 170 | /* Hull */ |
| 171 | ellipse(x, y, 50, 40, 199, 109, BLACK, &pix); |
| 172 | ellipse(x, y, 51, 41, 198, 108, LIGHT_BLUE, &pix); |
| 173 | |
| 174 | /* Port holes */ |
| 175 | ellipse(x, y, 79, 57, 109, 82, BLACK, &pix); |
| 176 | ellipse(x, y, 80, 58, 108, 81, LIGHT_BLUE, &pix); |
| 177 | ellipse(x, y, 83, 61, 105, 78, BLACK, &pix); |
| 178 | ellipse(x, y, 84, 62, 104, 77, YELLOW, &pix); |
| 179 | /* |
| 180 | * This port hole is created by copying |
| 181 | * ellipse(x, y, 119, 57, 149, 82, BLACK, &pix); |
| 182 | * ellipse(x, y, 120, 58, 148, 81, LIGHT_BLUE, &pix); |
| 183 | * ellipse(x, y, 123, 61, 145, 78, BLACK, &pix); |
| 184 | * ellipse(x, y, 124, 62, 144, 77, YELLOW, &pix); |
| 185 | */ |
| 186 | ellipse(x, y, 159, 57, 189, 82, BLACK, &pix); |
| 187 | ellipse(x, y, 160, 58, 188, 81, LIGHT_BLUE, &pix); |
| 188 | ellipse(x, y, 163, 61, 185, 78, BLACK, &pix); |
| 189 | ellipse(x, y, 164, 62, 184, 77, YELLOW, &pix); |
| 190 | |
| 191 | bitmap[WIDTH * y + x] = pix; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return EFI_ST_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Tear down unit test. |
| 200 | * |
| 201 | * @return: EFI_ST_SUCCESS for success |
| 202 | */ |
| 203 | static int teardown(void) |
| 204 | { |
| 205 | efi_status_t ret; |
| 206 | |
| 207 | if (bitmap) { |
| 208 | ret = boottime->free_pool(bitmap); |
| 209 | if (ret != EFI_SUCCESS) { |
| 210 | efi_st_error("FreePool failed\n"); |
| 211 | return EFI_ST_FAILURE; |
| 212 | } |
| 213 | } |
| 214 | if (event) { |
| 215 | ret = boottime->close_event(event); |
| 216 | event = NULL; |
| 217 | if (ret != EFI_SUCCESS) { |
| 218 | efi_st_error("could not close event\n"); |
| 219 | return EFI_ST_FAILURE; |
| 220 | } |
| 221 | } |
| 222 | return EFI_ST_SUCCESS; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Execute unit test. |
| 227 | * |
| 228 | * @return: EFI_ST_SUCCESS for success |
| 229 | */ |
| 230 | static int execute(void) |
| 231 | { |
| 232 | u32 max_mode; |
| 233 | efi_status_t ret; |
| 234 | struct efi_gop_mode_info *info; |
| 235 | |
| 236 | if (!gop) |
| 237 | return EFI_ST_SUCCESS; |
| 238 | |
| 239 | if (!gop->mode) { |
| 240 | efi_st_error("EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE missing\n"); |
| 241 | return EFI_ST_FAILURE; |
| 242 | } |
| 243 | info = gop->mode->info; |
| 244 | max_mode = gop->mode->max_mode; |
| 245 | if (!max_mode) { |
| 246 | efi_st_error("No graphical mode available\n"); |
| 247 | return EFI_ST_FAILURE; |
| 248 | } |
| 249 | |
| 250 | /* Fill background */ |
| 251 | ret = gop->blt(gop, bitmap, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0, |
| 252 | info->width, info->height, 0); |
| 253 | if (ret != EFI_SUCCESS) { |
| 254 | efi_st_error("EFI_BLT_VIDEO_FILL failed\n"); |
| 255 | return EFI_ST_FAILURE; |
| 256 | } |
| 257 | |
| 258 | /* Copy image to video */ |
| 259 | ret = gop->blt(gop, bitmap, EFI_BLT_BUFFER_TO_VIDEO, 0, 0, 0, DEPTH, |
| 260 | WIDTH, HEIGHT, 0); |
| 261 | if (ret != EFI_SUCCESS) { |
| 262 | efi_st_error("EFI_BLT_BUFFER_TO_VIDEO failed\n"); |
| 263 | return EFI_ST_FAILURE; |
| 264 | } |
| 265 | |
| 266 | /* Copy left port hole */ |
| 267 | ret = gop->blt(gop, bitmap, EFI_BLT_VIDEO_TO_VIDEO, |
| 268 | 79, 57 + DEPTH, 119, 57 + DEPTH, |
| 269 | 31, 26, 0); |
| 270 | if (ret != EFI_SUCCESS) { |
| 271 | efi_st_error("EFI_BLT_VIDEO_TO_VIDEO failed\n"); |
| 272 | return EFI_ST_FAILURE; |
| 273 | } |
| 274 | |
| 275 | /* Copy port holes back to buffer */ |
| 276 | ret = gop->blt(gop, bitmap, EFI_BLT_VIDEO_TO_BLT_BUFFER, |
| 277 | 94, 57 + DEPTH, 94, 57, |
Heinrich Schuchardt | 5f86d33 | 2018-03-14 19:57:02 +0100 | [diff] [blame] | 278 | 90, 26, WIDTH * sizeof(struct efi_gop_pixel)); |
Heinrich Schuchardt | 68e7f2d | 2018-02-07 22:14:23 +0100 | [diff] [blame] | 279 | if (ret != EFI_SUCCESS) { |
| 280 | efi_st_error("EFI_BLT_VIDEO_TO_BLT_BUFFER failed\n"); |
| 281 | return EFI_ST_FAILURE; |
| 282 | } |
| 283 | |
| 284 | /* Set 250ms timer */ |
| 285 | xpos = WIDTH; |
| 286 | ret = boottime->set_timer(event, EFI_TIMER_PERIODIC, 250000); |
| 287 | if (ret != EFI_SUCCESS) { |
| 288 | efi_st_error("Could not set timer\n"); |
| 289 | return EFI_ST_FAILURE; |
| 290 | } |
| 291 | |
| 292 | con_out->set_cursor_position(con_out, 0, 0); |
| 293 | con_out->set_attribute(con_out, EFI_WHITE | EFI_BACKGROUND_BLUE); |
| 294 | efi_st_printf("The submarine should have three yellow port holes.\n"); |
| 295 | efi_st_printf("Press any key to continue"); |
| 296 | efi_st_get_key(); |
| 297 | con_out->set_attribute(con_out, EFI_LIGHTGRAY); |
| 298 | efi_st_printf("\n"); |
| 299 | |
| 300 | return EFI_ST_SUCCESS; |
| 301 | } |
| 302 | |
| 303 | EFI_UNIT_TEST(bitblt) = { |
| 304 | .name = "block image transfer", |
| 305 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 306 | .setup = setup, |
| 307 | .execute = execute, |
| 308 | .teardown = teardown, |
| 309 | .on_request = true, |
| 310 | }; |