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