RenderNodeDrawableTests.cpp revision 2f06e8ad1a1c4d0866bb66854d2759e275898635
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 17#include <gtest/gtest.h> 18#include <VectorDrawable.h> 19 20#include "AnimationContext.h" 21#include "DamageAccumulator.h" 22#include "IContextFactory.h" 23#include "pipeline/skia/SkiaDisplayList.h" 24#include "pipeline/skia/SkiaRecordingCanvas.h" 25#include "renderthread/CanvasContext.h" 26#include "tests/common/TestUtils.h" 27#include "SkiaCanvas.h" 28#include <SkLiteRecorder.h> 29#include <string.h> 30 31 32using namespace android; 33using namespace android::uirenderer; 34using namespace android::uirenderer::renderthread; 35using namespace android::uirenderer::skiapipeline; 36 37TEST(RenderNodeDrawable, create) { 38 auto rootNode = TestUtils::createNode(0, 0, 200, 400, 39 [](RenderProperties& props, Canvas& canvas) { 40 canvas.drawColor(Color::Red_500, SkBlendMode::kSrcOver); 41 }); 42 43 auto skLiteDL = SkLiteDL::New(SkRect::MakeWH(1, 1)); 44 SkLiteRecorder canvas; 45 canvas.reset(skLiteDL.get()); 46 canvas.translate(100, 100); 47 RenderNodeDrawable drawable(rootNode.get(), &canvas); 48 49 ASSERT_EQ(drawable.getRenderNode(), rootNode.get()); 50 ASSERT_EQ(&drawable.getNodeProperties(), &rootNode->properties()); 51 ASSERT_EQ(drawable.getRecordedMatrix(), canvas.getTotalMatrix()); 52} 53 54static void drawOrderedRect(Canvas* canvas, uint8_t expectedDrawOrder) { 55 SkPaint paint; 56 // order put in blue channel, transparent so overlapped content doesn't get rejected 57 paint.setColor(SkColorSetARGB(1, 0, 0, expectedDrawOrder)); 58 canvas->drawRect(0, 0, 100, 100, paint); 59} 60 61static void drawOrderedNode(Canvas* canvas, uint8_t expectedDrawOrder, float z) { 62 auto node = TestUtils::createSkiaNode(0, 0, 100, 100, 63 [expectedDrawOrder, z](RenderProperties& props, SkiaRecordingCanvas& canvas) { 64 drawOrderedRect(&canvas, expectedDrawOrder); 65 props.setTranslationZ(z); 66 }); 67 canvas->drawRenderNode(node.get()); // canvas takes reference/sole ownership 68} 69 70TEST(RenderNodeDrawable, zReorder) { 71 class ZReorderCanvas : public SkCanvas { 72 public: 73 ZReorderCanvas(int width, int height) : SkCanvas(width, height) {} 74 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { 75 int expectedOrder = SkColorGetB(paint.getColor()); // extract order from blue channel 76 EXPECT_EQ(expectedOrder, mIndex++) << "An op was drawn out of order"; 77 } 78 int getIndex() { return mIndex; } 79 protected: 80 int mIndex = 0; 81 }; 82 83 auto parent = TestUtils::createSkiaNode(0, 0, 100, 100, 84 [](RenderProperties& props, SkiaRecordingCanvas& canvas) { 85 drawOrderedNode(&canvas, 0, 10.0f); // in reorder=false at this point, so played inorder 86 drawOrderedRect(&canvas, 1); 87 canvas.insertReorderBarrier(true); 88 drawOrderedNode(&canvas, 6, 2.0f); 89 drawOrderedRect(&canvas, 3); 90 drawOrderedNode(&canvas, 4, 0.0f); 91 drawOrderedRect(&canvas, 5); 92 drawOrderedNode(&canvas, 2, -2.0f); 93 drawOrderedNode(&canvas, 7, 2.0f); 94 canvas.insertReorderBarrier(false); 95 drawOrderedRect(&canvas, 8); 96 drawOrderedNode(&canvas, 9, -10.0f); // in reorder=false at this point, so played inorder 97 }); 98 99 //create a canvas not backed by any device/pixels, but with dimensions to avoid quick rejection 100 ZReorderCanvas canvas(100, 100); 101 RenderNodeDrawable drawable(parent.get(), &canvas, false); 102 canvas.drawDrawable(&drawable); 103 EXPECT_EQ(10, canvas.getIndex()); 104} 105 106TEST(RenderNodeDrawable, composeOnLayer) 107{ 108 auto surface = SkSurface::MakeRasterN32Premul(1, 1); 109 SkCanvas& canvas = *surface->getCanvas(); 110 canvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); 111 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); 112 113 auto rootNode = TestUtils::createSkiaNode(0, 0, 1, 1, 114 [](RenderProperties& props, SkiaRecordingCanvas& recorder) { 115 recorder.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); 116 }); 117 118 //attach a layer to the render node 119 auto surfaceLayer = SkSurface::MakeRasterN32Premul(1, 1); 120 auto canvas2 = surfaceLayer->getCanvas(); 121 canvas2->drawColor(SK_ColorWHITE, SkBlendMode::kSrcOver); 122 rootNode->setLayerSurface(surfaceLayer); 123 124 RenderNodeDrawable drawable1(rootNode.get(), &canvas, false); 125 canvas.drawDrawable(&drawable1); 126 ASSERT_EQ(SK_ColorRED, TestUtils::getColor(surface, 0, 0)); 127 128 RenderNodeDrawable drawable2(rootNode.get(), &canvas, true); 129 canvas.drawDrawable(&drawable2); 130 ASSERT_EQ(SK_ColorWHITE, TestUtils::getColor(surface, 0, 0)); 131 132 RenderNodeDrawable drawable3(rootNode.get(), &canvas, false); 133 canvas.drawDrawable(&drawable3); 134 ASSERT_EQ(SK_ColorRED, TestUtils::getColor(surface, 0, 0)); 135 136 rootNode->setLayerSurface(sk_sp<SkSurface>()); 137} 138 139//TODO: refactor to cover test cases from FrameBuilderTests_projectionReorder 140//validate with bounds and projection path mask. 141//TODO: research if we could hook in and mock/validate different aspects of the drawing, 142//instead of validating pixels 143TEST(RenderNodeDrawable, projectDraw) { 144 auto surface = SkSurface::MakeRasterN32Premul(1, 1); 145 SkCanvas& canvas = *surface->getCanvas(); 146 canvas.drawColor(SK_ColorBLUE, SkBlendMode::kSrcOver); 147 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorBLUE); 148 149 auto redNode = TestUtils::createSkiaNode(0, 0, 1, 1, 150 [](RenderProperties& props, SkiaRecordingCanvas& redCanvas) { 151 redCanvas.drawColor(SK_ColorRED, SkBlendMode::kSrcOver); 152 }, "redNode"); 153 154 auto greenNodeWithRedChild = TestUtils::createSkiaNode(0, 0, 1, 1, 155 [&](RenderProperties& props, SkiaRecordingCanvas& greenCanvasWithRedChild) { 156 greenCanvasWithRedChild.drawRenderNode(redNode.get()); 157 greenCanvasWithRedChild.drawColor(SK_ColorGREEN, SkBlendMode::kSrcOver); 158 }, "greenNodeWithRedChild"); 159 160 auto rootNode = TestUtils::createSkiaNode(0, 0, 1, 1, 161 [&](RenderProperties& props, SkiaRecordingCanvas& rootCanvas) { 162 rootCanvas.drawRenderNode(greenNodeWithRedChild.get()); 163 }, "rootNode"); 164 SkiaDisplayList* rootDisplayList = static_cast<SkiaDisplayList*>( 165 (const_cast<DisplayList*>(rootNode->getDisplayList()))); 166 167 RenderNodeDrawable rootDrawable(rootNode.get(), &canvas, false); 168 canvas.drawDrawable(&rootDrawable); 169 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorGREEN); 170 171 //project redNode on rootNode, which will change the test outcome, 172 //because redNode will draw after greenNodeWithRedChild 173 rootDisplayList->mIsProjectionReceiver = true; 174 redNode->animatorProperties().setProjectBackwards(true); 175 canvas.drawDrawable(&rootDrawable); 176 ASSERT_EQ(TestUtils::getColor(surface, 0, 0), SK_ColorRED); 177} 178