efi_selftest_bitblt.c 8.1 KB

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