SkiaDisplayListTests.cpp revision 021693b967a2c5556dddd183eb0247df4079e1ad
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 "renderthread/CanvasContext.h"
25#include "tests/common/TestUtils.h"
26
27using namespace android;
28using namespace android::uirenderer;
29using namespace android::uirenderer::renderthread;
30using namespace android::uirenderer::skiapipeline;
31
32TEST(SkiaDisplayList, create) {
33    SkRect bounds = SkRect::MakeWH(200, 200);
34    SkiaDisplayList skiaDL(bounds);
35    ASSERT_TRUE(skiaDL.isEmpty());
36    ASSERT_FALSE(skiaDL.mIsProjectionReceiver);
37    ASSERT_EQ(skiaDL.mDrawable->getBounds(), bounds);
38}
39
40TEST(SkiaDisplayList, reset) {
41    SkRect bounds = SkRect::MakeWH(200, 200);
42    SkiaDisplayList skiaDL(bounds);
43
44    SkCanvas dummyCanvas;
45    skiaDL.mChildNodes.emplace_back(nullptr, &dummyCanvas);
46    skiaDL.mChildFunctors.emplace_back(nullptr, nullptr, &dummyCanvas);
47    skiaDL.mMutableImages.push_back(nullptr);
48    skiaDL.mVectorDrawables.push_back(nullptr);
49    skiaDL.mDrawable->drawAnnotation(bounds, "testAnnotation", nullptr);
50    skiaDL.mIsProjectionReceiver = true;
51
52    ASSERT_EQ(skiaDL.mDrawable->getBounds(), bounds);
53    ASSERT_FALSE(skiaDL.mChildNodes.empty());
54    ASSERT_FALSE(skiaDL.mChildFunctors.empty());
55    ASSERT_FALSE(skiaDL.mMutableImages.empty());
56    ASSERT_FALSE(skiaDL.mVectorDrawables.empty());
57    ASSERT_FALSE(skiaDL.isEmpty());
58    ASSERT_TRUE(skiaDL.mIsProjectionReceiver);
59
60    bounds = SkRect::MakeWH(100, 100);
61    skiaDL.reset(nullptr, bounds);
62
63    ASSERT_EQ(skiaDL.mDrawable->getBounds(), bounds);
64    ASSERT_TRUE(skiaDL.mChildNodes.empty());
65    ASSERT_TRUE(skiaDL.mChildFunctors.empty());
66    ASSERT_TRUE(skiaDL.mMutableImages.empty());
67    ASSERT_TRUE(skiaDL.mVectorDrawables.empty());
68    ASSERT_TRUE(skiaDL.isEmpty());
69    ASSERT_FALSE(skiaDL.mIsProjectionReceiver);
70}
71
72TEST(SkiaDisplayList, reuseDisplayList) {
73    sp<RenderNode> renderNode = new RenderNode();
74    std::unique_ptr<SkiaDisplayList> availableList;
75
76    // no list has been attached so it should return a nullptr
77    availableList = renderNode->detachAvailableList();
78    ASSERT_EQ(availableList.get(), nullptr);
79
80    // attach a displayList for reuse
81    SkiaDisplayList skiaDL(SkRect::MakeWH(200, 200));
82    ASSERT_TRUE(skiaDL.reuseDisplayList(renderNode.get(), nullptr));
83
84    // detach the list that you just attempted to reuse
85    availableList = renderNode->detachAvailableList();
86    ASSERT_EQ(availableList.get(), &skiaDL);
87    availableList.release(); // prevents an invalid free since our DL is stack allocated
88
89    // after detaching there should return no available list
90    availableList = renderNode->detachAvailableList();
91    ASSERT_EQ(availableList.get(), nullptr);
92}
93
94TEST(SkiaDisplayList, syncContexts) {
95    SkRect bounds = SkRect::MakeWH(200, 200);
96    SkiaDisplayList skiaDL(bounds);
97
98    SkCanvas dummyCanvas;
99    TestUtils::MockFunctor functor;
100    skiaDL.mChildFunctors.emplace_back(&functor, nullptr, &dummyCanvas);
101
102    VectorDrawableRoot vectorDrawable(new VectorDrawable::Group());
103    vectorDrawable.mutateStagingProperties()->setBounds(bounds);
104    skiaDL.mVectorDrawables.push_back(&vectorDrawable);
105
106    // ensure that the functor and vectorDrawable are properly synced
107    skiaDL.syncContents();
108
109    ASSERT_EQ(functor.getLastMode(), DrawGlInfo::kModeSync);
110    ASSERT_EQ(vectorDrawable.mutateProperties()->getBounds(), bounds);
111}
112
113class ContextFactory : public IContextFactory {
114public:
115    virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
116        return new AnimationContext(clock);
117    }
118};
119
120RENDERTHREAD_TEST(SkiaDisplayList, prepareListAndChildren) {
121    auto rootNode = TestUtils::createNode(0, 0, 200, 400, nullptr);
122    ContextFactory contextFactory;
123    std::unique_ptr<CanvasContext> canvasContext(CanvasContext::create(
124            renderThread, false, rootNode.get(), &contextFactory));
125    TreeInfo info(TreeInfo::MODE_FULL, *canvasContext.get());
126    DamageAccumulator damageAccumulator;
127    info.damageAccumulator = &damageAccumulator;
128    info.observer = nullptr;
129
130    SkiaDisplayList skiaDL(SkRect::MakeWH(200, 200));
131
132    // prepare with a clean VD
133    VectorDrawableRoot cleanVD(new VectorDrawable::Group());
134    skiaDL.mVectorDrawables.push_back(&cleanVD);
135    cleanVD.getBitmapUpdateIfDirty(); // this clears the dirty bit
136
137    ASSERT_FALSE(cleanVD.isDirty());
138    ASSERT_FALSE(cleanVD.getPropertyChangeWillBeConsumed());
139    ASSERT_FALSE(skiaDL.prepareListAndChildren(info, false, [](RenderNode*, TreeInfo&, bool) {}));
140    ASSERT_TRUE(cleanVD.getPropertyChangeWillBeConsumed());
141
142    // prepare again this time adding a dirty VD
143    VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
144    skiaDL.mVectorDrawables.push_back(&dirtyVD);
145
146    ASSERT_TRUE(dirtyVD.isDirty());
147    ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
148    ASSERT_TRUE(skiaDL.prepareListAndChildren(info, false, [](RenderNode*, TreeInfo&, bool) {}));
149    ASSERT_TRUE(dirtyVD.getPropertyChangeWillBeConsumed());
150
151    // prepare again this time adding a RenderNode and a callback
152    sp<RenderNode> renderNode = new RenderNode();
153    TreeInfo* infoPtr = &info;
154    SkCanvas dummyCanvas;
155    skiaDL.mChildNodes.emplace_back(renderNode.get(), &dummyCanvas);
156    bool hasRun = false;
157    ASSERT_TRUE(skiaDL.prepareListAndChildren(info, false,
158            [&hasRun, renderNode, infoPtr](RenderNode* n, TreeInfo& i, bool r) {
159        hasRun = true;
160        ASSERT_EQ(renderNode.get(), n);
161        ASSERT_EQ(infoPtr, &i);
162        ASSERT_FALSE(r);
163    }));
164    ASSERT_TRUE(hasRun);
165
166    canvasContext->destroy(nullptr);
167}
168
169TEST(SkiaDisplayList, updateChildren) {
170    SkRect bounds = SkRect::MakeWH(200, 200);
171    SkiaDisplayList skiaDL(bounds);
172
173    sp<RenderNode> renderNode = new RenderNode();
174    SkCanvas dummyCanvas;
175    skiaDL.mChildNodes.emplace_back(renderNode.get(), &dummyCanvas);
176    skiaDL.updateChildren([renderNode](RenderNode* n) {
177        ASSERT_EQ(renderNode.get(), n);
178    });
179}
180