Screen.cpp 22 KB

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