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