Screen.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Screen.h"
  8. #include "Compositor.h"
  9. #include "Event.h"
  10. #include "EventLoop.h"
  11. #include "ScreenBackend.h"
  12. #include "VirtualScreenBackend.h"
  13. #include "WindowManager.h"
  14. #include <AK/Debug.h>
  15. #include <AK/Format.h>
  16. #include <Kernel/API/Graphics.h>
  17. #include <Kernel/API/MousePacket.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <sys/mman.h>
  21. #include <unistd.h>
  22. namespace WindowServer {
  23. Vector<NonnullRefPtr<Screen>, default_screen_count> Screen::s_screens;
  24. Screen* Screen::s_main_screen { nullptr };
  25. Gfx::IntRect Screen::s_bounding_screens_rect {};
  26. ScreenLayout Screen::s_layout;
  27. Vector<int, default_scale_factors_in_use_count> Screen::s_scale_factors_in_use;
  28. struct FlushRectData {
  29. Vector<FBRect, 32> pending_flush_rects;
  30. bool too_many_pending_flush_rects { false };
  31. };
  32. ScreenInput& ScreenInput::the()
  33. {
  34. static ScreenInput s_the;
  35. return s_the;
  36. }
  37. Screen& ScreenInput::cursor_location_screen()
  38. {
  39. auto* screen = Screen::find_by_location(m_cursor_location);
  40. VERIFY(screen);
  41. return *screen;
  42. }
  43. Screen const& ScreenInput::cursor_location_screen() const
  44. {
  45. auto* screen = Screen::find_by_location(m_cursor_location);
  46. VERIFY(screen);
  47. return *screen;
  48. }
  49. bool Screen::apply_layout(ScreenLayout&& screen_layout, DeprecatedString& error_msg)
  50. {
  51. if (!screen_layout.is_valid(&error_msg))
  52. return false;
  53. if (screen_layout == s_layout)
  54. return true;
  55. bool place_cursor_on_main_screen = find_by_location(ScreenInput::the().cursor_location()) == nullptr;
  56. HashMap<size_t, size_t> current_to_new_indices_map;
  57. HashMap<size_t, size_t> new_to_current_indices_map;
  58. HashMap<size_t, NonnullRefPtr<Screen>> devices_no_longer_used;
  59. for (size_t i = 0; i < s_layout.screens.size(); i++) {
  60. auto& screen = s_layout.screens[i];
  61. bool found = false;
  62. for (size_t j = 0; j < screen_layout.screens.size(); j++) {
  63. auto& new_screen = screen_layout.screens[j];
  64. if (new_screen.device == screen.device) {
  65. current_to_new_indices_map.set(i, j);
  66. new_to_current_indices_map.set(j, i);
  67. found = true;
  68. break;
  69. }
  70. }
  71. if (!found)
  72. devices_no_longer_used.set(i, s_screens[i]);
  73. }
  74. HashMap<Screen*, size_t> screens_with_resolution_change;
  75. HashMap<Screen*, size_t> screens_with_scale_change;
  76. for (auto& it : current_to_new_indices_map) {
  77. auto& screen = s_layout.screens[it.key];
  78. auto& new_screen = screen_layout.screens[it.value];
  79. if (screen.resolution != new_screen.resolution)
  80. screens_with_resolution_change.set(s_screens[it.key], it.value);
  81. if (screen.scale_factor != new_screen.scale_factor)
  82. screens_with_scale_change.set(s_screens[it.key], it.value);
  83. }
  84. auto screens_backup = move(s_screens);
  85. auto layout_backup = move(s_layout);
  86. for (auto& it : screens_with_resolution_change) {
  87. auto& existing_screen = *it.key;
  88. dbgln("Closing device {} in preparation for resolution change", layout_backup.screens[existing_screen.index()].device.value_or("<virtual screen>"));
  89. existing_screen.close_device();
  90. }
  91. AK::ArmedScopeGuard rollback([&] {
  92. for (auto& screen : s_screens)
  93. screen->close_device();
  94. s_screens = move(screens_backup);
  95. s_layout = move(layout_backup);
  96. for (size_t i = 0; i < s_screens.size(); i++) {
  97. auto& old_screen = s_screens[i];
  98. // Restore the original screen index in case it changed
  99. old_screen->set_index(i);
  100. if (i == s_layout.main_screen_index)
  101. old_screen->make_main_screen();
  102. bool changed_scale = screens_with_scale_change.contains(old_screen);
  103. if (screens_with_resolution_change.contains(old_screen)) {
  104. if (old_screen->open_device()) {
  105. // The resolution was changed, so we also implicitly applied the new scale factor
  106. changed_scale = false;
  107. } else {
  108. // Don't set error_msg here, it should already be set
  109. dbgln("Rolling back screen layout failed: could not open device");
  110. }
  111. }
  112. old_screen->update_virtual_and_physical_rects();
  113. if (changed_scale)
  114. old_screen->scale_factor_changed();
  115. }
  116. update_bounding_rect();
  117. });
  118. s_layout = move(screen_layout);
  119. for (size_t index = 0; index < s_layout.screens.size(); index++) {
  120. Screen* screen;
  121. bool need_to_open_device;
  122. if (auto it = new_to_current_indices_map.find(index); it != new_to_current_indices_map.end()) {
  123. // Re-use the existing screen instance
  124. screen = screens_backup[it->value];
  125. s_screens.append(*screen);
  126. screen->set_index(index);
  127. need_to_open_device = screens_with_resolution_change.contains(screen);
  128. } else {
  129. screen = WindowServer::Screen::create(index);
  130. if (!screen) {
  131. error_msg = DeprecatedString::formatted("Error creating screen #{}", index);
  132. return false;
  133. }
  134. need_to_open_device = false;
  135. }
  136. if (need_to_open_device && !screen->open_device()) {
  137. error_msg = DeprecatedString::formatted("Error opening device for screen #{}", index);
  138. return false;
  139. }
  140. screen->update_virtual_and_physical_rects();
  141. if (!need_to_open_device && screens_with_scale_change.contains(screen))
  142. screen->scale_factor_changed();
  143. VERIFY(screen);
  144. VERIFY(index == screen->index());
  145. if (s_layout.main_screen_index == index)
  146. screen->make_main_screen();
  147. }
  148. rollback.disarm();
  149. if (place_cursor_on_main_screen) {
  150. ScreenInput::the().set_cursor_location(Screen::main().rect().center());
  151. } else {
  152. auto cursor_location = ScreenInput::the().cursor_location();
  153. if (!find_by_location(cursor_location)) {
  154. // Cursor is off screen, try to find the closest location on another screen
  155. float closest_distance = 0;
  156. Optional<Gfx::IntPoint> closest_point;
  157. for (auto& screen : s_screens) {
  158. auto closest_point_on_screen_rect = screen->rect().closest_to(cursor_location);
  159. auto distance = closest_point_on_screen_rect.distance_from(cursor_location);
  160. if (!closest_point.has_value() || distance < closest_distance) {
  161. closest_distance = distance;
  162. closest_point = closest_point_on_screen_rect;
  163. }
  164. }
  165. ScreenInput::the().set_cursor_location(closest_point.value()); // We should always have one
  166. }
  167. }
  168. update_bounding_rect();
  169. update_scale_factors_in_use();
  170. return true;
  171. }
  172. void Screen::update_scale_factors_in_use()
  173. {
  174. s_scale_factors_in_use.clear();
  175. for_each([&](auto& screen) {
  176. auto scale_factor = screen.scale_factor();
  177. // The This doesn't have to be extremely efficient as this
  178. // code is only run when we start up or the screen configuration
  179. // changes. But using a vector allows for efficient iteration,
  180. // which is the most common use case.
  181. if (!s_scale_factors_in_use.contains_slow(scale_factor))
  182. s_scale_factors_in_use.append(scale_factor);
  183. return IterationDecision::Continue;
  184. });
  185. }
  186. Screen::Screen(size_t screen_index)
  187. : m_index(screen_index)
  188. , m_flush_rects(adopt_own(*new FlushRectData()))
  189. , m_compositor_screen_data(Compositor::create_screen_data({}))
  190. {
  191. update_virtual_and_physical_rects();
  192. open_device();
  193. }
  194. Screen::~Screen()
  195. {
  196. close_device();
  197. }
  198. bool Screen::open_device()
  199. {
  200. close_device();
  201. auto& info = screen_layout_info();
  202. switch (info.mode) {
  203. case ScreenLayout::Screen::Mode::Device: {
  204. m_backend = make<HardwareScreenBackend>(info.device.value());
  205. auto return_value = m_backend->open();
  206. if (return_value.is_error()) {
  207. dbgln("Screen #{}: Failed to open backend: {}", index(), return_value.error());
  208. return false;
  209. }
  210. set_resolution(true);
  211. return true;
  212. }
  213. case ScreenLayout::Screen::Mode::Virtual: {
  214. m_backend = make<VirtualScreenBackend>();
  215. // Virtual device open should never fail.
  216. MUST(m_backend->open());
  217. set_resolution(true);
  218. return true;
  219. }
  220. default:
  221. dbgln("Unsupported screen type {}", ScreenLayout::Screen::mode_to_string(info.mode));
  222. return false;
  223. }
  224. }
  225. void Screen::close_device()
  226. {
  227. m_backend = nullptr;
  228. }
  229. void Screen::update_virtual_and_physical_rects()
  230. {
  231. auto& screen_info = screen_layout_info();
  232. m_virtual_rect = { screen_info.location, { screen_info.resolution.width() / screen_info.scale_factor, screen_info.resolution.height() / screen_info.scale_factor } };
  233. m_physical_rect = { Gfx::IntPoint { 0, 0 }, { screen_info.resolution.width(), screen_info.resolution.height() } };
  234. dbgln("update_virtual_and_physical_rects for screen #{}: {}", index(), m_virtual_rect);
  235. }
  236. void Screen::scale_factor_changed()
  237. {
  238. // Flush rects are affected by the screen factor
  239. constrain_pending_flush_rects();
  240. }
  241. Screen& Screen::closest_to_rect(Gfx::IntRect const& rect)
  242. {
  243. Screen* best_screen = nullptr;
  244. int best_area = 0;
  245. for (auto& screen : s_screens) {
  246. auto r = screen->rect().intersected(rect);
  247. int area = r.width() * r.height();
  248. if (!best_screen || area > best_area) {
  249. best_screen = screen;
  250. best_area = area;
  251. }
  252. }
  253. if (!best_screen) {
  254. // TODO: try to find the best screen in close proximity
  255. best_screen = &Screen::main();
  256. }
  257. return *best_screen;
  258. }
  259. Screen& Screen::closest_to_location(Gfx::IntPoint point)
  260. {
  261. for (auto& screen : s_screens) {
  262. if (screen->rect().contains(point))
  263. return screen;
  264. }
  265. // TODO: guess based on how close the point is to the next screen rectangle
  266. return Screen::main();
  267. }
  268. void Screen::update_bounding_rect()
  269. {
  270. if (!s_screens.is_empty()) {
  271. s_bounding_screens_rect = s_screens[0]->rect();
  272. for (size_t i = 1; i < s_screens.size(); i++)
  273. s_bounding_screens_rect = s_bounding_screens_rect.united(s_screens[i]->rect());
  274. } else {
  275. s_bounding_screens_rect = {};
  276. }
  277. }
  278. bool Screen::set_resolution(bool initial)
  279. {
  280. // Remember the screen that the cursor is on. Make sure it stays on the same screen if we change its resolution...
  281. Screen* screen_with_cursor = nullptr;
  282. if (!initial)
  283. screen_with_cursor = &ScreenInput::the().cursor_location_screen();
  284. auto& info = screen_layout_info();
  285. ErrorOr<void> return_value = Error::from_errno(EINVAL);
  286. {
  287. GraphicsHeadModeSetting requested_mode_setting;
  288. memset(&requested_mode_setting, 0, sizeof(GraphicsHeadModeSetting));
  289. requested_mode_setting.horizontal_stride = info.resolution.width() * 4;
  290. requested_mode_setting.pixel_clock_in_khz = 0;
  291. requested_mode_setting.horizontal_active = info.resolution.width();
  292. requested_mode_setting.horizontal_front_porch_pixels = 0;
  293. requested_mode_setting.horizontal_sync_time_pixels = 0;
  294. requested_mode_setting.horizontal_blank_pixels = 0;
  295. requested_mode_setting.vertical_active = info.resolution.height();
  296. requested_mode_setting.vertical_front_porch_lines = 0;
  297. requested_mode_setting.vertical_sync_time_lines = 0;
  298. requested_mode_setting.vertical_blank_lines = 0;
  299. requested_mode_setting.horizontal_offset = 0;
  300. requested_mode_setting.vertical_offset = 0;
  301. return_value = m_backend->set_head_mode_setting(requested_mode_setting);
  302. }
  303. dbgln_if(WSSCREEN_DEBUG, "Screen #{}: fb_set_resolution() - success", index());
  304. auto on_change_resolution = [&]() -> ErrorOr<void> {
  305. if (initial) {
  306. TRY(m_backend->unmap_framebuffer());
  307. TRY(m_backend->map_framebuffer());
  308. }
  309. auto mode_setting = TRY(m_backend->get_head_mode_setting());
  310. info.resolution = { mode_setting.horizontal_active, mode_setting.vertical_active };
  311. update_virtual_and_physical_rects();
  312. // Since pending flush rects are affected by the scale factor
  313. // update even if only the scale factor changed
  314. constrain_pending_flush_rects();
  315. if (this == screen_with_cursor) {
  316. auto& screen_input = ScreenInput::the();
  317. screen_input.set_cursor_location(screen_input.cursor_location().constrained(rect()));
  318. }
  319. return {};
  320. };
  321. if (!return_value.is_error()) {
  322. return_value = on_change_resolution();
  323. if (!return_value.is_error())
  324. return true;
  325. }
  326. if (return_value.is_error() && return_value.error() != Error::from_errno(EOVERFLOW)) {
  327. dbgln("Screen #{}: Failed to set resolution {}: {}", index(), info.resolution, return_value.error());
  328. MUST(on_change_resolution());
  329. return false;
  330. }
  331. dbgln("Screen #{}: Failed to set resolution {}: {}, falling back to safe resolution", index(), info.resolution, return_value.error());
  332. MUST(m_backend->set_safe_head_mode_setting());
  333. MUST(on_change_resolution());
  334. return false;
  335. }
  336. void Screen::set_buffer(int index)
  337. {
  338. m_backend->set_head_buffer(index);
  339. }
  340. size_t Screen::buffer_offset(int index) const
  341. {
  342. if (index == 0)
  343. return 0;
  344. if (index == 1)
  345. return m_backend->m_back_buffer_offset;
  346. VERIFY_NOT_REACHED();
  347. }
  348. void ScreenInput::set_acceleration_factor(double factor)
  349. {
  350. VERIFY(factor >= mouse_accel_min && factor <= mouse_accel_max);
  351. m_acceleration_factor = factor;
  352. }
  353. void ScreenInput::set_scroll_step_size(unsigned step_size)
  354. {
  355. VERIFY(step_size >= scroll_step_size_min);
  356. m_scroll_step_size = step_size;
  357. }
  358. void ScreenInput::on_receive_mouse_data(MousePacket const& packet)
  359. {
  360. auto& current_screen = cursor_location_screen();
  361. auto prev_location = m_cursor_location;
  362. if (packet.is_relative) {
  363. m_cursor_location.translate_by(packet.x * m_acceleration_factor, packet.y * m_acceleration_factor);
  364. dbgln_if(WSSCREEN_DEBUG, "Screen: New Relative mouse point @ {}", m_cursor_location);
  365. } else {
  366. m_cursor_location = { packet.x * current_screen.width() / 0xffff, packet.y * current_screen.height() / 0xffff };
  367. dbgln_if(WSSCREEN_DEBUG, "Screen: New Absolute mouse point @ {}", m_cursor_location);
  368. }
  369. auto* moved_to_screen = Screen::find_by_location(m_cursor_location);
  370. if (!moved_to_screen) {
  371. m_cursor_location.constrain(current_screen.rect());
  372. moved_to_screen = &current_screen;
  373. }
  374. unsigned buttons = packet.buttons;
  375. unsigned prev_buttons = m_mouse_button_state;
  376. m_mouse_button_state = buttons;
  377. unsigned changed_buttons = prev_buttons ^ buttons;
  378. auto post_mousedown_or_mouseup_if_needed = [&](MouseButton button) {
  379. if (!(changed_buttons & (unsigned)button))
  380. return;
  381. auto message = make<MouseEvent>(buttons & (unsigned)button ? Event::MouseDown : Event::MouseUp, m_cursor_location, buttons, button, m_modifiers);
  382. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  383. };
  384. post_mousedown_or_mouseup_if_needed(MouseButton::Primary);
  385. post_mousedown_or_mouseup_if_needed(MouseButton::Secondary);
  386. post_mousedown_or_mouseup_if_needed(MouseButton::Middle);
  387. post_mousedown_or_mouseup_if_needed(MouseButton::Backward);
  388. post_mousedown_or_mouseup_if_needed(MouseButton::Forward);
  389. if (m_cursor_location != prev_location) {
  390. auto message = make<MouseEvent>(Event::MouseMove, m_cursor_location, buttons, MouseButton::None, m_modifiers);
  391. if (WindowManager::the().dnd_client())
  392. message->set_mime_data(WindowManager::the().dnd_mime_data());
  393. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  394. }
  395. if (packet.z || packet.w) {
  396. auto message = make<MouseEvent>(Event::MouseWheel, m_cursor_location, buttons, MouseButton::None, m_modifiers, packet.w * m_scroll_step_size, packet.z * m_scroll_step_size, packet.w, packet.z);
  397. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  398. }
  399. if (m_cursor_location != prev_location)
  400. Compositor::the().invalidate_cursor();
  401. }
  402. void ScreenInput::on_receive_keyboard_data(::KeyEvent kernel_event)
  403. {
  404. m_modifiers = kernel_event.modifiers();
  405. auto message = make<KeyEvent>(kernel_event.is_press() ? Event::KeyDown : Event::KeyUp, kernel_event.key, kernel_event.code_point, kernel_event.modifiers(), kernel_event.scancode);
  406. Core::EventLoop::current().post_event(WindowManager::the(), move(message));
  407. }
  408. void Screen::constrain_pending_flush_rects()
  409. {
  410. auto& flush_rects = *m_flush_rects;
  411. if (flush_rects.pending_flush_rects.is_empty())
  412. return;
  413. Gfx::IntRect screen_rect({}, rect().size());
  414. Gfx::DisjointIntRectSet rects;
  415. for (auto& fb_rect : flush_rects.pending_flush_rects) {
  416. Gfx::IntRect rect { (int)fb_rect.x, (int)fb_rect.y, (int)fb_rect.width, (int)fb_rect.height };
  417. auto intersected_rect = rect.intersected(screen_rect);
  418. if (!intersected_rect.is_empty())
  419. rects.add(intersected_rect);
  420. }
  421. flush_rects.pending_flush_rects.clear_with_capacity();
  422. for (auto const& rect : rects.rects()) {
  423. flush_rects.pending_flush_rects.append({
  424. .head_index = 0,
  425. .x = (unsigned)rect.x(),
  426. .y = (unsigned)rect.y(),
  427. .width = (unsigned)rect.width(),
  428. .height = (unsigned)rect.height(),
  429. });
  430. }
  431. }
  432. void Screen::queue_flush_display_rect(Gfx::IntRect const& flush_region)
  433. {
  434. // NOTE: we don't scale until in Screen::flush_display so that when
  435. // there are too many rectangles that we end up throwing away, we didn't
  436. // waste accounting for scale factor!
  437. auto& flush_rects = *m_flush_rects;
  438. if (flush_rects.too_many_pending_flush_rects) {
  439. // We already have too many, just make sure we extend it if needed
  440. VERIFY(!flush_rects.pending_flush_rects.is_empty());
  441. if (flush_rects.pending_flush_rects.size() == 1) {
  442. auto& union_rect = flush_rects.pending_flush_rects[0];
  443. auto new_union = flush_region.united(Gfx::IntRect((int)union_rect.x, (int)union_rect.y, (int)union_rect.width, (int)union_rect.height));
  444. union_rect.x = new_union.left();
  445. union_rect.y = new_union.top();
  446. union_rect.width = new_union.width();
  447. union_rect.height = new_union.height();
  448. } else {
  449. // Convert all the rectangles into one union
  450. auto new_union = flush_region;
  451. for (auto& flush_rect : flush_rects.pending_flush_rects)
  452. new_union = new_union.united(Gfx::IntRect((int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height));
  453. flush_rects.pending_flush_rects.resize(1, true);
  454. auto& union_rect = flush_rects.pending_flush_rects[0];
  455. union_rect.x = new_union.left();
  456. union_rect.y = new_union.top();
  457. union_rect.width = new_union.width();
  458. union_rect.height = new_union.height();
  459. }
  460. return;
  461. }
  462. VERIFY(flush_rects.pending_flush_rects.size() < flush_rects.pending_flush_rects.capacity());
  463. flush_rects.pending_flush_rects.append({ 0,
  464. (unsigned)flush_region.left(),
  465. (unsigned)flush_region.top(),
  466. (unsigned)flush_region.width(),
  467. (unsigned)flush_region.height() });
  468. if (flush_rects.pending_flush_rects.size() == flush_rects.pending_flush_rects.capacity()) {
  469. // If we get one more rectangle then we need to convert it to a single union rectangle
  470. flush_rects.too_many_pending_flush_rects = true;
  471. }
  472. }
  473. void Screen::flush_display(int buffer_index)
  474. {
  475. VERIFY(m_backend->m_can_device_flush_buffers || m_backend->m_can_device_flush_entire_framebuffer);
  476. auto& flush_rects = *m_flush_rects;
  477. if (flush_rects.pending_flush_rects.is_empty())
  478. return;
  479. // Now that we have a final set of rects, apply the scale factor
  480. auto scale_factor = this->scale_factor();
  481. for (auto& flush_rect : flush_rects.pending_flush_rects) {
  482. VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains({ (int)flush_rect.x, (int)flush_rect.y, (int)flush_rect.width, (int)flush_rect.height }));
  483. flush_rect.x *= scale_factor;
  484. flush_rect.y *= scale_factor;
  485. flush_rect.width *= scale_factor;
  486. flush_rect.height *= scale_factor;
  487. }
  488. if (m_backend->m_can_device_flush_entire_framebuffer) {
  489. auto return_value = m_backend->flush_framebuffer();
  490. if (return_value.is_error())
  491. dbgln("Screen #{}: Error flushing display: {}", index(), return_value.error());
  492. } else {
  493. auto return_value = m_backend->flush_framebuffer_rects(buffer_index, flush_rects.pending_flush_rects.span());
  494. if (return_value.is_error())
  495. dbgln("Screen #{}: Error flushing display: {}", index(), return_value.error());
  496. }
  497. flush_rects.too_many_pending_flush_rects = false;
  498. flush_rects.pending_flush_rects.clear_with_capacity();
  499. }
  500. void Screen::flush_display_entire_framebuffer()
  501. {
  502. VERIFY(m_backend->m_can_device_flush_entire_framebuffer);
  503. auto return_value = m_backend->flush_framebuffer();
  504. if (return_value.is_error())
  505. dbgln("Screen #{}: Error flushing display front buffer: {}", index(), return_value.error());
  506. }
  507. void Screen::flush_display_front_buffer(int front_buffer_index, Gfx::IntRect& rect)
  508. {
  509. VERIFY(m_backend->m_can_device_flush_buffers);
  510. auto scale_factor = this->scale_factor();
  511. FBRect flush_rect {
  512. .head_index = 0,
  513. .x = (unsigned)(rect.x() * scale_factor),
  514. .y = (unsigned)(rect.y() * scale_factor),
  515. .width = (unsigned)(rect.width() * scale_factor),
  516. .height = (unsigned)(rect.height() * scale_factor)
  517. };
  518. VERIFY(Gfx::IntRect({}, m_virtual_rect.size()).contains(rect));
  519. auto return_value = m_backend->flush_framebuffer_rects(front_buffer_index, { &flush_rect, 1 });
  520. if (return_value.is_error())
  521. dbgln("Screen #{}: Error flushing display front buffer: {}", index(), return_value.error());
  522. }
  523. }