• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
20 
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 
29 constexpr int SLIDE_SIZE = 256;
30 
init_bitmap(SkColorType ct,SkBitmap * bitmap)31 static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
32     bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
33                                           kPremul_SkAlphaType));
34     bitmap->eraseColor(SK_ColorWHITE);
35 }
36 
make_argb8888_gradient()37 static SkBitmap make_argb8888_gradient() {
38     SkBitmap bitmap;
39     init_bitmap(kN32_SkColorType, &bitmap);
40     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++) {
43             dst[x] = SkPackARGB32(y, y, y, y);
44         }
45     }
46     return bitmap;
47 }
48 
make_argb4444_gradient()49 static SkBitmap make_argb4444_gradient() {
50     SkBitmap bitmap;
51     init_bitmap(kARGB_4444_SkColorType, &bitmap);
52     // 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);
56     return bitmap;
57 }
58 
make_argb8888_stripes()59 static SkBitmap make_argb8888_stripes() {
60     SkBitmap bitmap;
61     init_bitmap(kN32_SkColorType, &bitmap);
62     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 
make_argb4444_stripes()78 static SkBitmap make_argb4444_stripes() {
79     SkBitmap bitmap;
80     init_bitmap(kARGB_4444_SkColorType, &bitmap);
81     // 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);
85     return bitmap;
86 }
87 
88 namespace skiagm {
89 
90 class BitmapPremulGM : public GM {
91 public:
BitmapPremulGM()92     BitmapPremulGM() {
93         this->setBGColor(SK_ColorWHITE);
94     }
95 
96 protected:
onShortName()97     SkString onShortName() override {
98         return SkString("bitmap_premul");
99     }
100 
onISize()101     SkISize onISize() override {
102         return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
103     }
104 
onDraw(SkCanvas * canvas)105     void onDraw(SkCanvas* canvas) override {
106         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 
113 private:
114     typedef GM INHERITED;
115 };
116 
117 DEF_GM( return new BitmapPremulGM; )
118 }
119