capture.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #define grub_video_render_target grub_video_fbrender_target
  2. #include <grub/video.h>
  3. #include <grub/video_fb.h>
  4. #include <grub/mm.h>
  5. #include <grub/misc.h>
  6. static struct
  7. {
  8. struct grub_video_mode_info mode_info;
  9. struct grub_video_render_target *render_target;
  10. grub_uint8_t *ptr;
  11. } framebuffer;
  12. void (*grub_video_capture_refresh_cb) (void);
  13. static grub_err_t
  14. grub_video_capture_swap_buffers (void)
  15. {
  16. if (grub_video_capture_refresh_cb)
  17. grub_video_capture_refresh_cb ();
  18. return GRUB_ERR_NONE;
  19. }
  20. static grub_err_t
  21. grub_video_capture_set_active_render_target (struct grub_video_render_target *target)
  22. {
  23. if (target == GRUB_VIDEO_RENDER_TARGET_DISPLAY)
  24. target = framebuffer.render_target;
  25. return grub_video_fb_set_active_render_target (target);
  26. }
  27. static grub_err_t
  28. grub_video_capture_fini (void)
  29. {
  30. return GRUB_ERR_NONE;
  31. }
  32. static struct grub_video_adapter grub_video_capture_adapter =
  33. {
  34. .name = "Render capture",
  35. .prio = 0,
  36. .id = GRUB_VIDEO_ADAPTER_CAPTURE,
  37. .fini = grub_video_capture_fini,
  38. .get_info = grub_video_fb_get_info,
  39. .get_info_and_fini = 0,
  40. .set_palette = grub_video_fb_set_palette,
  41. .get_palette = grub_video_fb_get_palette,
  42. .set_viewport = grub_video_fb_set_viewport,
  43. .get_viewport = grub_video_fb_get_viewport,
  44. .set_region = grub_video_fb_set_region,
  45. .get_region = grub_video_fb_get_region,
  46. .set_area_status = grub_video_fb_set_area_status,
  47. .get_area_status = grub_video_fb_get_area_status,
  48. .map_color = grub_video_fb_map_color,
  49. .map_rgb = grub_video_fb_map_rgb,
  50. .map_rgba = grub_video_fb_map_rgba,
  51. .unmap_color = grub_video_fb_unmap_color,
  52. .fill_rect = grub_video_fb_fill_rect,
  53. .blit_bitmap = grub_video_fb_blit_bitmap,
  54. .blit_render_target = grub_video_fb_blit_render_target,
  55. .scroll = grub_video_fb_scroll,
  56. .swap_buffers = grub_video_capture_swap_buffers,
  57. .create_render_target = grub_video_fb_create_render_target,
  58. .delete_render_target = grub_video_fb_delete_render_target,
  59. .set_active_render_target = grub_video_capture_set_active_render_target,
  60. .get_active_render_target = grub_video_fb_get_active_render_target,
  61. .next = 0
  62. };
  63. static struct grub_video_adapter *saved;
  64. static struct grub_video_mode_info saved_mode_info;
  65. grub_err_t
  66. grub_video_capture_start (const struct grub_video_mode_info *mode_info,
  67. struct grub_video_palette_data *palette,
  68. unsigned int palette_size)
  69. {
  70. grub_err_t err;
  71. grub_memset (&framebuffer, 0, sizeof (framebuffer));
  72. grub_video_fb_init ();
  73. framebuffer.mode_info = *mode_info;
  74. framebuffer.mode_info.blit_format = grub_video_get_blit_format (&framebuffer.mode_info);
  75. framebuffer.ptr = grub_malloc (framebuffer.mode_info.height * framebuffer.mode_info.pitch);
  76. if (!framebuffer.ptr)
  77. return grub_errno;
  78. err = grub_video_fb_create_render_target_from_pointer (&framebuffer.render_target,
  79. &framebuffer.mode_info,
  80. framebuffer.ptr);
  81. if (err)
  82. return err;
  83. err = grub_video_fb_set_active_render_target (framebuffer.render_target);
  84. if (err)
  85. return err;
  86. err = grub_video_fb_set_palette (0, palette_size, palette);
  87. if (err)
  88. return err;
  89. saved = grub_video_adapter_active;
  90. if (saved)
  91. {
  92. grub_video_get_info (&saved_mode_info);
  93. if (saved->fini)
  94. saved->fini ();
  95. }
  96. grub_video_adapter_active = &grub_video_capture_adapter;
  97. return GRUB_ERR_NONE;
  98. }
  99. void *
  100. grub_video_capture_get_framebuffer (void)
  101. {
  102. return framebuffer.ptr;
  103. }
  104. void
  105. grub_video_capture_end (void)
  106. {
  107. grub_video_fb_delete_render_target (framebuffer.render_target);
  108. grub_free (framebuffer.ptr);
  109. grub_video_fb_fini ();
  110. grub_video_adapter_active = saved;
  111. if (saved)
  112. {
  113. if (saved->init)
  114. saved->init ();
  115. if (saved->setup)
  116. saved->setup (saved_mode_info.width, saved_mode_info.height, 0, 0);
  117. }
  118. }