• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 class ListViewAnimation;
23 
24 static TestScene::Registrar _ListView(TestScene::Info{
25     "listview",
26     "A mock ListView of scrolling content. Doesn't re-bind/re-record views as they are recycled, so"
27     "won't upload much content (either glyphs, or bitmaps).",
28     TestScene::simpleCreateScene<ListViewAnimation>
29 });
30 
31 class ListViewAnimation : public TestListViewSceneBase {
createRandomCharIcon(int cardHeight)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 = SkColorGetR(randomColor) + SkColorGetG(randomColor) + SkColorGetB(randomColor)
46                 < 128 * 3;
47         paint.setColor(bgDark ? Color::White : Color::Grey_700);
48         paint.setTextAlign(SkPaint::kCenter_Align);
49         paint.setTextSize(size / 2);
50         char charToShow = 'A' + (rand() % 26);
51         const SkPoint pos[] = {{
52                 SkIntToScalar(size / 2),
53                 /*approximate centering*/ SkFloatToScalar(size * 0.7f)}};
54         canvas.drawPosText(&charToShow, 1, pos, paint);
55         return bitmap;
56     }
57 
createBoxBitmap(bool filled)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 
createListItem(RenderProperties & props,Canvas & canvas,int cardId,int itemWidth,int itemHeight)75     void createListItem(RenderProperties& props, Canvas& canvas, int cardId,
76             int itemWidth, 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.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
87         textPaint.setColor(rand() % 2 ? Color::Black : Color::Grey_500);
88         textPaint.setTextSize(dp(20));
89         textPaint.setAntiAlias(true);
90         char buf[256];
91         snprintf(buf, sizeof(buf), "This card is #%d", cardId);
92         TestUtils::drawUtf8ToCanvas(&canvas, buf, textPaint, itemHeight, dp(25));
93         textPaint.setTextSize(dp(15));
94         TestUtils::drawUtf8ToCanvas(&canvas, "This is some more text on the card", textPaint,
95                 itemHeight, dp(45));
96 
97         auto randomIcon = createRandomCharIcon(itemHeight);
98         canvas.drawBitmap(*randomIcon, dp(10), dp(10), nullptr);
99 
100         auto box = rand() % 2 ? filledBox : strokedBox;
101         canvas.drawBitmap(*box, itemWidth - dp(10) - box->width(), dp(10), nullptr);
102     }
103 };
104