vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 10 | #include "include/core/SkBlendMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkColorPriv.h" |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 14 | #include "include/core/SkImage.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkPaint.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
| 20 | #include "include/core/SkTypes.h" |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * This GM checks that bitmap pixels are unpremultiplied before being exported |
| 24 | * to other formats. If unpremultiplication is implemented properly, this |
| 25 | * GM should come out completely white. If not, this GM looks like a row of two |
| 26 | * greyscale gradients above a row of grey lines. |
| 27 | * This tests both the ARGB4444 and ARGB8888 bitmap configurations. |
| 28 | */ |
| 29 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 30 | constexpr int SLIDE_SIZE = 256; |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 32 | static void init_bitmap(SkColorType ct, SkBitmap* bitmap) { |
| 33 | bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct, |
| 34 | kPremul_SkAlphaType)); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 35 | bitmap->eraseColor(SK_ColorWHITE); |
| 36 | } |
| 37 | |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 38 | static sk_sp<SkImage> make_argb8888_gradient() { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 39 | SkBitmap bitmap; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 40 | init_bitmap(kN32_SkColorType, &bitmap); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 41 | for (int y = 0; y < SLIDE_SIZE; y++) { |
| 42 | uint32_t* dst = bitmap.getAddr32(0, y); |
| 43 | for (int x = 0; x < SLIDE_SIZE; x++) { |
Brian Osman | bdd0f51 | 2018-11-02 16:34:01 -0400 | [diff] [blame] | 44 | dst[x] = SkPackARGB32(y, y, y, y); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 47 | return bitmap.asImage(); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 50 | static sk_sp<SkImage> make_argb4444_gradient() { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 51 | SkBitmap bitmap; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 52 | init_bitmap(kARGB_4444_SkColorType, &bitmap); |
Brian Osman | bdd0f51 | 2018-11-02 16:34:01 -0400 | [diff] [blame] | 53 | // Using draw rather than readPixels to suppress dither |
| 54 | SkPaint paint; |
| 55 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 56 | SkCanvas{ bitmap }.drawImage(make_argb8888_gradient(), 0, 0, SkSamplingOptions(), &paint); |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 57 | return bitmap.asImage(); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 60 | static sk_sp<SkImage> make_argb8888_stripes() { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 61 | SkBitmap bitmap; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 62 | init_bitmap(kN32_SkColorType, &bitmap); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 63 | uint8_t rowColor = 0; |
| 64 | for (int y = 0; y < SLIDE_SIZE; y++) { |
| 65 | uint32_t* dst = bitmap.getAddr32(0, y); |
| 66 | for (int x = 0; x < SLIDE_SIZE; x++) { |
| 67 | dst[x] = SkPackARGB32(rowColor, rowColor, |
| 68 | rowColor, rowColor); |
| 69 | } |
| 70 | if (rowColor == 0) { |
| 71 | rowColor = 255; |
| 72 | } else { |
| 73 | rowColor = 0; |
| 74 | } |
| 75 | } |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 76 | return bitmap.asImage(); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 79 | static sk_sp<SkImage> make_argb4444_stripes() { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 80 | SkBitmap bitmap; |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 81 | init_bitmap(kARGB_4444_SkColorType, &bitmap); |
Brian Osman | bdd0f51 | 2018-11-02 16:34:01 -0400 | [diff] [blame] | 82 | // Using draw rather than readPixels to suppress dither |
| 83 | SkPaint paint; |
| 84 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | e02d7f8 | 2021-01-21 22:25:21 -0500 | [diff] [blame] | 85 | SkCanvas{ bitmap }.drawImage(make_argb8888_stripes(), 0, 0, SkSamplingOptions(), &paint); |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 86 | return bitmap.asImage(); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | namespace skiagm { |
| 90 | |
| 91 | class BitmapPremulGM : public GM { |
| 92 | public: |
| 93 | BitmapPremulGM() { |
| 94 | this->setBGColor(SK_ColorWHITE); |
| 95 | } |
| 96 | |
| 97 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 98 | SkString onShortName() override { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 99 | return SkString("bitmap_premul"); |
| 100 | } |
| 101 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 102 | SkISize onISize() override { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 103 | return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2); |
| 104 | } |
| 105 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 106 | void onDraw(SkCanvas* canvas) override { |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 107 | SkScalar slideSize = SkIntToScalar(SLIDE_SIZE); |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 108 | canvas->drawImage(make_argb8888_gradient(), 0, 0); |
| 109 | canvas->drawImage(make_argb4444_gradient(), slideSize, 0); |
| 110 | canvas->drawImage(make_argb8888_stripes(), 0, slideSize); |
| 111 | canvas->drawImage(make_argb4444_stripes(), slideSize, slideSize); |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 115 | using INHERITED = GM; |
vandebo@chromium.org | ece95c3 | 2013-10-24 15:20:00 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | DEF_GM( return new BitmapPremulGM; ) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 119 | } // namespace skiagm |