blob: 4111bd24847e481e3e47d7a2dd49ef364acfda91 [file] [log] [blame]
Chris Craik54fa17f2015-11-25 14:14:53 -08001/*
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"
sergeyv06a62f62016-06-16 14:52:20 -070018#include "tests/common/TestListViewSceneBase.h"
Mike Reed3905e832019-01-02 10:35:42 -050019#include <SkFont.h>
Chris Craik54fa17f2015-11-25 14:14:53 -080020#include <cstdio>
21
22class ListViewAnimation;
23
Chris Craik27e58b42015-12-07 10:01:38 -080024static TestScene::Registrar _ListView(TestScene::Info{
John Reck1bcacfd2017-11-03 10:12:19 -070025 "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>});
Chris Craik54fa17f2015-11-25 14:14:53 -080030
sergeyv06a62f62016-06-16 14:52:20 -070031class ListViewAnimation : public TestListViewSceneBase {
sergeyvaed7f582016-10-14 16:30:21 -070032 sk_sp<Bitmap> createRandomCharIcon(int cardHeight) {
33 SkBitmap skBitmap;
Chris Craik54fa17f2015-11-25 14:14:53 -080034 int size = cardHeight - (dp(10) * 2);
sergeyvaed7f582016-10-14 16:30:21 -070035 sk_sp<Bitmap> bitmap(TestUtils::createBitmap(size, size, &skBitmap));
36 SkCanvas canvas(skBitmap);
Chris Craik54fa17f2015-11-25 14:14:53 -080037 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
John Reck1bcacfd2017-11-03 10:12:19 -070045 bool bgDark =
46 SkColorGetR(randomColor) + SkColorGetG(randomColor) + SkColorGetB(randomColor) <
47 128 * 3;
Chris Craik54fa17f2015-11-25 14:14:53 -080048 paint.setColor(bgDark ? Color::White : Color::Grey_700);
Mike Reed3905e832019-01-02 10:35:42 -050049
50 SkFont font;
51 font.setSize(size / 2);
Chris Craik54fa17f2015-11-25 14:14:53 -080052 char charToShow = 'A' + (rand() % 26);
Mike Reed3905e832019-01-02 10:35:42 -050053 const SkPoint pos = {SkIntToScalar(size / 2),
54 /*approximate centering*/ SkFloatToScalar(size * 0.7f)};
55 canvas.drawSimpleText(&charToShow, 1, kUTF8_SkTextEncoding, pos.fX, pos.fY, font, paint);
Chris Craik54fa17f2015-11-25 14:14:53 -080056 return bitmap;
57 }
58
sergeyvaed7f582016-10-14 16:30:21 -070059 static sk_sp<Bitmap> createBoxBitmap(bool filled) {
Chris Craik54fa17f2015-11-25 14:14:53 -080060 int size = dp(20);
61 int stroke = dp(2);
sergeyvaed7f582016-10-14 16:30:21 -070062 SkBitmap skBitmap;
63 auto bitmap = TestUtils::createBitmap(size, size, &skBitmap);
64 SkCanvas canvas(skBitmap);
Chris Craik54fa17f2015-11-25 14:14:53 -080065 canvas.clear(Color::Transparent);
66
67 SkPaint paint;
68 paint.setAntiAlias(true);
69 paint.setColor(filled ? Color::Yellow_500 : Color::Grey_700);
70 paint.setStyle(filled ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
71 paint.setStrokeWidth(stroke);
72 canvas.drawRect(SkRect::MakeLTRB(stroke, stroke, size - stroke, size - stroke), paint);
73 return bitmap;
74 }
75
John Reck1bcacfd2017-11-03 10:12:19 -070076 void createListItem(RenderProperties& props, Canvas& canvas, int cardId, int itemWidth,
77 int itemHeight) override {
sergeyvaed7f582016-10-14 16:30:21 -070078 static sk_sp<Bitmap> filledBox(createBoxBitmap(true));
79 static sk_sp<Bitmap> strokedBox(createBoxBitmap(false));
sergeyv06a62f62016-06-16 14:52:20 -070080 // TODO: switch to using round rect clipping, once merging correctly handles that
81 SkPaint roundRectPaint;
82 roundRectPaint.setAntiAlias(true);
83 roundRectPaint.setColor(Color::White);
84 canvas.drawRoundRect(0, 0, itemWidth, itemHeight, dp(6), dp(6), roundRectPaint);
Chris Craik54fa17f2015-11-25 14:14:53 -080085
sergeyv06a62f62016-06-16 14:52:20 -070086 SkPaint textPaint;
sergeyv06a62f62016-06-16 14:52:20 -070087 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,
John Reck1bcacfd2017-11-03 10:12:19 -070095 itemHeight, dp(45));
Chris Craik54fa17f2015-11-25 14:14:53 -080096
sergeyvaed7f582016-10-14 16:30:21 -070097 auto randomIcon = createRandomCharIcon(itemHeight);
98 canvas.drawBitmap(*randomIcon, dp(10), dp(10), nullptr);
Chris Craik54fa17f2015-11-25 14:14:53 -080099
sergeyvaed7f582016-10-14 16:30:21 -0700100 auto box = rand() % 2 ? filledBox : strokedBox;
101 canvas.drawBitmap(*box, itemWidth - dp(10) - box->width(), dp(10), nullptr);
Chris Craik54fa17f2015-11-25 14:14:53 -0800102 }
103};