blob: af3e3be90d04cd9f311f58a1529d28715265f539 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040010#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#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.orgece95c32013-10-24 15:20:00 +000020
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
mtkleindbfd7ab2016-09-01 11:24:54 -070029constexpr int SLIDE_SIZE = 256;
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000030
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000031static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
32 bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
33 kPremul_SkAlphaType));
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000034 bitmap->eraseColor(SK_ColorWHITE);
35}
36
37static SkBitmap make_argb8888_gradient() {
38 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000039 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000040 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 Osmanbdd0f512018-11-02 16:34:01 -040043 dst[x] = SkPackARGB32(y, y, y, y);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000044 }
45 }
46 return bitmap;
47}
48
49static SkBitmap make_argb4444_gradient() {
50 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000051 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040052 // 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.orgece95c32013-10-24 15:20:00 +000056 return bitmap;
57}
58
59static SkBitmap make_argb8888_stripes() {
60 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000061 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000062 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
78static SkBitmap make_argb4444_stripes() {
79 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000080 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040081 // 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.orgece95c32013-10-24 15:20:00 +000085 return bitmap;
86}
87
88namespace skiagm {
89
90class BitmapPremulGM : public GM {
91public:
92 BitmapPremulGM() {
93 this->setBGColor(SK_ColorWHITE);
94 }
95
96protected:
mtklein36352bf2015-03-25 18:17:31 -070097 SkString onShortName() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000098 return SkString("bitmap_premul");
99 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 SkISize onISize() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +0000102 return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
103 }
104
mtklein36352bf2015-03-25 18:17:31 -0700105 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +0000106 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
113private:
114 typedef GM INHERITED;
115};
116
117DEF_GM( return new BitmapPremulGM; )
118}