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