1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +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:
Andreas Kling 2019-02-19 14:49:23 +01:00
parent 420b7bd55f
commit 98784ad3cb
7 changed files with 127 additions and 21 deletions

View 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);
}

View 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;
};

View file

@ -33,3 +33,47 @@ Rect Rect::united(const Rect& other) const
rect.set_bottom(max(bottom(), other.bottom()));
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;
}

View file

@ -170,6 +170,8 @@ public:
Point location() const { return m_location; }
Size size() const { return m_size; }
Vector<Rect> shatter(const Rect& hammer) const;
operator WSAPI_Rect() const;
bool operator==(const Rect& other) const