blob: 60aec88ddf3604bf7b250fa485837f0f3e960c0e [file] [log] [blame]
halcanarydb0dcc72015-03-20 12:31:52 -07001/*
2 * Copyright 2015 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
halcanarydb0dcc72015-03-20 12:31:52 -07008#include "SkSurface.h"
9#include "Resources.h"
10#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040011#include "sk_tool_utils.h"
halcanarydb0dcc72015-03-20 12:31:52 -070012
13#include "SkMath.h"
14#include "SkColorPriv.h"
15
16static SkBitmap copy_bitmap(const SkBitmap& src, SkColorType colorType) {
17 SkBitmap copy;
18 src.copyTo(&copy, colorType);
19 copy.setImmutable();
20 return copy;
21}
22
23#define SCALE 128
24
25// Make either A8 or gray8 bitmap.
26static SkBitmap make_bitmap(SkColorType ct) {
27 SkBitmap bm;
28 switch (ct) {
29 case kAlpha_8_SkColorType:
30 bm.allocPixels(SkImageInfo::MakeA8(SCALE, SCALE));
31 break;
32 case kGray_8_SkColorType:
33 bm.allocPixels(
34 SkImageInfo::Make(SCALE, SCALE, ct, kOpaque_SkAlphaType));
35 break;
36 default:
37 SkASSERT(false);
38 return bm;
39 }
40 SkAutoLockPixels autoLockPixels(bm);
41 uint8_t spectrum[256];
42 for (int y = 0; y < 256; ++y) {
43 spectrum[y] = y;
44 }
45 for (int y = 0; y < 128; ++y) {
46 // Shift over one byte each scanline.
47 memcpy(bm.getAddr8(0, y), &spectrum[y], 128);
48 }
49 bm.setImmutable();
50 return bm;
51}
52
53static void draw_center_letter(char c,
54 SkPaint* p,
55 SkColor color,
56 SkScalar x,
57 SkScalar y,
58 SkCanvas* canvas) {
59 SkRect bounds;
60 p->setColor(color);
61 p->measureText(&c, 1, &bounds);
62 canvas->drawText(&c, 1, x - bounds.centerX(), y - bounds.centerY(), *p);
63}
64
65static void color_wheel_native(SkCanvas* canvas) {
66 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
67 canvas->translate(0.5f * SCALE, 0.5f * SCALE);
68 SkPaint p;
69 p.setAntiAlias(false);
70 p.setColor(SK_ColorWHITE);
71 canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, p);
72
73 const double sqrt_3_over_2 = 0.8660254037844387;
74 const SkScalar Z = 0.0f;
75 const SkScalar D = 0.3f * SkIntToScalar(SCALE);
76 const SkScalar X = SkDoubleToScalar(D * sqrt_3_over_2);
77 const SkScalar Y = D * SK_ScalarHalf;
mbocee6a9912016-05-31 11:42:36 -070078 sk_tool_utils::set_portable_typeface(&p, nullptr, SkFontStyle::FromOldStyle(SkTypeface::kBold));
halcanarydb0dcc72015-03-20 12:31:52 -070079 p.setTextSize(0.28125f * SCALE);
80 draw_center_letter('K', &p, SK_ColorBLACK, Z, Z, canvas);
81 draw_center_letter('R', &p, SK_ColorRED, Z, D, canvas);
82 draw_center_letter('G', &p, SK_ColorGREEN, -X, -Y, canvas);
83 draw_center_letter('B', &p, SK_ColorBLUE, X, -Y, canvas);
84 draw_center_letter('C', &p, SK_ColorCYAN, Z, -D, canvas);
85 draw_center_letter('M', &p, SK_ColorMAGENTA, X, Y, canvas);
86 draw_center_letter('Y', &p, SK_ColorYELLOW, -X, Y, canvas);
87}
88
89template <typename T>
90int find(T* array, int N, T item) {
91 for (int i = 0; i < N; ++i) {
92 if (array[i] == item) {
93 return i;
94 }
95 }
96 return -1;
97}
98
99static SkPMColor premultiply_color(SkColor c) {
100 return SkPremultiplyARGBInline(SkColorGetA(c), SkColorGetR(c),
101 SkColorGetG(c), SkColorGetB(c));
102}
103
104static SkBitmap indexed_bitmap() {
105 SkBitmap n32bitmap;
106 n32bitmap.allocN32Pixels(SCALE, SCALE);
107 n32bitmap.eraseColor(SK_ColorTRANSPARENT);
108
109 SkCanvas canvas(n32bitmap);
110 color_wheel_native(&canvas);
111 const SkColor colors[] = {
112 SK_ColorTRANSPARENT,
113 SK_ColorWHITE,
114 SK_ColorBLACK,
115 SK_ColorRED,
116 SK_ColorGREEN,
117 SK_ColorBLUE,
118 SK_ColorCYAN,
119 SK_ColorMAGENTA,
120 SK_ColorYELLOW,
121 };
122 SkPMColor pmColors[SK_ARRAY_COUNT(colors)];
123 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
124 pmColors[i] = premultiply_color(colors[i]);
125 }
126 SkBitmap bm;
halcanarydb0dcc72015-03-20 12:31:52 -0700127 SkImageInfo info = SkImageInfo::Make(SCALE, SCALE, kIndex_8_SkColorType,
128 kPremul_SkAlphaType);
Mike Reed6b3155c2017-04-03 14:41:44 -0400129 bm.allocPixels(info, SkColorTable::Make(pmColors, SK_ARRAY_COUNT(pmColors)));
halcanarydb0dcc72015-03-20 12:31:52 -0700130 SkAutoLockPixels autoLockPixels1(n32bitmap);
131 SkAutoLockPixels autoLockPixels2(bm);
132 for (int y = 0; y < SCALE; ++y) {
133 for (int x = 0; x < SCALE; ++x) {
134 SkPMColor c = *n32bitmap.getAddr32(x, y);
135 int idx = find(pmColors, SK_ARRAY_COUNT(pmColors), c);
136 *bm.getAddr8(x, y) = SkClampMax(idx, SK_ARRAY_COUNT(pmColors) - 1);
137 }
138 }
139 return bm;
140}
141
142static void draw(SkCanvas* canvas,
143 const SkPaint& p,
144 const SkBitmap& src,
145 SkColorType colorType,
146 const char text[]) {
147 SkASSERT(src.colorType() == colorType);
148 canvas->drawBitmap(src, 0.0f, 0.0f);
149 canvas->drawText(text, strlen(text), 0.0f, 12.0f, p);
150}
151
152DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) {
153 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
154 SkPaint p;
155 p.setColor(SK_ColorBLACK);
156 p.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -0700157 sk_tool_utils::set_portable_typeface(&p, nullptr);
halcanarydb0dcc72015-03-20 12:31:52 -0700158
159 sk_tool_utils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
160
161 SkBitmap bitmap;
162 if (GetResourceAsBitmap("color_wheel.png", &bitmap)) {
163 bitmap.setImmutable();
164 draw(canvas, p, bitmap, kN32_SkColorType, "Native 32");
165
166 canvas->translate(0.0f, SkIntToScalar(SCALE));
167 SkBitmap copy565 = copy_bitmap(bitmap, kRGB_565_SkColorType);
168 p.setColor(SK_ColorRED);
169 draw(canvas, p, copy565, kRGB_565_SkColorType, "RGB 565");
170 p.setColor(SK_ColorBLACK);
171
172 canvas->translate(0.0f, SkIntToScalar(SCALE));
173 SkBitmap copy4444 = copy_bitmap(bitmap, kARGB_4444_SkColorType);
174 draw(canvas, p, copy4444, kARGB_4444_SkColorType, "ARGB 4444");
175 } else {
176 canvas->translate(0.0f, SkIntToScalar(2 * SCALE));
177 }
178
179 canvas->translate(0.0f, SkIntToScalar(SCALE));
180 SkBitmap bitmapIndexed = indexed_bitmap();
181 draw(canvas, p, bitmapIndexed, kIndex_8_SkColorType, "Index 8");
182
183 canvas->translate(0.0f, SkIntToScalar(SCALE));
184 SkBitmap bitmapA8 = make_bitmap(kAlpha_8_SkColorType);
185 draw(canvas, p, bitmapA8, kAlpha_8_SkColorType, "Alpha 8");
186
187 p.setColor(SK_ColorRED);
188 canvas->translate(0.0f, SkIntToScalar(SCALE));
189 SkBitmap bitmapG8 = make_bitmap(kGray_8_SkColorType);
190 draw(canvas, p, bitmapG8, kGray_8_SkColorType, "Gray 8");
191}
halcanaryaa4ba902015-11-06 07:27:23 -0800192
reed9ce9d672016-03-17 10:51:11 -0700193sk_sp<SkImage> make_not_native32_color_wheel() {
halcanaryaa4ba902015-11-06 07:27:23 -0800194 SkBitmap n32bitmap, notN32bitmap;
195 n32bitmap.allocN32Pixels(SCALE, SCALE);
196 n32bitmap.eraseColor(SK_ColorTRANSPARENT);
197 SkCanvas n32canvas(n32bitmap);
198 color_wheel_native(&n32canvas);
199 n32canvas.flush();
200 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
201 const SkColorType ct = kRGBA_8888_SkColorType;
202 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
203 const SkColorType ct = kBGRA_8888_SkColorType;
204 #endif
205 static_assert(ct != kN32_SkColorType, "BRGA!=RGBA");
206 SkAssertResult(n32bitmap.copyTo(&notN32bitmap, ct));
207 SkASSERT(notN32bitmap.colorType() == ct);
reed9ce9d672016-03-17 10:51:11 -0700208 return SkImage::MakeFromBitmap(notN32bitmap);
halcanaryaa4ba902015-11-06 07:27:23 -0800209}
210
211DEF_SIMPLE_GM(not_native32_bitmap_config, canvas, SCALE, SCALE) {
reed9ce9d672016-03-17 10:51:11 -0700212 sk_sp<SkImage> notN32image(make_not_native32_color_wheel());
halcanaryaa4ba902015-11-06 07:27:23 -0800213 SkASSERT(notN32image);
214 sk_tool_utils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
reed9ce9d672016-03-17 10:51:11 -0700215 canvas->drawImage(notN32image.get(), 0.0f, 0.0f);
halcanaryaa4ba902015-11-06 07:27:23 -0800216}
herb58238462016-06-03 09:44:53 -0700217
218static uint32_t make_pixel(int x, int y, SkAlphaType alphaType) {
219 SkASSERT(x >= 0 && x < SCALE);
220 SkASSERT(y >= 0 && y < SCALE);
221
222 SkScalar R = SCALE / 2.0f;
223
224 uint32_t alpha = 0x00;
225
226 if ((x - R) * (x - R) + (y - R) * (y - R) < R * R) {
227 alpha = 0xFF;
228 }
229
230 uint32_t component;
231 switch (alphaType) {
232 case kPremul_SkAlphaType:
233 component = alpha;
234 break;
235 case kUnpremul_SkAlphaType:
236 component = 0xFF;
237 break;
238 default:
239 SkFAIL("Should not get here - invalid alpha type");
240 return 0xFF000000;
241 }
242 return alpha << 24 | component;
243}
244
245static void make_color_test_bitmap_variant(
246 SkColorType colorType,
247 SkAlphaType alphaType,
brianosman52ede1d2016-06-20 08:25:02 -0700248 sk_sp<SkColorSpace> colorSpace,
herb58238462016-06-03 09:44:53 -0700249 SkBitmap* bm)
250{
251 SkASSERT(colorType == kRGBA_8888_SkColorType || colorType == kBGRA_8888_SkColorType);
252 SkASSERT(alphaType == kPremul_SkAlphaType || alphaType == kUnpremul_SkAlphaType);
253 bm->allocPixels(
brianosman52ede1d2016-06-20 08:25:02 -0700254 SkImageInfo::Make(SCALE, SCALE, colorType, alphaType, colorSpace));
herb58238462016-06-03 09:44:53 -0700255 SkPixmap pm;
256 bm->peekPixels(&pm);
257 for (int y = 0; y < bm->height(); y++) {
258 for (int x = 0; x < bm->width(); x++) {
259 *pm.writable_addr32(x, y) = make_pixel(x, y, alphaType);
260 }
261 }
262}
263
Joe Gregorioc4859072017-01-20 14:21:27 +0000264DEF_SIMPLE_GM(all_variants_8888, canvas, 4 * SCALE + 30, 2 * SCALE + 10) {
herb58238462016-06-03 09:44:53 -0700265 sk_tool_utils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
266
brianosman52ede1d2016-06-20 08:25:02 -0700267 sk_sp<SkColorSpace> colorSpaces[] {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500268 SkColorSpace::MakeSRGB(),
brianosman52ede1d2016-06-20 08:25:02 -0700269 nullptr,
brianosman52ede1d2016-06-20 08:25:02 -0700270 };
271 for (auto colorSpace : colorSpaces) {
herb58238462016-06-03 09:44:53 -0700272 canvas->save();
273 for (auto alphaType : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) {
274 canvas->save();
275 for (auto colorType : {kRGBA_8888_SkColorType, kBGRA_8888_SkColorType}) {
276 SkBitmap bm;
brianosman52ede1d2016-06-20 08:25:02 -0700277 make_color_test_bitmap_variant(colorType, alphaType, colorSpace, &bm);
herb58238462016-06-03 09:44:53 -0700278 canvas->drawBitmap(bm, 0.0f, 0.0f);
279 canvas->translate(SCALE + 10, 0.0f);
280 }
281 canvas->restore();
Joe Gregorioc4859072017-01-20 14:21:27 +0000282 canvas->translate(0.0f, SCALE + 10);
herb58238462016-06-03 09:44:53 -0700283 }
284 canvas->restore();
285 canvas->translate(2 * (SCALE + 10), 0.0f);
286 }
287}