blob: dfbf6d358ce5e71f40c2ebcf91e53013cc33fd41 [file] [log] [blame]
Chris Craik95571062015-09-02 12:55:52 -07001/*
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 "CanvasState.h"
18
19#include "Matrix.h"
20#include "Rect.h"
21#include "utils/LinearAllocator.h"
22
23#include <gtest/gtest.h>
24#include <SkPath.h>
25#include <SkRegion.h>
26#include <SkCanvas.h>
27
28namespace android {
29namespace uirenderer {
30
31class NullClient: public CanvasStateClient {
32 void onViewportInitialized() override {}
33 void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
34 GLuint getTargetFbo() const override { return 0; }
35};
36
37static NullClient sNullClient;
38
39static bool approxEqual(const Matrix4& a, const Matrix4& b) {
40 for (int i = 0; i < 16; i++) {
41 if (!MathUtils::areEqual(a[i], b[i])) {
42 return false;
43 }
44 }
45 return true;
46}
47
48TEST(CanvasState, gettersAndSetters) {
49 CanvasState state(sNullClient);
Chris Craik64e445b2015-09-02 14:23:49 -070050 state.initializeSaveStack(200, 200,
51 0, 0, 200, 200, Vector3());
Chris Craik95571062015-09-02 12:55:52 -070052
53 ASSERT_EQ(state.getWidth(), 200);
54 ASSERT_EQ(state.getHeight(), 200);
55
56 Matrix4 simpleTranslate;
57 simpleTranslate.loadTranslate(10, 20, 0);
58 state.setMatrix(simpleTranslate);
59
60 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 200, 200));
61 ASSERT_EQ(state.getLocalClipBounds(), Rect(-10, -20, 190, 180));
62 EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
63 EXPECT_TRUE(state.clipIsSimple());
64}
65
66TEST(CanvasState, simpleClipping) {
67 CanvasState state(sNullClient);
Chris Craik64e445b2015-09-02 14:23:49 -070068 state.initializeSaveStack(200, 200,
69 0, 0, 200, 200, Vector3());
Chris Craik95571062015-09-02 12:55:52 -070070
71 state.clipRect(0, 0, 100, 100, SkRegion::kIntersect_Op);
72 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 100, 100));
73
74 state.clipRect(10, 10, 200, 200, SkRegion::kIntersect_Op);
75 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(10, 10, 100, 100));
76
77 state.clipRect(50, 50, 150, 150, SkRegion::kReplace_Op);
78 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(50, 50, 150, 150));
79}
80
81TEST(CanvasState, complexClipping) {
82 CanvasState state(sNullClient);
Chris Craik64e445b2015-09-02 14:23:49 -070083 state.initializeSaveStack(200, 200,
84 0, 0, 200, 200, Vector3());
Chris Craik95571062015-09-02 12:55:52 -070085
86 state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
87 {
88 // rotated clip causes complex clip
89 state.rotate(10);
90 EXPECT_TRUE(state.clipIsSimple());
91 state.clipRect(0, 0, 200, 200, SkRegion::kIntersect_Op);
92 EXPECT_FALSE(state.clipIsSimple());
93 }
94 state.restore();
95
96 state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
97 {
98 // subtracted clip causes complex clip
99 EXPECT_TRUE(state.clipIsSimple());
100 state.clipRect(50, 50, 150, 150, SkRegion::kDifference_Op);
101 EXPECT_FALSE(state.clipIsSimple());
102 }
103 state.restore();
104
105 state.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
106 {
107 // complex path causes complex clip
108 SkPath path;
109 path.addOval(SkRect::MakeWH(200, 200));
110 EXPECT_TRUE(state.clipIsSimple());
111 state.clipPath(&path, SkRegion::kDifference_Op);
112 EXPECT_FALSE(state.clipIsSimple());
113 }
114 state.restore();
115}
116
117TEST(CanvasState, saveAndRestore) {
118 CanvasState state(sNullClient);
Chris Craik64e445b2015-09-02 14:23:49 -0700119 state.initializeSaveStack(200, 200,
120 0, 0, 200, 200, Vector3());
Chris Craik95571062015-09-02 12:55:52 -0700121
122 state.save(SkCanvas::kClip_SaveFlag);
123 {
124 state.clipRect(0, 0, 10, 10, SkRegion::kIntersect_Op);
125 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10));
126 }
127 state.restore();
128 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 200, 200)); // verify restore
129
130 Matrix4 simpleTranslate;
131 simpleTranslate.loadTranslate(10, 10, 0);
132 state.save(SkCanvas::kMatrix_SaveFlag);
133 {
134 state.translate(10, 10, 0);
135 EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
136 }
137 state.restore();
138 EXPECT_FALSE(approxEqual(*state.currentTransform(), simpleTranslate));
139}
140
141TEST(CanvasState, saveAndRestoreButNotTooMuch) {
142 CanvasState state(sNullClient);
Chris Craik64e445b2015-09-02 14:23:49 -0700143 state.initializeSaveStack(200, 200,
144 0, 0, 200, 200, Vector3());
Chris Craik95571062015-09-02 12:55:52 -0700145
Chris Craik64e445b2015-09-02 14:23:49 -0700146 state.save(SkCanvas::kMatrix_SaveFlag); // NOTE: clip not saved
Chris Craik95571062015-09-02 12:55:52 -0700147 {
148 state.clipRect(0, 0, 10, 10, SkRegion::kIntersect_Op);
149 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10));
150 }
151 state.restore();
152 ASSERT_EQ(state.getRenderTargetClipBounds(), Rect(0, 0, 10, 10)); // verify not restored
153
154 Matrix4 simpleTranslate;
155 simpleTranslate.loadTranslate(10, 10, 0);
156 state.save(SkCanvas::kClip_SaveFlag); // NOTE: matrix not saved
157 {
158 state.translate(10, 10, 0);
159 EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate));
160 }
161 state.restore();
162 EXPECT_TRUE(approxEqual(*state.currentTransform(), simpleTranslate)); // verify not restored
163}
164
165}
166}