1 /*
2  * Copyright (C) 2015 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 #include "utils/Color.h"
19 
20 #include <SkBlendMode.h>
21 
22 #include <cstdio>
23 
24 class ShapeAnimation;
25 
26 static TestScene::Registrar _Shapes(TestScene::Info{"shapes", "A grid of shape drawing test cases.",
27                                                     TestScene::simpleCreateScene<ShapeAnimation>});
28 
29 class ShapeAnimation : public TestScene {
30 public:
31     sp<RenderNode> card;
createContent(int width,int height,Canvas & canvas)32     void createContent(int width, int height, Canvas& canvas) override {
33         card = TestUtils::createNode(
34                 0, 0, width, height, [width](RenderProperties& props, Canvas& canvas) {
35                     std::function<void(Canvas&, float, const Paint&)> ops[] = {
36                             [](Canvas& canvas, float size, const Paint& paint) {
37                                 canvas.drawArc(0, 0, size, size, 50, 189, true, paint);
38                             },
39                             [](Canvas& canvas, float size, const Paint& paint) {
40                                 canvas.drawOval(0, 0, size, size, paint);
41                             },
42                             [](Canvas& canvas, float size, const Paint& paint) {
43                                 SkPath diamondPath;
44                                 diamondPath.moveTo(size / 2, 0);
45                                 diamondPath.lineTo(size, size / 2);
46                                 diamondPath.lineTo(size / 2, size);
47                                 diamondPath.lineTo(0, size / 2);
48                                 diamondPath.close();
49                                 canvas.drawPath(diamondPath, paint);
50                             },
51                             [](Canvas& canvas, float size, const Paint& paint) {
52                                 float data[] = {0, 0, size, size, 0, size, size, 0};
53                                 canvas.drawLines(data, sizeof(data) / sizeof(float), paint);
54                             },
55                             [](Canvas& canvas, float size, const Paint& paint) {
56                                 float data[] = {0, 0, size, size, 0, size, size, 0};
57                                 canvas.drawPoints(data, sizeof(data) / sizeof(float), paint);
58                             },
59                             [](Canvas& canvas, float size, const Paint& paint) {
60                                 canvas.drawRect(0, 0, size, size, paint);
61                             },
62                             [](Canvas& canvas, float size, const Paint& paint) {
63                                 float rad = size / 4;
64                                 canvas.drawRoundRect(0, 0, size, size, rad, rad, paint);
65                             }};
66                     float cellSpace = dp(4);
67                     float cellSize = floorf(width / 7 - cellSpace);
68 
69                     // each combination of strokeWidth + style gets a column
70                     int outerCount = canvas.save(SaveFlags::MatrixClip);
71                     Paint paint;
72                     paint.setAntiAlias(true);
73                     SkPaint::Style styles[] = {SkPaint::kStroke_Style, SkPaint::kFill_Style,
74                                                SkPaint::kStrokeAndFill_Style};
75                     for (auto style : styles) {
76                         paint.setStyle(style);
77                         for (auto strokeWidth : {0.0f, 0.5f, 8.0f}) {
78                             paint.setStrokeWidth(strokeWidth);
79                             // fill column with each op
80                             int middleCount = canvas.save(SaveFlags::MatrixClip);
81                             for (const auto& op : ops) {
82                                 int innerCount = canvas.save(SaveFlags::MatrixClip);
83                                 canvas.clipRect(0, 0, cellSize, cellSize, SkClipOp::kIntersect);
84                                 canvas.drawColor(Color::White, SkBlendMode::kSrcOver);
85                                 op(canvas, cellSize, paint);
86                                 canvas.restoreToCount(innerCount);
87                                 canvas.translate(cellSize + cellSpace, 0);
88                             }
89                             canvas.restoreToCount(middleCount);
90                             canvas.translate(0, cellSize + cellSpace);
91                         }
92                     }
93                     canvas.restoreToCount(outerCount);
94                 });
95         canvas.drawColor(Color::Grey_500, SkBlendMode::kSrcOver);
96         canvas.drawRenderNode(card.get());
97     }
98 
doFrame(int frameNr)99     void doFrame(int frameNr) override {
100         card->mutateStagingProperties().setTranslationY(frameNr % 150);
101         card->setPropertyFieldsDirty(RenderNode::Y);
102     }
103 };
104