1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontStyle.h"
15 #include "include/core/SkFontTypes.h"
16 #include "include/core/SkImage.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkPaint.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkTypeface.h"
24 #include "include/core/SkTypes.h"
25 #include "tools/Resources.h"
26 #include "tools/ToolUtils.h"
27 
28 #include <string.h>
29 #include <initializer_list>
30 
copy_bitmap(const SkBitmap & src,SkColorType colorType)31 static SkBitmap copy_bitmap(const SkBitmap& src, SkColorType colorType) {
32     const SkBitmap* srcPtr = &src;
33     SkBitmap tmp(src);
34     if (kRGB_565_SkColorType == colorType) {
35         tmp.setAlphaType(kOpaque_SkAlphaType);
36         srcPtr = &tmp;
37     }
38 
39     SkBitmap copy;
40     ToolUtils::copy_to(&copy, colorType, *srcPtr);
41     copy.setImmutable();
42     return copy;
43 }
44 
45 #define SCALE 128
46 
47 // Make either A8 or gray8 bitmap.
make_bitmap(SkColorType ct)48 static SkBitmap make_bitmap(SkColorType ct) {
49     SkBitmap bm;
50     switch (ct) {
51         case kAlpha_8_SkColorType:
52             bm.allocPixels(SkImageInfo::MakeA8(SCALE, SCALE));
53             break;
54         case kGray_8_SkColorType:
55             bm.allocPixels(
56                     SkImageInfo::Make(SCALE, SCALE, ct, kOpaque_SkAlphaType));
57             break;
58         default:
59             SkASSERT(false);
60             return bm;
61     }
62     uint8_t spectrum[256];
63     for (int y = 0; y < 256; ++y) {
64         spectrum[y] = y;
65     }
66     for (int y = 0; y < 128; ++y) {
67         // Shift over one byte each scanline.
68         memcpy(bm.getAddr8(0, y), &spectrum[y], 128);
69     }
70     bm.setImmutable();
71     return bm;
72 }
73 
draw_center_letter(char c,const SkFont & font,SkColor color,SkScalar x,SkScalar y,SkCanvas * canvas)74 static void draw_center_letter(char c, const SkFont& font, SkColor color,
75                                SkScalar x, SkScalar y, SkCanvas* canvas) {
76     SkRect bounds;
77     font.measureText(&c, 1, SkTextEncoding::kUTF8, &bounds);
78     canvas->drawSimpleText(&c, 1, SkTextEncoding::kUTF8,
79                            x - bounds.centerX(), y - bounds.centerY(),
80                            font, SkPaint(SkColor4f::FromColor(color)));
81 }
82 
color_wheel_native(SkCanvas * canvas)83 static void color_wheel_native(SkCanvas* canvas) {
84     SkAutoCanvasRestore autoCanvasRestore(canvas, true);
85     canvas->translate(0.5f * SCALE, 0.5f * SCALE);
86     canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, SkPaint(SkColors::kWhite));
87 
88     const double sqrt_3_over_2 = 0.8660254037844387;
89     const SkScalar Z = 0.0f;
90     const SkScalar D = 0.3f * SkIntToScalar(SCALE);
91     const SkScalar X = SkDoubleToScalar(D * sqrt_3_over_2);
92     const SkScalar Y = D * SK_ScalarHalf;
93 
94     SkFont font;
95     font.setEdging(SkFont::Edging::kAlias);
96     font.setTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold()));
97     font.setSize(0.28125f * SCALE);
98     draw_center_letter('K', font, SK_ColorBLACK, Z, Z, canvas);
99     draw_center_letter('R', font, SK_ColorRED, Z, D, canvas);
100     draw_center_letter('G', font, SK_ColorGREEN, -X, -Y, canvas);
101     draw_center_letter('B', font, SK_ColorBLUE, X, -Y, canvas);
102     draw_center_letter('C', font, SK_ColorCYAN, Z, -D, canvas);
103     draw_center_letter('M', font, SK_ColorMAGENTA, X, Y, canvas);
104     draw_center_letter('Y', font, SK_ColorYELLOW, -X, Y, canvas);
105 }
106 
107 template <typename T>
find(T * array,int N,T item)108 int find(T* array, int N, T item) {
109     for (int i = 0; i < N; ++i) {
110         if (array[i] == item) {
111             return i;
112         }
113     }
114     return -1;
115 }
116 
draw(SkCanvas * canvas,const SkPaint & p,const SkFont & font,const SkBitmap & src,SkColorType colorType,const char text[])117 static void draw(SkCanvas* canvas,
118                  const SkPaint& p,
119                  const SkFont& font,
120                  const SkBitmap& src,
121                  SkColorType colorType,
122                  const char text[]) {
123     SkASSERT(src.colorType() == colorType);
124     canvas->drawBitmap(src, 0.0f, 0.0f);
125     canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0.0f, 12.0f, font, p);
126 }
127 
128 DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) {
129     SkAutoCanvasRestore autoCanvasRestore(canvas, true);
130     SkPaint p(SkColors::kBlack);
131     p.setAntiAlias(true);
132 
133     SkFont font(ToolUtils::create_portable_typeface());
134 
135     ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
136 
137     SkBitmap bitmap;
138     if (GetResourceAsBitmap("images/color_wheel.png", &bitmap)) {
139         bitmap.setImmutable();
140         draw(canvas, p, font, bitmap, kN32_SkColorType, "Native 32");
141 
142         canvas->translate(0.0f, SkIntToScalar(SCALE));
143         SkBitmap copy565 = copy_bitmap(bitmap, kRGB_565_SkColorType);
144         p.setColor(SK_ColorRED);
145         draw(canvas, p, font, copy565, kRGB_565_SkColorType, "RGB 565");
146         p.setColor(SK_ColorBLACK);
147 
148         canvas->translate(0.0f, SkIntToScalar(SCALE));
149         SkBitmap copy4444 = copy_bitmap(bitmap, kARGB_4444_SkColorType);
150         draw(canvas, p, font, copy4444, kARGB_4444_SkColorType, "ARGB 4444");
151 
152         canvas->translate(0.0f, SkIntToScalar(SCALE));
153         SkBitmap copyF16 = copy_bitmap(bitmap, kRGBA_F16_SkColorType);
154         draw(canvas, p, font, copyF16, kRGBA_F16_SkColorType, "RGBA F16");
155 
156     } else {
157         canvas->translate(0.0f, SkIntToScalar(3 * SCALE));
158     }
159 
160     canvas->translate(0.0f, SkIntToScalar(SCALE));
161     SkBitmap bitmapA8 = make_bitmap(kAlpha_8_SkColorType);
162     draw(canvas, p, font, bitmapA8, kAlpha_8_SkColorType, "Alpha 8");
163 
164     p.setColor(SK_ColorRED);
165     canvas->translate(0.0f, SkIntToScalar(SCALE));
166     SkBitmap bitmapG8 = make_bitmap(kGray_8_SkColorType);
167     draw(canvas, p, font, bitmapG8, kGray_8_SkColorType, "Gray 8");
168 }
169 
make_not_native32_color_wheel()170 sk_sp<SkImage> make_not_native32_color_wheel() {
171     SkBitmap n32bitmap, notN32bitmap;
172     n32bitmap.allocN32Pixels(SCALE, SCALE);
173     n32bitmap.eraseColor(SK_ColorTRANSPARENT);
174     SkCanvas n32canvas(n32bitmap);
175     color_wheel_native(&n32canvas);
176     #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
177         const SkColorType ct = kRGBA_8888_SkColorType;
178     #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
179         const SkColorType ct = kBGRA_8888_SkColorType;
180     #endif
181     static_assert(ct != kN32_SkColorType, "BRGA!=RGBA");
182     SkAssertResult(ToolUtils::copy_to(&notN32bitmap, ct, n32bitmap));
183     SkASSERT(notN32bitmap.colorType() == ct);
184     return SkImage::MakeFromBitmap(notN32bitmap);
185 }
186 
DEF_SIMPLE_GM(not_native32_bitmap_config,canvas,SCALE,SCALE)187 DEF_SIMPLE_GM(not_native32_bitmap_config, canvas, SCALE, SCALE) {
188     sk_sp<SkImage> notN32image(make_not_native32_color_wheel());
189     SkASSERT(notN32image);
190     ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
191     canvas->drawImage(notN32image.get(), 0.0f, 0.0f);
192 }
193 
make_pixel(int x,int y,SkAlphaType alphaType)194 static uint32_t make_pixel(int x, int y, SkAlphaType alphaType) {
195     SkASSERT(x >= 0 && x < SCALE);
196     SkASSERT(y >= 0 && y < SCALE);
197 
198     SkScalar R = SCALE / 2.0f;
199 
200     uint32_t alpha = 0x00;
201 
202     if ((x - R) * (x - R) + (y - R) * (y - R) < R * R) {
203         alpha = 0xFF;
204     }
205 
206     uint32_t component;
207     switch (alphaType) {
208         case kPremul_SkAlphaType:
209             component = alpha;
210             break;
211         case kUnpremul_SkAlphaType:
212             component = 0xFF;
213             break;
214         default:
215             SK_ABORT("Should not get here - invalid alpha type");
216     }
217     return alpha << 24 | component;
218 }
219 
make_color_test_bitmap_variant(SkColorType colorType,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace,SkBitmap * bm)220 static void make_color_test_bitmap_variant(
221     SkColorType colorType,
222     SkAlphaType alphaType,
223     sk_sp<SkColorSpace> colorSpace,
224     SkBitmap* bm)
225 {
226     SkASSERT(colorType == kRGBA_8888_SkColorType || colorType == kBGRA_8888_SkColorType);
227     SkASSERT(alphaType == kPremul_SkAlphaType || alphaType == kUnpremul_SkAlphaType);
228     bm->allocPixels(
229         SkImageInfo::Make(SCALE, SCALE, colorType, alphaType, colorSpace));
230     const SkPixmap& pm = bm->pixmap();
231     for (int y = 0; y < pm.height(); y++) {
232         for (int x = 0; x < pm.width(); x++) {
233             *pm.writable_addr32(x, y) = make_pixel(x, y, alphaType);
234         }
235     }
236 }
237 
238 DEF_SIMPLE_GM(all_variants_8888, canvas, 4 * SCALE + 30, 2 * SCALE + 10) {
239     ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8);
240 
241     sk_sp<SkColorSpace> colorSpaces[] {
242         SkColorSpace::MakeSRGB(),
243         nullptr,
244     };
245     for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
246         canvas->save();
247         for (auto alphaType : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) {
248             canvas->save();
249             for (auto colorType : {kRGBA_8888_SkColorType, kBGRA_8888_SkColorType}) {
250                 SkBitmap bm;
251                 make_color_test_bitmap_variant(colorType, alphaType, colorSpace, &bm);
252                 canvas->drawBitmap(bm, 0.0f, 0.0f);
253                 canvas->translate(SCALE + 10, 0.0f);
254             }
255             canvas->restore();
256             canvas->translate(0.0f, SCALE + 10);
257         }
258         canvas->restore();
259         canvas->translate(2 * (SCALE + 10), 0.0f);
260     }
261 }
262