ClipAreaTests.cpp revision 8160f20b0aca8c6595d4b385d673f59b6bcd16a4
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(0, 0, 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    Matrix4 transform;
96    transform.loadIdentity();
97    SkPath path;
98    SkScalar r = 100;
99    path.addCircle(r, r, r);
100    area.clipPathWithTransform(path, &transform, SkRegion::kIntersect_Op);
101    EXPECT_FALSE(area.isEmpty());
102    EXPECT_FALSE(area.isSimple());
103    EXPECT_FALSE(area.isRectangleList());
104
105    Rect clipRect(area.getClipRect());
106    Rect expected(0, 0, r * 2, r * 2);
107    EXPECT_EQ(expected, clipRect);
108    SkRegion clipRegion(area.getClipRegion());
109    auto skRect(clipRegion.getBounds());
110    Rect regionBounds;
111    regionBounds.set(skRect);
112    EXPECT_EQ(expected, regionBounds);
113}
114
115TEST(ClipArea, replaceNegative) {
116    ClipArea area(createClipArea());
117    area.setClip(0, 0, 100, 100);
118
119    Matrix4 transform;
120    transform.loadIdentity();
121    Rect expected(-50, -50, 50, 50);
122    area.clipRectWithTransform(expected, &transform, SkRegion::kReplace_Op);
123    EXPECT_EQ(expected, area.getClipRect());
124}
125}
126}
127