Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 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" |
sergeyv | 06a62f6 | 2016-06-16 14:52:20 -0700 | [diff] [blame] | 18 | #include "tests/common/TestListViewSceneBase.h" |
Mike Reed | 3905e83 | 2019-01-02 10:35:42 -0500 | [diff] [blame] | 19 | #include <SkFont.h> |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | |
| 22 | class ListViewAnimation; |
| 23 | |
Chris Craik | 27e58b4 | 2015-12-07 10:01:38 -0800 | [diff] [blame] | 24 | static TestScene::Registrar _ListView(TestScene::Info{ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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>}); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 30 | |
sergeyv | 06a62f6 | 2016-06-16 14:52:20 -0700 | [diff] [blame] | 31 | class ListViewAnimation : public TestListViewSceneBase { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 32 | sk_sp<Bitmap> createRandomCharIcon(int cardHeight) { |
| 33 | SkBitmap skBitmap; |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 34 | int size = cardHeight - (dp(10) * 2); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 35 | sk_sp<Bitmap> bitmap(TestUtils::createBitmap(size, size, &skBitmap)); |
| 36 | SkCanvas canvas(skBitmap); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 45 | bool bgDark = |
| 46 | SkColorGetR(randomColor) + SkColorGetG(randomColor) + SkColorGetB(randomColor) < |
| 47 | 128 * 3; |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 48 | paint.setColor(bgDark ? Color::White : Color::Grey_700); |
Mike Reed | 3905e83 | 2019-01-02 10:35:42 -0500 | [diff] [blame] | 49 | |
| 50 | SkFont font; |
| 51 | font.setSize(size / 2); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 52 | char charToShow = 'A' + (rand() % 26); |
Mike Reed | 3905e83 | 2019-01-02 10:35:42 -0500 | [diff] [blame] | 53 | 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 Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 56 | return bitmap; |
| 57 | } |
| 58 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 59 | static sk_sp<Bitmap> createBoxBitmap(bool filled) { |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 60 | int size = dp(20); |
| 61 | int stroke = dp(2); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 62 | SkBitmap skBitmap; |
| 63 | auto bitmap = TestUtils::createBitmap(size, size, &skBitmap); |
| 64 | SkCanvas canvas(skBitmap); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 65 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | void createListItem(RenderProperties& props, Canvas& canvas, int cardId, int itemWidth, |
| 77 | int itemHeight) override { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 78 | static sk_sp<Bitmap> filledBox(createBoxBitmap(true)); |
| 79 | static sk_sp<Bitmap> strokedBox(createBoxBitmap(false)); |
sergeyv | 06a62f6 | 2016-06-16 14:52:20 -0700 | [diff] [blame] | 80 | // 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 Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 85 | |
sergeyv | 06a62f6 | 2016-06-16 14:52:20 -0700 | [diff] [blame] | 86 | SkPaint textPaint; |
sergeyv | 06a62f6 | 2016-06-16 14:52:20 -0700 | [diff] [blame] | 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, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | itemHeight, dp(45)); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 96 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 97 | auto randomIcon = createRandomCharIcon(itemHeight); |
| 98 | canvas.drawBitmap(*randomIcon, dp(10), dp(10), nullptr); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 99 | |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 100 | auto box = rand() % 2 ? filledBox : strokedBox; |
| 101 | canvas.drawBitmap(*box, itemWidth - dp(10) - box->width(), dp(10), nullptr); |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 102 | } |
| 103 | }; |