WSWindowManager.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include "Process.h"
  6. #include "MemoryManager.h"
  7. #include <Kernel/ProcFS.h>
  8. #include <SharedGraphics/Font.h>
  9. #include <SharedGraphics/Painter.h>
  10. #include <SharedGraphics/CharacterBitmap.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <Kernel/BochsVGADevice.h>
  13. //#define DEBUG_COUNTERS
  14. //#define DEBUG_WID_IN_TITLE_BAR
  15. static const int window_titlebar_height = 16;
  16. static inline Rect title_bar_rect(const Rect& window)
  17. {
  18. return {
  19. window.x() - 1,
  20. window.y() - window_titlebar_height,
  21. window.width() + 2,
  22. window_titlebar_height
  23. };
  24. }
  25. static inline Rect title_bar_text_rect(const Rect& window)
  26. {
  27. auto titlebar_rect = title_bar_rect(window);
  28. return {
  29. titlebar_rect.x() + 2,
  30. titlebar_rect.y(),
  31. titlebar_rect.width() - 4,
  32. titlebar_rect.height()
  33. };
  34. }
  35. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  36. {
  37. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  38. int close_button_margin = 1;
  39. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  40. return Rect {
  41. titlebar_inner_rect.right() - close_button_size + 1,
  42. titlebar_inner_rect.top() + close_button_margin,
  43. close_button_size,
  44. close_button_size - 1
  45. };
  46. }
  47. static inline Rect border_window_rect(const Rect& window)
  48. {
  49. auto titlebar_rect = title_bar_rect(window);
  50. return { titlebar_rect.x() - 1,
  51. titlebar_rect.y() - 1,
  52. titlebar_rect.width() + 2,
  53. window_titlebar_height + window.height() + 3
  54. };
  55. }
  56. static inline Rect outer_window_rect(const Rect& window)
  57. {
  58. auto rect = border_window_rect(window);
  59. rect.inflate(2, 2);
  60. return rect;
  61. }
  62. static WSWindowManager* s_the;
  63. WSWindowManager& WSWindowManager::the()
  64. {
  65. if (!s_the)
  66. s_the = new WSWindowManager;
  67. return *s_the;
  68. }
  69. static const char* cursor_bitmap_inner_ascii = {
  70. " # "
  71. " ## "
  72. " ### "
  73. " #### "
  74. " ##### "
  75. " ###### "
  76. " ####### "
  77. " ######## "
  78. " ######### "
  79. " ########## "
  80. " ###### "
  81. " ## ## "
  82. " # ## "
  83. " ## "
  84. " ## "
  85. " ## "
  86. " "
  87. };
  88. static const char* cursor_bitmap_outer_ascii = {
  89. "## "
  90. "# # "
  91. "# # "
  92. "# # "
  93. "# # "
  94. "# # "
  95. "# # "
  96. "# # "
  97. "# # "
  98. "# # "
  99. "# #### "
  100. "# ## # "
  101. "# # # # "
  102. "## # # "
  103. " # # "
  104. " # # "
  105. " ## "
  106. };
  107. void WSWindowManager::flip_buffers()
  108. {
  109. swap(m_front_bitmap, m_back_bitmap);
  110. swap(m_front_painter, m_back_painter);
  111. if (m_buffers_are_flipped)
  112. BochsVGADevice::the().set_y_offset(0);
  113. else
  114. BochsVGADevice::the().set_y_offset(m_screen_rect.height());
  115. m_buffers_are_flipped = !m_buffers_are_flipped;
  116. }
  117. WSWindowManager::WSWindowManager()
  118. : m_screen(WSScreen::the())
  119. , m_screen_rect(m_screen.rect())
  120. , m_lock("WSWindowManager")
  121. , m_flash_flush(false)
  122. {
  123. #ifndef DEBUG_COUNTERS
  124. (void)m_compose_count;
  125. (void)m_flush_count;
  126. #endif
  127. auto size = m_screen_rect.size();
  128. m_front_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(0));
  129. m_back_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(size.height()));
  130. m_front_painter = make<Painter>(*m_front_bitmap);
  131. m_back_painter = make<Painter>(*m_back_bitmap);
  132. m_font = Font::default_font();
  133. m_front_painter->set_font(font());
  134. m_back_painter->set_font(font());
  135. m_background_color = Color(50, 50, 50);
  136. m_active_window_border_color = Color(110, 34, 9);
  137. m_active_window_border_color2 = Color(244, 202, 158);
  138. m_active_window_title_color = Color::White;
  139. m_inactive_window_border_color = Color(128, 128, 128);
  140. m_inactive_window_border_color2 = Color(192, 192, 192);
  141. m_inactive_window_title_color = Color(213, 208, 199);
  142. m_dragging_window_border_color = Color(161, 50, 13);
  143. m_dragging_window_border_color2 = Color(250, 220, 187);
  144. m_dragging_window_title_color = Color::White;
  145. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  146. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  147. {
  148. LOCKER(m_wallpaper_path.lock());
  149. m_wallpaper_path.resource() = "/res/wallpapers/gray-wood.rgb";
  150. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  151. }
  152. ProcFS::the().add_sys_bool("wm_flash_flush", m_flash_flush);
  153. ProcFS::the().add_sys_string("wm_wallpaper", m_wallpaper_path, [this] {
  154. LOCKER(m_wallpaper_path.lock());
  155. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  156. invalidate(m_screen_rect);
  157. });
  158. invalidate();
  159. compose();
  160. }
  161. WSWindowManager::~WSWindowManager()
  162. {
  163. }
  164. static const char* s_close_button_bitmap_data = {
  165. "## ##"
  166. "### ###"
  167. " ###### "
  168. " #### "
  169. " ## "
  170. " #### "
  171. " ###### "
  172. "### ###"
  173. "## ##"
  174. };
  175. static CharacterBitmap* s_close_button_bitmap;
  176. static const int s_close_button_bitmap_width = 8;
  177. static const int s_close_button_bitmap_height = 9;
  178. void WSWindowManager::paint_window_frame(WSWindow& window)
  179. {
  180. LOCKER(m_lock);
  181. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  182. auto titlebar_rect = title_bar_rect(window.rect());
  183. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  184. auto outer_rect = outer_window_rect(window.rect());
  185. auto border_rect = border_window_rect(window.rect());
  186. auto close_button_rect = close_button_rect_for_window(window.rect());
  187. auto titlebar_title_rect = titlebar_inner_rect;
  188. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  189. Rect inner_border_rect {
  190. window.x() - 1,
  191. window.y() - 1,
  192. window.width() + 2,
  193. window.height() + 2
  194. };
  195. Color title_color;
  196. Color border_color;
  197. Color border_color2;
  198. Color middle_border_color;
  199. if (&window == m_drag_window.ptr()) {
  200. border_color = m_dragging_window_border_color;
  201. border_color2 = m_dragging_window_border_color2;
  202. title_color = m_dragging_window_title_color;
  203. middle_border_color = Color::White;
  204. } else if (&window == m_active_window.ptr()) {
  205. border_color = m_active_window_border_color;
  206. border_color2 = m_active_window_border_color2;
  207. title_color = m_active_window_title_color;
  208. middle_border_color = Color::MidGray;
  209. } else {
  210. border_color = m_inactive_window_border_color;
  211. border_color2 = m_inactive_window_border_color2;
  212. title_color = m_inactive_window_title_color;
  213. middle_border_color = Color::MidGray;
  214. }
  215. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  216. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  217. m_back_painter->draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  218. }
  219. m_back_painter->draw_rect(border_rect, middle_border_color);
  220. m_back_painter->draw_rect(outer_rect, border_color);
  221. m_back_painter->draw_rect(inner_border_rect, border_color);
  222. m_back_painter->draw_text(titlebar_title_rect, window.title(), Painter::TextAlignment::CenterLeft, title_color);
  223. if (!s_close_button_bitmap)
  224. s_close_button_bitmap = CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  225. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  226. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  227. auto x_location = close_button_rect.center();
  228. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  229. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  230. #ifdef DEBUG_WID_IN_TITLE_BAR
  231. Color metadata_color(96, 96, 96);
  232. m_back_painter->draw_text(
  233. titlebar_inner_rect,
  234. String::format("%d:%d", window.pid(), window.window_id()),
  235. Painter::TextAlignment::CenterRight,
  236. metadata_color
  237. );
  238. #endif
  239. }
  240. void WSWindowManager::add_window(WSWindow& window)
  241. {
  242. LOCKER(m_lock);
  243. m_windows.set(&window);
  244. m_windows_in_order.append(&window);
  245. if (!active_window())
  246. set_active_window(&window);
  247. }
  248. void WSWindowManager::move_to_front(WSWindow& window)
  249. {
  250. LOCKER(m_lock);
  251. if (m_windows_in_order.tail() != &window)
  252. invalidate(window);
  253. m_windows_in_order.remove(&window);
  254. m_windows_in_order.append(&window);
  255. }
  256. void WSWindowManager::remove_window(WSWindow& window)
  257. {
  258. LOCKER(m_lock);
  259. if (!m_windows.contains(&window))
  260. return;
  261. invalidate(window);
  262. m_windows.remove(&window);
  263. m_windows_in_order.remove(&window);
  264. if (!active_window() && !m_windows.is_empty())
  265. set_active_window(*m_windows.begin());
  266. }
  267. void WSWindowManager::notify_title_changed(WSWindow& window)
  268. {
  269. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  270. invalidate(outer_window_rect(window.rect()));
  271. }
  272. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  273. {
  274. printf("[WM] WSWindow %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, old_rect.x(), old_rect.y(), old_rect.width(), old_rect.height(), new_rect.x(), new_rect.y(), new_rect.width(), new_rect.height());
  275. ASSERT_INTERRUPTS_ENABLED();
  276. invalidate(outer_window_rect(old_rect));
  277. invalidate(outer_window_rect(new_rect));
  278. }
  279. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  280. {
  281. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  282. #ifdef DRAG_DEBUG
  283. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  284. #endif
  285. m_drag_window = window.make_weak_ptr();;
  286. m_drag_origin = event.position();
  287. m_drag_window_origin = window.position();
  288. m_drag_start_rect = outer_window_rect(window.rect());
  289. window.set_is_being_dragged(true);
  290. invalidate(window);
  291. return;
  292. }
  293. }
  294. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  295. {
  296. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  297. WSMessage message(WSMessage::WindowCloseRequest);
  298. window.on_message(message);
  299. return;
  300. }
  301. }
  302. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  303. {
  304. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  305. if (m_drag_window) {
  306. #ifdef DRAG_DEBUG
  307. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  308. #endif
  309. invalidate(*m_drag_window);
  310. m_drag_window->set_is_being_dragged(false);
  311. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  312. m_drag_window = nullptr;
  313. return;
  314. }
  315. }
  316. if (event.type() == WSMessage::MouseMove) {
  317. if (m_drag_window) {
  318. auto old_window_rect = m_drag_window->rect();
  319. Point pos = m_drag_window_origin;
  320. #ifdef DRAG_DEBUG
  321. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  322. #endif
  323. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  324. m_drag_window->set_position_without_repaint(pos);
  325. invalidate(outer_window_rect(old_window_rect));
  326. invalidate(outer_window_rect(m_drag_window->rect()));
  327. return;
  328. }
  329. }
  330. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  331. if (!window->global_cursor_tracking())
  332. continue;
  333. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  334. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  335. window->on_message(*local_event);
  336. }
  337. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  338. if (title_bar_rect(window->rect()).contains(event.position())) {
  339. if (event.type() == WSMessage::MouseDown) {
  340. move_to_front(*window);
  341. set_active_window(window);
  342. }
  343. if (close_button_rect_for_window(window->rect()).contains(event.position())) {
  344. handle_close_button_mouse_event(*window, event);
  345. return;
  346. }
  347. handle_titlebar_mouse_event(*window, event);
  348. return;
  349. }
  350. if (window->rect().contains(event.position())) {
  351. if (event.type() == WSMessage::MouseDown) {
  352. move_to_front(*window);
  353. set_active_window(window);
  354. }
  355. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  356. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  357. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  358. window->on_message(*local_event);
  359. return;
  360. }
  361. }
  362. }
  363. void WSWindowManager::compose()
  364. {
  365. LOCKER(m_lock);
  366. auto dirty_rects = move(m_dirty_rects);
  367. auto cursor_location = m_screen.cursor_location();
  368. dirty_rects.append(m_last_cursor_rect);
  369. dirty_rects.append({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  370. #ifdef DEBUG_COUNTERS
  371. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  372. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  373. #endif
  374. auto any_window_contains_rect = [this] (const Rect& r) {
  375. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  376. if (outer_window_rect(window->rect()).contains(r))
  377. return true;
  378. }
  379. return false;
  380. };
  381. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  382. auto window_rect = outer_window_rect(window.rect());
  383. for (auto& dirty_rect : dirty_rects) {
  384. if (dirty_rect.intersects(window_rect))
  385. return true;
  386. }
  387. return false;
  388. };
  389. for (auto& dirty_rect : dirty_rects) {
  390. if (any_window_contains_rect(dirty_rect)) {
  391. continue;
  392. }
  393. LOCKER(m_wallpaper_path.lock());
  394. if (!m_wallpaper)
  395. m_back_painter->fill_rect(dirty_rect, m_background_color);
  396. else
  397. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  398. }
  399. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  400. WSWindowLocker locker(*window);
  401. RetainPtr<GraphicsBitmap> backing = window->backing();
  402. if (!backing)
  403. continue;
  404. if (!any_dirty_rect_intersects_window(*window))
  405. continue;
  406. for (auto& dirty_rect : dirty_rects) {
  407. m_back_painter->set_clip_rect(dirty_rect);
  408. paint_window_frame(*window);
  409. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  410. if (dirty_rect_in_window_coordinates.is_empty())
  411. continue;
  412. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  413. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  414. auto dst = window->position();
  415. dst.move_by(dirty_rect_in_window_coordinates.location());
  416. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  417. m_back_painter->clear_clip_rect();
  418. }
  419. m_back_painter->clear_clip_rect();
  420. }
  421. draw_cursor();
  422. if (m_flash_flush.lock_and_copy()) {
  423. for (auto& rect : dirty_rects)
  424. m_front_painter->fill_rect(rect, Color::Yellow);
  425. }
  426. flip_buffers();
  427. for (auto& r : dirty_rects)
  428. flush(r);
  429. }
  430. void WSWindowManager::invalidate_cursor()
  431. {
  432. LOCKER(m_lock);
  433. auto cursor_location = m_screen.cursor_location();
  434. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  435. invalidate(cursor_rect);
  436. }
  437. void WSWindowManager::draw_cursor()
  438. {
  439. ASSERT_INTERRUPTS_ENABLED();
  440. LOCKER(m_lock);
  441. auto cursor_location = m_screen.cursor_location();
  442. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  443. Color inner_color = Color::White;
  444. Color outer_color = Color::Black;
  445. if (m_screen.left_mouse_button_pressed())
  446. swap(inner_color, outer_color);
  447. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  448. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  449. m_last_cursor_rect = cursor_rect;
  450. }
  451. void WSWindowManager::on_message(WSMessage& message)
  452. {
  453. ASSERT_INTERRUPTS_ENABLED();
  454. LOCKER(m_lock);
  455. if (message.is_mouse_event())
  456. return process_mouse_event(static_cast<WSMouseEvent&>(message));
  457. if (message.is_key_event()) {
  458. // FIXME: This is a good place to hook key events globally. :)
  459. if (m_active_window)
  460. return m_active_window->on_message(message);
  461. return;
  462. }
  463. if (message.type() == WSMessage::WM_DeferredCompose) {
  464. m_pending_compose_event = false;
  465. compose();
  466. return;
  467. }
  468. }
  469. void WSWindowManager::set_active_window(WSWindow* window)
  470. {
  471. LOCKER(m_lock);
  472. if (window == m_active_window.ptr())
  473. return;
  474. if (auto* previously_active_window = m_active_window.ptr()) {
  475. WSMessageLoop::the().post_message(previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  476. invalidate(*previously_active_window);
  477. }
  478. m_active_window = window->make_weak_ptr();
  479. if (m_active_window) {
  480. WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
  481. invalidate(*m_active_window);
  482. }
  483. }
  484. void WSWindowManager::invalidate()
  485. {
  486. LOCKER(m_lock);
  487. m_dirty_rects.clear_with_capacity();
  488. m_dirty_rects.append(m_screen_rect);
  489. }
  490. void WSWindowManager::invalidate(const Rect& a_rect)
  491. {
  492. LOCKER(m_lock);
  493. auto rect = Rect::intersection(a_rect, m_screen_rect);
  494. if (rect.is_empty())
  495. return;
  496. for (auto& r : m_dirty_rects) {
  497. if (r.contains(rect))
  498. return;
  499. if (r.intersects(rect)) {
  500. // Unite with the existing dirty rect.
  501. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  502. r = r.united(rect);
  503. return;
  504. }
  505. }
  506. m_dirty_rects.append(rect);
  507. if (!m_pending_compose_event) {
  508. ASSERT_INTERRUPTS_ENABLED();
  509. WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  510. m_pending_compose_event = true;
  511. }
  512. }
  513. void WSWindowManager::invalidate(const WSWindow& window)
  514. {
  515. ASSERT_INTERRUPTS_ENABLED();
  516. LOCKER(m_lock);
  517. invalidate(outer_window_rect(window.rect()));
  518. }
  519. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  520. {
  521. if (rect.is_empty()) {
  522. invalidate(window);
  523. return;
  524. }
  525. ASSERT_INTERRUPTS_ENABLED();
  526. LOCKER(m_lock);
  527. auto outer_rect = outer_window_rect(window.rect());
  528. auto inner_rect = rect;
  529. inner_rect.move_by(window.position());
  530. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  531. inner_rect.intersect(outer_rect);
  532. invalidate(inner_rect);
  533. }
  534. void WSWindowManager::flush(const Rect& a_rect)
  535. {
  536. auto rect = Rect::intersection(a_rect, m_screen_rect);
  537. #ifdef DEBUG_COUNTERS
  538. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  539. #endif
  540. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  541. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  542. size_t pitch = m_back_bitmap->pitch();
  543. for (int y = 0; y < rect.height(); ++y) {
  544. fast_dword_copy(back_ptr, front_ptr, rect.width());
  545. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  546. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  547. }
  548. }