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