Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -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 | 500a0c3 | 2016-10-26 10:30:09 -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 | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 19 | |
Kevin Lubick | 4e8ce46 | 2022-12-01 20:29:16 +0000 | [diff] [blame] | 20 | #include <SkBlendMode.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 21 | #include <SkClipStack.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 22 | #include <SkSurface_Base.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 23 | #include <string.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include "AnimationContext.h" |
| 25 | #include "DamageAccumulator.h" |
| 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" |
| 29 | #include "pipeline/skia/SkiaDisplayList.h" |
| 30 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
| 31 | #include "pipeline/skia/SkiaRecordingCanvas.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 32 | #include "pipeline/skia/SkiaUtils.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | #include "renderthread/CanvasContext.h" |
John Reck | 23462d8 | 2019-05-29 16:55:06 -0700 | [diff] [blame] | 34 | #include "tests/common/TestContext.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | #include "tests/common/TestUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 36 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 37 | #include <gui/BufferItemConsumer.h> |
| 38 | #include <gui/Surface.h> |
| 39 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 40 | using namespace android; |
| 41 | using namespace android::uirenderer; |
| 42 | using namespace android::uirenderer::renderthread; |
| 43 | using namespace android::uirenderer::skiapipeline; |
| 44 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 45 | RENDERTHREAD_TEST(SkiaPipeline, renderFrame) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | auto redNode = TestUtils::createSkiaNode( |
| 47 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 48 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 49 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 50 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame] | 51 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 52 | std::vector<sp<RenderNode>> renderNodes; |
| 53 | renderNodes.push_back(redNode); |
| 54 | bool opaque = true; |
| 55 | android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1); |
| 56 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 57 | auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(1, 1)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 58 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 59 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 60 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 61 | SkMatrix::I()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 62 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorRED); |
| 63 | } |
| 64 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 65 | RENDERTHREAD_TEST(SkiaPipeline, renderFrameCheckOpaque) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 66 | auto halfGreenNode = TestUtils::createSkiaNode( |
| 67 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& bottomHalfGreenCanvas) { |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 68 | Paint greenPaint; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | greenPaint.setColor(SK_ColorGREEN); |
| 70 | greenPaint.setStyle(SkPaint::kFill_Style); |
| 71 | bottomHalfGreenCanvas.drawRect(0, 1, 2, 2, greenPaint); |
| 72 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 73 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame] | 74 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 75 | std::vector<sp<RenderNode>> renderNodes; |
| 76 | renderNodes.push_back(halfGreenNode); |
| 77 | android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2); |
| 78 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 79 | auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(2, 2)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 80 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 81 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 82 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, surface, |
| 83 | SkMatrix::I()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 84 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 85 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 86 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, false, contentDrawBounds, surface, |
| 87 | SkMatrix::I()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 88 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned int)SK_ColorTRANSPARENT); |
| 89 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN); |
| 90 | } |
| 91 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 92 | RENDERTHREAD_TEST(SkiaPipeline, renderFrameCheckDirtyRect) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | auto redNode = TestUtils::createSkiaNode( |
| 94 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 95 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 96 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 97 | LayerUpdateQueue layerUpdateQueue; |
| 98 | SkRect dirty = SkRect::MakeXYWH(0, 1, 2, 1); |
| 99 | std::vector<sp<RenderNode>> renderNodes; |
| 100 | renderNodes.push_back(redNode); |
| 101 | android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2); |
| 102 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 103 | auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(2, 2)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 104 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 105 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 106 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, surface, |
| 107 | SkMatrix::I()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 108 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 109 | ASSERT_EQ(TestUtils::getColor(surface, 1, 0), SK_ColorBLUE); |
| 110 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorRED); |
| 111 | ASSERT_EQ(TestUtils::getColor(surface, 1, 1), SK_ColorRED); |
| 112 | } |
| 113 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 114 | RENDERTHREAD_TEST(SkiaPipeline, renderLayer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 115 | auto redNode = TestUtils::createSkiaNode( |
| 116 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 117 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 118 | }); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 119 | auto surfaceLayer1 = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(1, 1)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 120 | surfaceLayer1->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 121 | ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorWHITE); |
| 122 | redNode->setLayerSurface(surfaceLayer1); |
| 123 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | // create a 2nd 2x2 layer and add it to the queue as well. |
| 125 | // make the layer's dirty area one half of the layer and verify only the dirty half is updated. |
| 126 | auto blueNode = TestUtils::createSkiaNode( |
| 127 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& blueCanvas) { |
| 128 | blueCanvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 129 | }); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 130 | auto surfaceLayer2 = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(2, 2)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 131 | surfaceLayer2->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 132 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorWHITE); |
| 133 | blueNode->setLayerSurface(surfaceLayer2); |
| 134 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 135 | // attach both layers to the update queue |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 136 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame] | 137 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 138 | layerUpdateQueue.enqueueLayerWithDamage(redNode.get(), dirty); |
| 139 | layerUpdateQueue.enqueueLayerWithDamage(blueNode.get(), SkRect::MakeWH(2, 1)); |
| 140 | ASSERT_EQ(layerUpdateQueue.entries().size(), 2UL); |
| 141 | |
| 142 | bool opaque = true; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 143 | LightGeometry lightGeometry; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 144 | lightGeometry.radius = 1.0f; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 145 | lightGeometry.center = {0.0f, 0.0f, 0.0f}; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 146 | LightInfo lightInfo; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 147 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 148 | pipeline->renderLayers(lightGeometry, &layerUpdateQueue, opaque, lightInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 149 | ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorRED); |
| 150 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorBLUE); |
| 151 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 1), SK_ColorWHITE); |
| 152 | ASSERT_TRUE(layerUpdateQueue.entries().empty()); |
| 153 | redNode->setLayerSurface(sk_sp<SkSurface>()); |
| 154 | blueNode->setLayerSurface(sk_sp<SkSurface>()); |
| 155 | } |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 156 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 157 | RENDERTHREAD_TEST(SkiaPipeline, renderOverdraw) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 158 | ScopedProperty<bool> prop(Properties::debugOverdraw, true); |
| 159 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | auto whiteNode = TestUtils::createSkiaNode( |
| 161 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 162 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 163 | }); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 164 | LayerUpdateQueue layerUpdateQueue; |
| 165 | SkRect dirty = SkRect::MakeXYWH(0, 0, 1, 1); |
| 166 | std::vector<sp<RenderNode>> renderNodes; |
| 167 | renderNodes.push_back(whiteNode); |
| 168 | bool opaque = true; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 169 | // empty contentDrawBounds is avoiding backdrop/content logic, which would lead to less overdraw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 170 | android::uirenderer::Rect contentDrawBounds(0, 0, 0, 0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 171 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 172 | auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(1, 1)); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 173 | |
| 174 | // Initialize the canvas to blue. |
| 175 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 176 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 177 | |
| 178 | // Single draw, should be white. |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 179 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 180 | SkMatrix::I()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 181 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE); |
| 182 | |
| 183 | // 1 Overdraw, should be blue blended onto white. |
Matt Sarett | 341480b | 2017-01-23 19:45:42 -0500 | [diff] [blame] | 184 | renderNodes.push_back(whiteNode); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 185 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 186 | SkMatrix::I()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 187 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0d0ff); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 188 | |
| 189 | // 2 Overdraw, should be green blended onto white |
| 190 | renderNodes.push_back(whiteNode); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 191 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 192 | SkMatrix::I()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 193 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0ffd0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 194 | |
| 195 | // 3 Overdraw, should be pink blended onto white. |
| 196 | renderNodes.push_back(whiteNode); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 197 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 198 | SkMatrix::I()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 199 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffffc0c0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 200 | |
| 201 | // 4 Overdraw, should be red blended onto white. |
| 202 | renderNodes.push_back(whiteNode); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 203 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 204 | SkMatrix::I()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 205 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 206 | |
| 207 | // 5 Overdraw, should be red blended onto white. |
| 208 | renderNodes.push_back(whiteNode); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 209 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 210 | SkMatrix::I()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 211 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 212 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 213 | |
| 214 | namespace { |
| 215 | template <typename T> |
| 216 | class DeferLayer : public SkSurface_Base { |
| 217 | public: |
Derek Sollenberger | 624ad83 | 2017-01-11 11:09:48 -0500 | [diff] [blame] | 218 | DeferLayer() : SkSurface_Base(T().imageInfo(), nullptr) {} |
| 219 | virtual ~DeferLayer() {} |
| 220 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 221 | SkCanvas* onNewCanvas() override { return new T(); } |
| 222 | sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override { return nullptr; } |
Derek Sollenberger | dbb4bc8 | 2018-11-21 08:47:31 -0500 | [diff] [blame] | 223 | sk_sp<SkImage> onNewImageSnapshot(const SkIRect* bounds) override { return nullptr; } |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 224 | T* canvas() { return static_cast<T*>(getCanvas()); } |
Ben Wagner | 9b87556 | 2021-10-28 21:42:35 +0000 | [diff] [blame] | 225 | bool onCopyOnWrite(ContentChangeMode) override { return true; } |
Leon Scroggins III | 71195ab | 2018-02-08 17:14:28 -0500 | [diff] [blame] | 226 | void onWritePixels(const SkPixmap&, int x, int y) override {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 227 | }; |
| 228 | } |
| 229 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 230 | RENDERTHREAD_TEST(SkiaPipeline, deferRenderNodeScene) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 231 | class DeferTestCanvas : public SkCanvas { |
| 232 | public: |
| 233 | DeferTestCanvas() : SkCanvas(800, 600) {} |
| 234 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
| 235 | SkMatrix expected; |
| 236 | switch (mDrawCounter++) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 237 | case 0: |
| 238 | // background - left side |
| 239 | EXPECT_EQ(SkRect::MakeLTRB(600, 100, 700, 500), TestUtils::getClipBounds(this)); |
| 240 | expected.setTranslate(100, 100); |
| 241 | break; |
| 242 | case 1: |
| 243 | // background - top side |
| 244 | EXPECT_EQ(SkRect::MakeLTRB(100, 400, 600, 500), TestUtils::getClipBounds(this)); |
| 245 | expected.setTranslate(100, 100); |
| 246 | break; |
| 247 | case 2: |
| 248 | // content |
| 249 | EXPECT_EQ(SkRect::MakeLTRB(100, 100, 700, 500), TestUtils::getClipBounds(this)); |
| 250 | expected.setTranslate(-50, -50); |
| 251 | break; |
| 252 | case 3: |
| 253 | // overlay |
| 254 | EXPECT_EQ(SkRect::MakeLTRB(0, 0, 800, 600), TestUtils::getClipBounds(this)); |
| 255 | expected.reset(); |
| 256 | break; |
| 257 | default: |
| 258 | ADD_FAILURE() << "Too many rects observed"; |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 259 | } |
| 260 | EXPECT_EQ(expected, getTotalMatrix()); |
| 261 | } |
| 262 | int mDrawCounter = 0; |
| 263 | }; |
| 264 | |
| 265 | std::vector<sp<RenderNode>> nodes; |
Mike Reed | c2dbc03 | 2019-07-25 12:28:29 -0400 | [diff] [blame] | 266 | Paint transparentPaint; |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 267 | transparentPaint.setAlpha(128); |
| 268 | |
| 269 | // backdrop |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 270 | nodes.push_back(TestUtils::createSkiaNode( |
| 271 | 100, 100, 700, 500, // 600x400 |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 272 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | canvas.drawRect(0, 0, 600, 400, transparentPaint); |
| 274 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 275 | |
| 276 | // content |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 277 | android::uirenderer::Rect contentDrawBounds(150, 150, 650, 450); // 500x300 |
| 278 | nodes.push_back(TestUtils::createSkiaNode( |
| 279 | 0, 0, 800, 600, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 280 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 281 | canvas.drawRect(0, 0, 800, 600, transparentPaint); |
| 282 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 283 | |
| 284 | // overlay |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 285 | nodes.push_back(TestUtils::createSkiaNode( |
| 286 | 0, 0, 800, 600, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 287 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 288 | canvas.drawRect(0, 0, 800, 200, transparentPaint); |
| 289 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 290 | |
| 291 | LayerUpdateQueue layerUpdateQueue; |
| 292 | SkRect dirty = SkRect::MakeWH(800, 600); |
| 293 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 294 | sk_sp<DeferLayer<DeferTestCanvas>> surface(new DeferLayer<DeferTestCanvas>()); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 295 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, contentDrawBounds, surface, |
| 296 | SkMatrix::I()); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 297 | EXPECT_EQ(4, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 298 | } |
| 299 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 300 | RENDERTHREAD_TEST(SkiaPipeline, clipped) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 301 | static const int CANVAS_WIDTH = 200; |
| 302 | static const int CANVAS_HEIGHT = 200; |
| 303 | class ClippedTestCanvas : public SkCanvas { |
| 304 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 305 | ClippedTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 306 | void onDrawImage2(const SkImage*, SkScalar dx, SkScalar dy, const SkSamplingOptions&, |
| 307 | const SkPaint*) override { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 308 | EXPECT_EQ(0, mDrawCounter++); |
| 309 | EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(this)); |
| 310 | EXPECT_TRUE(getTotalMatrix().isIdentity()); |
| 311 | } |
| 312 | int mDrawCounter = 0; |
| 313 | }; |
| 314 | |
| 315 | std::vector<sp<RenderNode>> nodes; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 316 | nodes.push_back(TestUtils::createSkiaNode( |
| 317 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 318 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 319 | sk_sp<Bitmap> bitmap(TestUtils::createBitmap(CANVAS_WIDTH, CANVAS_HEIGHT)); |
| 320 | canvas.drawBitmap(*bitmap, 0, 0, nullptr); |
| 321 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 322 | |
| 323 | LayerUpdateQueue layerUpdateQueue; |
| 324 | SkRect dirty = SkRect::MakeLTRB(10, 20, 30, 40); |
| 325 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 326 | sk_sp<DeferLayer<ClippedTestCanvas>> surface(new DeferLayer<ClippedTestCanvas>()); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 327 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 328 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, SkMatrix::I()); |
| 329 | EXPECT_EQ(1, surface->canvas()->mDrawCounter); |
| 330 | } |
| 331 | |
| 332 | // Test renderFrame with a dirty clip and a pre-transform matrix. |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 333 | RENDERTHREAD_TEST(SkiaPipeline, clipped_rotated) { |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 334 | static const int CANVAS_WIDTH = 200; |
| 335 | static const int CANVAS_HEIGHT = 100; |
| 336 | static const SkMatrix rotateMatrix = SkMatrix::MakeAll(0, -1, CANVAS_HEIGHT, 1, 0, 0, 0, 0, 1); |
| 337 | static const SkRect dirty = SkRect::MakeLTRB(10, 20, 20, 40); |
| 338 | class ClippedTestCanvas : public SkCanvas { |
| 339 | public: |
| 340 | ClippedTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Mike Reed | 2d1279f | 2020-12-30 09:57:25 -0500 | [diff] [blame] | 341 | void onDrawImage2(const SkImage*, SkScalar dx, SkScalar dy, const SkSamplingOptions&, |
| 342 | const SkPaint*) override { |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 343 | EXPECT_EQ(0, mDrawCounter++); |
| 344 | // Expect clip to be rotated. |
| 345 | EXPECT_EQ(SkRect::MakeLTRB(CANVAS_HEIGHT - dirty.fTop - dirty.height(), dirty.fLeft, |
| 346 | CANVAS_HEIGHT - dirty.fTop, dirty.fLeft + dirty.width()), |
| 347 | TestUtils::getClipBounds(this)); |
| 348 | EXPECT_EQ(rotateMatrix, getTotalMatrix()); |
| 349 | } |
| 350 | int mDrawCounter = 0; |
| 351 | }; |
| 352 | |
| 353 | std::vector<sp<RenderNode>> nodes; |
| 354 | nodes.push_back(TestUtils::createSkiaNode( |
| 355 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
| 356 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 357 | sk_sp<Bitmap> bitmap(TestUtils::createBitmap(CANVAS_WIDTH, CANVAS_HEIGHT)); |
| 358 | canvas.drawBitmap(*bitmap, 0, 0, nullptr); |
| 359 | })); |
| 360 | |
| 361 | LayerUpdateQueue layerUpdateQueue; |
| 362 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 363 | sk_sp<DeferLayer<ClippedTestCanvas>> surface(new DeferLayer<ClippedTestCanvas>()); |
| 364 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, |
| 365 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, rotateMatrix); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 366 | EXPECT_EQ(1, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 367 | } |
| 368 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 369 | RENDERTHREAD_TEST(SkiaPipeline, clip_replace) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 370 | static const int CANVAS_WIDTH = 50; |
| 371 | static const int CANVAS_HEIGHT = 50; |
| 372 | class ClipReplaceTestCanvas : public SkCanvas { |
| 373 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 374 | ClipReplaceTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 375 | void onDrawPaint(const SkPaint&) { |
| 376 | EXPECT_EQ(0, mDrawCounter++); |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 377 | EXPECT_EQ(SkRect::MakeLTRB(20, 10, 30, 40), TestUtils::getClipBounds(this)) |
| 378 | << "Expect resolved clip to be intersection of viewport clip and clip op"; |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 379 | } |
| 380 | int mDrawCounter = 0; |
| 381 | }; |
| 382 | |
| 383 | std::vector<sp<RenderNode>> nodes; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 384 | nodes.push_back(TestUtils::createSkiaNode( |
| 385 | 20, 20, 30, 30, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
Michael Ludwig | 70cf50c2 | 2021-07-21 17:02:39 +0000 | [diff] [blame] | 386 | canvas.replaceClipRect_deprecated(0, -20, 10, 30); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 387 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 388 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 389 | |
| 390 | LayerUpdateQueue layerUpdateQueue; |
| 391 | SkRect dirty = SkRect::MakeLTRB(10, 10, 40, 40); |
| 392 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 393 | sk_sp<DeferLayer<ClipReplaceTestCanvas>> surface(new DeferLayer<ClipReplaceTestCanvas>()); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 394 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 395 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface, SkMatrix::I()); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 396 | EXPECT_EQ(1, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 397 | } |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 398 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 399 | RENDERTHREAD_TEST(SkiaPipeline, context_lost) { |
John Reck | 23462d8 | 2019-05-29 16:55:06 -0700 | [diff] [blame] | 400 | test::TestContext context; |
| 401 | auto surface = context.surface(); |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 402 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 403 | EXPECT_FALSE(pipeline->isSurfaceReady()); |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 404 | EXPECT_TRUE(pipeline->setSurface(surface.get(), SwapBehavior::kSwap_default)); |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 405 | EXPECT_TRUE(pipeline->isSurfaceReady()); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 406 | renderThread.destroyRenderingContext(); |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 407 | EXPECT_FALSE(pipeline->isSurfaceReady()); |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 408 | |
| 409 | pipeline->makeCurrent(); |
| 410 | EXPECT_TRUE(pipeline->isSurfaceReady()); |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 411 | } |
Derek Sollenberger | 5f9753d | 2020-04-01 15:59:02 -0400 | [diff] [blame] | 412 | |
John Reck | 1efef7b | 2023-07-17 23:29:30 -0400 | [diff] [blame] | 413 | RENDERTHREAD_TEST(SkiaPipeline, pictureCallback) { |
Derek Sollenberger | 5f9753d | 2020-04-01 15:59:02 -0400 | [diff] [blame] | 414 | // create a pipeline and add a picture callback |
| 415 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 416 | int callbackCount = 0; |
| 417 | pipeline->setPictureCapturedCallback( |
| 418 | [&callbackCount](sk_sp<SkPicture>&& picture) { callbackCount += 1; }); |
| 419 | |
| 420 | // create basic red frame and render it |
| 421 | auto redNode = TestUtils::createSkiaNode( |
| 422 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 423 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 424 | }); |
| 425 | LayerUpdateQueue layerUpdateQueue; |
| 426 | SkRect dirty = SkRectMakeLargest(); |
| 427 | std::vector<sp<RenderNode>> renderNodes; |
| 428 | renderNodes.push_back(redNode); |
| 429 | bool opaque = true; |
| 430 | android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1); |
Kevin Lubick | 6817fc4 | 2023-05-12 19:27:20 +0000 | [diff] [blame] | 431 | auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(1, 1)); |
Derek Sollenberger | 5f9753d | 2020-04-01 15:59:02 -0400 | [diff] [blame] | 432 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 433 | SkMatrix::I()); |
| 434 | |
| 435 | // verify the callback was called |
| 436 | EXPECT_EQ(1, callbackCount); |
| 437 | |
| 438 | // render a second frame and check the callback count |
| 439 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 440 | SkMatrix::I()); |
| 441 | EXPECT_EQ(2, callbackCount); |
| 442 | |
| 443 | // unset the callback, render another frame, check callback was not invoked |
| 444 | pipeline->setPictureCapturedCallback(nullptr); |
| 445 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
| 446 | SkMatrix::I()); |
| 447 | EXPECT_EQ(2, callbackCount); |
| 448 | } |