ClipAreaTests.cpp revision 04d46eb69fb4f4c4c332c36c6ae845da3b2ae848
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18#include <SkPath.h>
19#include <SkRegion.h>
20
21#include "ClipArea.h"
22
23#include "Matrix.h"
24#include "Rect.h"
25#include "utils/LinearAllocator.h"
26
27namespace android {
28namespace uirenderer {
29
30static Rect kViewportBounds(2048, 2048);
31
32static ClipArea createClipArea() {
33    ClipArea area;
34    area.setViewportDimensions(kViewportBounds.getWidth(), kViewportBounds.getHeight());
35    return area;
36}
37
38TEST(TransformedRectangle, basics) {
39    Rect r(0, 0, 100, 100);
40    Matrix4 minus90;
41    minus90.loadRotate(-90);
42    minus90.mapRect(r);
43    Rect r2(20, 40, 120, 60);
44
45    Matrix4 m90;
46    m90.loadRotate(90);
47    TransformedRectangle tr(r, m90);
48    EXPECT_TRUE(tr.canSimplyIntersectWith(tr));
49
50    Matrix4 m0;
51    TransformedRectangle tr0(r2, m0);
52    EXPECT_FALSE(tr.canSimplyIntersectWith(tr0));
53
54    Matrix4 m45;
55    m45.loadRotate(45);
56    TransformedRectangle tr2(r, m45);
57    EXPECT_FALSE(tr2.canSimplyIntersectWith(tr));
58}
59
60TEST(RectangleList, basics) {
61    RectangleList list;
62    EXPECT_TRUE(list.isEmpty());
63
64    Rect r(0, 0, 100, 100);
65    Matrix4 m45;
66    m45.loadRotate(45);
67    list.set(r, m45);
68    EXPECT_FALSE(list.isEmpty());
69
70    Rect r2(20, 20, 200, 200);
71    list.intersectWith(r2, m45);
72    EXPECT_FALSE(list.isEmpty());
73    EXPECT_EQ(1, list.getTransformedRectanglesCount());
74
75    Rect r3(20, 20, 200, 200);
76    Matrix4 m30;
77    m30.loadRotate(30);
78    list.intersectWith(r2, m30);
79    EXPECT_FALSE(list.isEmpty());
80    EXPECT_EQ(2, list.getTransformedRectanglesCount());
81
82    SkRegion clip;
83    clip.setRect(0, 0, 2000, 2000);
84    SkRegion rgn(list.convertToRegion(clip));
85    EXPECT_FALSE(rgn.isEmpty());
86}
87
88TEST(ClipArea, basics) {
89    ClipArea area(createClipArea());
90    EXPECT_FALSE(area.isEmpty());
91}
92
93TEST(ClipArea, paths) {
94    ClipArea area(createClipArea());
95    SkPath path;
96    SkScalar r = 100;
97    path.addCircle(r, r, r);
98    area.clipPathWithTransform(path, &Matrix4::identity(), SkRegion::kIntersect_Op);
99    EXPECT_FALSE(area.isEmpty());
100    EXPECT_FALSE(area.isSimple());
101    EXPECT_FALSE(area.isRectangleList());
102
103    Rect clipRect(area.getClipRect());
104    Rect expected(0, 0, r * 2, r * 2);
105    EXPECT_EQ(expected, clipRect);
106    SkRegion clipRegion(area.getClipRegion());
107    auto skRect(clipRegion.getBounds());
108    Rect regionBounds;
109    regionBounds.set(skRect);
110    EXPECT_EQ(expected, regionBounds);
111}
112
113TEST(ClipArea, replaceNegative) {
114    ClipArea area(createClipArea());
115    area.setClip(0, 0, 100, 100);
116
117    Rect expected(-50, -50, 50, 50);
118    area.clipRectWithTransform(expected, &Matrix4::identity(), SkRegion::kReplace_Op);
119    EXPECT_EQ(expected, area.getClipRect());
120}
121
122TEST(ClipArea, serializeClip) {
123    ClipArea area(createClipArea());
124    LinearAllocator allocator;
125
126    // unset clip
127    EXPECT_EQ(nullptr, area.serializeClip(allocator));
128
129    // rect clip
130    area.setClip(0, 0, 200, 200);
131    {
132        auto serializedClip = area.serializeClip(allocator);
133        ASSERT_NE(nullptr, serializedClip);
134        ASSERT_EQ(ClipMode::Rectangle, serializedClip->mode);
135        EXPECT_EQ(Rect(200, 200), serializedClip->rect);
136        EXPECT_EQ(serializedClip, area.serializeClip(allocator))
137                << "Requery of clip on unmodified ClipArea must return same pointer.";
138    }
139
140    // rect list
141    Matrix4 rotate;
142    rotate.loadRotate(5.0f);
143    area.clipRectWithTransform(Rect(50, 50, 150, 150), &rotate, SkRegion::kIntersect_Op);
144    {
145        auto serializedClip = area.serializeClip(allocator);
146        ASSERT_NE(nullptr, serializedClip);
147        ASSERT_EQ(ClipMode::RectangleList, serializedClip->mode);
148        auto clipRectList = reinterpret_cast<const ClipRectList*>(serializedClip);
149        EXPECT_EQ(2, clipRectList->rectList.getTransformedRectanglesCount());
150        EXPECT_EQ(Rect(37, 54, 145, 163), clipRectList->rect);
151        EXPECT_EQ(serializedClip, area.serializeClip(allocator))
152                << "Requery of clip on unmodified ClipArea must return same pointer.";
153    }
154
155    // region
156    SkPath circlePath;
157    circlePath.addCircle(100, 100, 100);
158    area.clipPathWithTransform(circlePath, &Matrix4::identity(), SkRegion::kReplace_Op);
159    {
160        auto serializedClip = area.serializeClip(allocator);
161        ASSERT_NE(nullptr, serializedClip);
162        ASSERT_EQ(ClipMode::Region, serializedClip->mode);
163        auto clipRegion = reinterpret_cast<const ClipRegion*>(serializedClip);
164        EXPECT_EQ(SkIRect::MakeWH(200, 200), clipRegion->region.getBounds())
165                << "Clip region should be 200x200";
166        EXPECT_EQ(Rect(200, 200), clipRegion->rect);
167        EXPECT_EQ(serializedClip, area.serializeClip(allocator))
168                << "Requery of clip on unmodified ClipArea must return same pointer.";
169    }
170}
171
172TEST(ClipArea, serializeIntersectedClip) {
173    ClipArea area(createClipArea());
174    LinearAllocator allocator;
175
176    // simple state;
177    EXPECT_EQ(nullptr, area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity()));
178    area.setClip(0, 0, 200, 200);
179    {
180        auto origRectClip = area.serializeClip(allocator);
181        ASSERT_NE(nullptr, origRectClip);
182        EXPECT_EQ(origRectClip, area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity()));
183    }
184
185    // rect
186    {
187        ClipRect recordedClip(Rect(100, 100));
188        Matrix4 translateScale;
189        translateScale.loadTranslate(100, 100, 0);
190        translateScale.scale(2, 3, 1);
191        auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
192        ASSERT_NE(nullptr, resolvedClip);
193        ASSERT_EQ(ClipMode::Rectangle, resolvedClip->mode);
194        EXPECT_EQ(Rect(100, 100, 200, 200), resolvedClip->rect);
195
196        EXPECT_EQ(resolvedClip, area.serializeIntersectedClip(allocator, &recordedClip, translateScale))
197                << "Must return previous serialization, since input is same";
198
199        ClipRect recordedClip2(Rect(100, 100));
200        EXPECT_NE(resolvedClip, area.serializeIntersectedClip(allocator, &recordedClip2, translateScale))
201                << "Shouldn't return previous serialization, since matrix location is different";
202    }
203
204    // rect list
205    Matrix4 rotate;
206    rotate.loadRotate(2.0f);
207    area.clipRectWithTransform(Rect(200, 200), &rotate, SkRegion::kIntersect_Op);
208    {
209        ClipRect recordedClip(Rect(100, 100));
210        auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, Matrix4::identity());
211        ASSERT_NE(nullptr, resolvedClip);
212        ASSERT_EQ(ClipMode::RectangleList, resolvedClip->mode);
213        auto clipRectList = reinterpret_cast<const ClipRectList*>(resolvedClip);
214        EXPECT_EQ(2, clipRectList->rectList.getTransformedRectanglesCount());
215    }
216
217    // region
218    SkPath circlePath;
219    circlePath.addCircle(100, 100, 100);
220    area.clipPathWithTransform(circlePath, &Matrix4::identity(), SkRegion::kReplace_Op);
221    {
222        SkPath ovalPath;
223        ovalPath.addOval(SkRect::MakeLTRB(50, 0, 150, 200));
224
225        ClipRegion recordedClip;
226        recordedClip.region.setPath(ovalPath, SkRegion(SkIRect::MakeWH(200, 200)));
227        recordedClip.rect = Rect(200, 200);
228
229        Matrix4 translate10x20;
230        translate10x20.loadTranslate(10, 20, 0);
231        auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip,
232                translate10x20); // Note: only translate for now, others not handled correctly
233        ASSERT_NE(nullptr, resolvedClip);
234        ASSERT_EQ(ClipMode::Region, resolvedClip->mode);
235        auto clipRegion = reinterpret_cast<const ClipRegion*>(resolvedClip);
236        EXPECT_EQ(SkIRect::MakeLTRB(60, 20, 160, 200), clipRegion->region.getBounds());
237    }
238}
239
240TEST(ClipArea, serializeIntersectedClip_snap) {
241    ClipArea area(createClipArea());
242    area.setClip(100.2, 100.4, 500.6, 500.8);
243    LinearAllocator allocator;
244
245    {
246        // no recorded clip case
247        auto resolvedClip = area.serializeIntersectedClip(allocator, nullptr, Matrix4::identity());
248        EXPECT_EQ(Rect(100, 100, 501, 501), resolvedClip->rect);
249    }
250    {
251        // recorded clip case
252        ClipRect recordedClip(Rect(100.12, 100.74));
253        Matrix4 translateScale;
254        translateScale.loadTranslate(100, 100, 0);
255        translateScale.scale(2, 3, 1); // recorded clip will have non-int coords, even after transform
256        auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
257        ASSERT_NE(nullptr, resolvedClip);
258        EXPECT_EQ(ClipMode::Rectangle, resolvedClip->mode);
259        EXPECT_EQ(Rect(100, 100, 300, 402), resolvedClip->rect);
260    }
261}
262
263} // namespace uirenderer
264} // namespace android
265