blob: b11f47d1acd2ac87de6bad8b1edd32c3011a312c [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"
Mike Reed6dbeac52021-01-13 13:14:23 -050014#include "include/core/SkImage.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#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.orgece95c32013-10-24 15:20:00 +000021
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
mtkleindbfd7ab2016-09-01 11:24:54 -070030constexpr int SLIDE_SIZE = 256;
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000031
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000032static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
33 bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
34 kPremul_SkAlphaType));
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000035 bitmap->eraseColor(SK_ColorWHITE);
36}
37
Mike Reed6dbeac52021-01-13 13:14:23 -050038static sk_sp<SkImage> make_argb8888_gradient() {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000039 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000040 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000041 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 Osmanbdd0f512018-11-02 16:34:01 -040044 dst[x] = SkPackARGB32(y, y, y, y);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000045 }
46 }
Mike Reed6dbeac52021-01-13 13:14:23 -050047 return bitmap.asImage();
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000048}
49
Mike Reed6dbeac52021-01-13 13:14:23 -050050static sk_sp<SkImage> make_argb4444_gradient() {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000051 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000052 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040053 // Using draw rather than readPixels to suppress dither
54 SkPaint paint;
55 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reede02d7f82021-01-21 22:25:21 -050056 SkCanvas{ bitmap }.drawImage(make_argb8888_gradient(), 0, 0, SkSamplingOptions(), &paint);
Mike Reed6dbeac52021-01-13 13:14:23 -050057 return bitmap.asImage();
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000058}
59
Mike Reed6dbeac52021-01-13 13:14:23 -050060static sk_sp<SkImage> make_argb8888_stripes() {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000061 SkBitmap bitmap;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000062 init_bitmap(kN32_SkColorType, &bitmap);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000063 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 Reed6dbeac52021-01-13 13:14:23 -050076 return bitmap.asImage();
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000077}
78
Mike Reed6dbeac52021-01-13 13:14:23 -050079static sk_sp<SkImage> make_argb4444_stripes() {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000080 SkBitmap bitmap;
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000081 init_bitmap(kARGB_4444_SkColorType, &bitmap);
Brian Osmanbdd0f512018-11-02 16:34:01 -040082 // Using draw rather than readPixels to suppress dither
83 SkPaint paint;
84 paint.setBlendMode(SkBlendMode::kSrc);
Mike Reede02d7f82021-01-21 22:25:21 -050085 SkCanvas{ bitmap }.drawImage(make_argb8888_stripes(), 0, 0, SkSamplingOptions(), &paint);
Mike Reed6dbeac52021-01-13 13:14:23 -050086 return bitmap.asImage();
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000087}
88
89namespace skiagm {
90
91class BitmapPremulGM : public GM {
92public:
93 BitmapPremulGM() {
94 this->setBGColor(SK_ColorWHITE);
95 }
96
97protected:
mtklein36352bf2015-03-25 18:17:31 -070098 SkString onShortName() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000099 return SkString("bitmap_premul");
100 }
101
mtklein36352bf2015-03-25 18:17:31 -0700102 SkISize onISize() override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +0000103 return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
104 }
105
mtklein36352bf2015-03-25 18:17:31 -0700106 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.orgece95c32013-10-24 15:20:00 +0000107 SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
Mike Reed6dbeac52021-01-13 13:14:23 -0500108 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.orgece95c32013-10-24 15:20:00 +0000112 }
113
114private:
John Stiles7571f9e2020-09-02 22:42:33 -0400115 using INHERITED = GM;
vandebo@chromium.orgece95c32013-10-24 15:20:00 +0000116};
117
118DEF_GM( return new BitmapPremulGM; )
John Stilesa6841be2020-08-06 14:11:56 -0400119} // namespace skiagm