1 /*
2 * Copyright (C) 2016 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 <VectorDrawable.h>
18 #include <gtest/gtest.h>
19
20 #include <SkCanvas.h>
21 #include <SkClipStack.h>
22 #include <SkSurface_Base.h>
23 #include <string.h>
24 #include "AnimationContext.h"
25 #include "DamageAccumulator.h"
26 #include "FatalTestCanvas.h"
27 #include "IContextFactory.h"
28 #include "hwui/Paint.h"
29 #include "SkiaCanvas.h"
30 #include "pipeline/skia/SkiaDisplayList.h"
31 #include "pipeline/skia/SkiaPipeline.h"
32 #include "pipeline/skia/SkiaRecordingCanvas.h"
33 #include "renderthread/CanvasContext.h"
34 #include "tests/common/TestUtils.h"
35
36 using namespace android;
37 using namespace android::uirenderer;
38 using namespace android::uirenderer::renderthread;
39 using namespace android::uirenderer::skiapipeline;
40
41 namespace {
42
testProperty(std::function<void (RenderProperties &)> propSetupCallback,std::function<void (const SkCanvas &)> opValidateCallback)43 static void testProperty(std::function<void(RenderProperties&)> propSetupCallback,
44 std::function<void(const SkCanvas&)> opValidateCallback) {
45 static const int CANVAS_WIDTH = 100;
46 static const int CANVAS_HEIGHT = 100;
47 class PropertyTestCanvas : public TestCanvasBase {
48 public:
49 explicit PropertyTestCanvas(std::function<void(const SkCanvas&)> callback)
50 : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT), mCallback(callback) {}
51 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
52 EXPECT_EQ(mDrawCounter++, 0);
53 mCallback(*this);
54 }
55 void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
56 SkCanvas::onClipRRect(rrect, op, style);
57 }
58 std::function<void(const SkCanvas&)> mCallback;
59 };
60
61 auto node = TestUtils::createSkiaNode(
62 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
63 [propSetupCallback](RenderProperties& props, SkiaRecordingCanvas& canvas) {
64 propSetupCallback(props);
65 Paint paint;
66 paint.setColor(SK_ColorWHITE);
67 canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint);
68 });
69
70 PropertyTestCanvas canvas(opValidateCallback);
71 RenderNodeDrawable drawable(node.get(), &canvas, true);
72 canvas.drawDrawable(&drawable);
73 EXPECT_EQ(1, canvas.mDrawCounter);
74 }
75 }
76
TEST(RenderNodeDrawable,renderPropClipping)77 TEST(RenderNodeDrawable, renderPropClipping) {
78 testProperty(
79 [](RenderProperties& properties) {
80 properties.setClipToBounds(true);
81 properties.setClipBounds(android::uirenderer::Rect(10, 20, 300, 400));
82 },
83 [](const SkCanvas& canvas) {
84 EXPECT_EQ(SkRect::MakeLTRB(10, 20, 100, 100), TestUtils::getClipBounds(&canvas))
85 << "Clip rect should be intersection of node bounds and clip bounds";
86 });
87 }
88
TEST(RenderNodeDrawable,renderPropRevealClip)89 TEST(RenderNodeDrawable, renderPropRevealClip) {
90 testProperty(
91 [](RenderProperties& properties) {
92 properties.mutableRevealClip().set(true, 50, 50, 25);
93 },
94 [](const SkCanvas& canvas) {
95 EXPECT_EQ(SkRect::MakeLTRB(25, 25, 75, 75), TestUtils::getClipBounds(&canvas));
96 });
97 }
98
TEST(RenderNodeDrawable,renderPropOutlineClip)99 TEST(RenderNodeDrawable, renderPropOutlineClip) {
100 testProperty(
101 [](RenderProperties& properties) {
102 properties.mutableOutline().setShouldClip(true);
103 properties.mutableOutline().setRoundRect(10, 20, 30, 40, 5.0f, 0.5f);
104 },
105 [](const SkCanvas& canvas) {
106 EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(&canvas));
107 });
108 }
109
TEST(RenderNodeDrawable,renderPropTransform)110 TEST(RenderNodeDrawable, renderPropTransform) {
111 testProperty(
112 [](RenderProperties& properties) {
113 properties.setLeftTopRightBottom(10, 10, 110, 110);
114
115 SkMatrix staticMatrix = SkMatrix::Scale(1.2f, 1.2f);
116 properties.setStaticMatrix(&staticMatrix);
117
118 // ignored, since static overrides animation
119 SkMatrix animationMatrix = SkMatrix::Translate(15, 15);
120 properties.setAnimationMatrix(&animationMatrix);
121
122 properties.setTranslationX(10);
123 properties.setTranslationY(20);
124 properties.setScaleX(0.5f);
125 properties.setScaleY(0.7f);
126 },
127 [](const SkCanvas& canvas) {
128 Matrix4 matrix;
129 matrix.loadTranslate(10, 10, 0); // left, top
130 matrix.scale(1.2f, 1.2f, 1); // static matrix
131 // ignore animation matrix, since static overrides it
132
133 // translation xy
134 matrix.translate(10, 20);
135
136 // scale xy (from default pivot - center)
137 matrix.translate(50, 50);
138 matrix.scale(0.5f, 0.7f, 1);
139 matrix.translate(-50, -50);
140 Matrix4 actual(canvas.getTotalMatrix());
141 EXPECT_MATRIX_APPROX_EQ(matrix, actual) << "Op draw matrix must match expected "
142 "combination of transformation "
143 "properties";
144 });
145 }
146