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 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 20 | #include <SkClipStack.h> |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 21 | #include <SkSurface_Base.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 22 | #include <string.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | #include "AnimationContext.h" |
| 24 | #include "DamageAccumulator.h" |
| 25 | #include "IContextFactory.h" |
| 26 | #include "SkiaCanvas.h" |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame^] | 27 | #include "pipeline/skia/SkiaUtils.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 28 | #include "pipeline/skia/SkiaDisplayList.h" |
| 29 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
| 30 | #include "pipeline/skia/SkiaRecordingCanvas.h" |
| 31 | #include "renderthread/CanvasContext.h" |
| 32 | #include "tests/common/TestUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 33 | |
| 34 | using namespace android; |
| 35 | using namespace android::uirenderer; |
| 36 | using namespace android::uirenderer::renderthread; |
| 37 | using namespace android::uirenderer::skiapipeline; |
| 38 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 39 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrame) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 40 | auto redNode = TestUtils::createSkiaNode( |
| 41 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 42 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 43 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 44 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame^] | 45 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 46 | std::vector<sp<RenderNode>> renderNodes; |
| 47 | renderNodes.push_back(redNode); |
| 48 | bool opaque = true; |
| 49 | android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1); |
| 50 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 51 | auto surface = SkSurface::MakeRasterN32Premul(1, 1); |
| 52 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 53 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 54 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 55 | surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 56 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorRED); |
| 57 | } |
| 58 | |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 59 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, testOnPrepareTree) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | auto redNode = TestUtils::createSkiaNode( |
| 61 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 62 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 63 | }); |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 64 | |
| 65 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame^] | 66 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 67 | std::vector<sp<RenderNode>> renderNodes; |
| 68 | renderNodes.push_back(redNode); |
| 69 | bool opaque = true; |
| 70 | android::uirenderer::Rect contentDrawBounds(0, 0, 1, 1); |
| 71 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 72 | { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | // add a pointer to a deleted vector drawable object in the pipeline |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 74 | sp<VectorDrawableRoot> dirtyVD(new VectorDrawableRoot(new VectorDrawable::Group())); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | dirtyVD->mutateProperties()->setScaledSize(5, 5); |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 76 | pipeline->getVectorDrawables()->push_back(dirtyVD.get()); |
| 77 | } |
| 78 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | // pipeline should clean list of dirty vector drawables before prepare tree |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 80 | pipeline->onPrepareTree(); |
| 81 | |
| 82 | auto surface = SkSurface::MakeRasterN32Premul(1, 1); |
| 83 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 84 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 85 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 86 | // drawFrame will crash if "SkiaPipeline::onPrepareTree" did not clean invalid VD pointer |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 87 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 88 | surface); |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 89 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorRED); |
| 90 | } |
| 91 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 92 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrameCheckOpaque) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 93 | auto halfGreenNode = TestUtils::createSkiaNode( |
| 94 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& bottomHalfGreenCanvas) { |
| 95 | SkPaint greenPaint; |
| 96 | greenPaint.setColor(SK_ColorGREEN); |
| 97 | greenPaint.setStyle(SkPaint::kFill_Style); |
| 98 | bottomHalfGreenCanvas.drawRect(0, 1, 2, 2, greenPaint); |
| 99 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 100 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame^] | 101 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 102 | std::vector<sp<RenderNode>> renderNodes; |
| 103 | renderNodes.push_back(halfGreenNode); |
| 104 | android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2); |
| 105 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 106 | auto surface = SkSurface::MakeRasterN32Premul(2, 2); |
| 107 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 108 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 109 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 111 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 112 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 113 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, false, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 115 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned int)SK_ColorTRANSPARENT); |
| 116 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorGREEN); |
| 117 | } |
| 118 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 119 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderFrameCheckDirtyRect) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | auto redNode = TestUtils::createSkiaNode( |
| 121 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 122 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 123 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 124 | LayerUpdateQueue layerUpdateQueue; |
| 125 | SkRect dirty = SkRect::MakeXYWH(0, 1, 2, 1); |
| 126 | std::vector<sp<RenderNode>> renderNodes; |
| 127 | renderNodes.push_back(redNode); |
| 128 | android::uirenderer::Rect contentDrawBounds(0, 0, 2, 2); |
| 129 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 130 | auto surface = SkSurface::MakeRasterN32Premul(2, 2); |
| 131 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 132 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 133 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, true, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 135 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 136 | ASSERT_EQ(TestUtils::getColor(surface, 1, 0), SK_ColorBLUE); |
| 137 | ASSERT_EQ(TestUtils::getColor(surface, 0, 1), SK_ColorRED); |
| 138 | ASSERT_EQ(TestUtils::getColor(surface, 1, 1), SK_ColorRED); |
| 139 | } |
| 140 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 141 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderLayer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 142 | auto redNode = TestUtils::createSkiaNode( |
| 143 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { |
| 144 | redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); |
| 145 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 146 | auto surfaceLayer1 = SkSurface::MakeRasterN32Premul(1, 1); |
| 147 | surfaceLayer1->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 148 | ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorWHITE); |
| 149 | redNode->setLayerSurface(surfaceLayer1); |
| 150 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 151 | // create a 2nd 2x2 layer and add it to the queue as well. |
| 152 | // make the layer's dirty area one half of the layer and verify only the dirty half is updated. |
| 153 | auto blueNode = TestUtils::createSkiaNode( |
| 154 | 0, 0, 2, 2, [](RenderProperties& props, SkiaRecordingCanvas& blueCanvas) { |
| 155 | blueCanvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 156 | }); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 157 | auto surfaceLayer2 = SkSurface::MakeRasterN32Premul(2, 2); |
| 158 | surfaceLayer2->getCanvas()->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 159 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorWHITE); |
| 160 | blueNode->setLayerSurface(surfaceLayer2); |
| 161 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | // attach both layers to the update queue |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 163 | LayerUpdateQueue layerUpdateQueue; |
Mike Reed | f3338bd | 2018-10-09 11:54:39 -0400 | [diff] [blame^] | 164 | SkRect dirty = SkRectMakeLargest(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 165 | layerUpdateQueue.enqueueLayerWithDamage(redNode.get(), dirty); |
| 166 | layerUpdateQueue.enqueueLayerWithDamage(blueNode.get(), SkRect::MakeWH(2, 1)); |
| 167 | ASSERT_EQ(layerUpdateQueue.entries().size(), 2UL); |
| 168 | |
| 169 | bool opaque = true; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 170 | LightGeometry lightGeometry; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 171 | lightGeometry.radius = 1.0f; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 172 | lightGeometry.center = {0.0f, 0.0f, 0.0f}; |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 173 | LightInfo lightInfo; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 174 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 175 | pipeline->renderLayers(lightGeometry, &layerUpdateQueue, opaque, lightInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 176 | ASSERT_EQ(TestUtils::getColor(surfaceLayer1, 0, 0), SK_ColorRED); |
| 177 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 0), SK_ColorBLUE); |
| 178 | ASSERT_EQ(TestUtils::getColor(surfaceLayer2, 0, 1), SK_ColorWHITE); |
| 179 | ASSERT_TRUE(layerUpdateQueue.entries().empty()); |
| 180 | redNode->setLayerSurface(sk_sp<SkSurface>()); |
| 181 | blueNode->setLayerSurface(sk_sp<SkSurface>()); |
| 182 | } |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 183 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 184 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, renderOverdraw) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 185 | ScopedProperty<bool> prop(Properties::debugOverdraw, true); |
| 186 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 187 | auto whiteNode = TestUtils::createSkiaNode( |
| 188 | 0, 0, 1, 1, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 189 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 190 | }); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 191 | LayerUpdateQueue layerUpdateQueue; |
| 192 | SkRect dirty = SkRect::MakeXYWH(0, 0, 1, 1); |
| 193 | std::vector<sp<RenderNode>> renderNodes; |
| 194 | renderNodes.push_back(whiteNode); |
| 195 | bool opaque = true; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 196 | // 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] | 197 | android::uirenderer::Rect contentDrawBounds(0, 0, 0, 0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 198 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 199 | auto surface = SkSurface::MakeRasterN32Premul(1, 1); |
| 200 | |
| 201 | // Initialize the canvas to blue. |
| 202 | surface->getCanvas()->drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); |
| 203 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); |
| 204 | |
| 205 | // Single draw, should be white. |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 206 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 207 | surface); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 208 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorWHITE); |
| 209 | |
| 210 | // 1 Overdraw, should be blue blended onto white. |
Matt Sarett | 341480b | 2017-01-23 19:45:42 -0500 | [diff] [blame] | 211 | renderNodes.push_back(whiteNode); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 212 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 213 | surface); |
| 214 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0d0ff); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 215 | |
| 216 | // 2 Overdraw, should be green blended onto white |
| 217 | renderNodes.push_back(whiteNode); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 218 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 219 | surface); |
| 220 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffd0ffd0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 221 | |
| 222 | // 3 Overdraw, should be pink blended onto white. |
| 223 | renderNodes.push_back(whiteNode); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 224 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 225 | surface); |
| 226 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffffc0c0); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 227 | |
| 228 | // 4 Overdraw, should be red blended onto white. |
| 229 | renderNodes.push_back(whiteNode); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 230 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 231 | surface); |
| 232 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 233 | |
| 234 | // 5 Overdraw, should be red blended onto white. |
| 235 | renderNodes.push_back(whiteNode); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 236 | pipeline->renderFrame(layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 237 | surface); |
| 238 | ASSERT_EQ(TestUtils::getColor(surface, 0, 0), (unsigned)0xffff8080); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 239 | } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 240 | |
| 241 | namespace { |
| 242 | template <typename T> |
| 243 | class DeferLayer : public SkSurface_Base { |
| 244 | public: |
Derek Sollenberger | 624ad83 | 2017-01-11 11:09:48 -0500 | [diff] [blame] | 245 | DeferLayer() : SkSurface_Base(T().imageInfo(), nullptr) {} |
| 246 | virtual ~DeferLayer() {} |
| 247 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 248 | SkCanvas* onNewCanvas() override { return new T(); } |
| 249 | sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override { return nullptr; } |
| 250 | sk_sp<SkImage> onNewImageSnapshot() override { return nullptr; } |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 251 | T* canvas() { return static_cast<T*>(getCanvas()); } |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 252 | void onCopyOnWrite(ContentChangeMode) override {} |
Leon Scroggins III | 71195ab | 2018-02-08 17:14:28 -0500 | [diff] [blame] | 253 | void onWritePixels(const SkPixmap&, int x, int y) override {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 254 | }; |
| 255 | } |
| 256 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 257 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, deferRenderNodeScene) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 258 | class DeferTestCanvas : public SkCanvas { |
| 259 | public: |
| 260 | DeferTestCanvas() : SkCanvas(800, 600) {} |
| 261 | void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
| 262 | SkMatrix expected; |
| 263 | switch (mDrawCounter++) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 264 | case 0: |
| 265 | // background - left side |
| 266 | EXPECT_EQ(SkRect::MakeLTRB(600, 100, 700, 500), TestUtils::getClipBounds(this)); |
| 267 | expected.setTranslate(100, 100); |
| 268 | break; |
| 269 | case 1: |
| 270 | // background - top side |
| 271 | EXPECT_EQ(SkRect::MakeLTRB(100, 400, 600, 500), TestUtils::getClipBounds(this)); |
| 272 | expected.setTranslate(100, 100); |
| 273 | break; |
| 274 | case 2: |
| 275 | // content |
| 276 | EXPECT_EQ(SkRect::MakeLTRB(100, 100, 700, 500), TestUtils::getClipBounds(this)); |
| 277 | expected.setTranslate(-50, -50); |
| 278 | break; |
| 279 | case 3: |
| 280 | // overlay |
| 281 | EXPECT_EQ(SkRect::MakeLTRB(0, 0, 800, 600), TestUtils::getClipBounds(this)); |
| 282 | expected.reset(); |
| 283 | break; |
| 284 | default: |
| 285 | ADD_FAILURE() << "Too many rects observed"; |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 286 | } |
| 287 | EXPECT_EQ(expected, getTotalMatrix()); |
| 288 | } |
| 289 | int mDrawCounter = 0; |
| 290 | }; |
| 291 | |
| 292 | std::vector<sp<RenderNode>> nodes; |
| 293 | SkPaint transparentPaint; |
| 294 | transparentPaint.setAlpha(128); |
| 295 | |
| 296 | // backdrop |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 297 | nodes.push_back(TestUtils::createSkiaNode( |
| 298 | 100, 100, 700, 500, // 600x400 |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 299 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 300 | canvas.drawRect(0, 0, 600, 400, transparentPaint); |
| 301 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 302 | |
| 303 | // content |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 304 | android::uirenderer::Rect contentDrawBounds(150, 150, 650, 450); // 500x300 |
| 305 | nodes.push_back(TestUtils::createSkiaNode( |
| 306 | 0, 0, 800, 600, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 307 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 308 | canvas.drawRect(0, 0, 800, 600, transparentPaint); |
| 309 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 310 | |
| 311 | // overlay |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 312 | nodes.push_back(TestUtils::createSkiaNode( |
| 313 | 0, 0, 800, 600, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 314 | [&transparentPaint](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 315 | canvas.drawRect(0, 0, 800, 200, transparentPaint); |
| 316 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 317 | |
| 318 | LayerUpdateQueue layerUpdateQueue; |
| 319 | SkRect dirty = SkRect::MakeWH(800, 600); |
| 320 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 321 | sk_sp<DeferLayer<DeferTestCanvas>> surface(new DeferLayer<DeferTestCanvas>()); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 322 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, contentDrawBounds, surface); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 323 | EXPECT_EQ(4, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 326 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, clipped) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 327 | static const int CANVAS_WIDTH = 200; |
| 328 | static const int CANVAS_HEIGHT = 200; |
| 329 | class ClippedTestCanvas : public SkCanvas { |
| 330 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 331 | ClippedTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 332 | void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*) override { |
| 333 | EXPECT_EQ(0, mDrawCounter++); |
| 334 | EXPECT_EQ(SkRect::MakeLTRB(10, 20, 30, 40), TestUtils::getClipBounds(this)); |
| 335 | EXPECT_TRUE(getTotalMatrix().isIdentity()); |
| 336 | } |
| 337 | int mDrawCounter = 0; |
| 338 | }; |
| 339 | |
| 340 | std::vector<sp<RenderNode>> nodes; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 341 | nodes.push_back(TestUtils::createSkiaNode( |
| 342 | 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 343 | [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 344 | sk_sp<Bitmap> bitmap(TestUtils::createBitmap(CANVAS_WIDTH, CANVAS_HEIGHT)); |
| 345 | canvas.drawBitmap(*bitmap, 0, 0, nullptr); |
| 346 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 347 | |
| 348 | LayerUpdateQueue layerUpdateQueue; |
| 349 | SkRect dirty = SkRect::MakeLTRB(10, 20, 30, 40); |
| 350 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 351 | sk_sp<DeferLayer<ClippedTestCanvas>> surface(new DeferLayer<ClippedTestCanvas>()); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 352 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 353 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 354 | EXPECT_EQ(1, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Greg Daniel | 98c78dad | 2017-01-04 14:45:56 -0500 | [diff] [blame] | 357 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, clip_replace) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 358 | static const int CANVAS_WIDTH = 50; |
| 359 | static const int CANVAS_HEIGHT = 50; |
| 360 | class ClipReplaceTestCanvas : public SkCanvas { |
| 361 | public: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 362 | ClipReplaceTestCanvas() : SkCanvas(CANVAS_WIDTH, CANVAS_HEIGHT) {} |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 363 | void onDrawPaint(const SkPaint&) { |
| 364 | EXPECT_EQ(0, mDrawCounter++); |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 365 | EXPECT_EQ(SkRect::MakeLTRB(20, 10, 30, 40), TestUtils::getClipBounds(this)) |
| 366 | << "Expect resolved clip to be intersection of viewport clip and clip op"; |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 367 | } |
| 368 | int mDrawCounter = 0; |
| 369 | }; |
| 370 | |
| 371 | std::vector<sp<RenderNode>> nodes; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 372 | nodes.push_back(TestUtils::createSkiaNode( |
| 373 | 20, 20, 30, 30, [](RenderProperties& props, SkiaRecordingCanvas& canvas) { |
| 374 | canvas.clipRect(0, -20, 10, 30, SkClipOp::kReplace_deprecated); |
| 375 | canvas.drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); |
| 376 | })); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 377 | |
| 378 | LayerUpdateQueue layerUpdateQueue; |
| 379 | SkRect dirty = SkRect::MakeLTRB(10, 10, 40, 40); |
| 380 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 381 | sk_sp<DeferLayer<ClipReplaceTestCanvas>> surface(new DeferLayer<ClipReplaceTestCanvas>()); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 382 | pipeline->renderFrame(layerUpdateQueue, dirty, nodes, true, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 383 | SkRect::MakeWH(CANVAS_WIDTH, CANVAS_HEIGHT), surface); |
Stan Iliev | ed4d58c | 2016-12-14 14:05:04 -0500 | [diff] [blame] | 384 | EXPECT_EQ(1, surface->canvas()->mDrawCounter); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 385 | } |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 386 | |
| 387 | RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaPipeline, context_lost) { |
| 388 | auto pipeline = std::make_unique<SkiaOpenGLPipeline>(renderThread); |
| 389 | EXPECT_FALSE(pipeline->isSurfaceReady()); |
Peiyong Lin | 189021b | 2018-09-27 16:41:40 -0700 | [diff] [blame] | 390 | EXPECT_TRUE(pipeline->setSurface((Surface*)0x01, SwapBehavior::kSwap_default, ColorMode::SRGB)); |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 391 | EXPECT_TRUE(pipeline->isSurfaceReady()); |
| 392 | renderThread.destroyGlContext(); |
| 393 | EXPECT_FALSE(pipeline->isSurfaceReady()); |
| 394 | } |
| 395 | |