116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck/*
216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * Copyright (C) 2015 The Android Open Source Project
316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck *
416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * Licensed under the Apache License, Version 2.0 (the "License");
516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * you may not use this file except in compliance with the License.
616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * You may obtain a copy of the License at
716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck *
816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck *      http://www.apache.org/licenses/LICENSE-2.0
916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck *
1016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * Unless required by applicable law or agreed to in writing, software
1116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * distributed under the License is distributed on an "AS IS" BASIS,
1216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * See the License for the specific language governing permissions and
1416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck * limitations under the License.
1516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck */
1616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
1716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck#include "TestSceneBase.h"
1854fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik#include "utils/Color.h"
1916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
2016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reckclass RecentsAnimation;
2116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
2227e58b4f54d693ff1db7ab2edb5d47ca296c1278Chris Craikstatic TestScene::Registrar _Recents(TestScene::Info{
2316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    "recents",
2416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    "A recents-like scrolling list of textures. "
2516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    "Consists of updating a texture every frame",
2627e58b4f54d693ff1db7ab2edb5d47ca296c1278Chris Craik    TestScene::simpleCreateScene<RecentsAnimation>
2716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck});
2816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
2916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reckclass RecentsAnimation : public TestScene {
3016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reckpublic:
3106152cdd06da50762716cd455dcf7ab0117f25b0Stan Iliev    void createContent(int width, int height, Canvas& renderer) override {
3216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        static SkColor COLORS[] = {
3354fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik                Color::Red_500,
3454fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik                Color::Purple_500,
3554fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik                Color::Blue_500,
3654fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik                Color::Green_500,
3716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        };
3816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
3916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        thumbnailSize = std::min(std::min(width, height) / 2, 720);
4016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        int cardsize = std::min(width, height) - dp(64);
4116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
42260ab726486317496bc12a57d599ea96dcde3284Mike Reed        renderer.drawColor(Color::White, SkBlendMode::kSrcOver);
4316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        renderer.insertReorderBarrier(true);
4416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
4516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        int x = dp(32);
4616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        for (int i = 0; i < 4; i++) {
4716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            int y = (height / 4) * i;
48fc9999505a36c66892d7ccce85187936105f4f36sergeyv            SkBitmap bitmap;
49fc9999505a36c66892d7ccce85187936105f4f36sergeyv            sk_sp<Bitmap> thumb(TestUtils::createBitmap(thumbnailSize, thumbnailSize, &bitmap));
50fc9999505a36c66892d7ccce85187936105f4f36sergeyv
51fc9999505a36c66892d7ccce85187936105f4f36sergeyv            bitmap.eraseColor(COLORS[i]);
52fc9999505a36c66892d7ccce85187936105f4f36sergeyv            sp<RenderNode> card = createCard(x, y, cardsize, cardsize, *thumb);
5316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            card->mutateStagingProperties().setElevation(i * dp(8));
5416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            renderer.drawRenderNode(card.get());
55fc9999505a36c66892d7ccce85187936105f4f36sergeyv            mThumbnail = bitmap;
5616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            mCards.push_back(card);
5716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        }
5816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
5916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        renderer.insertReorderBarrier(false);
6016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    }
6116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
6216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    void doFrame(int frameNr) override {
6316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        int curFrame = frameNr % 150;
6416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        for (size_t ci = 0; ci < mCards.size(); ci++) {
6516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            mCards[ci]->mutateStagingProperties().setTranslationY(curFrame);
6616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            mCards[ci]->setPropertyFieldsDirty(RenderNode::Y);
6716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        }
6816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        mThumbnail.eraseColor(TestUtils::interpolateColor(
6954fa17f667c285a5c9225e238c8132dfe830ef36Chris Craik                curFrame / 150.0f, Color::Green_500, Color::DeepOrange_500));
7016c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    }
7116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
7216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reckprivate:
73fc9999505a36c66892d7ccce85187936105f4f36sergeyv    sp<RenderNode> createCard(int x, int y, int width, int height, Bitmap& thumb) {
7416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        return TestUtils::createNode(x, y, x + width, y + height,
7506152cdd06da50762716cd455dcf7ab0117f25b0Stan Iliev                [&thumb, width, height](RenderProperties& props, Canvas& canvas) {
7616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            props.setElevation(dp(16));
7716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            props.mutableOutline().setRoundRect(0, 0, width, height, dp(10), 1);
7816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            props.mutableOutline().setShouldClip(true);
7916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
80260ab726486317496bc12a57d599ea96dcde3284Mike Reed            canvas.drawColor(Color::Grey_200, SkBlendMode::kSrcOver);
8116c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck            canvas.drawBitmap(thumb, 0, 0, thumb.width(), thumb.height(),
8216c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck                    0, 0, width, height, nullptr);
8316c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck        });
8416c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    }
8516c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck
8616c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    SkBitmap mThumbnail;
8716c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    std::vector< sp<RenderNode> > mCards;
8816c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck    int thumbnailSize;
8916c9d6a92e1b86d448c00c52a1630f3e71e6df76John Reck};
90