blob: c4d305e5de189f7529a485c6a86a0bf226f3d53a [file] [log] [blame]
Rob Tsuk487a92c2015-01-06 13:22:54 -08001/*
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());
Rob Tsuk487a92c2015-01-06 13:22:54 -080095 SkPath path;
96 SkScalar r = 100;
97 path.addCircle(r, r, r);
Chris Craik386aa032015-12-07 17:08:25 -080098 area.clipPathWithTransform(path, &Matrix4::identity(), SkRegion::kIntersect_Op);
Rob Tsuk487a92c2015-01-06 13:22:54 -080099 EXPECT_FALSE(area.isEmpty());
100 EXPECT_FALSE(area.isSimple());
101 EXPECT_FALSE(area.isRectangleList());
Chris Craikb565df12015-10-05 13:00:52 -0700102
Rob Tsuk487a92c2015-01-06 13:22:54 -0800103 Rect clipRect(area.getClipRect());
Rob Tsuk487a92c2015-01-06 13:22:54 -0800104 Rect expected(0, 0, r * 2, r * 2);
Rob Tsuk487a92c2015-01-06 13:22:54 -0800105 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}
Chris Craik8ce8f3f2015-07-16 13:07:45 -0700112
113TEST(ClipArea, replaceNegative) {
114 ClipArea area(createClipArea());
115 area.setClip(0, 0, 100, 100);
116
Chris Craik8ce8f3f2015-07-16 13:07:45 -0700117 Rect expected(-50, -50, 50, 50);
Chris Craik386aa032015-12-07 17:08:25 -0800118 area.clipRectWithTransform(expected, &Matrix4::identity(), SkRegion::kReplace_Op);
Chris Craik8ce8f3f2015-07-16 13:07:45 -0700119 EXPECT_EQ(expected, area.getClipRect());
120}
Chris Craik386aa032015-12-07 17:08:25 -0800121
Rob Tsuk487a92c2015-01-06 13:22:54 -0800122}
123}