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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <SkPath.h>
#include <SkRegion.h>
#include <gtest/gtest.h>
#include "ClipArea.h"
#include "Matrix.h"
#include "Rect.h"
#include "utils/LinearAllocator.h"
namespace android {
namespace uirenderer {
static Rect kViewportBounds(2048, 2048);
static ClipArea createClipArea() {
ClipArea area;
area.setViewportDimensions(kViewportBounds.getWidth(), kViewportBounds.getHeight());
return area;
}
TEST(TransformedRectangle, basics) {
Rect r(0, 0, 100, 100);
Matrix4 minus90;
minus90.loadRotate(-90);
minus90.mapRect(r);
Rect r2(20, 40, 120, 60);
Matrix4 m90;
m90.loadRotate(90);
TransformedRectangle tr(r, m90);
EXPECT_TRUE(tr.canSimplyIntersectWith(tr));
Matrix4 m0;
TransformedRectangle tr0(r2, m0);
EXPECT_FALSE(tr.canSimplyIntersectWith(tr0));
Matrix4 m45;
m45.loadRotate(45);
TransformedRectangle tr2(r, m45);
EXPECT_FALSE(tr2.canSimplyIntersectWith(tr));
}
TEST(RectangleList, basics) {
RectangleList list;
EXPECT_TRUE(list.isEmpty());
Rect r(0, 0, 100, 100);
Matrix4 m45;
m45.loadRotate(45);
list.set(r, m45);
EXPECT_FALSE(list.isEmpty());
Rect r2(20, 20, 200, 200);
list.intersectWith(r2, m45);
EXPECT_FALSE(list.isEmpty());
EXPECT_EQ(1, list.getTransformedRectanglesCount());
Rect r3(20, 20, 200, 200);
Matrix4 m30;
m30.loadRotate(30);
list.intersectWith(r2, m30);
EXPECT_FALSE(list.isEmpty());
EXPECT_EQ(2, list.getTransformedRectanglesCount());
SkRegion clip;
clip.setRect(0, 0, 2000, 2000);
SkRegion rgn(list.convertToRegion(clip));
EXPECT_FALSE(rgn.isEmpty());
}
TEST(ClipArea, basics) {
ClipArea area(createClipArea());
EXPECT_FALSE(area.isEmpty());
}
TEST(ClipArea, paths) {
ClipArea area(createClipArea());
SkPath path;
SkScalar r = 100;
path.addCircle(r, r, r);
area.clipPathWithTransform(path, &Matrix4::identity(), SkRegion::kIntersect_Op);
EXPECT_FALSE(area.isEmpty());
EXPECT_FALSE(area.isSimple());
EXPECT_FALSE(area.isRectangleList());
Rect clipRect(area.getClipRect());
Rect expected(0, 0, r * 2, r * 2);
EXPECT_EQ(expected, clipRect);
SkRegion clipRegion(area.getClipRegion());
auto skRect(clipRegion.getBounds());
Rect regionBounds;
regionBounds.set(skRect);
EXPECT_EQ(expected, regionBounds);
}
TEST(ClipArea, replaceNegative) {
ClipArea area(createClipArea());
area.setClip(0, 0, 100, 100);
Rect expected(-50, -50, 50, 50);
area.clipRectWithTransform(expected, &Matrix4::identity(), SkRegion::kReplace_Op);
EXPECT_EQ(expected, area.getClipRect());
}
TEST(ClipArea, serializeClip) {
ClipArea area(createClipArea());
LinearAllocator allocator;
// unset clip
EXPECT_EQ(nullptr, area.serializeClip(allocator));
// rect clip
area.setClip(0, 0, 200, 200);
{
auto serializedClip = area.serializeClip(allocator);
ASSERT_NE(nullptr, serializedClip);
ASSERT_EQ(ClipMode::Rectangle, serializedClip->mode);
ASSERT_FALSE(serializedClip->intersectWithRoot) << "No replace, so no intersectWithRoot";
EXPECT_EQ(Rect(200, 200), serializedClip->rect);
EXPECT_EQ(serializedClip, area.serializeClip(allocator))
<< "Requery of clip on unmodified ClipArea must return same pointer.";
}
// rect list
Matrix4 rotate;
rotate.loadRotate(5.0f);
area.clipRectWithTransform(Rect(50, 50, 150, 150), &rotate, SkRegion::kIntersect_Op);
{
auto serializedClip = area.serializeClip(allocator);
ASSERT_NE(nullptr, serializedClip);
ASSERT_EQ(ClipMode::RectangleList, serializedClip->mode);
ASSERT_FALSE(serializedClip->intersectWithRoot) << "No replace, so no intersectWithRoot";
auto clipRectList = reinterpret_cast<const ClipRectList*>(serializedClip);
EXPECT_EQ(2, clipRectList->rectList.getTransformedRectanglesCount());
EXPECT_EQ(Rect(37, 54, 145, 163), clipRectList->rect);
EXPECT_EQ(serializedClip, area.serializeClip(allocator))
<< "Requery of clip on unmodified ClipArea must return same pointer.";
}
// region
SkPath circlePath;
circlePath.addCircle(100, 100, 100);
area.clipPathWithTransform(circlePath, &Matrix4::identity(), SkRegion::kReplace_Op);
{
auto serializedClip = area.serializeClip(allocator);
ASSERT_NE(nullptr, serializedClip);
ASSERT_EQ(ClipMode::Region, serializedClip->mode);
ASSERT_TRUE(serializedClip->intersectWithRoot) << "Replace op, so expect intersectWithRoot";
auto clipRegion = reinterpret_cast<const ClipRegion*>(serializedClip);
EXPECT_EQ(SkIRect::MakeWH(200, 200), clipRegion->region.getBounds())
<< "Clip region should be 200x200";
EXPECT_EQ(Rect(200, 200), clipRegion->rect);
EXPECT_EQ(serializedClip, area.serializeClip(allocator))
<< "Requery of clip on unmodified ClipArea must return same pointer.";
}
}
TEST(ClipArea, serializeClip_pathIntersectWithRoot) {
ClipArea area(createClipArea());
LinearAllocator allocator;
SkPath circlePath;
circlePath.addCircle(100, 100, 100);
area.clipPathWithTransform(circlePath, &Matrix4::identity(), SkRegion::kIntersect_Op);
auto serializedClip = area.serializeClip(allocator);
ASSERT_NE(nullptr, serializedClip);
EXPECT_FALSE(serializedClip->intersectWithRoot) << "No replace, so no intersectWithRoot";
}
TEST(ClipArea, serializeIntersectedClip) {
ClipArea area(createClipArea());
LinearAllocator allocator;
// simple state;
EXPECT_EQ(nullptr, area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity()));
area.setClip(0, 0, 200, 200);
{
auto origRectClip = area.serializeClip(allocator);
ASSERT_NE(nullptr, origRectClip);
EXPECT_EQ(origRectClip,
area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity()));
}
// rect
{
ClipRect recordedClip(Rect(100, 100));
Matrix4 translateScale;
translateScale.loadTranslate(100, 100, 0);
translateScale.scale(2, 3, 1);
auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
ASSERT_NE(nullptr, resolvedClip);
ASSERT_EQ(ClipMode::Rectangle, resolvedClip->mode);
EXPECT_EQ(Rect(100, 100, 200, 200), resolvedClip->rect);
EXPECT_EQ(resolvedClip,
area.serializeIntersectedClip(allocator, &recordedClip, translateScale))
<< "Must return previous serialization, since input is same";
ClipRect recordedClip2(Rect(100, 100));
EXPECT_NE(resolvedClip,
area.serializeIntersectedClip(allocator, &recordedClip2, translateScale))
<< "Shouldn't return previous serialization, since matrix location is different";
}
// rect list
Matrix4 rotate;
rotate.loadRotate(2.0f);
area.clipRectWithTransform(Rect(200, 200), &rotate, SkRegion::kIntersect_Op);
{
ClipRect recordedClip(Rect(100, 100));
auto resolvedClip =
area.serializeIntersectedClip(allocator, &recordedClip, Matrix4::identity());
ASSERT_NE(nullptr, resolvedClip);
ASSERT_EQ(ClipMode::RectangleList, resolvedClip->mode);
auto clipRectList = reinterpret_cast<const ClipRectList*>(resolvedClip);
EXPECT_EQ(2, clipRectList->rectList.getTransformedRectanglesCount());
}
// region
SkPath circlePath;
circlePath.addCircle(100, 100, 100);
area.clipPathWithTransform(circlePath, &Matrix4::identity(), SkRegion::kReplace_Op);
{
SkPath ovalPath;
ovalPath.addOval(SkRect::MakeLTRB(50, 0, 150, 200));
ClipRegion recordedClip;
recordedClip.region.setPath(ovalPath, SkRegion(SkIRect::MakeWH(200, 200)));
recordedClip.rect = Rect(200, 200);
Matrix4 translate10x20;
translate10x20.loadTranslate(10, 20, 0);
auto resolvedClip = area.serializeIntersectedClip(
allocator, &recordedClip,
translate10x20); // Note: only translate for now, others not handled correctly
ASSERT_NE(nullptr, resolvedClip);
ASSERT_EQ(ClipMode::Region, resolvedClip->mode);
auto clipRegion = reinterpret_cast<const ClipRegion*>(resolvedClip);
EXPECT_EQ(SkIRect::MakeLTRB(60, 20, 160, 200), clipRegion->region.getBounds());
}
}
TEST(ClipArea, serializeIntersectedClip_snap) {
ClipArea area(createClipArea());
area.setClip(100.2, 100.4, 500.6, 500.8);
LinearAllocator allocator;
{
// no recorded clip case
auto resolvedClip = area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity());
EXPECT_EQ(Rect(100, 100, 501, 501), resolvedClip->rect);
}
{
// recorded clip case
ClipRect recordedClip(Rect(100.12, 100.74));
Matrix4 translateScale;
translateScale.loadTranslate(100, 100, 0);
translateScale.scale(2, 3,
1); // recorded clip will have non-int coords, even after transform
auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
ASSERT_NE(nullptr, resolvedClip);
EXPECT_EQ(ClipMode::Rectangle, resolvedClip->mode);
EXPECT_EQ(Rect(100, 100, 300, 402), resolvedClip->rect);
}
}
TEST(ClipArea, serializeIntersectedClip_scale) {
ClipArea area(createClipArea());
area.setClip(0, 0, 400, 400);
LinearAllocator allocator;
SkPath circlePath;
circlePath.addCircle(50, 50, 50);
ClipRegion recordedClip;
recordedClip.region.setPath(circlePath, SkRegion(SkIRect::MakeWH(100, 100)));
recordedClip.rect = Rect(100, 100);
Matrix4 translateScale;
translateScale.loadTranslate(100, 100, 0);
translateScale.scale(2, 2, 1);
auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
ASSERT_NE(nullptr, resolvedClip);
EXPECT_EQ(ClipMode::Region, resolvedClip->mode);
EXPECT_EQ(Rect(100, 100, 300, 300), resolvedClip->rect);
auto clipRegion = reinterpret_cast<const ClipRegion*>(resolvedClip);
EXPECT_EQ(SkIRect::MakeLTRB(100, 100, 300, 300), clipRegion->region.getBounds());
}
TEST(ClipArea, applyTransformToRegion_identity) {
SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
ClipArea::applyTransformToRegion(Matrix4::identity(), ®ion);
EXPECT_TRUE(region.isRect());
EXPECT_EQ(SkIRect::MakeLTRB(1, 2, 3, 4), region.getBounds());
}
TEST(ClipArea, applyTransformToRegion_translate) {
SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
Matrix4 transform;
transform.loadTranslate(10, 20, 0);
ClipArea::applyTransformToRegion(transform, ®ion);
EXPECT_TRUE(region.isRect());
EXPECT_EQ(SkIRect::MakeLTRB(11, 22, 13, 24), region.getBounds());
}
TEST(ClipArea, applyTransformToRegion_scale) {
SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
Matrix4 transform;
transform.loadScale(2, 3, 1);
ClipArea::applyTransformToRegion(transform, ®ion);
EXPECT_TRUE(region.isRect());
EXPECT_EQ(SkIRect::MakeLTRB(2, 6, 6, 12), region.getBounds());
}
TEST(ClipArea, applyTransformToRegion_translateScale) {
SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
Matrix4 transform;
transform.translate(10, 20);
transform.scale(2, 3, 1);
ClipArea::applyTransformToRegion(transform, ®ion);
EXPECT_TRUE(region.isRect());
EXPECT_EQ(SkIRect::MakeLTRB(12, 26, 16, 32), region.getBounds());
}
TEST(ClipArea, applyTransformToRegion_rotate90) {
SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
Matrix4 transform;
transform.loadRotate(90);
ClipArea::applyTransformToRegion(transform, ®ion);
EXPECT_TRUE(region.isRect());
EXPECT_EQ(SkIRect::MakeLTRB(-4, 1, -2, 3), region.getBounds());
}
} // namespace uirenderer
} // namespace android
|