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
20class OpPropAnimation;
21
22static TestScene::Registrar _Shapes(TestScene::Info{
23    "opprops",
24    "A minimal demonstration of CanvasProperty drawing operations.",
25    TestScene::simpleCreateScene<OpPropAnimation>
26});
27
28class OpPropAnimation : public TestScene {
29public:
30    sp<CanvasPropertyPaint> mPaint = new CanvasPropertyPaint(SkPaint());
31
32    sp<CanvasPropertyPrimitive> mRoundRectLeft = new CanvasPropertyPrimitive(0);
33    sp<CanvasPropertyPrimitive> mRoundRectTop = new CanvasPropertyPrimitive(0);
34    sp<CanvasPropertyPrimitive> mRoundRectRight = new CanvasPropertyPrimitive(0);
35    sp<CanvasPropertyPrimitive> mRoundRectBottom = new CanvasPropertyPrimitive(0);
36    sp<CanvasPropertyPrimitive> mRoundRectRx = new CanvasPropertyPrimitive(0);
37    sp<CanvasPropertyPrimitive> mRoundRectRy = new CanvasPropertyPrimitive(0);
38
39    sp<CanvasPropertyPrimitive> mCircleX = new CanvasPropertyPrimitive(0);
40    sp<CanvasPropertyPrimitive> mCircleY = new CanvasPropertyPrimitive(0);
41    sp<CanvasPropertyPrimitive> mCircleRadius = new CanvasPropertyPrimitive(0);
42
43    sp<RenderNode> content;
44    void createContent(int width, int height, TestCanvas& canvas) override {
45        content = TestUtils::createNode(0, 0, width, height,
46                [this, width, height](RenderProperties& props, TestCanvas& canvas) {
47            mPaint->value.setAntiAlias(true);
48            mPaint->value.setColor(Color::Blue_500);
49
50            mRoundRectRight->value = width / 2;
51            mRoundRectBottom->value = height / 2;
52
53            mCircleX->value = width * 0.75;
54            mCircleY->value = height * 0.75;
55
56            canvas.drawColor(Color::White, SkXfermode::Mode::kSrcOver_Mode);
57            canvas.drawRoundRect(mRoundRectLeft.get(), mRoundRectTop.get(),
58                    mRoundRectRight.get(), mRoundRectBottom.get(),
59                    mRoundRectRx.get(), mRoundRectRy.get(), mPaint.get());
60            canvas.drawCircle(mCircleX.get(), mCircleY.get(), mCircleRadius.get(), mPaint.get());
61        });
62        canvas.drawRenderNode(content.get());
63    }
64
65    void doFrame(int frameNr) override {
66        float value = (abs((frameNr % 200) - 100)) / 100.0f;
67        mRoundRectRx->value = dp(10) + value * dp(40);
68        mRoundRectRy->value = dp(10) + value * dp(80);
69        mCircleRadius->value = value * dp(200);
70        content->setPropertyFieldsDirty(RenderNode::GENERIC);
71    }
72};
73