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