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