blob: 57d22d700a7c83b2415cca2c858ac83732b82ede [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
8#include "gm.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.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
21static const int SLIDE_SIZE = 256;
22static const int PIXEL_SIZE_8888 = SLIDE_SIZE / 256;
23static const int PIXEL_SIZE_4444 = SLIDE_SIZE / 16;
24
25static void init_bitmap(SkBitmap::Config config, SkBitmap* bitmap) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000026 bitmap->allocConfigPixels(config, SLIDE_SIZE, SLIDE_SIZE);
vandebo@chromium.orgece95c32013-10-24 15:20:00 +000027 bitmap->eraseColor(SK_ColorWHITE);
28}
29
30static SkBitmap make_argb8888_gradient() {
31 SkBitmap bitmap;
32 init_bitmap(SkBitmap::kARGB_8888_Config, &bitmap);
33 uint8_t rowColor = 0;
34 for (int y = 0; y < SLIDE_SIZE; y++) {
35 uint32_t* dst = bitmap.getAddr32(0, y);
36 for (int x = 0; x < SLIDE_SIZE; x++) {
37 dst[x] = SkPackARGB32(rowColor, rowColor,
38 rowColor, rowColor);
39 }
40 if (y % PIXEL_SIZE_8888 == PIXEL_SIZE_8888 - 1) {
41 rowColor++;
42 }
43 }
44 return bitmap;
45}
46
47static SkBitmap make_argb4444_gradient() {
48 SkBitmap bitmap;
49 init_bitmap(SkBitmap::kARGB_4444_Config, &bitmap);
50 uint8_t rowColor = 0;
51 for (int y = 0; y < SLIDE_SIZE; y++) {
52 uint16_t* dst = bitmap.getAddr16(0, y);
53 for (int x = 0; x < SLIDE_SIZE; x++) {
54 dst[x] = SkPackARGB4444(rowColor, rowColor,
55 rowColor, rowColor);
56 }
57 if (y % PIXEL_SIZE_4444 == PIXEL_SIZE_4444 - 1) {
58 rowColor++;
59 }
60 }
61 return bitmap;
62}
63
64static SkBitmap make_argb8888_stripes() {
65 SkBitmap bitmap;
66 init_bitmap(SkBitmap::kARGB_8888_Config, &bitmap);
67 uint8_t rowColor = 0;
68 for (int y = 0; y < SLIDE_SIZE; y++) {
69 uint32_t* dst = bitmap.getAddr32(0, y);
70 for (int x = 0; x < SLIDE_SIZE; x++) {
71 dst[x] = SkPackARGB32(rowColor, rowColor,
72 rowColor, rowColor);
73 }
74 if (rowColor == 0) {
75 rowColor = 255;
76 } else {
77 rowColor = 0;
78 }
79 }
80 return bitmap;
81}
82
83static SkBitmap make_argb4444_stripes() {
84 SkBitmap bitmap;
85 init_bitmap(SkBitmap::kARGB_4444_Config, &bitmap);
86 uint8_t rowColor = 0;;
87 for (int y = 0; y < SLIDE_SIZE; y++) {
88 uint16_t* dst = bitmap.getAddr16(0, y);
89 for (int x = 0; x < SLIDE_SIZE; x++) {
90 dst[x] = SkPackARGB4444(rowColor, rowColor,
91 rowColor, rowColor);
92 }
93 if (rowColor == 0) {
94 rowColor = 15;
95 } else {
96 rowColor = 0;
97 }
98 }
99 return bitmap;
100}
101
102namespace skiagm {
103
104class BitmapPremulGM : public GM {
105public:
106 BitmapPremulGM() {
107 this->setBGColor(SK_ColorWHITE);
108 }
109
110protected:
111 SkString onShortName() SK_OVERRIDE {
112 return SkString("bitmap_premul");
113 }
114
115 virtual SkISize onISize() SK_OVERRIDE {
116 return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
117 }
118
119 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
120 SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
121 canvas->drawBitmap(make_argb8888_gradient(), 0, 0);
122 canvas->drawBitmap(make_argb4444_gradient(), slideSize, 0);
123 canvas->drawBitmap(make_argb8888_stripes(), 0, slideSize);
124 canvas->drawBitmap(make_argb4444_stripes(), slideSize, slideSize);
125 }
126
127private:
128 typedef GM INHERITED;
129};
130
131DEF_GM( return new BitmapPremulGM; )
132}