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