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