Flutter Engine
The Flutter Engine
bitmappremul.cpp
Go to the documentation of this file.
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"
18#include "include/core/SkSize.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
30constexpr int SLIDE_SIZE = 256;
31
35 bitmap->eraseColor(SK_ColorWHITE);
36}
37
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
53 // Using draw rather than readPixels to suppress dither
55 paint.setBlendMode(SkBlendMode::kSrc);
57 return bitmap.asImage();
58}
59
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
82 // Using draw rather than readPixels to suppress dither
84 paint.setBlendMode(SkBlendMode::kSrc);
86 return bitmap.asImage();
87}
88
89namespace skiagm {
90
91class BitmapPremulGM : public GM {
92public:
95 }
96
97protected:
98 SkString getName() const override { return SkString("bitmap_premul"); }
99
100 SkISize getISize() override { return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2); }
101
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
110private:
111 using INHERITED = GM;
112};
113
114DEF_GM( return new BitmapPremulGM; )
115} // namespace skiagm
116
117static constexpr int kBoxSize = 31;
118static constexpr int kPadding = 5;
119
121 SkBitmap bmp;
122 // Odd dimensions so that we hit the different implementation in the SIMD tail handling
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
132DEF_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
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}
static const uint32_t bgra[kNumPixels]
static const uint32_t rgba[kNumPixels]
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
static SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColorPriv.h:106
SkColorType
Definition: SkColorType.h:19
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static sk_sp< SkImage > make_argb4444_stripes()
static constexpr int kPadding
static sk_sp< SkImage > make_argb4444_gradient()
static sk_sp< SkImage > make_out_of_gamut_image(SkColorType ct)
static sk_sp< SkImage > make_argb8888_gradient()
DEF_SIMPLE_GM(image_out_of_gamut, canvas, 2 *kBoxSize+3 *kPadding, kBoxSize+2 *kPadding)
static sk_sp< SkImage > make_argb8888_stripes()
constexpr int SLIDE_SIZE
static constexpr int kBoxSize
static void init_bitmap(SkColorType ct, SkBitmap *bitmap)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
SkString getName() const override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
double y
double x
Definition: bitmap.py:1
dst
Definition: cp.py:12
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)