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, Canvas& 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, SkBlendMode::kSrcOver);
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 bitmap;
49            sk_sp<Bitmap> thumb(TestUtils::createBitmap(thumbnailSize, thumbnailSize, &bitmap));
50
51            bitmap.eraseColor(COLORS[i]);
52            sp<RenderNode> card = createCard(x, y, cardsize, cardsize, *thumb);
53            card->mutateStagingProperties().setElevation(i * dp(8));
54            renderer.drawRenderNode(card.get());
55            mThumbnail = bitmap;
56            mCards.push_back(card);
57        }
58
59        renderer.insertReorderBarrier(false);
60    }
61
62    void doFrame(int frameNr) override {
63        int curFrame = frameNr % 150;
64        for (size_t ci = 0; ci < mCards.size(); ci++) {
65            mCards[ci]->mutateStagingProperties().setTranslationY(curFrame);
66            mCards[ci]->setPropertyFieldsDirty(RenderNode::Y);
67        }
68        mThumbnail.eraseColor(TestUtils::interpolateColor(
69                curFrame / 150.0f, Color::Green_500, Color::DeepOrange_500));
70    }
71
72private:
73    sp<RenderNode> createCard(int x, int y, int width, int height, Bitmap& thumb) {
74        return TestUtils::createNode(x, y, x + width, y + height,
75                [&thumb, width, height](RenderProperties& props, Canvas& canvas) {
76            props.setElevation(dp(16));
77            props.mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1);
78            props.mutableOutline().setShouldClip(true);
79
80            canvas.drawColor(Color::Grey_200, SkBlendMode::kSrcOver);
81            canvas.drawBitmap(thumb, 0, 0, thumb.width(), thumb.height(),
82                    0, 0, width, height, nullptr);
83        });
84    }
85
86    SkBitmap mThumbnail;
87    std::vector< sp<RenderNode> > mCards;
88    int thumbnailSize;
89};
90