• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
18 
19 #include "TestSceneBase.h"
20 
21 class PathClippingAnimation : public TestScene {
22 public:
23     int mSpacing, mSize;
24     bool mClip, mAnimateClip;
25     int mMaxCards;
26     std::vector<sp<RenderNode> > cards;
27 
PathClippingAnimation(int spacing,int size,bool clip,bool animateClip,int maxCards)28     PathClippingAnimation(int spacing, int size, bool clip, bool animateClip, int maxCards)
29             : mSpacing(spacing)
30             , mSize(size)
31             , mClip(clip)
32             , mAnimateClip(animateClip)
33             , mMaxCards(maxCards) {}
34 
PathClippingAnimation(int spacing,int size,bool clip,bool animateClip)35     PathClippingAnimation(int spacing, int size, bool clip, bool animateClip)
36             : PathClippingAnimation(spacing, size, clip, animateClip, INT_MAX) {}
37 
createContent(int width,int height,Canvas & canvas)38     void createContent(int width, int height, Canvas& canvas) override {
39         canvas.drawColor(0xFFFFFFFF, SkBlendMode::kSrcOver);
40         canvas.enableZ(true);
41         int ci = 0;
42         int numCards = 0;
43 
44         for (int x = 0; x < width; x += mSpacing) {
45             for (int y = 0; y < height; y += mSpacing) {
46                 auto color = BrightColors[ci++ % BrightColorsCount];
47                 auto card = TestUtils::createNode(
48                         x, y, x + mSize, y + mSize, [&](RenderProperties& props, Canvas& canvas) {
49                             canvas.drawColor(color, SkBlendMode::kSrcOver);
50                             if (mClip) {
51                                 // Create circular path that rounds around the inside of all
52                                 // four corners of the given square defined by mSize*mSize
53                                 SkPath path = setPath(mSize);
54                                 props.mutableOutline().setPath(&path, 1);
55                                 props.mutableOutline().setShouldClip(true);
56                             }
57                         });
58                 canvas.drawRenderNode(card.get());
59                 cards.push_back(card);
60                 ++numCards;
61                 if (numCards >= mMaxCards) {
62                     break;
63                 }
64             }
65             if (numCards >= mMaxCards) {
66                 break;
67             }
68         }
69 
70         canvas.enableZ(false);
71     }
72 
setPath(int size)73     SkPath setPath(int size) {
74         SkPath path;
75         path.moveTo(0, size / 2);
76         path.cubicTo(0, size * .75, size * .25, size, size / 2, size);
77         path.cubicTo(size * .75, size, size, size * .75, size, size / 2);
78         path.cubicTo(size, size * .25, size * .75, 0, size / 2, 0);
79         path.cubicTo(size / 4, 0, 0, size / 4, 0, size / 2);
80         return path;
81     }
82 
doFrame(int frameNr)83     void doFrame(int frameNr) override {
84         int curFrame = frameNr % 50;
85         if (curFrame > 25) curFrame = 50 - curFrame;
86         for (auto& card : cards) {
87             if (mAnimateClip) {
88                 SkPath path = setPath(mSize - curFrame);
89                 card->mutateStagingProperties().mutableOutline().setPath(&path, 1);
90             }
91             card->mutateStagingProperties().setTranslationX(curFrame);
92             card->mutateStagingProperties().setTranslationY(curFrame);
93             card->setPropertyFieldsDirty(RenderNode::X | RenderNode::Y | RenderNode::DISPLAY_LIST);
94         }
95     }
96 };
97 
98 static TestScene::Registrar _PathClippingUnclipped(TestScene::Info{
99         "pathClipping-unclipped", "Multiple RenderNodes, unclipped.",
__anon24a6f6280202(const TestScene::Options&) 100         [](const TestScene::Options&) -> test::TestScene* {
101             return new PathClippingAnimation(dp(100), dp(80), false, false);
102         }});
103 
104 static TestScene::Registrar _PathClippingUnclippedSingle(TestScene::Info{
105         "pathClipping-unclippedsingle", "A single RenderNode, unclipped.",
__anon24a6f6280302(const TestScene::Options&) 106         [](const TestScene::Options&) -> test::TestScene* {
107             return new PathClippingAnimation(dp(100), dp(80), false, false, 1);
108         }});
109 
110 static TestScene::Registrar _PathClippingUnclippedSingleLarge(TestScene::Info{
111         "pathClipping-unclippedsinglelarge", "A single large RenderNode, unclipped.",
__anon24a6f6280402(const TestScene::Options&) 112         [](const TestScene::Options&) -> test::TestScene* {
113             return new PathClippingAnimation(dp(100), dp(350), false, false, 1);
114         }});
115 
116 static TestScene::Registrar _PathClippingClipped80(TestScene::Info{
117         "pathClipping-clipped80", "Multiple RenderNodes, clipped by paths.",
__anon24a6f6280502(const TestScene::Options&) 118         [](const TestScene::Options&) -> test::TestScene* {
119             return new PathClippingAnimation(dp(100), dp(80), true, false);
120         }});
121 
122 static TestScene::Registrar _PathClippingClippedSingle(TestScene::Info{
123         "pathClipping-clippedsingle", "A single RenderNode, clipped by a path.",
__anon24a6f6280602(const TestScene::Options&) 124         [](const TestScene::Options&) -> test::TestScene* {
125             return new PathClippingAnimation(dp(100), dp(80), true, false, 1);
126         }});
127 
128 static TestScene::Registrar _PathClippingClippedSingleLarge(TestScene::Info{
129         "pathClipping-clippedsinglelarge", "A single large RenderNode, clipped by a path.",
__anon24a6f6280702(const TestScene::Options&) 130         [](const TestScene::Options&) -> test::TestScene* {
131             return new PathClippingAnimation(dp(100), dp(350), true, false, 1);
132         }});
133 
134 static TestScene::Registrar _PathClippingAnimated(TestScene::Info{
135         "pathClipping-animated",
136         "Multiple RenderNodes, clipped by paths which are being altered every frame.",
__anon24a6f6280802(const TestScene::Options&) 137         [](const TestScene::Options&) -> test::TestScene* {
138             return new PathClippingAnimation(dp(100), dp(80), true, true);
139         }});
140 
141 static TestScene::Registrar _PathClippingAnimatedSingle(TestScene::Info{
142         "pathClipping-animatedsingle",
143         "A single RenderNode, clipped by a path which is being altered every frame.",
__anon24a6f6280902(const TestScene::Options&) 144         [](const TestScene::Options&) -> test::TestScene* {
145             return new PathClippingAnimation(dp(100), dp(80), true, true, 1);
146         }});
147 
148 static TestScene::Registrar _PathClippingAnimatedSingleLarge(TestScene::Info{
149         "pathClipping-animatedsinglelarge",
150         "A single large RenderNode, clipped by a path which is being altered every frame.",
__anon24a6f6280a02(const TestScene::Options&) 151         [](const TestScene::Options&) -> test::TestScene* {
152             return new PathClippingAnimation(dp(100), dp(350), true, true, 1);
153         }});
154