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 "tests/common/TestListViewSceneBase.h"
19
20#include <cstdio>
21
22class ListViewAnimation;
23
24static TestScene::Registrar _ListView(TestScene::Info{
25        "listview",
26        "A mock ListView of scrolling content. Doesn't re-bind/re-record views as they are "
27        "recycled, so"
28        "won't upload much content (either glyphs, or bitmaps).",
29        TestScene::simpleCreateScene<ListViewAnimation>});
30
31class ListViewAnimation : public TestListViewSceneBase {
32    sk_sp<Bitmap> createRandomCharIcon(int cardHeight) {
33        SkBitmap skBitmap;
34        int size = cardHeight - (dp(10) * 2);
35        sk_sp<Bitmap> bitmap(TestUtils::createBitmap(size, size, &skBitmap));
36        SkCanvas canvas(skBitmap);
37        canvas.clear(0);
38
39        SkPaint paint;
40        paint.setAntiAlias(true);
41        SkColor randomColor = BrightColors[rand() % BrightColorsCount];
42        paint.setColor(randomColor);
43        canvas.drawCircle(size / 2, size / 2, size / 2, paint);
44
45        bool bgDark =
46                SkColorGetR(randomColor) + SkColorGetG(randomColor) + SkColorGetB(randomColor) <
47                128 * 3;
48        paint.setColor(bgDark ? Color::White : Color::Grey_700);
49        paint.setTextAlign(SkPaint::kCenter_Align);
50        paint.setTextSize(size / 2);
51        char charToShow = 'A' + (rand() % 26);
52        const SkPoint pos[] = {{SkIntToScalar(size / 2),
53                                /*approximate centering*/ SkFloatToScalar(size * 0.7f)}};
54        canvas.drawPosText(&charToShow, 1, pos, paint);
55        return bitmap;
56    }
57
58    static sk_sp<Bitmap> createBoxBitmap(bool filled) {
59        int size = dp(20);
60        int stroke = dp(2);
61        SkBitmap skBitmap;
62        auto bitmap = TestUtils::createBitmap(size, size, &skBitmap);
63        SkCanvas canvas(skBitmap);
64        canvas.clear(Color::Transparent);
65
66        SkPaint paint;
67        paint.setAntiAlias(true);
68        paint.setColor(filled ? Color::Yellow_500 : Color::Grey_700);
69        paint.setStyle(filled ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
70        paint.setStrokeWidth(stroke);
71        canvas.drawRect(SkRect::MakeLTRB(stroke, stroke, size - stroke, size - stroke), paint);
72        return bitmap;
73    }
74
75    void createListItem(RenderProperties& props, Canvas& canvas, int cardId, int itemWidth,
76                        int itemHeight) override {
77        static sk_sp<Bitmap> filledBox(createBoxBitmap(true));
78        static sk_sp<Bitmap> strokedBox(createBoxBitmap(false));
79        // TODO: switch to using round rect clipping, once merging correctly handles that
80        SkPaint roundRectPaint;
81        roundRectPaint.setAntiAlias(true);
82        roundRectPaint.setColor(Color::White);
83        canvas.drawRoundRect(0, 0, itemWidth, itemHeight, dp(6), dp(6), roundRectPaint);
84
85        SkPaint textPaint;
86        textPaint.setColor(rand() % 2 ? Color::Black : Color::Grey_500);
87        textPaint.setTextSize(dp(20));
88        textPaint.setAntiAlias(true);
89        char buf[256];
90        snprintf(buf, sizeof(buf), "This card is #%d", cardId);
91        TestUtils::drawUtf8ToCanvas(&canvas, buf, textPaint, itemHeight, dp(25));
92        textPaint.setTextSize(dp(15));
93        TestUtils::drawUtf8ToCanvas(&canvas, "This is some more text on the card", textPaint,
94                                    itemHeight, dp(45));
95
96        auto randomIcon = createRandomCharIcon(itemHeight);
97        canvas.drawBitmap(*randomIcon, dp(10), dp(10), nullptr);
98
99        auto box = rand() % 2 ? filledBox : strokedBox;
100        canvas.drawBitmap(*box, itemWidth - dp(10) - box->width(), dp(10), nullptr);
101    }
102};
103