halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tools/Resources.h" |
| 26 | #include "tools/ToolUtils.h" |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 27 | |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <initializer_list> |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 30 | |
| 31 | static SkBitmap copy_bitmap(const SkBitmap& src, SkColorType colorType) { |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 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 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 39 | SkBitmap copy; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 40 | ToolUtils::copy_to(©, colorType, *srcPtr); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 41 | copy.setImmutable(); |
| 42 | return copy; |
| 43 | } |
| 44 | |
| 45 | #define SCALE 128 |
| 46 | |
| 47 | // Make either A8 or gray8 bitmap. |
| 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 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 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 | |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 74 | static void draw_center_letter(char c, const SkFont& font, SkColor color, |
| 75 | SkScalar x, SkScalar y, SkCanvas* canvas) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 76 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 77 | font.measureText(&c, 1, SkTextEncoding::kUTF8, &bounds); |
| 78 | canvas->drawSimpleText(&c, 1, SkTextEncoding::kUTF8, |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 79 | x - bounds.centerX(), y - bounds.centerY(), |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 80 | font, SkPaint(SkColor4f::FromColor(color))); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void color_wheel_native(SkCanvas* canvas) { |
| 84 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 85 | canvas->translate(0.5f * SCALE, 0.5f * SCALE); |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 86 | canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, SkPaint(SkColors::kWhite)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 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; |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 93 | |
| 94 | SkFont font; |
| 95 | font.setEdging(SkFont::Edging::kAlias); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 96 | font.setTypeface(ToolUtils::create_portable_typeface(nullptr, SkFontStyle::Bold())); |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 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); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | template <typename T> |
| 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 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 117 | static void draw(SkCanvas* canvas, |
| 118 | const SkPaint& p, |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 119 | const SkFont& font, |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 120 | const SkBitmap& src, |
| 121 | SkColorType colorType, |
| 122 | const char text[]) { |
| 123 | SkASSERT(src.colorType() == colorType); |
Mike Reed | 6dbeac5 | 2021-01-13 13:14:23 -0500 | [diff] [blame] | 124 | canvas->drawImage(src.asImage(), 0.0f, 0.0f); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 125 | canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0.0f, 12.0f, font, p); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Brian Osman | 567f4fd | 2018-09-12 11:48:38 -0400 | [diff] [blame] | 128 | DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 129 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
Hal Canary | 0a7b393 | 2019-05-02 11:31:28 -0400 | [diff] [blame] | 130 | SkPaint p(SkColors::kBlack); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 131 | p.setAntiAlias(true); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 132 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 133 | SkFont font(ToolUtils::create_portable_typeface()); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 134 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 135 | ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 136 | |
| 137 | SkBitmap bitmap; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 138 | if (GetResourceAsBitmap("images/color_wheel.png", &bitmap)) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 139 | bitmap.setImmutable(); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 140 | draw(canvas, p, font, bitmap, kN32_SkColorType, "Native 32"); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 141 | |
| 142 | canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 143 | SkBitmap copy565 = copy_bitmap(bitmap, kRGB_565_SkColorType); |
| 144 | p.setColor(SK_ColorRED); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 145 | draw(canvas, p, font, copy565, kRGB_565_SkColorType, "RGB 565"); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 146 | p.setColor(SK_ColorBLACK); |
| 147 | |
| 148 | canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 149 | SkBitmap copy4444 = copy_bitmap(bitmap, kARGB_4444_SkColorType); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 150 | draw(canvas, p, font, copy4444, kARGB_4444_SkColorType, "ARGB 4444"); |
Brian Osman | 567f4fd | 2018-09-12 11:48:38 -0400 | [diff] [blame] | 151 | |
| 152 | canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 153 | SkBitmap copyF16 = copy_bitmap(bitmap, kRGBA_F16_SkColorType); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 154 | draw(canvas, p, font, copyF16, kRGBA_F16_SkColorType, "RGBA F16"); |
Brian Osman | 567f4fd | 2018-09-12 11:48:38 -0400 | [diff] [blame] | 155 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 156 | } else { |
Brian Osman | 567f4fd | 2018-09-12 11:48:38 -0400 | [diff] [blame] | 157 | canvas->translate(0.0f, SkIntToScalar(3 * SCALE)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | canvas->translate(0.0f, SkIntToScalar(SCALE)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 161 | SkBitmap bitmapA8 = make_bitmap(kAlpha_8_SkColorType); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 162 | draw(canvas, p, font, bitmapA8, kAlpha_8_SkColorType, "Alpha 8"); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 163 | |
| 164 | p.setColor(SK_ColorRED); |
| 165 | canvas->translate(0.0f, SkIntToScalar(SCALE)); |
| 166 | SkBitmap bitmapG8 = make_bitmap(kGray_8_SkColorType); |
Mike Reed | d5cdc2c | 2019-01-04 15:57:33 -0500 | [diff] [blame] | 167 | draw(canvas, p, font, bitmapG8, kGray_8_SkColorType, "Gray 8"); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 168 | } |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 169 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 170 | sk_sp<SkImage> make_not_native32_color_wheel() { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 171 | SkBitmap n32bitmap, notN32bitmap; |
| 172 | n32bitmap.allocN32Pixels(SCALE, SCALE); |
| 173 | n32bitmap.eraseColor(SK_ColorTRANSPARENT); |
| 174 | SkCanvas n32canvas(n32bitmap); |
| 175 | color_wheel_native(&n32canvas); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 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"); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 182 | SkAssertResult(ToolUtils::copy_to(¬N32bitmap, ct, n32bitmap)); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 183 | SkASSERT(notN32bitmap.colorType() == ct); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 184 | return notN32bitmap.asImage(); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | DEF_SIMPLE_GM(not_native32_bitmap_config, canvas, SCALE, SCALE) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 188 | sk_sp<SkImage> notN32image(make_not_native32_color_wheel()); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 189 | SkASSERT(notN32image); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 190 | ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 191 | canvas->drawImage(notN32image.get(), 0.0f, 0.0f); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 192 | } |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 193 | |
| 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: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 215 | SK_ABORT("Should not get here - invalid alpha type"); |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 216 | } |
| 217 | return alpha << 24 | component; |
| 218 | } |
| 219 | |
| 220 | static void make_color_test_bitmap_variant( |
| 221 | SkColorType colorType, |
| 222 | SkAlphaType alphaType, |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 223 | sk_sp<SkColorSpace> colorSpace, |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 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( |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 229 | SkImageInfo::Make(SCALE, SCALE, colorType, alphaType, colorSpace)); |
Hal Canary | 99578d2 | 2017-12-14 21:13:47 -0500 | [diff] [blame] | 230 | const SkPixmap& pm = bm->pixmap(); |
Hal Canary | a088365 | 2017-12-14 15:33:23 -0500 | [diff] [blame] | 231 | for (int y = 0; y < pm.height(); y++) { |
| 232 | for (int x = 0; x < pm.width(); x++) { |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 233 | *pm.writable_addr32(x, y) = make_pixel(x, y, alphaType); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Joe Gregorio | c485907 | 2017-01-20 14:21:27 +0000 | [diff] [blame] | 238 | DEF_SIMPLE_GM(all_variants_8888, canvas, 4 * SCALE + 30, 2 * SCALE + 10) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 239 | ToolUtils::draw_checkerboard(canvas, SK_ColorLTGRAY, SK_ColorWHITE, 8); |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 240 | |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 241 | sk_sp<SkColorSpace> colorSpaces[] { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 242 | SkColorSpace::MakeSRGB(), |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 243 | nullptr, |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 244 | }; |
John Stiles | bd3ffa4 | 2020-07-30 20:24:57 -0400 | [diff] [blame] | 245 | for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) { |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 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; |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 251 | make_color_test_bitmap_variant(colorType, alphaType, colorSpace, &bm); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame] | 252 | canvas->drawImage(bm.asImage(), 0.0f, 0.0f); |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 253 | canvas->translate(SCALE + 10, 0.0f); |
| 254 | } |
| 255 | canvas->restore(); |
Joe Gregorio | c485907 | 2017-01-20 14:21:27 +0000 | [diff] [blame] | 256 | canvas->translate(0.0f, SCALE + 10); |
herb | 5823846 | 2016-06-03 09:44:53 -0700 | [diff] [blame] | 257 | } |
| 258 | canvas->restore(); |
| 259 | canvas->translate(2 * (SCALE + 10), 0.0f); |
| 260 | } |
| 261 | } |