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 RecentsAnimation;
21
22static TestScene::Registrar _Recents(TestScene::Info{
23    "recents",
24    "A recents-like scrolling list of textures. "
25    "Consists of updating a texture every frame",
26    TestScene::simpleCreateScene<RecentsAnimation>
27});
28
29class RecentsAnimation : public TestScene {
30public:
31    void createContent(int width, int height, TestCanvas& renderer) override {
32        static SkColor COLORS[] = {
33                Color::Red_500,
34                Color::Purple_500,
35                Color::Blue_500,
36                Color::Green_500,
37        };
38
39        thumbnailSize = std::min(std::min(width, height) / 2, 720);
40        int cardsize = std::min(width, height) - dp(64);
41
42        renderer.drawColor(Color::White, SkXfermode::kSrcOver_Mode);
43        renderer.insertReorderBarrier(true);
44
45        int x = dp(32);
46        for (int i = 0; i < 4; i++) {
47            int y = (height / 4) * i;
48            SkBitmap thumb = TestUtils::createSkBitmap(thumbnailSize, thumbnailSize);
49            thumb.eraseColor(COLORS[i]);
50            sp<RenderNode> card = createCard(x, y, cardsize, cardsize, thumb);
51            card->mutateStagingProperties().setElevation(i * dp(8));
52            renderer.drawRenderNode(card.get());
53            mThumbnail = thumb;
54            mCards.push_back(card);
55        }
56
57        renderer.insertReorderBarrier(false);
58    }
59
60    void doFrame(int frameNr) override {
61        int curFrame = frameNr % 150;
62        for (size_t ci = 0; ci < mCards.size(); ci++) {
63            mCards[ci]->mutateStagingProperties().setTranslationY(curFrame);
64            mCards[ci]->setPropertyFieldsDirty(RenderNode::Y);
65        }
66        mThumbnail.eraseColor(TestUtils::interpolateColor(
67                curFrame / 150.0f, Color::Green_500, Color::DeepOrange_500));
68    }
69
70private:
71    sp<RenderNode> createCard(int x, int y, int width, int height,
72            const SkBitmap& thumb) {
73        return TestUtils::createNode(x, y, x + width, y + height,
74                [&thumb, width, height](RenderProperties& props, TestCanvas& canvas) {
75            props.setElevation(dp(16));
76            props.mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1);
77            props.mutableOutline().setShouldClip(true);
78
79            canvas.drawColor(Color::Grey_200, SkXfermode::kSrcOver_Mode);
80            canvas.drawBitmap(thumb, 0, 0, thumb.width(), thumb.height(),
81                    0, 0, width, height, nullptr);
82        });
83    }
84
85    SkBitmap mThumbnail;
86    std::vector< sp<RenderNode> > mCards;
87    int thumbnailSize;
88};
89