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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkImage.h"
15 #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"
21
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
30 constexpr int SLIDE_SIZE = 256;
31
init_bitmap(SkColorType ct,SkBitmap * bitmap)32 static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
33 bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
34 kPremul_SkAlphaType));
35 bitmap->eraseColor(SK_ColorWHITE);
36 }
37
make_argb8888_gradient()38 static sk_sp<SkImage> make_argb8888_gradient() {
39 SkBitmap bitmap;
40 init_bitmap(kN32_SkColorType, &bitmap);
41 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++) {
44 dst[x] = SkPackARGB32(y, y, y, y);
45 }
46 }
47 return bitmap.asImage();
48 }
49
make_argb4444_gradient()50 static sk_sp<SkImage> make_argb4444_gradient() {
51 SkBitmap bitmap;
52 init_bitmap(kARGB_4444_SkColorType, &bitmap);
53 // Using draw rather than readPixels to suppress dither
54 SkPaint paint;
55 paint.setBlendMode(SkBlendMode::kSrc);
56 SkCanvas{ bitmap }.drawImage(make_argb8888_gradient(), 0, 0, SkSamplingOptions(), &paint);
57 return bitmap.asImage();
58 }
59
make_argb8888_stripes()60 static sk_sp<SkImage> make_argb8888_stripes() {
61 SkBitmap bitmap;
62 init_bitmap(kN32_SkColorType, &bitmap);
63 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 }
76 return bitmap.asImage();
77 }
78
make_argb4444_stripes()79 static sk_sp<SkImage> make_argb4444_stripes() {
80 SkBitmap bitmap;
81 init_bitmap(kARGB_4444_SkColorType, &bitmap);
82 // Using draw rather than readPixels to suppress dither
83 SkPaint paint;
84 paint.setBlendMode(SkBlendMode::kSrc);
85 SkCanvas{ bitmap }.drawImage(make_argb8888_stripes(), 0, 0, SkSamplingOptions(), &paint);
86 return bitmap.asImage();
87 }
88
89 namespace skiagm {
90
91 class BitmapPremulGM : public GM {
92 public:
BitmapPremulGM()93 BitmapPremulGM() {
94 this->setBGColor(SK_ColorWHITE);
95 }
96
97 protected:
getName() const98 SkString getName() const override { return SkString("bitmap_premul"); }
99
getISize()100 SkISize getISize() override { return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2); }
101
onDraw(SkCanvas * canvas)102 void onDraw(SkCanvas* canvas) override {
103 SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
104 canvas->drawImage(make_argb8888_gradient(), 0, 0);
105 canvas->drawImage(make_argb4444_gradient(), slideSize, 0);
106 canvas->drawImage(make_argb8888_stripes(), 0, slideSize);
107 canvas->drawImage(make_argb4444_stripes(), slideSize, slideSize);
108 }
109
110 private:
111 using INHERITED = GM;
112 };
113
114 DEF_GM( return new BitmapPremulGM; )
115 } // namespace skiagm
116
117 static constexpr int kBoxSize = 31;
118 static constexpr int kPadding = 5;
119
make_out_of_gamut_image(SkColorType ct)120 static sk_sp<SkImage> make_out_of_gamut_image(SkColorType ct) {
121 SkBitmap bmp;
122 // Odd dimensions so that we hit the different implementation in the SIMD tail handling
123 bmp.allocPixels(SkImageInfo::Make(kBoxSize, kBoxSize, ct, kPremul_SkAlphaType));
124 for (int y = 0; y < kBoxSize; ++y) {
125 for (int x = 0; x < kBoxSize; ++x) {
126 *bmp.getAddr32(x, y) = (0x40000000 | ((x * 8) << 8) | ((y * 8) << 0));
127 }
128 }
129 return bmp.asImage();
130 }
131
132 DEF_SIMPLE_GM(image_out_of_gamut, canvas, 2 * kBoxSize + 3 * kPadding, kBoxSize + 2 * kPadding) {
133 // This GM draws an image with out-of-gamut colors (RGB > A). Historically, Skia assumed this
134 // was impossible, and contained numerous asserts and optimizations that would break if the
135 // rule were violated. With color spaces and/or SkSL shaders (among other things), it's no
136 // longer reasonable to make this claim. To catch issues with legacy blitters, this draws both
137 // RGBA and BGRA. (This ensures that we always hit the N32 -> N32 case).
138 canvas->clear(SK_ColorGRAY);
139
140 auto rgba = make_out_of_gamut_image(kRGBA_8888_SkColorType),
141 bgra = make_out_of_gamut_image(kBGRA_8888_SkColorType);
142
143 canvas->translate(kPadding, kPadding);
144 canvas->drawImage(rgba, 0, 0);
145 canvas->translate(kBoxSize + kPadding, 0);
146 canvas->drawImage(bgra, 0, 0);
147 }
148