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