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