blob: 15ecf5831f3a339d5974052835750d66902ab37c [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
Stan Iliev52771272016-11-17 09:54:38 -050017#include <VectorDrawable.h>
John Reck1bcacfd2017-11-03 10:12:19 -070018#include <gtest/gtest.h>
Stan Iliev52771272016-11-17 09:54:38 -050019
John Reck1bcacfd2017-11-03 10:12:19 -070020#include <SkClipStack.h>
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <SkSurface_Base.h>
22#include <string.h>
Stan Iliev52771272016-11-17 09:54:38 -050023#include "AnimationContext.h"
24#include "DamageAccumulator.h"
John Reck1bcacfd2017-11-03 10:12:19 -070025#include "FatalTestCanvas.h"
Stan Iliev52771272016-11-17 09:54:38 -050026#include "IContextFactory.h"
Mike Reedc2dbc032019-07-25 12:28:29 -040027#include "hwui/Paint.h"
John Reck1bcacfd2017-11-03 10:12:19 -070028#include "SkiaCanvas.h"
Stan Iliev52771272016-11-17 09:54:38 -050029#include "pipeline/skia/SkiaDisplayList.h"
30#include "pipeline/skia/SkiaPipeline.h"
31#include "pipeline/skia/SkiaRecordingCanvas.h"
32#include "renderthread/CanvasContext.h"
33#include "tests/common/TestUtils.h"
Stan Iliev52771272016-11-17 09:54:38 -050034
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,
John Reck1bcacfd2017-11-03 10:12:19 -070043 std::function<void(const SkCanvas&)> opValidateCallback) {
Stan Iliev52771272016-11-17 09:54:38 -050044 static const int CANVAS_WIDTH = 100;
45 static const int CANVAS_HEIGHT = 100;
46 class PropertyTestCanvas : public TestCanvasBase {
47 public:
Chih-Hung Hsieh1c573782018-12-20 13:49:51 -080048 explicit PropertyTestCanvas(std::function<void(const SkCanvas&)> callback)
Stan Iliev52771272016-11-17 09:54:38 -050049 : 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
John Reck1bcacfd2017-11-03 10:12:19 -070060 auto node = TestUtils::createSkiaNode(
61 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
Stan Iliev52771272016-11-17 09:54:38 -050062 [propSetupCallback](RenderProperties& props, SkiaRecordingCanvas& canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -070063 propSetupCallback(props);
Mike Reedc2dbc032019-07-25 12:28:29 -040064 Paint paint;
John Reck1bcacfd2017-11-03 10:12:19 -070065 paint.setColor(SK_ColorWHITE);
66 canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint);
67 });
Stan Iliev52771272016-11-17 09:54:38 -050068
Mike Reedd43eaa92016-12-13 12:32:37 -050069 PropertyTestCanvas canvas(opValidateCallback);
70 RenderNodeDrawable drawable(node.get(), &canvas, true);
71 canvas.drawDrawable(&drawable);
72 EXPECT_EQ(1, canvas.mDrawCounter);
Stan Iliev52771272016-11-17 09:54:38 -050073}
Stan Iliev52771272016-11-17 09:54:38 -050074}
75
Greg Daniel98c78dad2017-01-04 14:45:56 -050076TEST(RenderNodeDrawable, renderPropClipping) {
John Reck1bcacfd2017-11-03 10:12:19 -070077 testProperty(
78 [](RenderProperties& properties) {
79 properties.setClipToBounds(true);
80 properties.setClipBounds(android::uirenderer::Rect(10, 20, 300, 400));
81 },
82 [](const SkCanvas& canvas) {
83 EXPECT_EQ(SkRect::MakeLTRB(10, 20, 100, 100), TestUtils::getClipBounds(&canvas))
84 << "Clip rect should be intersection of node bounds and clip bounds";
85 });
Stan Iliev52771272016-11-17 09:54:38 -050086}
87
Greg Daniel98c78dad2017-01-04 14:45:56 -050088TEST(RenderNodeDrawable, renderPropRevealClip) {
John Reck1bcacfd2017-11-03 10:12:19 -070089 testProperty(
90 [](RenderProperties& properties) {
91 properties.mutableRevealClip().set(true, 50, 50, 25);
92 },
93 [](const SkCanvas& canvas) {
94 EXPECT_EQ(SkRect::MakeLTRB(25, 25, 75, 75), TestUtils::getClipBounds(&canvas));
95 });
Stan Iliev52771272016-11-17 09:54:38 -050096}
97
Greg Daniel98c78dad2017-01-04 14:45:56 -050098TEST(RenderNodeDrawable, renderPropOutlineClip) {
John Reck1bcacfd2017-11-03 10:12:19 -070099 testProperty(
100 [](RenderProperties& properties) {
101 properties.mutableOutline().setShouldClip(true);
102 properties.mutableOutline().setRoundRect(10, 20, 30, 40, 5.0f, 0.5f);
103 },
104 [](const SkCanvas& canvas) {
105 EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(&canvas));
106 });
Stan Iliev52771272016-11-17 09:54:38 -0500107}
108
Greg Daniel98c78dad2017-01-04 14:45:56 -0500109TEST(RenderNodeDrawable, renderPropTransform) {
John Reck1bcacfd2017-11-03 10:12:19 -0700110 testProperty(
111 [](RenderProperties& properties) {
112 properties.setLeftTopRightBottom(10, 10, 110, 110);
Stan Iliev52771272016-11-17 09:54:38 -0500113
Mike Reedc65b1d52020-11-24 12:14:20 -0500114 SkMatrix staticMatrix = SkMatrix::Scale(1.2f, 1.2f);
John Reck1bcacfd2017-11-03 10:12:19 -0700115 properties.setStaticMatrix(&staticMatrix);
Stan Iliev52771272016-11-17 09:54:38 -0500116
John Reck1bcacfd2017-11-03 10:12:19 -0700117 // ignored, since static overrides animation
Mike Reedc65b1d52020-11-24 12:14:20 -0500118 SkMatrix animationMatrix = SkMatrix::Translate(15, 15);
John Reck1bcacfd2017-11-03 10:12:19 -0700119 properties.setAnimationMatrix(&animationMatrix);
Stan Iliev52771272016-11-17 09:54:38 -0500120
John Reck1bcacfd2017-11-03 10:12:19 -0700121 properties.setTranslationX(10);
122 properties.setTranslationY(20);
123 properties.setScaleX(0.5f);
124 properties.setScaleY(0.7f);
125 },
126 [](const SkCanvas& canvas) {
127 Matrix4 matrix;
128 matrix.loadTranslate(10, 10, 0); // left, top
129 matrix.scale(1.2f, 1.2f, 1); // static matrix
130 // ignore animation matrix, since static overrides it
Stan Iliev52771272016-11-17 09:54:38 -0500131
John Reck1bcacfd2017-11-03 10:12:19 -0700132 // translation xy
133 matrix.translate(10, 20);
Stan Iliev52771272016-11-17 09:54:38 -0500134
John Reck1bcacfd2017-11-03 10:12:19 -0700135 // scale xy (from default pivot - center)
136 matrix.translate(50, 50);
137 matrix.scale(0.5f, 0.7f, 1);
138 matrix.translate(-50, -50);
139 Matrix4 actual(canvas.getTotalMatrix());
140 EXPECT_MATRIX_APPROX_EQ(matrix, actual) << "Op draw matrix must match expected "
141 "combination of transformation "
142 "properties";
143 });
Stan Iliev52771272016-11-17 09:54:38 -0500144}