blob: a8995104eff98bb27542a10fa7e4ca44780c0056 [file] [log] [blame]
vandebo@chromium.orgece95c32013-10-24 15:20:00 +00001/*
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 Clarka4083c92017-09-15 11:59:23 -040011#include "SkColorData.h"
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000012
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
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr int SLIDE_SIZE = 256;
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000022
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000023static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
24 bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
25 kPremul_SkAlphaType));
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000026 bitmap->eraseColor(SK_ColorWHITE);
27}
28
29static SkBitmap make_argb8888_gradient() {
30 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000031 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000032 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 Osmanbdd0f512018-11-02 16:34:01 -040035 dst[x] = SkPackARGB32(y, y, y, y);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000036 }
37 }
38 return bitmap;
39}
40
41static SkBitmap make_argb4444_gradient() {
42 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000043 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040044 // 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.orgece95c32013-10-24 15:20:00 +000048 return bitmap;
49}
50
51static SkBitmap make_argb8888_stripes() {
52 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000053 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000054 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
70static SkBitmap make_argb4444_stripes() {
71 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000072 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040073 // 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.orgece95c32013-10-24 15:20:00 +000077 return bitmap;
78}
79
80namespace skiagm {
81
82class BitmapPremulGM : public GM {
83public:
84 BitmapPremulGM() {
85 this->setBGColor(SK_ColorWHITE);
86 }
87
88protected:
mtklein36352bf2015-03-25 18:17:31 -070089 SkString onShortName() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000090 return SkString("bitmap_premul");
91 }
92
mtklein36352bf2015-03-25 18:17:31 -070093 SkISize onISize() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000094 return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
95 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000098 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
105private:
106 typedef GM INHERITED;
107};
108
109DEF_GM( return new BitmapPremulGM; )
110}