VirtIOFrameBufferDevice.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Graphics/GraphicsManagement.h>
  7. #include <Kernel/Graphics/VirtIOGPU/VirtIOFrameBufferDevice.h>
  8. #include <LibC/sys/ioctl_numbers.h>
  9. namespace Kernel::Graphics {
  10. VirtIOFrameBufferDevice::VirtIOFrameBufferDevice(VirtIOGPU& virtio_gpu, VirtIOGPUScanoutID scanout)
  11. : BlockDevice(29, GraphicsManagement::the().allocate_minor_device_number())
  12. , m_gpu(virtio_gpu)
  13. , m_scanout(scanout)
  14. {
  15. if (display_info().enabled) {
  16. Locker locker(m_gpu.operation_lock());
  17. create_framebuffer();
  18. }
  19. }
  20. VirtIOFrameBufferDevice::~VirtIOFrameBufferDevice()
  21. {
  22. }
  23. void VirtIOFrameBufferDevice::create_framebuffer()
  24. {
  25. auto& info = display_info();
  26. size_t buffer_length = page_round_up(calculate_framebuffer_size(info.rect.width, info.rect.height));
  27. // First delete any existing framebuffers to free the memory first
  28. m_framebuffer = nullptr;
  29. m_framebuffer_sink_vmobject = nullptr;
  30. // 1. Allocate frame buffer
  31. m_framebuffer = MM.allocate_kernel_region(buffer_length, String::formatted("VirtGPU FrameBuffer #{}", m_scanout.value()), Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  32. auto write_sink_page = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::No).release_nonnull();
  33. auto num_needed_pages = m_framebuffer->vmobject().page_count();
  34. NonnullRefPtrVector<PhysicalPage> pages;
  35. for (auto i = 0u; i < num_needed_pages; ++i) {
  36. pages.append(write_sink_page);
  37. }
  38. m_framebuffer_sink_vmobject = AnonymousVMObject::create_with_physical_pages(move(pages));
  39. // 2. Create BUFFER using VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
  40. if (m_resource_id.value() != 0)
  41. m_gpu.delete_resource(m_resource_id);
  42. m_resource_id = m_gpu.create_2d_resource(info.rect);
  43. // 3. Attach backing storage using VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
  44. m_gpu.ensure_backing_storage(*m_framebuffer, buffer_length, m_resource_id);
  45. // 4. Use VIRTIO_GPU_CMD_SET_SCANOUT to link the framebuffer to a display scanout.
  46. m_gpu.set_scanout_resource(m_scanout.value(), m_resource_id, info.rect);
  47. // 5. Render our test pattern
  48. draw_ntsc_test_pattern();
  49. // 6. Use VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D to update the host resource from guest memory.
  50. transfer_framebuffer_data_to_host(info.rect);
  51. // 7. Use VIRTIO_GPU_CMD_RESOURCE_FLUSH to flush the updated resource to the display.
  52. flush_displayed_image(info.rect);
  53. info.enabled = 1;
  54. }
  55. VirtIOGPURespDisplayInfo::VirtIOGPUDisplayOne const& VirtIOFrameBufferDevice::display_info() const
  56. {
  57. return m_gpu.display_info(m_scanout);
  58. }
  59. VirtIOGPURespDisplayInfo::VirtIOGPUDisplayOne& VirtIOFrameBufferDevice::display_info()
  60. {
  61. return m_gpu.display_info(m_scanout);
  62. }
  63. size_t VirtIOFrameBufferDevice::size_in_bytes() const
  64. {
  65. auto& info = display_info();
  66. return info.rect.width * info.rect.height * sizeof(u32);
  67. }
  68. void VirtIOFrameBufferDevice::flush_dirty_window(VirtIOGPURect const& dirty_rect)
  69. {
  70. m_gpu.flush_dirty_window(m_scanout, dirty_rect, m_resource_id);
  71. }
  72. void VirtIOFrameBufferDevice::transfer_framebuffer_data_to_host(VirtIOGPURect const& rect)
  73. {
  74. m_gpu.transfer_framebuffer_data_to_host(m_scanout, rect, m_resource_id);
  75. }
  76. void VirtIOFrameBufferDevice::flush_displayed_image(VirtIOGPURect const& dirty_rect)
  77. {
  78. m_gpu.flush_displayed_image(dirty_rect, m_resource_id);
  79. }
  80. bool VirtIOFrameBufferDevice::try_to_set_resolution(size_t width, size_t height)
  81. {
  82. if (width > MAX_VIRTIOGPU_RESOLUTION_WIDTH || height > MAX_VIRTIOGPU_RESOLUTION_HEIGHT)
  83. return false;
  84. auto& info = display_info();
  85. Locker locker(m_gpu.operation_lock());
  86. info.rect = {
  87. .x = 0,
  88. .y = 0,
  89. .width = (u32)width,
  90. .height = (u32)height,
  91. };
  92. create_framebuffer();
  93. return true;
  94. }
  95. int VirtIOFrameBufferDevice::ioctl(FileDescription&, unsigned request, FlatPtr arg)
  96. {
  97. REQUIRE_PROMISE(video);
  98. switch (request) {
  99. case FB_IOCTL_GET_SIZE_IN_BYTES: {
  100. auto* out = (size_t*)arg;
  101. size_t value = size_in_bytes();
  102. if (!copy_to_user(out, &value))
  103. return -EFAULT;
  104. return 0;
  105. }
  106. case FB_IOCTL_SET_RESOLUTION: {
  107. auto* user_resolution = (FBResolution*)arg;
  108. FBResolution resolution;
  109. if (!copy_from_user(&resolution, user_resolution))
  110. return -EFAULT;
  111. if (!try_to_set_resolution(resolution.width, resolution.height))
  112. return -EINVAL;
  113. resolution.pitch = pitch();
  114. if (!copy_to_user(user_resolution, &resolution))
  115. return -EFAULT;
  116. return 0;
  117. }
  118. case FB_IOCTL_GET_RESOLUTION: {
  119. auto* user_resolution = (FBResolution*)arg;
  120. FBResolution resolution;
  121. resolution.pitch = pitch();
  122. resolution.width = width();
  123. resolution.height = height();
  124. if (!copy_to_user(user_resolution, &resolution))
  125. return -EFAULT;
  126. return 0;
  127. }
  128. case FB_IOCTL_FLUSH_BUFFERS: {
  129. FBRects user_dirty_rects;
  130. if (!copy_from_user(&user_dirty_rects, (FBRects*)arg))
  131. return -EFAULT;
  132. if (Checked<unsigned>::multiplication_would_overflow(user_dirty_rects.count, sizeof(FBRect)))
  133. return -EFAULT;
  134. for (unsigned i = 0; i < user_dirty_rects.count; i++) {
  135. FBRect user_dirty_rect;
  136. if (!copy_from_user(&user_dirty_rect, &user_dirty_rects.rects[i]))
  137. return -EFAULT;
  138. if (m_are_writes_active) {
  139. VirtIOGPURect dirty_rect {
  140. .x = user_dirty_rect.x,
  141. .y = user_dirty_rect.y,
  142. .width = user_dirty_rect.width,
  143. .height = user_dirty_rect.height
  144. };
  145. flush_dirty_window(dirty_rect);
  146. }
  147. }
  148. return 0;
  149. }
  150. default:
  151. return -EINVAL;
  152. };
  153. }
  154. KResultOr<Region*> VirtIOFrameBufferDevice::mmap(Process& process, FileDescription&, const Range& range, u64 offset, int prot, bool shared)
  155. {
  156. REQUIRE_PROMISE(video);
  157. if (!shared)
  158. return ENODEV;
  159. if (offset != 0)
  160. return ENXIO;
  161. if (range.size() != page_round_up(size_in_bytes()))
  162. return EOVERFLOW;
  163. // We only allow one process to map the region
  164. if (m_userspace_mmap_region)
  165. return ENOMEM;
  166. auto vmobject = m_are_writes_active ? m_framebuffer->vmobject().clone() : m_framebuffer_sink_vmobject;
  167. if (vmobject.is_null())
  168. return ENOMEM;
  169. auto result = process.space().allocate_region_with_vmobject(
  170. range,
  171. vmobject.release_nonnull(),
  172. 0,
  173. "VirtIOGPU Framebuffer",
  174. prot,
  175. shared);
  176. if (result.is_error())
  177. return result;
  178. m_userspace_mmap_region = result.value();
  179. return result;
  180. }
  181. void VirtIOFrameBufferDevice::deactivate_writes()
  182. {
  183. m_are_writes_active = false;
  184. if (m_userspace_mmap_region) {
  185. auto* region = m_userspace_mmap_region.unsafe_ptr();
  186. auto vm_object = m_framebuffer_sink_vmobject->clone();
  187. VERIFY(vm_object);
  188. region->set_vmobject(vm_object.release_nonnull());
  189. region->remap();
  190. }
  191. }
  192. void VirtIOFrameBufferDevice::activate_writes()
  193. {
  194. m_are_writes_active = true;
  195. if (m_userspace_mmap_region) {
  196. auto* region = m_userspace_mmap_region.unsafe_ptr();
  197. region->set_vmobject(m_framebuffer->vmobject());
  198. region->remap();
  199. }
  200. }
  201. void VirtIOFrameBufferDevice::clear_to_black()
  202. {
  203. auto& info = display_info();
  204. size_t width = info.rect.width;
  205. size_t height = info.rect.height;
  206. u8* data = m_framebuffer->vaddr().as_ptr();
  207. for (size_t i = 0; i < width * height; ++i) {
  208. data[4 * i + 0] = 0x00;
  209. data[4 * i + 1] = 0x00;
  210. data[4 * i + 2] = 0x00;
  211. data[4 * i + 3] = 0xff;
  212. }
  213. }
  214. void VirtIOFrameBufferDevice::draw_ntsc_test_pattern()
  215. {
  216. static constexpr u8 colors[12][4] = {
  217. { 0xff, 0xff, 0xff, 0xff }, // White
  218. { 0x00, 0xff, 0xff, 0xff }, // Primary + Composite colors
  219. { 0xff, 0xff, 0x00, 0xff },
  220. { 0x00, 0xff, 0x00, 0xff },
  221. { 0xff, 0x00, 0xff, 0xff },
  222. { 0x00, 0x00, 0xff, 0xff },
  223. { 0xff, 0x00, 0x00, 0xff },
  224. { 0xba, 0x01, 0x5f, 0xff }, // Dark blue
  225. { 0x8d, 0x3d, 0x00, 0xff }, // Purple
  226. { 0x22, 0x22, 0x22, 0xff }, // Shades of gray
  227. { 0x10, 0x10, 0x10, 0xff },
  228. { 0x00, 0x00, 0x00, 0xff },
  229. };
  230. auto& info = display_info();
  231. size_t width = info.rect.width;
  232. size_t height = info.rect.height;
  233. u8* data = m_framebuffer->vaddr().as_ptr();
  234. // Draw NTSC test card
  235. for (size_t y = 0; y < height; ++y) {
  236. for (size_t x = 0; x < width; ++x) {
  237. size_t color = 0;
  238. if (3 * y < 2 * height) {
  239. // Top 2/3 of image is 7 vertical stripes of color spectrum
  240. color = (7 * x) / width;
  241. } else if (4 * y < 3 * height) {
  242. // 2/3 mark to 3/4 mark is backwards color spectrum alternating with black
  243. auto segment = (7 * x) / width;
  244. color = segment % 2 ? 10 : 6 - segment;
  245. } else {
  246. if (28 * x < 5 * width) {
  247. color = 8;
  248. } else if (28 * x < 10 * width) {
  249. color = 0;
  250. } else if (28 * x < 15 * width) {
  251. color = 7;
  252. } else if (28 * x < 20 * width) {
  253. color = 10;
  254. } else if (7 * x < 6 * width) {
  255. // Grayscale gradient
  256. color = 26 - ((21 * x) / width);
  257. } else {
  258. // Solid black
  259. color = 10;
  260. }
  261. }
  262. u8* pixel = &data[4 * (y * width + x)];
  263. for (int i = 0; i < 4; ++i) {
  264. pixel[i] = colors[color][i];
  265. }
  266. }
  267. }
  268. dbgln_if(VIRTIO_DEBUG, "Finish drawing the pattern");
  269. }
  270. u8* VirtIOFrameBufferDevice::framebuffer_data()
  271. {
  272. return m_framebuffer->vaddr().as_ptr();
  273. }
  274. }