• 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.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkColorData.h"
12 
13 /**
14  * This GM checks that bitmap pixels are unpremultiplied before being exported
15  * to other formats. If unpremultiplication is implemented properly, this
16  * GM should come out completely white. If not, this GM looks like a row of two
17  * greyscale gradients above a row of grey lines.
18  * This tests both the ARGB4444 and ARGB8888 bitmap configurations.
19  */
20 
21 constexpr int SLIDE_SIZE = 256;
22 
init_bitmap(SkColorType ct,SkBitmap * bitmap)23 static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
24     bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
25                                           kPremul_SkAlphaType));
26     bitmap->eraseColor(SK_ColorWHITE);
27 }
28 
make_argb8888_gradient()29 static SkBitmap make_argb8888_gradient() {
30     SkBitmap bitmap;
31     init_bitmap(kN32_SkColorType, &bitmap);
32     for (int y = 0; y < SLIDE_SIZE; y++) {
33         uint32_t* dst = bitmap.getAddr32(0, y);
34         for (int x = 0; x < SLIDE_SIZE; x++) {
35             dst[x] = SkPackARGB32(y, y, y, y);
36         }
37     }
38     return bitmap;
39 }
40 
make_argb4444_gradient()41 static SkBitmap make_argb4444_gradient() {
42     SkBitmap bitmap;
43     init_bitmap(kARGB_4444_SkColorType, &bitmap);
44     // Using draw rather than readPixels to suppress dither
45     SkPaint paint;
46     paint.setBlendMode(SkBlendMode::kSrc);
47     SkCanvas{ bitmap }.drawBitmap(make_argb8888_gradient(), 0, 0, &paint);
48     return bitmap;
49 }
50 
make_argb8888_stripes()51 static SkBitmap make_argb8888_stripes() {
52     SkBitmap bitmap;
53     init_bitmap(kN32_SkColorType, &bitmap);
54     uint8_t rowColor = 0;
55     for (int y = 0; y < SLIDE_SIZE; y++) {
56         uint32_t* dst = bitmap.getAddr32(0, y);
57         for (int x = 0; x < SLIDE_SIZE; x++) {
58             dst[x] = SkPackARGB32(rowColor, rowColor,
59                                   rowColor, rowColor);
60         }
61         if (rowColor == 0) {
62             rowColor = 255;
63         } else {
64             rowColor = 0;
65         }
66     }
67     return bitmap;
68 }
69 
make_argb4444_stripes()70 static SkBitmap make_argb4444_stripes() {
71     SkBitmap bitmap;
72     init_bitmap(kARGB_4444_SkColorType, &bitmap);
73     // Using draw rather than readPixels to suppress dither
74     SkPaint paint;
75     paint.setBlendMode(SkBlendMode::kSrc);
76     SkCanvas{ bitmap }.drawBitmap(make_argb8888_stripes(), 0, 0, &paint);
77     return bitmap;
78 }
79 
80 namespace skiagm {
81 
82 class BitmapPremulGM : public GM {
83 public:
BitmapPremulGM()84     BitmapPremulGM() {
85         this->setBGColor(SK_ColorWHITE);
86     }
87 
88 protected:
onShortName()89     SkString onShortName() override {
90         return SkString("bitmap_premul");
91     }
92 
onISize()93     SkISize onISize() override {
94         return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
95     }
96 
onDraw(SkCanvas * canvas)97     void onDraw(SkCanvas* canvas) override {
98         SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
99         canvas->drawBitmap(make_argb8888_gradient(), 0, 0);
100         canvas->drawBitmap(make_argb4444_gradient(), slideSize, 0);
101         canvas->drawBitmap(make_argb8888_stripes(), 0, slideSize);
102         canvas->drawBitmap(make_argb4444_stripes(), slideSize, slideSize);
103     }
104 
105 private:
106     typedef GM INHERITED;
107 };
108 
109 DEF_GM( return new BitmapPremulGM; )
110 }
111