efi_selftest_bitblt.c 8.2 KB

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