Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [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 | 021693b | 2016-10-17 16:26:15 -0400 | [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 | 021693b | 2016-10-17 16:26:15 -0400 | [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 | 021693b | 2016-10-17 16:26:15 -0400 | [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 | 021693b | 2016-10-17 16:26:15 -0400 | [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 | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 28 | #include "RecordingCanvas.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | #include "SkiaCanvas.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 30 | #include "pipeline/skia/SkiaDisplayList.h" |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 31 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 32 | #include "pipeline/skia/SkiaPipeline.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 33 | #include "pipeline/skia/SkiaRecordingCanvas.h" |
| 34 | #include "renderthread/CanvasContext.h" |
| 35 | #include "tests/common/TestUtils.h" |
Derek Sollenberger | 7290327 | 2018-09-20 14:56:27 -0400 | [diff] [blame] | 36 | #include "utils/Color.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 37 | |
| 38 | using namespace android; |
| 39 | using namespace android::uirenderer; |
| 40 | using namespace android::uirenderer::renderthread; |
| 41 | using namespace android::uirenderer::skiapipeline; |
| 42 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 43 | TEST(RenderNodeDrawable, create) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 44 | auto rootNode = |
| 45 | TestUtils::createNode(0, 0, 200, 400, [](RenderProperties& props, Canvas& canvas) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 46 | canvas.drawColor(Color::Red_500, SkBlendMode::kSrcOver); |
| 47 | }); |
| 48 | |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 49 | DisplayListData skLiteDL; |
| 50 | RecordingCanvas canvas; |
Derek Sollenberger | ea1fe9b | 2017-03-01 13:02:43 -0500 | [diff] [blame] | 51 | canvas.reset(&skLiteDL, SkIRect::MakeWH(1, 1)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 52 | canvas.translate(100, 100); |
| 53 | RenderNodeDrawable drawable(rootNode.get(), &canvas); |
| 54 | |
| 55 | ASSERT_EQ(drawable.getRenderNode(), rootNode.get()); |
| 56 | ASSERT_EQ(&drawable.getNodeProperties(), &rootNode->properties()); |
| 57 | ASSERT_EQ(drawable.getRecordedMatrix(), canvas.getTotalMatrix()); |
| 58 | } |
| 59 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 60 | namespace { |
| 61 | |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 62 | static void drawOrderedRect(Canvas* canvas, uint8_t expectedDrawOrder) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 63 | Paint paint; |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 64 | // order put in blue channel, transparent so overlapped content doesn't get rejected |
| 65 | paint.setColor(SkColorSetARGB(1, 0, 0, expectedDrawOrder)); |
| 66 | canvas->drawRect(0, 0, 100, 100, paint); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 69 | static void drawOrderedNode(Canvas* canvas, uint8_t expectedDrawOrder, float z) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 70 | auto node = TestUtils::createSkiaNode( |
| 71 | 0, 0, 100, 100, |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 72 | [expectedDrawOrder, z](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | drawOrderedRect(&canvas, expectedDrawOrder); |
| 74 | props.setTranslationZ(z); |
| 75 | }); |
| 76 | canvas->drawRenderNode(node.get()); // canvas takes reference/sole ownership |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 77 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | static void drawOrderedNode( |
| 80 | Canvas* canvas, uint8_t expectedDrawOrder, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 81 | std::function<void(RenderProperties& props, SkiaRecordingCanvas& canvas)> setup) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | auto node = TestUtils::createSkiaNode( |
| 83 | 0, 0, 100, 100, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 84 | [expectedDrawOrder, setup](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 85 | drawOrderedRect(&canvas, expectedDrawOrder); |
| 86 | if (setup) { |
| 87 | setup(props, canvas); |
| 88 | } |
| 89 | }); |
| 90 | canvas->drawRenderNode(node.get()); // canvas takes reference/sole ownership |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | class ZReorderCanvas : public SkCanvas { |
| 94 | public: |
| 95 | ZReorderCanvas(int width, int height) : SkCanvas(width, height) {} |
| 96 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 97 | int expectedOrder = SkColorGetB(paint.getColor()); // extract order from blue channel |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 98 | EXPECT_EQ(expectedOrder, mDrawCounter++) << "An op was drawn out of order"; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 99 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 100 | int getIndex() { return mDrawCounter; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 101 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 102 | protected: |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 103 | int mDrawCounter = 0; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 104 | }; |
| 105 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 106 | } // end anonymous namespace |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 107 | |
| 108 | TEST(RenderNodeDrawable, zReorder) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | auto parent = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 110 | SkiaRecordingCanvas& canvas) { |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 111 | canvas.enableZ(true); |
| 112 | canvas.enableZ(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 113 | drawOrderedNode(&canvas, 0, 10.0f); // in reorder=false at this point, so played inorder |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 114 | drawOrderedRect(&canvas, 1); |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 115 | canvas.enableZ(true); |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 116 | drawOrderedNode(&canvas, 6, 2.0f); |
| 117 | drawOrderedRect(&canvas, 3); |
| 118 | drawOrderedNode(&canvas, 4, 0.0f); |
| 119 | drawOrderedRect(&canvas, 5); |
| 120 | drawOrderedNode(&canvas, 2, -2.0f); |
| 121 | drawOrderedNode(&canvas, 7, 2.0f); |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 122 | canvas.enableZ(false); |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 123 | drawOrderedRect(&canvas, 8); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | drawOrderedNode(&canvas, 9, -10.0f); // in reorder=false at this point, so played inorder |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 125 | canvas.enableZ(true); // reorder a node ahead of drawrect op |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 126 | drawOrderedRect(&canvas, 11); |
| 127 | drawOrderedNode(&canvas, 10, -1.0f); |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 128 | canvas.enableZ(false); |
| 129 | canvas.enableZ(true); // test with two empty reorder sections |
| 130 | canvas.enableZ(true); |
| 131 | canvas.enableZ(false); |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 132 | drawOrderedRect(&canvas, 12); |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 133 | }); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 134 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 135 | // create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 136 | ZReorderCanvas canvas(100, 100); |
| 137 | RenderNodeDrawable drawable(parent.get(), &canvas, false); |
| 138 | canvas.drawDrawable(&drawable); |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 139 | EXPECT_EQ(13, canvas.getIndex()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 140 | } |
| 141 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 142 | TEST(RenderNodeDrawable, composeOnLayer) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 143 | auto surface = SkSurface::MakeRasterN32Premul(1, 1); |
| 144 | SkCanvas& canvas = *surface->getCanvas(); |
| 145 | canvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 146 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 147 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 148 | auto rootNode = TestUtils::createSkiaNode( |
| 149 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& recorder) { |
| 150 | recorder.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 151 | }); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 152 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 153 | // attach a layer to the render node |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 154 | auto surfaceLayer = SkSurface::MakeRasterN32Premul(1, 1); |
| 155 | auto canvas2 = surfaceLayer->getCanvas(); |
| 156 | canvas2->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 157 | rootNode->setLayerSurface(surfaceLayer); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 158 | |
| 159 | RenderNodeDrawable drawable1(rootNode.get(), &canvas, false); |
| 160 | canvas.drawDrawable(&drawable1); |
| 161 | ASSERT_EQ(SK_ColorRED, TestUtils::getColor(surface, 0, 0)); |
| 162 | |
| 163 | RenderNodeDrawable drawable2(rootNode.get(), &canvas, true); |
| 164 | canvas.drawDrawable(&drawable2); |
| 165 | ASSERT_EQ(SK_ColorWHITE, TestUtils::getColor(surface, 0, 0)); |
| 166 | |
| 167 | RenderNodeDrawable drawable3(rootNode.get(), &canvas, false); |
| 168 | canvas.drawDrawable(&drawable3); |
| 169 | ASSERT_EQ(SK_ColorRED, TestUtils::getColor(surface, 0, 0)); |
| 170 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 171 | rootNode->setLayerSurface(sk_sp<SkSurface>()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 174 | namespace { |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 175 | static SkRect getRecorderClipBounds(const SkiaRecordingCanvas& recorder) { |
| 176 | SkRect clipBounds; |
| 177 | recorder.getClipBounds(&clipBounds); |
| 178 | return clipBounds; |
| 179 | } |
| 180 | |
| 181 | static SkMatrix getRecorderMatrix(const SkiaRecordingCanvas& recorder) { |
| 182 | SkMatrix matrix; |
| 183 | recorder.getMatrix(&matrix); |
| 184 | return matrix; |
| 185 | } |
| 186 | } |
| 187 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 188 | TEST(RenderNodeDrawable, saveLayerClipAndMatrixRestore) { |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 189 | auto surface = SkSurface::MakeRasterN32Premul(400, 800); |
| 190 | SkCanvas& canvas = *surface->getCanvas(); |
| 191 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 192 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE); |
| 193 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 194 | auto rootNode = TestUtils::createSkiaNode( |
| 195 | 0, 0, 400, 800, [](RenderProperties& props, SkiaRecordingCanvas& recorder) { |
| 196 | SkPaint layerPaint; |
| 197 | ASSERT_EQ(SkRect::MakeLTRB(0, 0, 400, 800), getRecorderClipBounds(recorder)); |
| 198 | EXPECT_TRUE(getRecorderMatrix(recorder).isIdentity()); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 199 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 200 | // note we don't pass SaveFlags::MatrixClip, but matrix and clip will be saved |
John Reck | a00eef21 | 2020-11-16 12:45:55 -0500 | [diff] [blame] | 201 | recorder.saveLayer(0, 0, 400, 400, &layerPaint); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 202 | ASSERT_EQ(SkRect::MakeLTRB(0, 0, 400, 400), getRecorderClipBounds(recorder)); |
| 203 | EXPECT_TRUE(getRecorderMatrix(recorder).isIdentity()); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 204 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 205 | recorder.clipRect(50, 50, 350, 350, SkClipOp::kIntersect); |
| 206 | ASSERT_EQ(SkRect::MakeLTRB(50, 50, 350, 350), getRecorderClipBounds(recorder)); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 207 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 208 | recorder.translate(300.0f, 400.0f); |
Mike Reed | c65b1d5 | 2020-11-24 12:14:20 -0500 | [diff] [blame] | 209 | EXPECT_EQ(SkMatrix::Translate(300.0f, 400.0f), getRecorderMatrix(recorder)); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 210 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 211 | recorder.restore(); |
| 212 | ASSERT_EQ(SkRect::MakeLTRB(0, 0, 400, 800), getRecorderClipBounds(recorder)); |
| 213 | EXPECT_TRUE(getRecorderMatrix(recorder).isIdentity()); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 214 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 215 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 216 | paint.setAntiAlias(true); |
| 217 | paint.setColor(SK_ColorGREEN); |
| 218 | recorder.drawRect(0.0f, 400.0f, 400.0f, 800.0f, paint); |
| 219 | }); |
Stan Iliev | 68885e3 | 2016-12-14 11:18:34 -0500 | [diff] [blame] | 220 | |
| 221 | RenderNodeDrawable drawable(rootNode.get(), &canvas, true); |
| 222 | canvas.drawDrawable(&drawable); |
| 223 | ASSERT_EQ(SK_ColorGREEN, TestUtils::getColor(surface, 200, 600)); |
| 224 | } |
| 225 | |
| 226 | namespace { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 227 | class ContextFactory : public IContextFactory { |
| 228 | public: |
| 229 | virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override { |
| 230 | return new AnimationContext(clock); |
| 231 | } |
| 232 | }; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 233 | } // end anonymous namespace |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 234 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 235 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorder) { |
| 236 | static const int SCROLL_X = 5; |
| 237 | static const int SCROLL_Y = 10; |
| 238 | class ProjectionTestCanvas : public SkCanvas { |
| 239 | public: |
| 240 | ProjectionTestCanvas(int width, int height) : SkCanvas(width, height) {} |
| 241 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 242 | const int index = mDrawCounter++; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 243 | SkMatrix expectedMatrix; |
| 244 | ; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 245 | switch (index) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 246 | case 0: // this is node "B" |
| 247 | EXPECT_EQ(SkRect::MakeWH(100, 100), rect); |
| 248 | EXPECT_EQ(SK_ColorWHITE, paint.getColor()); |
| 249 | expectedMatrix.reset(); |
| 250 | EXPECT_EQ(SkRect::MakeLTRB(0, 0, 100, 100), TestUtils::getClipBounds(this)); |
| 251 | break; |
| 252 | case 1: // this is node "P" |
| 253 | EXPECT_EQ(SkRect::MakeLTRB(-10, -10, 60, 60), rect); |
| 254 | EXPECT_EQ(SK_ColorDKGRAY, paint.getColor()); |
| 255 | expectedMatrix.setTranslate(50 - SCROLL_X, 50 - SCROLL_Y); |
| 256 | EXPECT_EQ(SkRect::MakeLTRB(-35, -30, 45, 50), |
| 257 | TestUtils::getLocalClipBounds(this)); |
| 258 | break; |
| 259 | case 2: // this is node "C" |
| 260 | EXPECT_EQ(SkRect::MakeWH(100, 50), rect); |
| 261 | EXPECT_EQ(SK_ColorBLUE, paint.getColor()); |
| 262 | expectedMatrix.setTranslate(-SCROLL_X, 50 - SCROLL_Y); |
| 263 | EXPECT_EQ(SkRect::MakeLTRB(0, 40, 95, 90), TestUtils::getClipBounds(this)); |
| 264 | break; |
| 265 | default: |
| 266 | ADD_FAILURE(); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 267 | } |
| 268 | EXPECT_EQ(expectedMatrix, getTotalMatrix()); |
| 269 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 270 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 271 | int getIndex() { return mDrawCounter; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 272 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 273 | protected: |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 274 | int mDrawCounter = 0; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 275 | }; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 276 | |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 277 | /** |
| 278 | * Construct a tree of nodes, where the root (A) has a receiver background (B), and a child (C) |
| 279 | * with a projecting child (P) of its own. P would normally draw between B and C's "background" |
| 280 | * draw, but because it is projected backwards, it's drawn in between B and C. |
| 281 | * |
| 282 | * The parent is scrolled by SCROLL_X/SCROLL_Y, but this does not affect the background |
| 283 | * (which isn't affected by scroll). |
| 284 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 285 | auto receiverBackground = TestUtils::createSkiaNode( |
| 286 | 0, 0, 100, 100, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 287 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 288 | properties.setProjectionReceiver(true); |
| 289 | // scroll doesn't apply to background, so undone via translationX/Y |
| 290 | // NOTE: translationX/Y only! no other transform properties may be set for a proj |
| 291 | // receiver! |
| 292 | properties.setTranslationX(SCROLL_X); |
| 293 | properties.setTranslationY(SCROLL_Y); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 294 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 295 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 296 | paint.setColor(SK_ColorWHITE); |
| 297 | canvas.drawRect(0, 0, 100, 100, paint); |
| 298 | }, |
| 299 | "B"); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 300 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 301 | auto projectingRipple = TestUtils::createSkiaNode( |
| 302 | 50, 0, 100, 50, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 303 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 304 | properties.setProjectBackwards(true); |
| 305 | properties.setClipToBounds(false); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 306 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 307 | paint.setColor(SK_ColorDKGRAY); |
| 308 | canvas.drawRect(-10, -10, 60, 60, paint); |
| 309 | }, |
| 310 | "P"); |
| 311 | auto child = TestUtils::createSkiaNode( |
| 312 | 0, 50, 100, 100, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 313 | [&projectingRipple](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 314 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 315 | paint.setColor(SK_ColorBLUE); |
| 316 | canvas.drawRect(0, 0, 100, 50, paint); |
| 317 | canvas.drawRenderNode(projectingRipple.get()); |
| 318 | }, |
| 319 | "C"); |
| 320 | auto parent = TestUtils::createSkiaNode( |
| 321 | 0, 0, 100, 100, |
| 322 | [&receiverBackground, &child](RenderProperties& properties, |
| 323 | SkiaRecordingCanvas& canvas) { |
| 324 | // Set a rect outline for the projecting ripple to be masked against. |
| 325 | properties.mutableOutline().setRoundRect(10, 10, 90, 90, 5, 1.0f); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 326 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 327 | canvas.save(SaveFlags::MatrixClip); |
| 328 | canvas.translate(-SCROLL_X, |
| 329 | -SCROLL_Y); // Apply scroll (note: bg undoes this internally) |
| 330 | canvas.drawRenderNode(receiverBackground.get()); |
| 331 | canvas.drawRenderNode(child.get()); |
| 332 | canvas.restore(); |
| 333 | }, |
| 334 | "A"); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 335 | ContextFactory contextFactory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 336 | std::unique_ptr<CanvasContext> canvasContext( |
| 337 | CanvasContext::create(renderThread, false, parent.get(), &contextFactory)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 338 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *canvasContext.get()); |
| 339 | DamageAccumulator damageAccumulator; |
| 340 | info.damageAccumulator = &damageAccumulator; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 341 | parent->prepareTree(info); |
| 342 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 343 | // parent(A) -> (receiverBackground, child) |
| 344 | // child(C) -> (rect[0, 0, 100, 50], projectingRipple) |
| 345 | // projectingRipple(P) -> (rect[-10, -10, 60, 60]) -> projects backwards |
| 346 | // receiverBackground(B) -> (rect[0, 0, 100, 100]) -> projection receiver |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 347 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 348 | // create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 349 | ProjectionTestCanvas canvas(100, 100); |
| 350 | RenderNodeDrawable drawable(parent.get(), &canvas, true); |
| 351 | canvas.drawDrawable(&drawable); |
| 352 | EXPECT_EQ(3, canvas.getIndex()); |
| 353 | } |
| 354 | |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 355 | RENDERTHREAD_SKIA_PIPELINE_TEST(RenderNodeDrawable, emptyReceiver) { |
| 356 | class ProjectionTestCanvas : public SkCanvas { |
| 357 | public: |
| 358 | ProjectionTestCanvas(int width, int height) : SkCanvas(width, height) {} |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 359 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { mDrawCounter++; } |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 360 | |
| 361 | int getDrawCounter() { return mDrawCounter; } |
| 362 | |
| 363 | private: |
| 364 | int mDrawCounter = 0; |
| 365 | }; |
| 366 | |
| 367 | auto receiverBackground = TestUtils::createSkiaNode( |
| 368 | 0, 0, 100, 100, |
| 369 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
| 370 | properties.setProjectionReceiver(true); |
| 371 | }, |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 372 | "B"); // a receiver with an empty display list |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 373 | |
| 374 | auto projectingRipple = TestUtils::createSkiaNode( |
| 375 | 0, 0, 100, 100, |
| 376 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
| 377 | properties.setProjectBackwards(true); |
| 378 | properties.setClipToBounds(false); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 379 | Paint paint; |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 380 | canvas.drawRect(0, 0, 100, 100, paint); |
| 381 | }, |
| 382 | "P"); |
| 383 | auto child = TestUtils::createSkiaNode( |
| 384 | 0, 0, 100, 100, |
| 385 | [&projectingRipple](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 386 | Paint paint; |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 387 | canvas.drawRect(0, 0, 100, 100, paint); |
| 388 | canvas.drawRenderNode(projectingRipple.get()); |
| 389 | }, |
| 390 | "C"); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 391 | auto parent = |
| 392 | TestUtils::createSkiaNode(0, 0, 100, 100, |
| 393 | [&receiverBackground, &child](RenderProperties& properties, |
| 394 | SkiaRecordingCanvas& canvas) { |
| 395 | canvas.drawRenderNode(receiverBackground.get()); |
| 396 | canvas.drawRenderNode(child.get()); |
| 397 | }, |
| 398 | "A"); |
Yuqian Li | 70910fd | 2017-11-29 13:38:40 -0500 | [diff] [blame] | 399 | ContextFactory contextFactory; |
| 400 | std::unique_ptr<CanvasContext> canvasContext( |
| 401 | CanvasContext::create(renderThread, false, parent.get(), &contextFactory)); |
| 402 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *canvasContext.get()); |
| 403 | DamageAccumulator damageAccumulator; |
| 404 | info.damageAccumulator = &damageAccumulator; |
| 405 | parent->prepareTree(info); |
| 406 | |
| 407 | // parent(A) -> (receiverBackground, child) |
| 408 | // child(C) -> (rect[0, 0, 100, 100], projectingRipple) |
| 409 | // projectingRipple(P) -> (rect[0, 0, 100, 100]) -> projects backwards |
| 410 | // receiverBackground(B) -> (empty) -> projection receiver |
| 411 | |
| 412 | // create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection |
| 413 | ProjectionTestCanvas canvas(100, 100); |
| 414 | RenderNodeDrawable drawable(parent.get(), &canvas, true); |
| 415 | canvas.drawDrawable(&drawable); |
| 416 | EXPECT_EQ(2, canvas.getDrawCounter()); |
| 417 | } |
| 418 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 419 | RENDERTHREAD_SKIA_PIPELINE_TEST(RenderNodeDrawable, projectionHwLayer) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 420 | /* R is backward projected on B and C is a layer. |
| 421 | A |
| 422 | / \ |
| 423 | B C |
| 424 | | |
| 425 | R |
| 426 | */ |
| 427 | static const int SCROLL_X = 5; |
| 428 | static const int SCROLL_Y = 10; |
| 429 | static const int CANVAS_WIDTH = 400; |
| 430 | static const int CANVAS_HEIGHT = 400; |
| 431 | static const int LAYER_WIDTH = 200; |
| 432 | static const int LAYER_HEIGHT = 200; |
| 433 | class ProjectionTestCanvas : public SkCanvas { |
| 434 | public: |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 435 | ProjectionTestCanvas(int* drawCounter) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 436 | : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT), mDrawCounter(drawCounter) {} |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 437 | void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 438 | const SkPaint&) override { |
| 439 | EXPECT_EQ(0, (*mDrawCounter)++); // part of painting the layer |
| 440 | EXPECT_EQ(SkRect::MakeLTRB(0, 0, LAYER_WIDTH, LAYER_HEIGHT), |
| 441 | TestUtils::getClipBounds(this)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 442 | } |
| 443 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 444 | EXPECT_EQ(1, (*mDrawCounter)++); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 445 | EXPECT_EQ(SkRect::MakeLTRB(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT), |
| 446 | TestUtils::getClipBounds(this)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 447 | } |
| 448 | void onDrawOval(const SkRect&, const SkPaint&) override { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 449 | EXPECT_EQ(2, (*mDrawCounter)++); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 450 | SkMatrix expectedMatrix; |
| 451 | expectedMatrix.setTranslate(100 - SCROLL_X, 100 - SCROLL_Y); |
| 452 | EXPECT_EQ(expectedMatrix, getTotalMatrix()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 453 | EXPECT_EQ(SkRect::MakeLTRB(-85, -80, 295, 300), TestUtils::getLocalClipBounds(this)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 454 | } |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 455 | int* mDrawCounter; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 456 | }; |
| 457 | |
| 458 | class ProjectionLayer : public SkSurface_Base { |
| 459 | public: |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 460 | ProjectionLayer(int* drawCounter) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 461 | : SkSurface_Base(SkImageInfo::MakeN32Premul(LAYER_WIDTH, LAYER_HEIGHT), nullptr) |
| 462 | , mDrawCounter(drawCounter) {} |
Derek Sollenberger | dbb4bc8 | 2018-11-21 08:47:31 -0500 | [diff] [blame] | 463 | virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* bounds) override { |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 464 | EXPECT_EQ(3, (*mDrawCounter)++); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 465 | EXPECT_EQ(SkRect::MakeLTRB(100 - SCROLL_X, 100 - SCROLL_Y, 300 - SCROLL_X, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 466 | 300 - SCROLL_Y), |
| 467 | TestUtils::getClipBounds(this->getCanvas())); |
Derek Sollenberger | 03e6cff7 | 2017-12-04 15:07:08 -0500 | [diff] [blame] | 468 | return nullptr; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 469 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 470 | SkCanvas* onNewCanvas() override { return new ProjectionTestCanvas(mDrawCounter); } |
| 471 | sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override { return nullptr; } |
Ben Wagner | 9b87556 | 2021-10-28 21:42:35 +0000 | [diff] [blame^] | 472 | #ifdef SK_SURFACE_COPY_ON_WRITE_CRASHES |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 473 | void onCopyOnWrite(ContentChangeMode) override {} |
Ben Wagner | 9b87556 | 2021-10-28 21:42:35 +0000 | [diff] [blame^] | 474 | #else |
| 475 | bool onCopyOnWrite(ContentChangeMode) override { return true; } |
| 476 | #endif |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 477 | int* mDrawCounter; |
Leon Scroggins III | 71195ab | 2018-02-08 17:14:28 -0500 | [diff] [blame] | 478 | void onWritePixels(const SkPixmap&, int x, int y) {} |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 479 | }; |
| 480 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 481 | auto receiverBackground = TestUtils::createSkiaNode( |
| 482 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 483 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 484 | properties.setProjectionReceiver(true); |
| 485 | // scroll doesn't apply to background, so undone via translationX/Y |
| 486 | // NOTE: translationX/Y only! no other transform properties may be set for a proj |
| 487 | // receiver! |
| 488 | properties.setTranslationX(SCROLL_X); |
| 489 | properties.setTranslationY(SCROLL_Y); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 490 | |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 491 | canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, Paint()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 492 | }, |
| 493 | "B"); // B |
| 494 | auto projectingRipple = TestUtils::createSkiaNode( |
| 495 | 0, 0, LAYER_WIDTH, LAYER_HEIGHT, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 496 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 497 | properties.setProjectBackwards(true); |
| 498 | properties.setClipToBounds(false); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 499 | canvas.drawOval(100, 100, 300, 300, Paint()); // drawn mostly out of layer bounds |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 500 | }, |
| 501 | "R"); // R |
| 502 | auto child = TestUtils::createSkiaNode( |
| 503 | 100, 100, 300, 300, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 504 | [&projectingRipple](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 505 | canvas.drawRenderNode(projectingRipple.get()); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 506 | canvas.drawArc(0, 0, LAYER_WIDTH, LAYER_HEIGHT, 0.0f, 280.0f, true, Paint()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 507 | }, |
| 508 | "C"); // C |
| 509 | auto parent = TestUtils::createSkiaNode( |
| 510 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 511 | [&receiverBackground, &child](RenderProperties& properties, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 512 | SkiaRecordingCanvas& canvas) { |
| 513 | // Set a rect outline for the projecting ripple to be masked against. |
| 514 | properties.mutableOutline().setRoundRect(10, 10, 390, 390, 0, 1.0f); |
| 515 | canvas.translate(-SCROLL_X, |
| 516 | -SCROLL_Y); // Apply scroll (note: bg undoes this internally) |
| 517 | canvas.drawRenderNode(receiverBackground.get()); |
| 518 | canvas.drawRenderNode(child.get()); |
| 519 | }, |
| 520 | "A"); // A |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 521 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 522 | // prepareTree is required to find, which receivers have backward projected nodes |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 523 | ContextFactory contextFactory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 524 | std::unique_ptr<CanvasContext> canvasContext( |
| 525 | CanvasContext::create(renderThread, false, parent.get(), &contextFactory)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 526 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *canvasContext.get()); |
| 527 | DamageAccumulator damageAccumulator; |
| 528 | info.damageAccumulator = &damageAccumulator; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 529 | parent->prepareTree(info); |
| 530 | |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 531 | int drawCounter = 0; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 532 | // set a layer after prepareTree to avoid layer logic there |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 533 | child->animatorProperties().mutateLayerProperties().setType(LayerType::RenderLayer); |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 534 | sk_sp<SkSurface> surfaceLayer1(new ProjectionLayer(&drawCounter)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 535 | child->setLayerSurface(surfaceLayer1); |
| 536 | Matrix4 windowTransform; |
| 537 | windowTransform.loadTranslate(100, 100, 0); |
| 538 | child->getSkiaLayer()->inverseTransformInWindow.loadInverse(windowTransform); |
| 539 | |
| 540 | LayerUpdateQueue layerUpdateQueue; |
| 541 | layerUpdateQueue.enqueueLayerWithDamage(child.get(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 542 | android::uirenderer::Rect(LAYER_WIDTH, LAYER_HEIGHT)); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 543 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 544 | pipeline->renderLayersImpl(layerUpdateQueue, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 545 | EXPECT_EQ(1, drawCounter); // assert index 0 is drawn on the layer |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 546 | |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 547 | RenderNodeDrawable drawable(parent.get(), surfaceLayer1->getCanvas(), true); |
| 548 | surfaceLayer1->getCanvas()->drawDrawable(&drawable); |
| 549 | EXPECT_EQ(4, drawCounter); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 550 | |
| 551 | // clean up layer pointer, so we can safely destruct RenderNode |
| 552 | child->setLayerSurface(nullptr); |
| 553 | } |
| 554 | |
| 555 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionChildScroll) { |
| 556 | /* R is backward projected on B. |
| 557 | A |
| 558 | / \ |
| 559 | B C |
| 560 | | |
| 561 | R |
| 562 | */ |
| 563 | static const int SCROLL_X = 500000; |
| 564 | static const int SCROLL_Y = 0; |
| 565 | static const int CANVAS_WIDTH = 400; |
| 566 | static const int CANVAS_HEIGHT = 400; |
| 567 | class ProjectionChildScrollTestCanvas : public SkCanvas { |
| 568 | public: |
| 569 | ProjectionChildScrollTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
| 570 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 571 | EXPECT_EQ(0, mDrawCounter++); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 572 | EXPECT_TRUE(getTotalMatrix().isIdentity()); |
| 573 | } |
| 574 | void onDrawOval(const SkRect&, const SkPaint&) override { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 575 | EXPECT_EQ(1, mDrawCounter++); |
| 576 | EXPECT_EQ(SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), TestUtils::getClipBounds(this)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 577 | EXPECT_TRUE(getTotalMatrix().isIdentity()); |
| 578 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 579 | int mDrawCounter = 0; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 580 | }; |
| 581 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 582 | auto receiverBackground = TestUtils::createSkiaNode( |
| 583 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 584 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 585 | properties.setProjectionReceiver(true); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 586 | canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, Paint()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 587 | }, |
| 588 | "B"); // B |
| 589 | auto projectingRipple = TestUtils::createSkiaNode( |
| 590 | 0, 0, 200, 200, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 591 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 592 | // scroll doesn't apply to background, so undone via translationX/Y |
| 593 | // NOTE: translationX/Y only! no other transform properties may be set for a proj |
| 594 | // receiver! |
| 595 | properties.setTranslationX(SCROLL_X); |
| 596 | properties.setTranslationY(SCROLL_Y); |
| 597 | properties.setProjectBackwards(true); |
| 598 | properties.setClipToBounds(false); |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 599 | canvas.drawOval(0, 0, 200, 200, Paint()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 600 | }, |
| 601 | "R"); // R |
| 602 | auto child = TestUtils::createSkiaNode( |
| 603 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 604 | [&projectingRipple](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 605 | // Record time clip will be ignored by projectee |
| 606 | canvas.clipRect(100, 100, 300, 300, SkClipOp::kIntersect); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 607 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 608 | canvas.translate(-SCROLL_X, |
| 609 | -SCROLL_Y); // Apply scroll (note: bg undoes this internally) |
| 610 | canvas.drawRenderNode(projectingRipple.get()); |
| 611 | }, |
| 612 | "C"); // C |
| 613 | auto parent = |
| 614 | TestUtils::createSkiaNode(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
| 615 | [&receiverBackground, &child](RenderProperties& properties, |
| 616 | SkiaRecordingCanvas& canvas) { |
| 617 | canvas.drawRenderNode(receiverBackground.get()); |
| 618 | canvas.drawRenderNode(child.get()); |
| 619 | }, |
| 620 | "A"); // A |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 621 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 622 | // prepareTree is required to find, which receivers have backward projected nodes |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 623 | ContextFactory contextFactory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 624 | std::unique_ptr<CanvasContext> canvasContext( |
| 625 | CanvasContext::create(renderThread, false, parent.get(), &contextFactory)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 626 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *canvasContext.get()); |
| 627 | DamageAccumulator damageAccumulator; |
| 628 | info.damageAccumulator = &damageAccumulator; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 629 | parent->prepareTree(info); |
| 630 | |
Mike Reed | 6acfe16 | 2016-11-18 17:21:09 -0500 | [diff] [blame] | 631 | std::unique_ptr<ProjectionChildScrollTestCanvas> canvas(new ProjectionChildScrollTestCanvas()); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 632 | RenderNodeDrawable drawable(parent.get(), canvas.get(), true); |
| 633 | canvas->drawDrawable(&drawable); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 634 | EXPECT_EQ(2, canvas->mDrawCounter); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | namespace { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 638 | static int drawNode(RenderThread& renderThread, const sp<RenderNode>& renderNode) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 639 | ContextFactory contextFactory; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 640 | std::unique_ptr<CanvasContext> canvasContext( |
| 641 | CanvasContext::create(renderThread, false, renderNode.get(), &contextFactory)); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 642 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *canvasContext.get()); |
| 643 | DamageAccumulator damageAccumulator; |
| 644 | info.damageAccumulator = &damageAccumulator; |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 645 | renderNode->prepareTree(info); |
| 646 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 647 | // create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 648 | ZReorderCanvas canvas(100, 100); |
| 649 | RenderNodeDrawable drawable(renderNode.get(), &canvas, false); |
| 650 | canvas.drawDrawable(&drawable); |
| 651 | return canvas.getIndex(); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderProjectedInMiddle) { |
| 656 | /* R is backward projected on B |
| 657 | A |
| 658 | / \ |
| 659 | B C |
| 660 | | |
| 661 | R |
| 662 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 663 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 664 | SkiaRecordingCanvas& canvas) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 665 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 666 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 667 | }); // nodeB |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 668 | drawOrderedNode(&canvas, 2, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 669 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 670 | props.setProjectBackwards(true); |
| 671 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 672 | }); // nodeR |
| 673 | }); // nodeC |
| 674 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 675 | EXPECT_EQ(3, drawNode(renderThread, nodeA)); |
| 676 | } |
| 677 | |
| 678 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderProjectLast) { |
| 679 | /* R is backward projected on E |
| 680 | A |
| 681 | / | \ |
| 682 | / | \ |
| 683 | B C E |
| 684 | | |
| 685 | R |
| 686 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 687 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 688 | SkiaRecordingCanvas& canvas) { |
| 689 | drawOrderedNode(&canvas, 0, nullptr); // nodeB |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 690 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 691 | drawOrderedNode(&canvas, 3, [](RenderProperties& props, |
| 692 | SkiaRecordingCanvas& canvas) { // drawn as 2 |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 693 | props.setProjectBackwards(true); |
| 694 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 695 | }); // nodeR |
| 696 | }); // nodeC |
| 697 | drawOrderedNode(&canvas, 2, |
| 698 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // drawn as 3 |
| 699 | props.setProjectionReceiver(true); |
| 700 | }); // nodeE |
| 701 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 702 | EXPECT_EQ(4, drawNode(renderThread, nodeA)); |
| 703 | } |
| 704 | |
| 705 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderNoReceivable) { |
| 706 | /* R is backward projected without receiver |
| 707 | A |
| 708 | / \ |
| 709 | B C |
| 710 | | |
| 711 | R |
| 712 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 713 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 714 | SkiaRecordingCanvas& canvas) { |
| 715 | drawOrderedNode(&canvas, 0, nullptr); // nodeB |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 716 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 717 | drawOrderedNode(&canvas, 255, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 718 | // not having a projection receiver is an undefined behavior |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 719 | props.setProjectBackwards(true); |
| 720 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 721 | }); // nodeR |
| 722 | }); // nodeC |
| 723 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 724 | EXPECT_EQ(2, drawNode(renderThread, nodeA)); |
| 725 | } |
| 726 | |
| 727 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderParentReceivable) { |
| 728 | /* R is backward projected on C |
| 729 | A |
| 730 | / \ |
| 731 | B C |
| 732 | | |
| 733 | R |
| 734 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 735 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 736 | SkiaRecordingCanvas& canvas) { |
| 737 | drawOrderedNode(&canvas, 0, nullptr); // nodeB |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 738 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 739 | props.setProjectionReceiver(true); |
| 740 | drawOrderedNode(&canvas, 2, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 741 | props.setProjectBackwards(true); |
| 742 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 743 | }); // nodeR |
| 744 | }); // nodeC |
| 745 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 746 | EXPECT_EQ(3, drawNode(renderThread, nodeA)); |
| 747 | } |
| 748 | |
| 749 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderSameNodeReceivable) { |
| 750 | /* R is backward projected on R |
| 751 | A |
| 752 | / \ |
| 753 | B C |
| 754 | | |
| 755 | R |
| 756 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 757 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 758 | SkiaRecordingCanvas& canvas) { |
| 759 | drawOrderedNode(&canvas, 0, nullptr); // nodeB |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 760 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 761 | drawOrderedNode(&canvas, 255, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 762 | // having a node that is projected on itself is an undefined/unexpected behavior |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 763 | props.setProjectionReceiver(true); |
| 764 | props.setProjectBackwards(true); |
| 765 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 766 | }); // nodeR |
| 767 | }); // nodeC |
| 768 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 769 | EXPECT_EQ(2, drawNode(renderThread, nodeA)); |
| 770 | } |
| 771 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 772 | // Note: the outcome for this test is different in HWUI |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 773 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderProjectedSibling) { |
| 774 | /* R is set to project on B, but R is not drawn because projecting on a sibling is not allowed. |
| 775 | A |
| 776 | /|\ |
| 777 | / | \ |
| 778 | B C R |
| 779 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 780 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 781 | SkiaRecordingCanvas& canvas) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 782 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 783 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 784 | }); // nodeB |
| 785 | drawOrderedNode(&canvas, 1, |
| 786 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) {}); // nodeC |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 787 | drawOrderedNode(&canvas, 255, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 788 | props.setProjectBackwards(true); |
| 789 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 790 | }); // nodeR |
| 791 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 792 | EXPECT_EQ(2, drawNode(renderThread, nodeA)); |
| 793 | } |
| 794 | |
| 795 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderProjectedSibling2) { |
| 796 | /* R is set to project on B, but R is not drawn because projecting on a sibling is not allowed. |
| 797 | A |
| 798 | | |
| 799 | G |
| 800 | /|\ |
| 801 | / | \ |
| 802 | B C R |
| 803 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 804 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 805 | SkiaRecordingCanvas& canvas) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 806 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 807 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 808 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 809 | }); // nodeB |
| 810 | drawOrderedNode(&canvas, 2, |
| 811 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) {}); // nodeC |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 812 | drawOrderedNode(&canvas, 255, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 813 | props.setProjectBackwards(true); |
| 814 | props.setClipToBounds(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 815 | }); // nodeR |
| 816 | }); // nodeG |
| 817 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 818 | EXPECT_EQ(3, drawNode(renderThread, nodeA)); |
| 819 | } |
| 820 | |
| 821 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderGrandparentReceivable) { |
| 822 | /* R is backward projected on B |
| 823 | A |
| 824 | | |
| 825 | B |
| 826 | | |
| 827 | C |
| 828 | | |
| 829 | R |
| 830 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 831 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 832 | SkiaRecordingCanvas& canvas) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 833 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 834 | props.setProjectionReceiver(true); |
| 835 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 836 | drawOrderedNode(&canvas, 2, |
| 837 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 838 | props.setProjectBackwards(true); |
| 839 | props.setClipToBounds(false); |
| 840 | }); // nodeR |
| 841 | }); // nodeC |
| 842 | }); // nodeB |
| 843 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 844 | EXPECT_EQ(3, drawNode(renderThread, nodeA)); |
| 845 | } |
| 846 | |
| 847 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderTwoReceivables) { |
| 848 | /* B and G are receivables, R is backward projected |
| 849 | A |
| 850 | / \ |
| 851 | B C |
| 852 | / \ |
| 853 | G R |
| 854 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 855 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 856 | SkiaRecordingCanvas& canvas) { |
| 857 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // B |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 858 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 859 | }); // nodeB |
| 860 | drawOrderedNode(&canvas, 2, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // C |
| 861 | drawOrderedNode(&canvas, 3, |
| 862 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // G |
| 863 | props.setProjectionReceiver(true); |
| 864 | }); // nodeG |
| 865 | drawOrderedNode(&canvas, 1, |
| 866 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // R |
| 867 | props.setProjectBackwards(true); |
| 868 | props.setClipToBounds(false); |
| 869 | }); // nodeR |
| 870 | }); // nodeC |
| 871 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 872 | EXPECT_EQ(4, drawNode(renderThread, nodeA)); |
| 873 | } |
| 874 | |
| 875 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderTwoReceivablesLikelyScenario) { |
| 876 | /* B and G are receivables, G is backward projected |
| 877 | A |
| 878 | / \ |
| 879 | B C |
| 880 | / \ |
| 881 | G R |
| 882 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 883 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 884 | SkiaRecordingCanvas& canvas) { |
| 885 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // B |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 886 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 887 | }); // nodeB |
| 888 | drawOrderedNode(&canvas, 2, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // C |
| 889 | drawOrderedNode(&canvas, 1, |
| 890 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // G |
| 891 | props.setProjectionReceiver(true); |
| 892 | props.setProjectBackwards(true); |
| 893 | props.setClipToBounds(false); |
| 894 | }); // nodeG |
| 895 | drawOrderedNode(&canvas, 3, |
| 896 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // R |
| 897 | }); // nodeR |
| 898 | }); // nodeC |
| 899 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 900 | EXPECT_EQ(4, drawNode(renderThread, nodeA)); |
| 901 | } |
| 902 | |
| 903 | RENDERTHREAD_TEST(RenderNodeDrawable, projectionReorderTwoReceivablesDeeper) { |
| 904 | /* B and G are receivables, R is backward projected |
| 905 | A |
| 906 | / \ |
| 907 | B C |
| 908 | / \ |
| 909 | G D |
| 910 | | |
| 911 | R |
| 912 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 913 | auto nodeA = TestUtils::createSkiaNode(0, 0, 100, 100, [](RenderProperties& props, |
| 914 | SkiaRecordingCanvas& canvas) { |
| 915 | drawOrderedNode(&canvas, 0, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // B |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 916 | props.setProjectionReceiver(true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 917 | }); // nodeB |
| 918 | drawOrderedNode(&canvas, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // C |
| 919 | drawOrderedNode(&canvas, 2, |
| 920 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // G |
| 921 | props.setProjectionReceiver(true); |
| 922 | }); // nodeG |
| 923 | drawOrderedNode(&canvas, 4, |
| 924 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { // D |
| 925 | drawOrderedNode(&canvas, 3, [](RenderProperties& props, |
| 926 | SkiaRecordingCanvas& canvas) { // R |
| 927 | props.setProjectBackwards(true); |
| 928 | props.setClipToBounds(false); |
| 929 | }); // nodeR |
| 930 | }); // nodeD |
| 931 | }); // nodeC |
| 932 | }); // nodeA |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 933 | EXPECT_EQ(5, drawNode(renderThread, nodeA)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 934 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 935 | |
| 936 | RENDERTHREAD_TEST(RenderNodeDrawable, simple) { |
| 937 | static const int CANVAS_WIDTH = 100; |
| 938 | static const int CANVAS_HEIGHT = 200; |
| 939 | class SimpleTestCanvas : public TestCanvasBase { |
| 940 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 941 | SimpleTestCanvas() : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 942 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
| 943 | EXPECT_EQ(0, mDrawCounter++); |
| 944 | } |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 945 | void onDrawImage2(const SkImage*, SkScalar dx, SkScalar dy, const SkSamplingOptions&, |
| 946 | const SkPaint*) override { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 947 | EXPECT_EQ(1, mDrawCounter++); |
| 948 | } |
| 949 | }; |
| 950 | |
| 951 | auto node = TestUtils::createSkiaNode(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 952 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 953 | sk_sp<Bitmap> bitmap(TestUtils::createBitmap(25, 25)); |
| 954 | canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 955 | Paint()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 956 | canvas.drawBitmap(*bitmap, 10, 10, nullptr); |
| 957 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 958 | |
| 959 | SimpleTestCanvas canvas; |
| 960 | RenderNodeDrawable drawable(node.get(), &canvas, true); |
| 961 | canvas.drawDrawable(&drawable); |
| 962 | EXPECT_EQ(2, canvas.mDrawCounter); |
| 963 | } |
| 964 | |
| 965 | RENDERTHREAD_TEST(RenderNodeDrawable, colorOp_unbounded) { |
| 966 | static const int CANVAS_WIDTH = 200; |
| 967 | static const int CANVAS_HEIGHT = 200; |
| 968 | class ColorTestCanvas : public TestCanvasBase { |
| 969 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 970 | ColorTestCanvas() : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 971 | void onDrawPaint(const SkPaint&) { |
| 972 | switch (mDrawCounter++) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 973 | case 0: |
| 974 | EXPECT_EQ(SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), |
| 975 | TestUtils::getClipBounds(this)); |
| 976 | break; |
| 977 | case 1: |
| 978 | EXPECT_EQ(SkRect::MakeWH(10, 10), TestUtils::getClipBounds(this)); |
| 979 | break; |
| 980 | default: |
| 981 | ADD_FAILURE(); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | }; |
| 985 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 986 | auto unclippedColorView = TestUtils::createSkiaNode( |
| 987 | 0, 0, 10, 10, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 988 | props.setClipToBounds(false); |
| 989 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 990 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 991 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 992 | auto clippedColorView = TestUtils::createSkiaNode( |
| 993 | 0, 0, 10, 10, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 994 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 995 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 996 | |
| 997 | ColorTestCanvas canvas; |
| 998 | RenderNodeDrawable drawable(unclippedColorView.get(), &canvas, true); |
| 999 | canvas.drawDrawable(&drawable); |
| 1000 | EXPECT_EQ(1, canvas.mDrawCounter); |
| 1001 | RenderNodeDrawable drawable2(clippedColorView.get(), &canvas, true); |
| 1002 | canvas.drawDrawable(&drawable2); |
| 1003 | EXPECT_EQ(2, canvas.mDrawCounter); |
| 1004 | } |
| 1005 | |
| 1006 | TEST(RenderNodeDrawable, renderNode) { |
| 1007 | static const int CANVAS_WIDTH = 200; |
| 1008 | static const int CANVAS_HEIGHT = 200; |
| 1009 | class RenderNodeTestCanvas : public TestCanvasBase { |
| 1010 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1011 | RenderNodeTestCanvas() : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1012 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1013 | switch (mDrawCounter++) { |
| 1014 | case 0: |
| 1015 | EXPECT_EQ(SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), |
| 1016 | TestUtils::getClipBounds(this)); |
| 1017 | EXPECT_EQ(SK_ColorDKGRAY, paint.getColor()); |
| 1018 | break; |
| 1019 | case 1: |
| 1020 | EXPECT_EQ(SkRect::MakeLTRB(50, 50, 150, 150), TestUtils::getClipBounds(this)); |
| 1021 | EXPECT_EQ(SK_ColorWHITE, paint.getColor()); |
| 1022 | break; |
| 1023 | default: |
| 1024 | ADD_FAILURE(); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | }; |
| 1028 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1029 | auto child = TestUtils::createSkiaNode( |
| 1030 | 10, 10, 110, 110, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 1031 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1032 | paint.setColor(SK_ColorWHITE); |
| 1033 | canvas.drawRect(0, 0, 100, 100, paint); |
| 1034 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1035 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1036 | auto parent = TestUtils::createSkiaNode( |
| 1037 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1038 | [&child](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 1039 | Paint paint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1040 | paint.setColor(SK_ColorDKGRAY); |
| 1041 | canvas.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, paint); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1042 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1043 | canvas.save(SaveFlags::MatrixClip); |
| 1044 | canvas.translate(40, 40); |
| 1045 | canvas.drawRenderNode(child.get()); |
| 1046 | canvas.restore(); |
| 1047 | }); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 1048 | |
| 1049 | RenderNodeTestCanvas canvas; |
| 1050 | RenderNodeDrawable drawable(parent.get(), &canvas, true); |
| 1051 | canvas.drawDrawable(&drawable); |
| 1052 | EXPECT_EQ(2, canvas.mDrawCounter); |
| 1053 | } |
| 1054 | |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 1055 | // Verify that layers are composed with linear filtering. |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 1056 | RENDERTHREAD_SKIA_PIPELINE_TEST(RenderNodeDrawable, layerComposeQuality) { |
| 1057 | static const int CANVAS_WIDTH = 1; |
| 1058 | static const int CANVAS_HEIGHT = 1; |
| 1059 | static const int LAYER_WIDTH = 1; |
| 1060 | static const int LAYER_HEIGHT = 1; |
| 1061 | class FrameTestCanvas : public TestCanvasBase { |
| 1062 | public: |
| 1063 | FrameTestCanvas() : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 1064 | void onDrawImageRect2(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 1065 | const SkSamplingOptions& sampling, const SkPaint* paint, |
| 1066 | SrcRectConstraint constraint) override { |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 1067 | mDrawCounter++; |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 1068 | EXPECT_FALSE(sampling.useCubic); |
| 1069 | EXPECT_EQ(SkFilterMode::kLinear, sampling.filter); |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 1070 | } |
| 1071 | }; |
| 1072 | |
| 1073 | auto layerNode = TestUtils::createSkiaNode( |
| 1074 | 0, 0, LAYER_WIDTH, LAYER_HEIGHT, |
| 1075 | [](RenderProperties& properties, SkiaRecordingCanvas& canvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 1076 | canvas.drawPaint(Paint()); |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 1077 | }); |
| 1078 | |
| 1079 | layerNode->animatorProperties().mutateLayerProperties().setType(LayerType::RenderLayer); |
| 1080 | layerNode->setLayerSurface(SkSurface::MakeRasterN32Premul(LAYER_WIDTH, LAYER_HEIGHT)); |
| 1081 | |
| 1082 | FrameTestCanvas canvas; |
| 1083 | RenderNodeDrawable drawable(layerNode.get(), &canvas, true); |
| 1084 | canvas.drawDrawable(&drawable); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 1085 | EXPECT_EQ(1, canvas.mDrawCounter); // make sure the layer was composed |
Stan Iliev | 7e100e9 | 2018-01-22 10:36:33 -0500 | [diff] [blame] | 1086 | |
| 1087 | // clean up layer pointer, so we can safely destruct RenderNode |
| 1088 | layerNode->setLayerSurface(nullptr); |
| 1089 | } |
| 1090 | |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1091 | TEST(ReorderBarrierDrawable, testShadowMatrix) { |
| 1092 | static const int CANVAS_WIDTH = 100; |
| 1093 | static const int CANVAS_HEIGHT = 100; |
| 1094 | static const float TRANSLATE_X = 11.0f; |
| 1095 | static const float TRANSLATE_Y = 22.0f; |
| 1096 | static const float CASTER_X = 40.0f; |
| 1097 | static const float CASTER_Y = 40.0f; |
| 1098 | static const float CASTER_WIDTH = 20.0f; |
| 1099 | static const float CASTER_HEIGHT = 20.0f; |
| 1100 | |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1101 | class ShadowTestCanvas : public SkCanvas { |
| 1102 | public: |
| 1103 | ShadowTestCanvas(int width, int height) : SkCanvas(width, height) {} |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1104 | int getDrawCounter() { return mDrawCounter; } |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1105 | |
| 1106 | virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override { |
Nathaniel Nifong | 31fee3a | 2019-07-11 16:27:14 -0400 | [diff] [blame] | 1107 | // Do not expect this to be called. See RecordingCanvas.cpp DrawDrawable for context. |
| 1108 | EXPECT_TRUE(false); |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | virtual void didTranslate(SkScalar dx, SkScalar dy) override { |
| 1112 | mDrawCounter++; |
| 1113 | EXPECT_EQ(dx, TRANSLATE_X); |
| 1114 | EXPECT_EQ(dy, TRANSLATE_Y); |
| 1115 | } |
| 1116 | |
Mike Reed | 6525763 | 2020-11-25 21:31:08 -0500 | [diff] [blame] | 1117 | virtual void didSetM44(const SkM44& matrix) override { |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1118 | mDrawCounter++; |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1119 | // First invocation is EndReorderBarrierDrawable::drawShadow to apply shadow matrix. |
| 1120 | // Second invocation is preparing the matrix for an elevated RenderNodeDrawable. |
Mike Reed | 6525763 | 2020-11-25 21:31:08 -0500 | [diff] [blame] | 1121 | EXPECT_TRUE(matrix == SkM44()); |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1122 | EXPECT_TRUE(getTotalMatrix().isIdentity()); |
| 1123 | } |
| 1124 | |
Mike Reed | 6525763 | 2020-11-25 21:31:08 -0500 | [diff] [blame] | 1125 | virtual void didConcat44(const SkM44& matrix) override { |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1126 | mDrawCounter++; |
| 1127 | if (mFirstDidConcat) { |
| 1128 | // First invocation is EndReorderBarrierDrawable::drawShadow to apply shadow matrix. |
| 1129 | mFirstDidConcat = false; |
Mike Reed | 6525763 | 2020-11-25 21:31:08 -0500 | [diff] [blame] | 1130 | EXPECT_EQ(SkM44::Translate(CASTER_X + TRANSLATE_X, CASTER_Y + TRANSLATE_Y), |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1131 | matrix); |
Mike Reed | c65b1d5 | 2020-11-24 12:14:20 -0500 | [diff] [blame] | 1132 | EXPECT_EQ(SkMatrix::Translate(CASTER_X + TRANSLATE_X, CASTER_Y + TRANSLATE_Y), |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1133 | getTotalMatrix()); |
| 1134 | } else { |
| 1135 | // Second invocation is preparing the matrix for an elevated RenderNodeDrawable. |
Mike Reed | 6525763 | 2020-11-25 21:31:08 -0500 | [diff] [blame] | 1136 | EXPECT_EQ(SkM44::Translate(TRANSLATE_X, TRANSLATE_Y), matrix); |
Mike Reed | c65b1d5 | 2020-11-24 12:14:20 -0500 | [diff] [blame] | 1137 | EXPECT_EQ(SkMatrix::Translate(TRANSLATE_X, TRANSLATE_Y), getTotalMatrix()); |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1138 | } |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1139 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1140 | |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1141 | protected: |
| 1142 | int mDrawCounter = 0; |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 1143 | |
Stan Iliev | 54d7032 | 2018-06-14 18:00:10 -0400 | [diff] [blame] | 1144 | private: |
| 1145 | bool mFirstDidConcat = true; |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1146 | }; |
| 1147 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1148 | auto parent = TestUtils::createSkiaNode( |
| 1149 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1150 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1151 | canvas.translate(TRANSLATE_X, TRANSLATE_Y); |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 1152 | canvas.enableZ(true); |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1153 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1154 | auto node = TestUtils::createSkiaNode( |
| 1155 | CASTER_X, CASTER_Y, CASTER_X + CASTER_WIDTH, CASTER_Y + CASTER_HEIGHT, |
| 1156 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 1157 | props.setElevation(42); |
| 1158 | props.mutableOutline().setRoundRect(0, 0, 20, 20, 5, 1); |
| 1159 | props.mutableOutline().setShouldClip(true); |
| 1160 | }); |
| 1161 | canvas.drawRenderNode(node.get()); |
Leon Scroggins III | 0f53e10 | 2020-05-05 15:53:29 -0400 | [diff] [blame] | 1162 | canvas.enableZ(false); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1163 | }); |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1164 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 1165 | // create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection |
Stan Iliev | d7410f7 | 2017-04-04 15:23:54 -0400 | [diff] [blame] | 1166 | ShadowTestCanvas canvas(CANVAS_WIDTH, CANVAS_HEIGHT); |
| 1167 | RenderNodeDrawable drawable(parent.get(), &canvas, false); |
Nathaniel Nifong | 31fee3a | 2019-07-11 16:27:14 -0400 | [diff] [blame] | 1168 | drawable.draw(&canvas); |
| 1169 | EXPECT_EQ(5, canvas.getDrawCounter()); |
Stan Iliev | 65e678f | 2018-02-07 14:07:30 -0500 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | // Draw a vector drawable twice but with different bounds and verify correct bounds are used. |
| 1173 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaRecordingCanvas, drawVectorDrawable) { |
| 1174 | static const int CANVAS_WIDTH = 100; |
| 1175 | static const int CANVAS_HEIGHT = 200; |
| 1176 | class VectorDrawableTestCanvas : public TestCanvasBase { |
| 1177 | public: |
| 1178 | VectorDrawableTestCanvas() : TestCanvasBase(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 1179 | void onDrawImageRect2(const SkImage*, const SkRect& src, const SkRect& dst, |
| 1180 | const SkSamplingOptions&, const SkPaint* paint, |
| 1181 | SrcRectConstraint constraint) override { |
Mike Reed | f2d08ac | 2020-03-11 09:19:03 -0400 | [diff] [blame] | 1182 | const int index = mDrawCounter++; |
| 1183 | switch (index) { |
| 1184 | case 0: |
| 1185 | EXPECT_EQ(dst, SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT)); |
| 1186 | break; |
| 1187 | case 1: |
| 1188 | EXPECT_EQ(dst, SkRect::MakeWH(CANVAS_WIDTH / 2, CANVAS_HEIGHT)); |
| 1189 | break; |
| 1190 | default: |
| 1191 | ADD_FAILURE(); |
| 1192 | } |
| 1193 | } |
Stan Iliev | 65e678f | 2018-02-07 14:07:30 -0500 | [diff] [blame] | 1194 | }; |
| 1195 | |
| 1196 | VectorDrawable::Group* group = new VectorDrawable::Group(); |
| 1197 | sp<VectorDrawableRoot> vectorDrawable(new VectorDrawableRoot(group)); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 1198 | vectorDrawable->mutateStagingProperties()->setScaledSize(CANVAS_WIDTH / 10, CANVAS_HEIGHT / 10); |
Stan Iliev | 65e678f | 2018-02-07 14:07:30 -0500 | [diff] [blame] | 1199 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 1200 | auto node = |
| 1201 | TestUtils::createSkiaNode(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
| 1202 | [&](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 1203 | vectorDrawable->mutateStagingProperties()->setBounds( |
| 1204 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT)); |
| 1205 | canvas.drawVectorDrawable(vectorDrawable.get()); |
| 1206 | vectorDrawable->mutateStagingProperties()->setBounds( |
| 1207 | SkRect::MakeWH(CANVAS_WIDTH / 2, CANVAS_HEIGHT)); |
| 1208 | canvas.drawVectorDrawable(vectorDrawable.get()); |
| 1209 | }); |
Stan Iliev | 65e678f | 2018-02-07 14:07:30 -0500 | [diff] [blame] | 1210 | |
| 1211 | VectorDrawableTestCanvas canvas; |
| 1212 | RenderNodeDrawable drawable(node.get(), &canvas, true); |
| 1213 | canvas.drawDrawable(&drawable); |
| 1214 | EXPECT_EQ(2, canvas.mDrawCounter); |
| 1215 | } |