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