FramebufferDevice.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/Graphics/GraphicsManagement.h>
  8. #include <Kernel/Graphics/VirtIOGPU/FramebufferDevice.h>
  9. #include <Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h>
  10. #include <LibC/sys/ioctl_numbers.h>
  11. namespace Kernel::Graphics::VirtIOGPU {
  12. RefPtr<GraphicsAdapter> FramebufferDevice::adapter() const
  13. {
  14. auto adapter = m_graphics_adapter.strong_ref();
  15. // FIXME: Propagate error gracefully
  16. VERIFY(adapter);
  17. return static_cast<GraphicsAdapter&>(*adapter);
  18. }
  19. ErrorOr<size_t> FramebufferDevice::buffer_length(size_t head) const
  20. {
  21. // Note: This FramebufferDevice class doesn't support multihead setup.
  22. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  23. // so if we happen to accidentally have a value different than 0, assert.
  24. VERIFY(head == 0);
  25. SpinlockLocker locker(m_resolution_lock);
  26. return display_info().rect.width * display_info().rect.height * 4;
  27. }
  28. ErrorOr<size_t> FramebufferDevice::pitch(size_t head) const
  29. {
  30. // Note: This FramebufferDevice class doesn't support multihead setup.
  31. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  32. // so if we happen to accidentally have a value different than 0, assert.
  33. VERIFY(head == 0);
  34. SpinlockLocker locker(m_resolution_lock);
  35. return display_info().rect.width * 4;
  36. }
  37. ErrorOr<size_t> FramebufferDevice::height(size_t head) const
  38. {
  39. // Note: This FramebufferDevice class doesn't support multihead setup.
  40. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  41. // so if we happen to accidentally have a value different than 0, assert.
  42. VERIFY(head == 0);
  43. SpinlockLocker locker(m_resolution_lock);
  44. return display_info().rect.height;
  45. }
  46. ErrorOr<size_t> FramebufferDevice::width(size_t head) const
  47. {
  48. // Note: This FramebufferDevice class doesn't support multihead setup.
  49. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  50. // so if we happen to accidentally have a value different than 0, assert.
  51. VERIFY(head == 0);
  52. SpinlockLocker locker(m_resolution_lock);
  53. return display_info().rect.width;
  54. }
  55. ErrorOr<size_t> FramebufferDevice::vertical_offset(size_t head) const
  56. {
  57. // Note: This FramebufferDevice class doesn't support multihead setup.
  58. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  59. // so if we happen to accidentally have a value different than 0, assert.
  60. VERIFY(head == 0);
  61. return 0;
  62. }
  63. ErrorOr<bool> FramebufferDevice::vertical_offsetted(size_t head) const
  64. {
  65. // Note: This FramebufferDevice class doesn't support multihead setup.
  66. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  67. // so if we happen to accidentally have a value different than 0, assert.
  68. VERIFY(head == 0);
  69. return false;
  70. }
  71. ErrorOr<void> FramebufferDevice::set_head_resolution(size_t head, size_t width, size_t height, size_t)
  72. {
  73. // Note: This class doesn't support multihead setup (yet!).
  74. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  75. // so if we happen to accidentally have a value different than 0, assert.
  76. VERIFY(head == 0);
  77. if (width > MAX_VIRTIOGPU_RESOLUTION_WIDTH || height > MAX_VIRTIOGPU_RESOLUTION_HEIGHT)
  78. return Error::from_errno(ENOTSUP);
  79. auto& info = display_info();
  80. SpinlockLocker locker(adapter()->operation_lock());
  81. info.rect = {
  82. .x = 0,
  83. .y = 0,
  84. .width = (u32)width,
  85. .height = (u32)height,
  86. };
  87. // FIXME: Would be nice to be able to return ErrorOr here.
  88. TRY(create_framebuffer());
  89. return {};
  90. }
  91. ErrorOr<void> FramebufferDevice::set_head_buffer(size_t, bool)
  92. {
  93. return Error::from_errno(ENOTSUP);
  94. }
  95. ErrorOr<void> FramebufferDevice::flush_head_buffer(size_t)
  96. {
  97. // Note: This class doesn't support flushing.
  98. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  99. // so if we happen to accidentally reach this code, assert.
  100. VERIFY_NOT_REACHED();
  101. }
  102. ErrorOr<void> FramebufferDevice::flush_rectangle(size_t buffer_index, FBRect const& rect)
  103. {
  104. SpinlockLocker locker(adapter()->operation_lock());
  105. Protocol::Rect dirty_rect {
  106. .x = rect.x,
  107. .y = rect.y,
  108. .width = rect.width,
  109. .height = rect.height
  110. };
  111. // FIXME: Find a better ErrorOr<void> here.
  112. if (!m_are_writes_active)
  113. return Error::from_errno(EIO);
  114. auto& buffer = buffer_from_index(buffer_index);
  115. transfer_framebuffer_data_to_host(dirty_rect, buffer);
  116. if (&buffer == m_current_buffer) {
  117. // Flushing directly to screen
  118. flush_displayed_image(dirty_rect, buffer);
  119. buffer.dirty_rect = {};
  120. } else {
  121. if (buffer.dirty_rect.width == 0 || buffer.dirty_rect.height == 0) {
  122. buffer.dirty_rect = dirty_rect;
  123. } else {
  124. auto current_dirty_right = buffer.dirty_rect.x + buffer.dirty_rect.width;
  125. auto current_dirty_bottom = buffer.dirty_rect.y + buffer.dirty_rect.height;
  126. buffer.dirty_rect.x = min(buffer.dirty_rect.x, dirty_rect.x);
  127. buffer.dirty_rect.y = min(buffer.dirty_rect.y, dirty_rect.y);
  128. buffer.dirty_rect.width = max(current_dirty_right, dirty_rect.x + dirty_rect.width) - buffer.dirty_rect.x;
  129. buffer.dirty_rect.height = max(current_dirty_bottom, dirty_rect.y + dirty_rect.height) - buffer.dirty_rect.y;
  130. }
  131. }
  132. return {};
  133. }
  134. ErrorOr<ByteBuffer> FramebufferDevice::get_edid(size_t head) const
  135. {
  136. // Note: This FramebufferDevice class doesn't support multihead setup.
  137. // We take care to verify this at the GenericFramebufferDevice::ioctl method
  138. // so if we happen to accidentally have a value different than 0, assert.
  139. VERIFY(head == 0);
  140. return adapter()->get_edid(m_scanout.value());
  141. }
  142. FramebufferDevice::FramebufferDevice(GraphicsAdapter const& adapter, ScanoutID scanout)
  143. : GenericFramebufferDevice(adapter)
  144. , m_scanout(scanout)
  145. {
  146. if (display_info().enabled) {
  147. // FIXME: This should be in a place where we can handle allocation failures.
  148. auto result = create_framebuffer();
  149. VERIFY(!result.is_error());
  150. }
  151. }
  152. FramebufferDevice::~FramebufferDevice()
  153. {
  154. }
  155. ErrorOr<void> FramebufferDevice::create_framebuffer()
  156. {
  157. // First delete any existing framebuffers to free the memory first
  158. m_framebuffer = nullptr;
  159. m_framebuffer_sink_vmobject = nullptr;
  160. // Allocate frame buffer for both front and back
  161. auto& info = display_info();
  162. m_buffer_size = calculate_framebuffer_size(info.rect.width, info.rect.height);
  163. auto region_name = TRY(KString::formatted("VirtGPU FrameBuffer #{}", m_scanout.value()));
  164. m_framebuffer = TRY(MM.allocate_kernel_region(m_buffer_size * 2, region_name->view(), Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow));
  165. auto write_sink_page = TRY(MM.allocate_user_physical_page(Memory::MemoryManager::ShouldZeroFill::No));
  166. auto num_needed_pages = m_framebuffer->vmobject().page_count();
  167. NonnullRefPtrVector<Memory::PhysicalPage> pages;
  168. for (auto i = 0u; i < num_needed_pages; ++i) {
  169. TRY(pages.try_append(write_sink_page));
  170. }
  171. m_framebuffer_sink_vmobject = TRY(Memory::AnonymousVMObject::try_create_with_physical_pages(pages.span()));
  172. SpinlockLocker locker(adapter()->operation_lock());
  173. m_current_buffer = &buffer_from_index(m_last_set_buffer_index.load());
  174. create_buffer(m_main_buffer, 0, m_buffer_size);
  175. create_buffer(m_back_buffer, m_buffer_size, m_buffer_size);
  176. return {};
  177. }
  178. void FramebufferDevice::create_buffer(Buffer& buffer, size_t framebuffer_offset, size_t framebuffer_size)
  179. {
  180. buffer.framebuffer_offset = framebuffer_offset;
  181. buffer.framebuffer_data = m_framebuffer->vaddr().as_ptr() + framebuffer_offset;
  182. auto& info = display_info();
  183. // 1. Create BUFFER using VIRTIO_GPU_CMD_RESOURCE_CREATE_2D
  184. if (buffer.resource_id.value() != 0)
  185. adapter()->delete_resource(buffer.resource_id);
  186. buffer.resource_id = adapter()->create_2d_resource(info.rect);
  187. // 2. Attach backing storage using VIRTIO_GPU_CMD_RESOURCE_ATTACH_BACKING
  188. adapter()->ensure_backing_storage(buffer.resource_id, *m_framebuffer, buffer.framebuffer_offset, framebuffer_size);
  189. // 3. Use VIRTIO_GPU_CMD_SET_SCANOUT to link the framebuffer to a display scanout.
  190. if (&buffer == m_current_buffer)
  191. adapter()->set_scanout_resource(m_scanout.value(), buffer.resource_id, info.rect);
  192. // 4. Render our test pattern
  193. draw_ntsc_test_pattern(buffer);
  194. // 5. Use VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D to update the host resource from guest memory.
  195. transfer_framebuffer_data_to_host(info.rect, buffer);
  196. // 6. Use VIRTIO_GPU_CMD_RESOURCE_FLUSH to flush the updated resource to the display.
  197. if (&buffer == m_current_buffer)
  198. flush_displayed_image(info.rect, buffer);
  199. // Make sure we constrain the existing dirty rect (if any)
  200. if (buffer.dirty_rect.width != 0 || buffer.dirty_rect.height != 0) {
  201. auto dirty_right = buffer.dirty_rect.x + buffer.dirty_rect.width;
  202. auto dirty_bottom = buffer.dirty_rect.y + buffer.dirty_rect.height;
  203. buffer.dirty_rect.width = min(dirty_right, info.rect.x + info.rect.width) - buffer.dirty_rect.x;
  204. buffer.dirty_rect.height = min(dirty_bottom, info.rect.y + info.rect.height) - buffer.dirty_rect.y;
  205. }
  206. info.enabled = 1;
  207. }
  208. Protocol::DisplayInfoResponse::Display const& FramebufferDevice::display_info() const
  209. {
  210. return adapter()->display_info(m_scanout);
  211. }
  212. Protocol::DisplayInfoResponse::Display& FramebufferDevice::display_info()
  213. {
  214. return adapter()->display_info(m_scanout);
  215. }
  216. void FramebufferDevice::transfer_framebuffer_data_to_host(Protocol::Rect const& rect, Buffer& buffer)
  217. {
  218. adapter()->transfer_framebuffer_data_to_host(m_scanout, buffer.resource_id, rect);
  219. }
  220. void FramebufferDevice::flush_dirty_window(Protocol::Rect const& dirty_rect, Buffer& buffer)
  221. {
  222. adapter()->flush_dirty_rectangle(m_scanout, buffer.resource_id, dirty_rect);
  223. }
  224. void FramebufferDevice::flush_displayed_image(Protocol::Rect const& dirty_rect, Buffer& buffer)
  225. {
  226. adapter()->flush_displayed_image(buffer.resource_id, dirty_rect);
  227. }
  228. void FramebufferDevice::set_buffer(int buffer_index)
  229. {
  230. auto& buffer = buffer_index == 0 ? m_main_buffer : m_back_buffer;
  231. SpinlockLocker locker(adapter()->operation_lock());
  232. if (&buffer == m_current_buffer)
  233. return;
  234. m_current_buffer = &buffer;
  235. adapter()->set_scanout_resource(m_scanout.value(), buffer.resource_id, display_info().rect);
  236. adapter()->flush_displayed_image(buffer.resource_id, buffer.dirty_rect); // QEMU SDL backend requires this (as per spec)
  237. buffer.dirty_rect = {};
  238. }
  239. ErrorOr<Memory::Region*> FramebufferDevice::mmap(Process& process, OpenFileDescription&, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
  240. {
  241. TRY(process.require_promise(Pledge::video));
  242. if (!shared)
  243. return ENODEV;
  244. if (offset != 0 || !m_framebuffer)
  245. return ENXIO;
  246. if (range.size() > m_framebuffer->size())
  247. return EOVERFLOW;
  248. // We only allow one process to map the region
  249. if (m_userspace_mmap_region)
  250. return ENOMEM;
  251. RefPtr<Memory::VMObject> vmobject;
  252. if (m_are_writes_active) {
  253. vmobject = TRY(m_framebuffer->vmobject().try_clone());
  254. } else {
  255. vmobject = m_framebuffer_sink_vmobject;
  256. if (vmobject.is_null())
  257. return ENOMEM;
  258. }
  259. m_userspace_mmap_region = TRY(process.address_space().allocate_region_with_vmobject(
  260. range,
  261. vmobject.release_nonnull(),
  262. 0,
  263. "VirtIOGPU Framebuffer",
  264. prot,
  265. shared));
  266. return m_userspace_mmap_region.unsafe_ptr();
  267. }
  268. void FramebufferDevice::deactivate_writes()
  269. {
  270. m_are_writes_active = false;
  271. if (m_userspace_mmap_region) {
  272. auto* region = m_userspace_mmap_region.unsafe_ptr();
  273. auto maybe_vm_object = m_framebuffer_sink_vmobject->try_clone();
  274. // FIXME: Would be nice to be able to return a ErrorOr<void> here.
  275. VERIFY(!maybe_vm_object.is_error());
  276. region->set_vmobject(maybe_vm_object.release_value());
  277. region->remap();
  278. }
  279. set_buffer(0);
  280. clear_to_black(buffer_from_index(0));
  281. }
  282. void FramebufferDevice::activate_writes()
  283. {
  284. m_are_writes_active = true;
  285. auto last_set_buffer_index = m_last_set_buffer_index.load();
  286. if (m_userspace_mmap_region) {
  287. auto* region = m_userspace_mmap_region.unsafe_ptr();
  288. region->set_vmobject(m_framebuffer->vmobject());
  289. region->remap();
  290. }
  291. set_buffer(last_set_buffer_index);
  292. }
  293. void FramebufferDevice::clear_to_black(Buffer& buffer)
  294. {
  295. auto& info = display_info();
  296. size_t width = info.rect.width;
  297. size_t height = info.rect.height;
  298. u8* data = buffer.framebuffer_data;
  299. for (size_t i = 0; i < width * height; ++i) {
  300. data[4 * i + 0] = 0x00;
  301. data[4 * i + 1] = 0x00;
  302. data[4 * i + 2] = 0x00;
  303. data[4 * i + 3] = 0xff;
  304. }
  305. }
  306. void FramebufferDevice::draw_ntsc_test_pattern(Buffer& buffer)
  307. {
  308. constexpr u8 colors[12][4] = {
  309. { 0xff, 0xff, 0xff, 0xff }, // White
  310. { 0x00, 0xff, 0xff, 0xff }, // Primary + Composite colors
  311. { 0xff, 0xff, 0x00, 0xff },
  312. { 0x00, 0xff, 0x00, 0xff },
  313. { 0xff, 0x00, 0xff, 0xff },
  314. { 0x00, 0x00, 0xff, 0xff },
  315. { 0xff, 0x00, 0x00, 0xff },
  316. { 0xba, 0x01, 0x5f, 0xff }, // Dark blue
  317. { 0x8d, 0x3d, 0x00, 0xff }, // Purple
  318. { 0x22, 0x22, 0x22, 0xff }, // Shades of gray
  319. { 0x10, 0x10, 0x10, 0xff },
  320. { 0x00, 0x00, 0x00, 0xff },
  321. };
  322. auto& info = display_info();
  323. size_t width = info.rect.width;
  324. size_t height = info.rect.height;
  325. u8* data = buffer.framebuffer_data;
  326. // Draw NTSC test card
  327. for (size_t y = 0; y < height; ++y) {
  328. for (size_t x = 0; x < width; ++x) {
  329. size_t color = 0;
  330. if (3 * y < 2 * height) {
  331. // Top 2/3 of image is 7 vertical stripes of color spectrum
  332. color = (7 * x) / width;
  333. } else if (4 * y < 3 * height) {
  334. // 2/3 mark to 3/4 mark is backwards color spectrum alternating with black
  335. auto segment = (7 * x) / width;
  336. color = segment % 2 ? 10 : 6 - segment;
  337. } else {
  338. if (28 * x < 5 * width) {
  339. color = 8;
  340. } else if (28 * x < 10 * width) {
  341. color = 0;
  342. } else if (28 * x < 15 * width) {
  343. color = 7;
  344. } else if (28 * x < 20 * width) {
  345. color = 10;
  346. } else if (7 * x < 6 * width) {
  347. // Grayscale gradient
  348. color = 26 - ((21 * x) / width);
  349. } else {
  350. // Solid black
  351. color = 10;
  352. }
  353. }
  354. u8* pixel = &data[4 * (y * width + x)];
  355. for (int i = 0; i < 4; ++i) {
  356. pixel[i] = colors[color][i];
  357. }
  358. }
  359. }
  360. dbgln_if(VIRTIO_DEBUG, "Finish drawing the pattern");
  361. }
  362. u8* FramebufferDevice::framebuffer_data()
  363. {
  364. return m_current_buffer->framebuffer_data;
  365. }
  366. }