File: SkiaDisplayListTests.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 326,092 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 117; sed: 19
file content (369 lines) | stat: -rw-r--r-- 14,644 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <VectorDrawable.h>
#include <gtest/gtest.h>

#include "AnimationContext.h"
#include "DamageAccumulator.h"
#include "IContextFactory.h"
#include "pipeline/skia/GLFunctorDrawable.h"
#include "pipeline/skia/SkiaDisplayList.h"
#include "renderthread/CanvasContext.h"
#include "tests/common/TestContext.h"
#include "tests/common/TestUtils.h"

using namespace android;
using namespace android::uirenderer;
using namespace android::uirenderer::renderthread;
using namespace android::uirenderer::skiapipeline;

TEST(SkiaDisplayList, create) {
    SkiaDisplayList skiaDL;
    ASSERT_TRUE(skiaDL.isEmpty());
    ASSERT_FALSE(skiaDL.mProjectionReceiver);
}

TEST(SkiaDisplayList, reset) {
    std::unique_ptr<SkiaDisplayList> skiaDL;
    {
        SkiaRecordingCanvas canvas{nullptr, 1, 1};
        canvas.drawColor(0, SkBlendMode::kSrc);
        skiaDL = canvas.finishRecording();
    }

    SkCanvas dummyCanvas;
    RenderNodeDrawable drawable(nullptr, &dummyCanvas);
    skiaDL->mChildNodes.emplace_back(nullptr, &dummyCanvas);
    int functor1 = WebViewFunctor_create(
            nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
    GLFunctorDrawable functorDrawable{functor1, &dummyCanvas};
    WebViewFunctor_release(functor1);
    skiaDL->mChildFunctors.push_back(&functorDrawable);
    skiaDL->mMutableImages.push_back(nullptr);
    skiaDL->appendVD(nullptr);
    skiaDL->mProjectionReceiver = &drawable;

    ASSERT_FALSE(skiaDL->mChildNodes.empty());
    ASSERT_FALSE(skiaDL->mChildFunctors.empty());
    ASSERT_FALSE(skiaDL->mMutableImages.empty());
    ASSERT_TRUE(skiaDL->hasVectorDrawables());
    ASSERT_FALSE(skiaDL->isEmpty());
    ASSERT_TRUE(skiaDL->mProjectionReceiver);

    skiaDL->reset();

    ASSERT_TRUE(skiaDL->mChildNodes.empty());
    ASSERT_TRUE(skiaDL->mChildFunctors.empty());
    ASSERT_TRUE(skiaDL->mMutableImages.empty());
    ASSERT_FALSE(skiaDL->hasVectorDrawables());
    ASSERT_TRUE(skiaDL->isEmpty());
    ASSERT_FALSE(skiaDL->mProjectionReceiver);
}

TEST(SkiaDisplayList, reuseDisplayList) {
    sp<RenderNode> renderNode = new RenderNode();
    std::unique_ptr<SkiaDisplayList> availableList;

    // no list has been attached so it should return a nullptr
    availableList = renderNode->detachAvailableList();
    ASSERT_EQ(availableList.get(), nullptr);

    // attach a displayList for reuse
    SkiaDisplayList skiaDL;
    ASSERT_TRUE(skiaDL.reuseDisplayList(renderNode.get()));

    // detach the list that you just attempted to reuse
    availableList = renderNode->detachAvailableList();
    ASSERT_EQ(availableList.get(), &skiaDL);
    availableList.release();  // prevents an invalid free since our DL is stack allocated

    // after detaching there should return no available list
    availableList = renderNode->detachAvailableList();
    ASSERT_EQ(availableList.get(), nullptr);
}

TEST(SkiaDisplayList, syncContexts) {
    SkiaDisplayList skiaDL;

    SkCanvas dummyCanvas;

    int functor1 = WebViewFunctor_create(
            nullptr, TestUtils::createMockFunctor(RenderMode::OpenGL_ES), RenderMode::OpenGL_ES);
    auto& counts = TestUtils::countsForFunctor(functor1);
    skiaDL.mChildFunctors.push_back(
            skiaDL.allocateDrawable<GLFunctorDrawable>(functor1, &dummyCanvas));
    WebViewFunctor_release(functor1);

    SkRect bounds = SkRect::MakeWH(200, 200);
    VectorDrawableRoot vectorDrawable(new VectorDrawable::Group());
    vectorDrawable.mutateStagingProperties()->setBounds(bounds);
    skiaDL.appendVD(&vectorDrawable);

    // ensure that the functor and vectorDrawable are properly synced
    TestUtils::runOnRenderThread([&](auto&) {
        skiaDL.syncContents(WebViewSyncData{
                .applyForceDark = false,
        });
    });

    EXPECT_EQ(counts.sync, 1);
    EXPECT_EQ(counts.destroyed, 0);
    EXPECT_EQ(vectorDrawable.mutateProperties()->getBounds(), bounds);

    skiaDL.reset();
    TestUtils::runOnRenderThread([](auto&) {
        // Fence
    });
    EXPECT_EQ(counts.destroyed, 1);
}

class ContextFactory : public IContextFactory {
public:
    virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) override {
        return new AnimationContext(clock);
    }
};

RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaDisplayList, prepareListAndChildren) {
    auto rootNode = TestUtils::createNode(0, 0, 200, 400, nullptr);
    ContextFactory contextFactory;
    std::unique_ptr<CanvasContext> canvasContext(
            CanvasContext::create(renderThread, false, rootNode.get(), &contextFactory));
    TreeInfo info(TreeInfo::MODE_FULL, *canvasContext.get());
    DamageAccumulator damageAccumulator;
    info.damageAccumulator = &damageAccumulator;

    SkiaDisplayList skiaDL;

    // The VectorDrawableRoot needs to have bounds on screen (and therefore not
    // empty) in order to have PropertyChangeWillBeConsumed set.
    const auto bounds = SkRect::MakeIWH(100, 100);

    // prepare with a clean VD
    VectorDrawableRoot cleanVD(new VectorDrawable::Group());
    cleanVD.mutateProperties()->setBounds(bounds);
    skiaDL.appendVD(&cleanVD);
    cleanVD.getBitmapUpdateIfDirty();  // this clears the dirty bit

    ASSERT_FALSE(cleanVD.isDirty());
    ASSERT_FALSE(cleanVD.getPropertyChangeWillBeConsumed());
    TestUtils::MockTreeObserver observer;
    ASSERT_FALSE(skiaDL.prepareListAndChildren(observer, info, false,
                                               [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
    ASSERT_FALSE(cleanVD.getPropertyChangeWillBeConsumed());

    // prepare again this time adding a dirty VD
    VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
    dirtyVD.mutateProperties()->setBounds(bounds);
    skiaDL.appendVD(&dirtyVD);

    ASSERT_TRUE(dirtyVD.isDirty());
    ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
    ASSERT_TRUE(skiaDL.prepareListAndChildren(observer, info, false,
                                              [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
    ASSERT_TRUE(dirtyVD.getPropertyChangeWillBeConsumed());

    // prepare again this time adding a RenderNode and a callback
    sp<RenderNode> renderNode = new RenderNode();
    TreeInfo* infoPtr = &info;
    SkCanvas dummyCanvas;
    skiaDL.mChildNodes.emplace_back(renderNode.get(), &dummyCanvas);
    bool hasRun = false;
    ASSERT_TRUE(skiaDL.prepareListAndChildren(
            observer, info, false,
            [&hasRun, renderNode, infoPtr](RenderNode* n, TreeObserver& observer, TreeInfo& i,
                                           bool r) {
                hasRun = true;
                ASSERT_EQ(renderNode.get(), n);
                ASSERT_EQ(infoPtr, &i);
                ASSERT_FALSE(r);
            }));
    ASSERT_TRUE(hasRun);

    canvasContext->destroy();
}

RENDERTHREAD_SKIA_PIPELINE_TEST(SkiaDisplayList, prepareListAndChildren_vdOffscreen) {
    auto rootNode = TestUtils::createNode(0, 0, 200, 400, nullptr);
    ContextFactory contextFactory;
    std::unique_ptr<CanvasContext> canvasContext(
            CanvasContext::create(renderThread, false, rootNode.get(), &contextFactory));

    // Set up a Surface so that we can position the VectorDrawable offscreen.
    test::TestContext testContext;
    testContext.setRenderOffscreen(true);
    auto surface = testContext.surface();
    int width = ANativeWindow_getWidth(surface.get());
    int height = ANativeWindow_getHeight(surface.get());
    canvasContext->setSurface(surface.get());

    TreeInfo info(TreeInfo::MODE_FULL, *canvasContext.get());
    DamageAccumulator damageAccumulator;
    info.damageAccumulator = &damageAccumulator;

    // The VectorDrawableRoot needs to have bounds on screen (and therefore not
    // empty) in order to have PropertyChangeWillBeConsumed set.
    const auto bounds = SkRect::MakeIWH(100, 100);

    for (const SkRect b : {bounds.makeOffset(width, 0),
                           bounds.makeOffset(0, height),
                           bounds.makeOffset(-bounds.width(), 0),
                           bounds.makeOffset(0, -bounds.height())}) {
        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(b);
        skiaDL.appendVD(&dirtyVD);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_FALSE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
    }

    // The DamageAccumulator's transform can also result in the
    // VectorDrawableRoot being offscreen.
    for (const SkISize translate : { SkISize{width, 0},
                                     SkISize{0, height},
                                     SkISize{-width, 0},
                                     SkISize{0, -height}}) {
        Matrix4 mat4;
        mat4.translate(translate.fWidth, translate.fHeight);
        damageAccumulator.pushTransform(&mat4);

        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(bounds);
        skiaDL.appendVD(&dirtyVD);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_FALSE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
        damageAccumulator.popTransform();
    }

    // Another way to be offscreen: a matrix from the draw call.
    for (const SkMatrix translate : { SkMatrix::Translate(width, 0),
                                      SkMatrix::Translate(0, height),
                                      SkMatrix::Translate(-width, 0),
                                      SkMatrix::Translate(0, -height)}) {
        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(bounds);
        skiaDL.appendVD(&dirtyVD, translate);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_FALSE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
    }

    // Verify that the matrices are combined in the right order.
    {
        // Rotate and then translate, so the VD is offscreen.
        Matrix4 mat4;
        mat4.loadRotate(180);
        damageAccumulator.pushTransform(&mat4);

        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(bounds);
        SkMatrix translate = SkMatrix::Translate(50, 50);
        skiaDL.appendVD(&dirtyVD, translate);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_FALSE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());
        damageAccumulator.popTransform();
    }
    {
        // Switch the order of rotate and translate, so it is on screen.
        Matrix4 mat4;
        mat4.translate(50, 50);
        damageAccumulator.pushTransform(&mat4);

        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(bounds);
        SkMatrix rotate;
        rotate.setRotate(180);
        skiaDL.appendVD(&dirtyVD, rotate);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_TRUE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_TRUE(dirtyVD.getPropertyChangeWillBeConsumed());
        damageAccumulator.popTransform();
    }
    {
        // An AVD that is larger than the screen.
        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(SkRect::MakeLTRB(-1, -1, width + 1, height + 1));
        skiaDL.appendVD(&dirtyVD);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_TRUE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_TRUE(dirtyVD.getPropertyChangeWillBeConsumed());
    }
    {
        // An AVD whose bounds are not a rectangle after applying a matrix.
        SkiaDisplayList skiaDL;
        VectorDrawableRoot dirtyVD(new VectorDrawable::Group());
        dirtyVD.mutateProperties()->setBounds(bounds);
        SkMatrix mat;
        mat.setRotate(45, 50, 50);
        skiaDL.appendVD(&dirtyVD, mat);

        ASSERT_TRUE(dirtyVD.isDirty());
        ASSERT_FALSE(dirtyVD.getPropertyChangeWillBeConsumed());

        TestUtils::MockTreeObserver observer;
        ASSERT_TRUE(skiaDL.prepareListAndChildren(
                observer, info, false, [](RenderNode*, TreeObserver&, TreeInfo&, bool) {}));
        ASSERT_TRUE(dirtyVD.getPropertyChangeWillBeConsumed());
    }
}

TEST(SkiaDisplayList, updateChildren) {
    SkiaDisplayList skiaDL;

    sp<RenderNode> renderNode = new RenderNode();
    SkCanvas dummyCanvas;
    skiaDL.mChildNodes.emplace_back(renderNode.get(), &dummyCanvas);
    skiaDL.updateChildren([renderNode](RenderNode* n) { ASSERT_EQ(renderNode.get(), n); });
}