blob: ced667eb76e5bc226ff59407e6377376c703e8d0 [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
Kevin Lubick856848e2022-02-24 16:24:09 +000020#include <SkCanvas.h>
John Reck1bcacfd2017-11-03 10:12:19 -070021#include <SkClipStack.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include <SkSurface_Base.h>
23#include <string.h>
Stan Iliev52771272016-11-17 09:54:38 -050024#include "AnimationContext.h"
25#include "DamageAccumulator.h"
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "FatalTestCanvas.h"
Stan Iliev52771272016-11-17 09:54:38 -050027#include "IContextFactory.h"
Mike Reedc2dbc032019-07-25 12:28:29 -040028#include "hwui/Paint.h"
John Reck1bcacfd2017-11-03 10:12:19 -070029#include "SkiaCanvas.h"
Stan Iliev52771272016-11-17 09:54:38 -050030#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"
Stan Iliev52771272016-11-17 09:54:38 -050035
36using namespace android;
37using namespace android::uirenderer;
38using namespace android::uirenderer::renderthread;
39using namespace android::uirenderer::skiapipeline;
40
41namespace {
42
43static void testProperty(std::function<void(RenderProperties&)> propSetupCallback,
John Reck1bcacfd2017-11-03 10:12:19 -070044 std::function<void(const SkCanvas&)> opValidateCallback) {
Stan Iliev52771272016-11-17 09:54:38 -050045 static const int CANVAS_WIDTH = 100;
46 static const int CANVAS_HEIGHT = 100;
47 class PropertyTestCanvas : public TestCanvasBase {
48 public:
Chih-Hung Hsieh1c573782018-12-20 13:49:51 -080049 explicit PropertyTestCanvas(std::function<void(const SkCanvas&)> callback)
Stan Iliev52771272016-11-17 09:54:38 -050050 : 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 }
Mike Reedd43eaa92016-12-13 12:32:37 -050055 void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
Stan Iliev52771272016-11-17 09:54:38 -050056 SkCanvas::onClipRRect(rrect, op, style);
57 }
58 std::function<void(const SkCanvas&)> mCallback;
59 };
60
John Reck1bcacfd2017-11-03 10:12:19 -070061 auto node = TestUtils::createSkiaNode(
62 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT,
Stan Iliev52771272016-11-17 09:54:38 -050063 [propSetupCallback](RenderProperties& props, SkiaRecordingCanvas& canvas) {
John Reck1bcacfd2017-11-03 10:12:19 -070064 propSetupCallback(props);
Mike Reedc2dbc032019-07-25 12:28:29 -040065 Paint paint;
John Reck1bcacfd2017-11-03 10:12:19 -070066 paint.setColor(SK_ColorWHITE);
67 canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint);
68 });
Stan Iliev52771272016-11-17 09:54:38 -050069
Mike Reedd43eaa92016-12-13 12:32:37 -050070 PropertyTestCanvas canvas(opValidateCallback);
71 RenderNodeDrawable drawable(node.get(), &canvas, true);
72 canvas.drawDrawable(&drawable);
73 EXPECT_EQ(1, canvas.mDrawCounter);
Stan Iliev52771272016-11-17 09:54:38 -050074}
Stan Iliev52771272016-11-17 09:54:38 -050075}
76
Greg Daniel98c78dad2017-01-04 14:45:56 -050077TEST(RenderNodeDrawable, renderPropClipping) {
John Reck1bcacfd2017-11-03 10:12:19 -070078 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 });
Stan Iliev52771272016-11-17 09:54:38 -050087}
88
Greg Daniel98c78dad2017-01-04 14:45:56 -050089TEST(RenderNodeDrawable, renderPropRevealClip) {
John Reck1bcacfd2017-11-03 10:12:19 -070090 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 });
Stan Iliev52771272016-11-17 09:54:38 -050097}
98
Greg Daniel98c78dad2017-01-04 14:45:56 -050099TEST(RenderNodeDrawable, renderPropOutlineClip) {
John Reck1bcacfd2017-11-03 10:12:19 -0700100 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 });
Stan Iliev52771272016-11-17 09:54:38 -0500108}
109
Greg Daniel98c78dad2017-01-04 14:45:56 -0500110TEST(RenderNodeDrawable, renderPropTransform) {
John Reck1bcacfd2017-11-03 10:12:19 -0700111 testProperty(
112 [](RenderProperties& properties) {
113 properties.setLeftTopRightBottom(10, 10, 110, 110);
Stan Iliev52771272016-11-17 09:54:38 -0500114
Mike Reedc65b1d52020-11-24 12:14:20 -0500115 SkMatrix staticMatrix = SkMatrix::Scale(1.2f, 1.2f);
John Reck1bcacfd2017-11-03 10:12:19 -0700116 properties.setStaticMatrix(&staticMatrix);
Stan Iliev52771272016-11-17 09:54:38 -0500117
John Reck1bcacfd2017-11-03 10:12:19 -0700118 // ignored, since static overrides animation
Mike Reedc65b1d52020-11-24 12:14:20 -0500119 SkMatrix animationMatrix = SkMatrix::Translate(15, 15);
John Reck1bcacfd2017-11-03 10:12:19 -0700120 properties.setAnimationMatrix(&animationMatrix);
Stan Iliev52771272016-11-17 09:54:38 -0500121
John Reck1bcacfd2017-11-03 10:12:19 -0700122 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
Stan Iliev52771272016-11-17 09:54:38 -0500132
John Reck1bcacfd2017-11-03 10:12:19 -0700133 // translation xy
134 matrix.translate(10, 20);
Stan Iliev52771272016-11-17 09:54:38 -0500135
John Reck1bcacfd2017-11-03 10:12:19 -0700136 // 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 });
Stan Iliev52771272016-11-17 09:54:38 -0500145}