Repository navigation
Expand file tree
/
Copy pathDirtyRegion.cpp
More file actions
184 lines (170 loc) · 4.79 KB
/
Copy pathDirtyRegion.cpp
File metadata and controls
184 lines (170 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "DirtyRegion.h"
#include <algorithm>
namespace DekiRendering
{
void DirtyRegion::Reset(int32_t width, int32_t height)
{
m_Width = width < 0 ? 0 : width;
m_Height = height < 0 ? 0 : height;
m_Full = false;
m_Rects.clear(); // keeps capacity, so no allocation once warmed up
}
void DirtyRegion::SetFull()
{
m_Full = true;
m_Rects.clear();
}
void DirtyRegion::Add(int32_t left, int32_t top, int32_t right, int32_t bottom)
{
if (m_Full)
{
return;
}
left = std::max<int32_t>(left, 0);
top = std::max<int32_t>(top, 0);
right = std::min<int32_t>(right, m_Width);
bottom = std::min<int32_t>(bottom, m_Height);
if (right <= left || bottom <= top)
{
return;
}
// A rectangle inside an existing one adds nothing; one that contains an
// existing one replaces it. Both are common (a sprite drawn twice, a clip
// child inside its parent's area) and keep the set small without a merge.
for (Deki::Rect& r : m_Rects)
{
if (left >= r.left && top >= r.top && right <= r.right && bottom <= r.bottom)
{
return;
}
if (left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom)
{
r = { left, top, right, bottom };
CollapseIfCovered();
return;
}
}
m_Rects.push_back({ left, top, right, bottom });
while (m_Rects.size() > m_MergeThreshold)
{
MergeOnePair();
}
CollapseIfCovered();
}
void DirtyRegion::Union(const DirtyRegion& other)
{
if (m_Full)
{
return;
}
if (other.m_Full)
{
SetFull();
return;
}
for (const Deki::Rect& r : other.m_Rects)
{
Add(r.left, r.top, r.right, r.bottom);
}
}
void DirtyRegion::Align(int32_t granularity)
{
if (m_Full || granularity <= 1)
{
return;
}
for (Deki::Rect& r : m_Rects)
{
r.left = (r.left / granularity) * granularity;
r.top = (r.top / granularity) * granularity;
r.right = std::min<int32_t>(((r.right + granularity - 1) / granularity) * granularity, m_Width);
r.bottom = std::min<int32_t>(((r.bottom + granularity - 1) / granularity) * granularity, m_Height);
}
// Alignment can make neighbours identical or nested; fold those.
std::vector<Deki::Rect> aligned;
aligned.swap(m_Rects);
for (const Deki::Rect& r : aligned)
{
Add(r.left, r.top, r.right, r.bottom);
}
}
int64_t DirtyRegion::CoveredArea() const
{
if (m_Full)
{
return static_cast<int64_t>(m_Width) * m_Height;
}
int64_t area = 0;
for (const Deki::Rect& r : m_Rects)
{
area += static_cast<int64_t>(r.Width()) * r.Height();
}
return area;
}
bool DirtyRegion::Contains(int32_t x, int32_t y) const
{
if (x < 0 || y < 0 || x >= m_Width || y >= m_Height)
{
return false;
}
if (m_Full)
{
return true;
}
for (const Deki::Rect& r : m_Rects)
{
if (x >= r.left && x < r.right && y >= r.top && y < r.bottom)
{
return true;
}
}
return false;
}
void DirtyRegion::MergeOnePair()
{
// Merge the pair whose bounding union adds the least new area. Quadratic
// in the rectangle count, which the threshold keeps small.
const size_t n = m_Rects.size();
if (n < 2)
{
return;
}
size_t bestA = 0, bestB = 1;
int64_t bestGrowth = INT64_MAX;
for (size_t i = 0; i < n; ++i)
{
const Deki::Rect& a = m_Rects[i];
const int64_t areaA = static_cast<int64_t>(a.Width()) * a.Height();
for (size_t j = i + 1; j < n; ++j)
{
const Deki::Rect& b = m_Rects[j];
const int32_t l = std::min(a.left, b.left), t = std::min(a.top, b.top);
const int32_t r = std::max(a.right, b.right), bt = std::max(a.bottom, b.bottom);
const int64_t unionArea = static_cast<int64_t>(r - l) * (bt - t);
const int64_t growth = unionArea - areaA - static_cast<int64_t>(b.Width()) * b.Height();
if (growth < bestGrowth)
{
bestGrowth = growth;
bestA = i;
bestB = j;
}
}
}
Deki::Rect& a = m_Rects[bestA];
const Deki::Rect b = m_Rects[bestB];
a = { std::min(a.left, b.left), std::min(a.top, b.top), std::max(a.right, b.right), std::max(a.bottom, b.bottom) };
m_Rects.erase(m_Rects.begin() + static_cast<std::ptrdiff_t>(bestB));
}
void DirtyRegion::CollapseIfCovered()
{
if (m_Full || m_Width <= 0 || m_Height <= 0)
{
return;
}
const int64_t total = static_cast<int64_t>(m_Width) * m_Height;
if (static_cast<double>(CoveredArea()) >= static_cast<double>(total) * m_FullRatio)
{
SetFull();
}
}
} // namespace DekiRendering