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 "TestSceneBase.h"
18 
19 #include <SkBlendMode.h>
20 
21 #include <vector>
22 
23 class RoundRectClippingAnimation : public TestScene {
24 public:
25     int mSpacing, mSize;
26     int mMaxCards;
27 
RoundRectClippingAnimation(int spacing,int size,int maxCards=INT_MAX)28     RoundRectClippingAnimation(int spacing, int size, int maxCards = INT_MAX)
29             : mSpacing(spacing), mSize(size), mMaxCards(maxCards) {}
30 
31     std::vector<sp<RenderNode> > cards;
createContent(int width,int height,Canvas & canvas)32     void createContent(int width, int height, Canvas& canvas) override {
33         canvas.drawColor(0xFFFFFFFF, SkBlendMode::kSrcOver);
34         canvas.enableZ(true);
35         int ci = 0;
36         int numCards = 0;
37 
38         for (int x = 0; x < width; x += mSpacing) {
39             for (int y = 0; y < height; y += mSpacing) {
40                 auto color = BrightColors[ci++ % BrightColorsCount];
41                 auto card = TestUtils::createNode(
42                         x, y, x + mSize, y + mSize, [&](RenderProperties& props, Canvas& canvas) {
43                             canvas.drawColor(color, SkBlendMode::kSrcOver);
44                             props.mutableOutline().setRoundRect(0, 0, props.getWidth(),
45                                                                 props.getHeight(), mSize * .25, 1);
46                             props.mutableOutline().setShouldClip(true);
47                         });
48                 canvas.drawRenderNode(card.get());
49                 cards.push_back(card);
50                 ++numCards;
51                 if (numCards >= mMaxCards) {
52                     break;
53                 }
54             }
55             if (numCards >= mMaxCards) {
56                 break;
57             }
58         }
59 
60         canvas.enableZ(false);
61     }
doFrame(int frameNr)62     void doFrame(int frameNr) override {
63         int curFrame = frameNr % 50;
64         if (curFrame > 25) curFrame = 50 - curFrame;
65         for (auto& card : cards) {
66             card->mutateStagingProperties().setTranslationX(curFrame);
67             card->mutateStagingProperties().setTranslationY(curFrame);
68             card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y);
69         }
70     }
71 };
72 
73 static TestScene::Registrar _RoundRectClippingGpu(TestScene::Info{
74         "roundRectClipping-gpu",
75         "A bunch of RenderNodes with round rect clipping outlines that's GPU limited.",
__anon6e2534650202(const TestScene::Options&) 76         [](const TestScene::Options&) -> test::TestScene* {
77             return new RoundRectClippingAnimation(dp(40), dp(200));
78         }});
79 
80 static TestScene::Registrar _RoundRectClippingCpu(TestScene::Info{
81         "roundRectClipping-cpu",
82         "A bunch of RenderNodes with round rect clipping outlines that's CPU limited.",
__anon6e2534650302(const TestScene::Options&) 83         [](const TestScene::Options&) -> test::TestScene* {
84             return new RoundRectClippingAnimation(dp(20), dp(20));
85         }});
86 
87 static TestScene::Registrar _RoundRectClippingGrid(TestScene::Info{
88         "roundRectClipping-grid", "A grid of RenderNodes with round rect clipping outlines.",
__anon6e2534650402(const TestScene::Options&) 89         [](const TestScene::Options&) -> test::TestScene* {
90             return new RoundRectClippingAnimation(dp(100), dp(80));
91         }});
92 
93 static TestScene::Registrar _RoundRectClippingSingle(TestScene::Info{
94         "roundRectClipping-single", "A single RenderNodes with round rect clipping outline.",
__anon6e2534650502(const TestScene::Options&) 95         [](const TestScene::Options&) -> test::TestScene* {
96             return new RoundRectClippingAnimation(dp(100), dp(80), 1);
97         }});
98 
99 static TestScene::Registrar _RoundRectClippingSingleLarge(TestScene::Info{
100         "roundRectClipping-singlelarge",
101         "A single large RenderNodes with round rect clipping outline.",
__anon6e2534650602(const TestScene::Options&) 102         [](const TestScene::Options&) -> test::TestScene* {
103             return new RoundRectClippingAnimation(dp(100), dp(350), 1);
104         }});
105