Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 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 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 17 | #include <VectorDrawable.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 19 | |
Kevin Lubick | 856848e | 2022-02-24 16:24:09 +0000 | [diff] [blame^] | 20 | #include <SkCanvas.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 21 | #include <SkClipStack.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <SkSurface_Base.h> |
| 23 | #include <string.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 24 | #include "AnimationContext.h" |
| 25 | #include "DamageAccumulator.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | #include "FatalTestCanvas.h" |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 27 | #include "IContextFactory.h" |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 28 | #include "hwui/Paint.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | #include "SkiaCanvas.h" |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 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" |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 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 | |
| 43 | static void testProperty(std::function<void(RenderProperties&)> propSetupCallback, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 44 | std::function<void(const SkCanvas&)> opValidateCallback) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 45 | static const int CANVAS_WIDTH = 100; |
| 46 | static const int CANVAS_HEIGHT = 100; |
| 47 | class PropertyTestCanvas : public TestCanvasBase { |
| 48 | public: |
Chih-Hung Hsieh | 1c57378 | 2018-12-20 13:49:51 -0800 | [diff] [blame] | 49 | explicit PropertyTestCanvas(std::function<void(const SkCanvas&)> callback) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 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 | } |
Mike Reed | d43eaa9 | 2016-12-13 12:32:37 -0500 | [diff] [blame] | 55 | void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 56 | SkCanvas::onClipRRect(rrect, op, style); |
| 57 | } |
| 58 | std::function<void(const SkCanvas&)> mCallback; |
| 59 | }; |
| 60 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 61 | auto node = TestUtils::createSkiaNode( |
| 62 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 63 | [propSetupCallback](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | propSetupCallback(props); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 65 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 66 | paint.setColor(SK_ColorWHITE); |
| 67 | canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint); |
| 68 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 69 | |
Mike Reed | d43eaa9 | 2016-12-13 12:32:37 -0500 | [diff] [blame] | 70 | PropertyTestCanvas canvas(opValidateCallback); |
| 71 | RenderNodeDrawable drawable(node.get(), &canvas, true); |
| 72 | canvas.drawDrawable(&drawable); |
| 73 | EXPECT_EQ(1, canvas.mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 74 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 77 | TEST(RenderNodeDrawable, renderPropClipping) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 89 | TEST(RenderNodeDrawable, renderPropRevealClip) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 99 | TEST(RenderNodeDrawable, renderPropOutlineClip) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 110 | TEST(RenderNodeDrawable, renderPropTransform) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | testProperty( |
| 112 | [](RenderProperties& properties) { |
| 113 | properties.setLeftTopRightBottom(10, 10, 110, 110); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 114 | |
Mike Reed | c65b1d5 | 2020-11-24 12:14:20 -0500 | [diff] [blame] | 115 | SkMatrix staticMatrix = SkMatrix::Scale(1.2f, 1.2f); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | properties.setStaticMatrix(&staticMatrix); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 117 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 118 | // ignored, since static overrides animation |
Mike Reed | c65b1d5 | 2020-11-24 12:14:20 -0500 | [diff] [blame] | 119 | SkMatrix animationMatrix = SkMatrix::Translate(15, 15); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | properties.setAnimationMatrix(&animationMatrix); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 121 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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 |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 132 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 133 | // translation xy |
| 134 | matrix.translate(10, 20); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 135 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 145 | } |