mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:17:42 +00:00
WindowServer: Avoid overdraw by shattering dirty rects into unique shards.
The algorithm I came up with is O(n^2) but given the small numbers of rects we're typically working with, it doesn't really matter. May need to revisit this in the future if we find ourselves with a huge number of rects.
This commit is contained in:
parent
420b7bd55f
commit
98784ad3cb
7 changed files with 127 additions and 21 deletions
46
SharedGraphics/DisjointRectSet.cpp
Normal file
46
SharedGraphics/DisjointRectSet.cpp
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#include <SharedGraphics/DisjointRectSet.h>
|
||||||
|
|
||||||
|
void DisjointRectSet::add(const Rect& new_rect)
|
||||||
|
{
|
||||||
|
for (auto& rect : m_rects) {
|
||||||
|
if (rect.contains(new_rect))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_rects.append(new_rect);
|
||||||
|
if (m_rects.size() > 1)
|
||||||
|
shatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DisjointRectSet::shatter()
|
||||||
|
{
|
||||||
|
Vector<Rect> output;
|
||||||
|
bool pass_had_intersections = false;
|
||||||
|
do {
|
||||||
|
pass_had_intersections = false;
|
||||||
|
output.clear_with_capacity();
|
||||||
|
for (size_t i = 0; i < m_rects.size(); ++i) {
|
||||||
|
auto& r1 = m_rects[i];
|
||||||
|
bool r1_had_intersections = false;
|
||||||
|
for (size_t j = 0; j < m_rects.size(); ++j) {
|
||||||
|
if (i == j)
|
||||||
|
continue;
|
||||||
|
auto& r2 = m_rects[j];
|
||||||
|
if (!r1.intersects(r2))
|
||||||
|
continue;
|
||||||
|
pass_had_intersections = true;
|
||||||
|
auto pieces = r1.shatter(r2);
|
||||||
|
for (auto& piece : pieces)
|
||||||
|
output.append(piece);
|
||||||
|
m_rects.remove(i);
|
||||||
|
for (; i < m_rects.size(); ++i)
|
||||||
|
output.append(m_rects[i]);
|
||||||
|
goto next_pass;
|
||||||
|
}
|
||||||
|
if (!r1_had_intersections)
|
||||||
|
output.append(r1);
|
||||||
|
}
|
||||||
|
next_pass:
|
||||||
|
swap(output, m_rects);
|
||||||
|
} while(pass_had_intersections);
|
||||||
|
}
|
23
SharedGraphics/DisjointRectSet.h
Normal file
23
SharedGraphics/DisjointRectSet.h
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
#include <SharedGraphics/Rect.h>
|
||||||
|
|
||||||
|
class DisjointRectSet {
|
||||||
|
public:
|
||||||
|
DisjointRectSet() { }
|
||||||
|
~DisjointRectSet() { }
|
||||||
|
DisjointRectSet(DisjointRectSet&& other) : m_rects(move(other.m_rects)) { }
|
||||||
|
|
||||||
|
void add(const Rect&);
|
||||||
|
|
||||||
|
void clear() { m_rects.clear(); }
|
||||||
|
void clear_with_capacity() { m_rects.clear_with_capacity(); }
|
||||||
|
const Vector<Rect>& rects() const { return m_rects; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void shatter();
|
||||||
|
|
||||||
|
Vector<Rect> m_rects;
|
||||||
|
};
|
||||||
|
|
|
@ -33,3 +33,47 @@ Rect Rect::united(const Rect& other) const
|
||||||
rect.set_bottom(max(bottom(), other.bottom()));
|
rect.set_bottom(max(bottom(), other.bottom()));
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<Rect> Rect::shatter(const Rect& hammer) const
|
||||||
|
{
|
||||||
|
Vector<Rect> pieces;
|
||||||
|
if (!intersects(hammer)) {
|
||||||
|
pieces.append(*this);
|
||||||
|
pieces.append(hammer);
|
||||||
|
return pieces;
|
||||||
|
}
|
||||||
|
Rect top_shard {
|
||||||
|
x(),
|
||||||
|
y(),
|
||||||
|
width(),
|
||||||
|
hammer.y() - y()
|
||||||
|
};
|
||||||
|
Rect bottom_shard {
|
||||||
|
x(),
|
||||||
|
hammer.y() + hammer.height(),
|
||||||
|
width(),
|
||||||
|
(y() + height()) - (hammer.y() + hammer.height())
|
||||||
|
};
|
||||||
|
Rect left_shard {
|
||||||
|
x(),
|
||||||
|
max(hammer.y(), y()),
|
||||||
|
hammer.x() - x(),
|
||||||
|
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
|
||||||
|
};
|
||||||
|
Rect right_shard {
|
||||||
|
hammer.x() + hammer.width(),
|
||||||
|
max(hammer.y(), y()),
|
||||||
|
(x() + width() - 1) - (hammer.x() + hammer.width() - 1),
|
||||||
|
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
|
||||||
|
};
|
||||||
|
if (intersects(top_shard))
|
||||||
|
pieces.append(top_shard);
|
||||||
|
if (intersects(bottom_shard))
|
||||||
|
pieces.append(bottom_shard);
|
||||||
|
if (intersects(left_shard))
|
||||||
|
pieces.append(left_shard);
|
||||||
|
if (intersects(right_shard))
|
||||||
|
pieces.append(right_shard);
|
||||||
|
|
||||||
|
return pieces;
|
||||||
|
}
|
||||||
|
|
|
@ -170,6 +170,8 @@ public:
|
||||||
Point location() const { return m_location; }
|
Point location() const { return m_location; }
|
||||||
Size size() const { return m_size; }
|
Size size() const { return m_size; }
|
||||||
|
|
||||||
|
Vector<Rect> shatter(const Rect& hammer) const;
|
||||||
|
|
||||||
operator WSAPI_Rect() const;
|
operator WSAPI_Rect() const;
|
||||||
|
|
||||||
bool operator==(const Rect& other) const
|
bool operator==(const Rect& other) const
|
||||||
|
|
|
@ -4,6 +4,7 @@ SHAREDGRAPHICS_OBJS = \
|
||||||
../SharedGraphics/Rect.o \
|
../SharedGraphics/Rect.o \
|
||||||
../SharedGraphics/GraphicsBitmap.o \
|
../SharedGraphics/GraphicsBitmap.o \
|
||||||
../SharedGraphics/CharacterBitmap.o \
|
../SharedGraphics/CharacterBitmap.o \
|
||||||
|
../SharedGraphics/DisjointRectSet.o \
|
||||||
../SharedGraphics/Color.o
|
../SharedGraphics/Color.o
|
||||||
|
|
||||||
WINDOWSERVER_OBJS = \
|
WINDOWSERVER_OBJS = \
|
||||||
|
|
|
@ -614,10 +614,10 @@ void WSWindowManager::compose()
|
||||||
{
|
{
|
||||||
auto dirty_rects = move(m_dirty_rects);
|
auto dirty_rects = move(m_dirty_rects);
|
||||||
auto cursor_location = m_screen.cursor_location();
|
auto cursor_location = m_screen.cursor_location();
|
||||||
dirty_rects.append(m_last_cursor_rect);
|
dirty_rects.add(m_last_cursor_rect);
|
||||||
dirty_rects.append({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
|
dirty_rects.add({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
|
||||||
#ifdef DEBUG_COUNTERS
|
#ifdef DEBUG_COUNTERS
|
||||||
dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
|
dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto any_opaque_window_contains_rect = [this] (const Rect& r) {
|
auto any_opaque_window_contains_rect = [this] (const Rect& r) {
|
||||||
|
@ -639,14 +639,14 @@ void WSWindowManager::compose()
|
||||||
|
|
||||||
auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
|
auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
|
||||||
auto window_rect = outer_window_rect(window.rect());
|
auto window_rect = outer_window_rect(window.rect());
|
||||||
for (auto& dirty_rect : dirty_rects) {
|
for (auto& dirty_rect : dirty_rects.rects()) {
|
||||||
if (dirty_rect.intersects(window_rect))
|
if (dirty_rect.intersects(window_rect))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
for (auto& dirty_rect : dirty_rects) {
|
for (auto& dirty_rect : dirty_rects.rects()) {
|
||||||
if (any_opaque_window_contains_rect(dirty_rect))
|
if (any_opaque_window_contains_rect(dirty_rect))
|
||||||
continue;
|
continue;
|
||||||
if (!m_wallpaper)
|
if (!m_wallpaper)
|
||||||
|
@ -661,7 +661,7 @@ void WSWindowManager::compose()
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
if (!any_dirty_rect_intersects_window(window))
|
if (!any_dirty_rect_intersects_window(window))
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
for (auto& dirty_rect : dirty_rects) {
|
for (auto& dirty_rect : dirty_rects.rects()) {
|
||||||
m_back_painter->set_clip_rect(dirty_rect);
|
m_back_painter->set_clip_rect(dirty_rect);
|
||||||
paint_window_frame(window);
|
paint_window_frame(window);
|
||||||
Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
|
Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
|
||||||
|
@ -685,12 +685,12 @@ void WSWindowManager::compose()
|
||||||
draw_cursor();
|
draw_cursor();
|
||||||
|
|
||||||
if (m_flash_flush) {
|
if (m_flash_flush) {
|
||||||
for (auto& rect : dirty_rects)
|
for (auto& rect : dirty_rects.rects())
|
||||||
m_front_painter->fill_rect(rect, Color::Yellow);
|
m_front_painter->fill_rect(rect, Color::Yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
flip_buffers();
|
flip_buffers();
|
||||||
for (auto& r : dirty_rects)
|
for (auto& r : dirty_rects.rects())
|
||||||
flush(r);
|
flush(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -808,18 +808,7 @@ void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compos
|
||||||
if (rect.is_empty())
|
if (rect.is_empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (auto& r : m_dirty_rects) {
|
m_dirty_rects.add(rect);
|
||||||
if (r.contains(rect))
|
|
||||||
return;
|
|
||||||
if (r.intersects(rect)) {
|
|
||||||
// Unite with the existing dirty rect.
|
|
||||||
// FIXME: It would be much nicer to compute the exact rects needing repaint.
|
|
||||||
r = r.united(rect);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dirty_rects.append(rect);
|
|
||||||
|
|
||||||
if (should_schedule_compose_event && !m_pending_compose_event) {
|
if (should_schedule_compose_event && !m_pending_compose_event) {
|
||||||
WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
|
WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <SharedGraphics/Rect.h>
|
#include <SharedGraphics/Rect.h>
|
||||||
#include <SharedGraphics/Color.h>
|
#include <SharedGraphics/Color.h>
|
||||||
#include <SharedGraphics/Painter.h>
|
#include <SharedGraphics/Painter.h>
|
||||||
|
#include <SharedGraphics/DisjointRectSet.h>
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
#include <AK/InlineLinkedList.h>
|
#include <AK/InlineLinkedList.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
|
@ -127,7 +128,7 @@ private:
|
||||||
RetainPtr<GraphicsBitmap> m_front_bitmap;
|
RetainPtr<GraphicsBitmap> m_front_bitmap;
|
||||||
RetainPtr<GraphicsBitmap> m_back_bitmap;
|
RetainPtr<GraphicsBitmap> m_back_bitmap;
|
||||||
|
|
||||||
Vector<Rect> m_dirty_rects;
|
DisjointRectSet m_dirty_rects;
|
||||||
|
|
||||||
bool m_pending_compose_event { false };
|
bool m_pending_compose_event { false };
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue