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