bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 1 | /* |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils_flags.h" |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 10 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 12 | #include "SkBitmap.h" |
| 13 | #include "SkCanvas.h" |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 14 | #include "SkCommonFlags.h" |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 15 | #include "SkFontMgr.h" |
| 16 | #include "SkFontStyle.h" |
Brian Osman | 2b25d34 | 2016-12-20 11:09:31 -0500 | [diff] [blame] | 17 | #include "SkPixelRef.h" |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 18 | #include "SkPoint3.h" |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 19 | #include "SkShader.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 20 | #include "SkTestScalerContext.h" |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 21 | #include "SkTextBlob.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 22 | |
| 23 | DEFINE_bool(portableFonts, false, "Use portable fonts"); |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 24 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 25 | #if SK_SUPPORT_GPU |
| 26 | #include "effects/GrSRGBEffect.h" |
| 27 | #include "SkColorFilter.h" |
| 28 | |
| 29 | // Color filter that just wraps GrSRGBEffect |
| 30 | class SkSRGBColorFilter : public SkColorFilter { |
| 31 | public: |
| 32 | static sk_sp<SkColorFilter> Make(GrSRGBEffect::Mode mode) { |
| 33 | return sk_sp<SkColorFilter>(new SkSRGBColorFilter(mode)); |
| 34 | } |
| 35 | |
| 36 | sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override { |
| 37 | return GrSRGBEffect::Make(fMode); |
| 38 | } |
| 39 | |
| 40 | void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override { |
| 41 | SK_ABORT("SkSRGBColorFilter is only implemented for GPU"); |
| 42 | } |
| 43 | Factory getFactory() const override { return nullptr; } |
| 44 | |
| 45 | #ifndef SK_IGNORE_TO_STRING |
| 46 | void toString(SkString* str) const override {} |
| 47 | #endif |
| 48 | |
| 49 | private: |
| 50 | SkSRGBColorFilter(GrSRGBEffect::Mode mode) : fMode(mode) {} |
| 51 | |
| 52 | GrSRGBEffect::Mode fMode; |
| 53 | typedef SkColorFilter INHERITED; |
| 54 | }; |
| 55 | #endif |
| 56 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 57 | namespace sk_tool_utils { |
| 58 | |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 59 | /* these are the default fonts chosen by Chrome for serif, sans-serif, and monospace */ |
| 60 | static const char* gStandardFontNames[][3] = { |
| 61 | { "Times", "Helvetica", "Courier" }, // Mac |
| 62 | { "Times New Roman", "Helvetica", "Courier" }, // iOS |
| 63 | { "Times New Roman", "Arial", "Courier New" }, // Win |
| 64 | { "Times New Roman", "Arial", "Monospace" }, // Ubuntu |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 65 | { "serif", "sans-serif", "monospace" }, // Android |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 66 | { "Tinos", "Arimo", "Cousine" } // ChromeOS |
| 67 | }; |
| 68 | |
| 69 | const char* platform_font_name(const char* name) { |
| 70 | SkString platform = major_platform_os_name(); |
| 71 | int index; |
| 72 | if (!strcmp(name, "serif")) { |
| 73 | index = 0; |
| 74 | } else if (!strcmp(name, "san-serif")) { |
| 75 | index = 1; |
| 76 | } else if (!strcmp(name, "monospace")) { |
| 77 | index = 2; |
| 78 | } else { |
| 79 | return name; |
| 80 | } |
| 81 | if (platform.equals("Mac")) { |
| 82 | return gStandardFontNames[0][index]; |
| 83 | } |
| 84 | if (platform.equals("iOS")) { |
| 85 | return gStandardFontNames[1][index]; |
| 86 | } |
| 87 | if (platform.equals("Win")) { |
| 88 | return gStandardFontNames[2][index]; |
| 89 | } |
| 90 | if (platform.equals("Ubuntu")) { |
| 91 | return gStandardFontNames[3][index]; |
| 92 | } |
| 93 | if (platform.equals("Android")) { |
| 94 | return gStandardFontNames[4][index]; |
| 95 | } |
| 96 | if (platform.equals("ChromeOS")) { |
| 97 | return gStandardFontNames[5][index]; |
| 98 | } |
| 99 | return name; |
| 100 | } |
| 101 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 102 | const char* platform_os_emoji() { |
| 103 | const char* osName = platform_os_name(); |
caryclark | e663341 | 2015-07-23 09:11:58 -0700 | [diff] [blame] | 104 | if (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu")) { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 105 | return "CBDT"; |
| 106 | } |
bungeman | fbbf01a | 2016-07-20 07:15:16 -0700 | [diff] [blame] | 107 | if (!strncmp(osName, "Mac", 3) || !strncmp(osName, "iOS", 3)) { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 108 | return "SBIX"; |
| 109 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 110 | if (!strncmp(osName, "Win", 3)) { |
| 111 | return "COLR"; |
| 112 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 113 | return ""; |
| 114 | } |
| 115 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 116 | sk_sp<SkTypeface> emoji_typeface() { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 117 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 118 | return MakeResourceAsTypeface("/fonts/Funkster.ttf"); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 119 | } |
| 120 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX")) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 121 | return SkTypeface::MakeFromName("Apple Color Emoji", SkFontStyle()); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 122 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 123 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "COLR")) { |
| 124 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 125 | const char *colorEmojiFontName = "Segoe UI Emoji"; |
| 126 | sk_sp<SkTypeface> typeface(fm->matchFamilyStyle(colorEmojiFontName, SkFontStyle())); |
| 127 | if (typeface) { |
| 128 | return typeface; |
| 129 | } |
| 130 | sk_sp<SkTypeface> fallback(fm->matchFamilyStyleCharacter( |
| 131 | colorEmojiFontName, SkFontStyle(), nullptr /* bcp47 */, 0 /* bcp47Count */, |
| 132 | 0x1f4b0 /* character: 💰 */)); |
| 133 | if (fallback) { |
| 134 | return fallback; |
| 135 | } |
| 136 | // If we don't have Segoe UI Emoji and can't find a fallback, try Segoe UI Symbol. |
| 137 | // Windows 7 does not have Segoe UI Emoji; Segoe UI Symbol has the (non - color) emoji. |
| 138 | return SkTypeface::MakeFromName("Segoe UI Symbol", SkFontStyle()); |
| 139 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 140 | return nullptr; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | const char* emoji_sample_text() { |
| 144 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "CBDT")) { |
| 145 | return "Hamburgefons"; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 146 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 147 | if (!strcmp(sk_tool_utils::platform_os_emoji(), "SBIX") || |
| 148 | !strcmp(sk_tool_utils::platform_os_emoji(), "COLR")) |
| 149 | { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 150 | return "\xF0\x9F\x92\xB0" "\xF0\x9F\x8F\xA1" "\xF0\x9F\x8E\x85" // 💰🏡🎅 |
| 151 | "\xF0\x9F\x8D\xAA" "\xF0\x9F\x8D\x95" "\xF0\x9F\x9A\x80" // 🍪🍕🚀 |
| 152 | "\xF0\x9F\x9A\xBB" "\xF0\x9F\x92\xA9" "\xF0\x9F\x93\xB7" // 🚻💩📷 |
| 153 | "\xF0\x9F\x93\xA6" // 📦 |
| 154 | "\xF0\x9F\x87\xBA" "\xF0\x9F\x87\xB8" "\xF0\x9F\x87\xA6"; // 🇺🇸🇦 |
| 155 | } |
| 156 | return ""; |
| 157 | } |
| 158 | |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 159 | const char* platform_os_name() { |
| 160 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 161 | if (!strcmp("os", FLAGS_key[index])) { |
| 162 | return FLAGS_key[index + 1]; |
| 163 | } |
| 164 | } |
| 165 | // when running SampleApp or dm without a --key pair, omit the platform name |
| 166 | return ""; |
| 167 | } |
| 168 | |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 169 | // omit version number in returned value |
| 170 | SkString major_platform_os_name() { |
| 171 | SkString name; |
| 172 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 173 | if (!strcmp("os", FLAGS_key[index])) { |
| 174 | const char* platform = FLAGS_key[index + 1]; |
| 175 | const char* end = platform; |
| 176 | while (*end && (*end < '0' || *end > '9')) { |
| 177 | ++end; |
| 178 | } |
| 179 | name.append(platform, end - platform); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | return name; |
| 184 | } |
| 185 | |
caryclark | 6531c36 | 2015-07-20 13:38:56 -0700 | [diff] [blame] | 186 | const char* platform_extra_config(const char* config) { |
| 187 | for (int index = 0; index < FLAGS_key.count(); index += 2) { |
| 188 | if (!strcmp("extra_config", FLAGS_key[index]) && !strcmp(config, FLAGS_key[index + 1])) { |
| 189 | return config; |
| 190 | } |
| 191 | } |
| 192 | return ""; |
| 193 | } |
| 194 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 195 | const char* colortype_name(SkColorType ct) { |
| 196 | switch (ct) { |
| 197 | case kUnknown_SkColorType: return "Unknown"; |
| 198 | case kAlpha_8_SkColorType: return "Alpha_8"; |
| 199 | case kIndex_8_SkColorType: return "Index_8"; |
| 200 | case kARGB_4444_SkColorType: return "ARGB_4444"; |
| 201 | case kRGB_565_SkColorType: return "RGB_565"; |
| 202 | case kRGBA_8888_SkColorType: return "RGBA_8888"; |
| 203 | case kBGRA_8888_SkColorType: return "BGRA_8888"; |
Brian Osman | f750fbc | 2017-02-08 10:47:28 -0500 | [diff] [blame] | 204 | case kRGBA_F16_SkColorType: return "RGBA_F16"; |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 205 | default: |
| 206 | SkASSERT(false); |
| 207 | return "unexpected colortype"; |
| 208 | } |
| 209 | } |
| 210 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 211 | SkColor color_to_565(SkColor color) { |
| 212 | SkPMColor pmColor = SkPreMultiplyColor(color); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 213 | U16CPU color16 = SkPixel32ToPixel16(pmColor); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 214 | return SkPixel16ToColor(color16); |
caryclark | d85093c | 2015-06-12 11:49:04 -0700 | [diff] [blame] | 215 | } |
| 216 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 217 | sk_sp<SkTypeface> create_portable_typeface(const char* name, SkFontStyle style) { |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 218 | return create_font(name, style); |
| 219 | } |
| 220 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 221 | void set_portable_typeface(SkPaint* paint, const char* name, SkFontStyle style) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 222 | paint->setTypeface(create_font(name, style)); |
caryclark | 83ca628 | 2015-06-10 09:31:09 -0700 | [diff] [blame] | 223 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 224 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 225 | void write_pixels(SkCanvas* canvas, const SkBitmap& bitmap, int x, int y, |
| 226 | SkColorType colorType, SkAlphaType alphaType) { |
| 227 | SkBitmap tmp(bitmap); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 228 | const SkImageInfo info = SkImageInfo::Make(tmp.width(), tmp.height(), colorType, alphaType); |
skia.committer@gmail.com | e62513f | 2014-03-08 03:02:06 +0000 | [diff] [blame] | 229 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 230 | canvas->writePixels(info, tmp.getPixels(), tmp.rowBytes(), x, y); |
| 231 | } |
| 232 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 233 | sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) { |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 234 | SkBitmap bm; |
brianosman | 2331a5f | 2016-09-28 14:02:10 -0700 | [diff] [blame] | 235 | bm.allocPixels(SkImageInfo::MakeS32(2 * size, 2 * size, kPremul_SkAlphaType)); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 236 | bm.eraseColor(c1); |
| 237 | bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2); |
| 238 | bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 239 | return SkShader::MakeBitmapShader( |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 240 | bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
| 241 | } |
| 242 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 243 | SkBitmap create_checkerboard_bitmap(int w, int h, SkColor c1, SkColor c2, int checkSize) { |
| 244 | SkBitmap bitmap; |
brianosman | 2331a5f | 2016-09-28 14:02:10 -0700 | [diff] [blame] | 245 | bitmap.allocPixels(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 246 | SkCanvas canvas(bitmap); |
| 247 | |
| 248 | sk_tool_utils::draw_checkerboard(&canvas, c1, c2, checkSize); |
| 249 | return bitmap; |
| 250 | } |
| 251 | |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 252 | void draw_checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) { |
| 253 | SkPaint paint; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 254 | paint.setShader(create_checkerboard_shader(c1, c2, size)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 255 | paint.setBlendMode(SkBlendMode::kSrc); |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 256 | canvas->drawPaint(paint); |
| 257 | } |
| 258 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 259 | SkBitmap create_string_bitmap(int w, int h, SkColor c, int x, int y, |
| 260 | int textSize, const char* str) { |
| 261 | SkBitmap bitmap; |
| 262 | bitmap.allocN32Pixels(w, h); |
| 263 | SkCanvas canvas(bitmap); |
| 264 | |
| 265 | SkPaint paint; |
| 266 | paint.setAntiAlias(true); |
| 267 | sk_tool_utils::set_portable_typeface(&paint); |
| 268 | paint.setColor(c); |
| 269 | paint.setTextSize(SkIntToScalar(textSize)); |
| 270 | |
| 271 | canvas.clear(0x00000000); |
| 272 | canvas.drawText(str, strlen(str), SkIntToScalar(x), SkIntToScalar(y), paint); |
| 273 | |
Brian Osman | 2b25d34 | 2016-12-20 11:09:31 -0500 | [diff] [blame] | 274 | // Tag data as sRGB (without doing any color space conversion). Color-space aware configs |
| 275 | // will process this correctly but legacy configs will render as if this returned N32. |
| 276 | SkBitmap result; |
| 277 | result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 278 | result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 279 | return result; |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 280 | } |
| 281 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 282 | void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkPaint& origPaint, |
| 283 | SkScalar x, SkScalar y) { |
| 284 | SkPaint paint(origPaint); |
| 285 | SkTDArray<uint16_t> glyphs; |
| 286 | |
| 287 | size_t len = strlen(text); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 288 | glyphs.append(paint.textToGlyphs(text, len, nullptr)); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 289 | paint.textToGlyphs(text, len, glyphs.begin()); |
| 290 | |
| 291 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 292 | const SkTextBlobBuilder::RunBuffer& run = builder->allocRun(paint, glyphs.count(), x, y, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 293 | nullptr); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 294 | memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); |
| 295 | } |
| 296 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 297 | static inline void norm_to_rgb(SkBitmap* bm, int x, int y, const SkVector3& norm) { |
| 298 | SkASSERT(SkScalarNearlyEqual(norm.length(), 1.0f)); |
| 299 | unsigned char r = static_cast<unsigned char>((0.5f * norm.fX + 0.5f) * 255); |
| 300 | unsigned char g = static_cast<unsigned char>((-0.5f * norm.fY + 0.5f) * 255); |
| 301 | unsigned char b = static_cast<unsigned char>((0.5f * norm.fZ + 0.5f) * 255); |
| 302 | *bm->getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 303 | } |
| 304 | |
| 305 | void create_hemi_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 306 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 307 | dst.fTop + (dst.height() / 2.0f)); |
| 308 | const SkPoint halfSize = SkPoint::Make(dst.width() / 2.0f, dst.height() / 2.0f); |
| 309 | |
| 310 | SkVector3 norm; |
| 311 | |
| 312 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 313 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 314 | norm.fX = (x + 0.5f - center.fX) / halfSize.fX; |
| 315 | norm.fY = (y + 0.5f - center.fY) / halfSize.fY; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 316 | |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 317 | SkScalar tmp = norm.fX * norm.fX + norm.fY * norm.fY; |
| 318 | if (tmp >= 1.0f) { |
| 319 | norm.set(0.0f, 0.0f, 1.0f); |
| 320 | } else { |
bsalomon | f3bf892 | 2015-08-19 06:12:36 -0700 | [diff] [blame] | 321 | norm.fZ = sqrtf(1.0f - tmp); |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | norm_to_rgb(bm, x, y, norm); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | void create_frustum_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 330 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 331 | dst.fTop + (dst.height() / 2.0f)); |
| 332 | |
| 333 | SkIRect inner = dst; |
| 334 | inner.inset(dst.width()/4, dst.height()/4); |
| 335 | |
| 336 | SkPoint3 norm; |
| 337 | const SkPoint3 left = SkPoint3::Make(-SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 338 | const SkPoint3 up = SkPoint3::Make(0.0f, -SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 339 | const SkPoint3 right = SkPoint3::Make(SK_ScalarRoot2Over2, 0.0f, SK_ScalarRoot2Over2); |
| 340 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 341 | |
| 342 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 343 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 344 | if (inner.contains(x, y)) { |
| 345 | norm.set(0.0f, 0.0f, 1.0f); |
| 346 | } else { |
| 347 | SkScalar locX = x + 0.5f - center.fX; |
| 348 | SkScalar locY = y + 0.5f - center.fY; |
| 349 | |
| 350 | if (locX >= 0.0f) { |
| 351 | if (locY > 0.0f) { |
| 352 | norm = locX >= locY ? right : down; // LR corner |
| 353 | } else { |
| 354 | norm = locX > -locY ? right : up; // UR corner |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 355 | } |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 356 | } else { |
| 357 | if (locY > 0.0f) { |
| 358 | norm = -locX > locY ? left : down; // LL corner |
| 359 | } else { |
| 360 | norm = locX > locY ? up : left; // UL corner |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 361 | } |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | norm_to_rgb(bm, x, y, norm); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void create_tetra_normal_map(SkBitmap* bm, const SkIRect& dst) { |
| 371 | const SkPoint center = SkPoint::Make(dst.fLeft + (dst.width() / 2.0f), |
| 372 | dst.fTop + (dst.height() / 2.0f)); |
| 373 | |
| 374 | static const SkScalar k1OverRoot3 = 0.5773502692f; |
| 375 | |
| 376 | SkPoint3 norm; |
| 377 | const SkPoint3 leftUp = SkPoint3::Make(-k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 378 | const SkPoint3 rightUp = SkPoint3::Make(k1OverRoot3, -k1OverRoot3, k1OverRoot3); |
| 379 | const SkPoint3 down = SkPoint3::Make(0.0f, SK_ScalarRoot2Over2, SK_ScalarRoot2Over2); |
| 380 | |
| 381 | for (int y = dst.fTop; y < dst.fBottom; ++y) { |
| 382 | for (int x = dst.fLeft; x < dst.fRight; ++x) { |
| 383 | SkScalar locX = x + 0.5f - center.fX; |
| 384 | SkScalar locY = y + 0.5f - center.fY; |
| 385 | |
| 386 | if (locX >= 0.0f) { |
| 387 | if (locY > 0.0f) { |
| 388 | norm = locX >= locY ? rightUp : down; // LR corner |
| 389 | } else { |
| 390 | norm = rightUp; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 391 | } |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 392 | } else { |
| 393 | if (locY > 0.0f) { |
| 394 | norm = -locX > locY ? leftUp : down; // LL corner |
| 395 | } else { |
| 396 | norm = leftUp; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 397 | } |
robertphillips | ea4529d | 2015-08-17 15:04:47 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | norm_to_rgb(bm, x, y, norm); |
| 401 | } |
| 402 | } |
| 403 | } |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 404 | |
Mike Klein | 88fa747 | 2016-10-12 17:06:48 -0400 | [diff] [blame] | 405 | #if defined(_MSC_VER) |
| 406 | // MSVC takes ~2 minutes to compile this function with optimization. |
| 407 | // We don't really care to wait that long for this function. |
| 408 | #pragma optimize("", off) |
| 409 | #endif |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 410 | void make_big_path(SkPath& path) { |
| 411 | #include "BigPathBench.inc" |
| 412 | } |
| 413 | |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 414 | static float gaussian2d_value(int x, int y, float sigma) { |
| 415 | // don't bother with the scale term since we're just going to normalize the |
| 416 | // kernel anyways |
bsalomon | 2f8ac35 | 2015-10-19 08:29:16 -0700 | [diff] [blame] | 417 | float temp = expf(-(x*x + y*y)/(2*sigma*sigma)); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 418 | return temp; |
| 419 | } |
| 420 | |
| 421 | static float* create_2d_kernel(float sigma, int* filterSize) { |
| 422 | // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel |
| 423 | // sizes are always odd) |
| 424 | int halfFilterSize = SkScalarCeilToInt(6*sigma)/2; |
| 425 | int wh = *filterSize = 2*halfFilterSize + 1; |
| 426 | |
| 427 | float* temp = new float[wh*wh]; |
| 428 | |
| 429 | float filterTot = 0.0f; |
| 430 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 431 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 432 | temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma); |
| 433 | |
| 434 | filterTot += temp[yOff*wh+xOff]; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // normalize the kernel |
| 439 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 440 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 441 | temp[yOff*wh+xOff] /= filterTot; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return temp; |
| 446 | } |
| 447 | |
| 448 | static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) { |
| 449 | SkASSERT(wh & 0x1); |
| 450 | |
| 451 | int halfFilterSize = (wh-1)/2; |
| 452 | |
| 453 | float r = 0.0f, g = 0.0f, b = 0.0f; |
| 454 | for (int yOff = 0; yOff < wh; ++yOff) { |
| 455 | int ySamp = y + yOff - halfFilterSize; |
| 456 | |
| 457 | if (ySamp < 0) { |
| 458 | ySamp = 0; |
| 459 | } else if (ySamp > bm.height()-1) { |
| 460 | ySamp = bm.height()-1; |
| 461 | } |
| 462 | |
| 463 | for (int xOff = 0; xOff < wh; ++xOff) { |
| 464 | int xSamp = x + xOff - halfFilterSize; |
| 465 | |
| 466 | if (xSamp < 0) { |
| 467 | xSamp = 0; |
| 468 | } else if (xSamp > bm.width()-1) { |
| 469 | xSamp = bm.width()-1; |
| 470 | } |
| 471 | |
| 472 | float filter = kernel[yOff*wh + xOff]; |
| 473 | |
| 474 | SkPMColor c = *bm.getAddr32(xSamp, ySamp); |
| 475 | |
| 476 | r += SkGetPackedR32(c) * filter; |
| 477 | g += SkGetPackedG32(c) * filter; |
| 478 | b += SkGetPackedB32(c) * filter; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | U8CPU r8, g8, b8; |
| 483 | |
| 484 | r8 = (U8CPU) (r+0.5f); |
| 485 | g8 = (U8CPU) (g+0.5f); |
| 486 | b8 = (U8CPU) (b+0.5f); |
| 487 | |
| 488 | return SkPackARGB32(255, r8, g8, b8); |
| 489 | } |
| 490 | |
| 491 | SkBitmap slow_blur(const SkBitmap& src, float sigma) { |
| 492 | SkBitmap dst; |
| 493 | |
| 494 | dst.allocN32Pixels(src.width(), src.height(), true); |
| 495 | |
| 496 | int wh; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 497 | std::unique_ptr<float[]> kernel(create_2d_kernel(sigma, &wh)); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 498 | |
| 499 | for (int y = 0; y < src.height(); ++y) { |
| 500 | for (int x = 0; x < src.width(); ++x) { |
| 501 | *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh); |
| 502 | } |
| 503 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 504 | |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 505 | return dst; |
| 506 | } |
| 507 | |
robertphillips | 276d328 | 2016-08-04 09:03:19 -0700 | [diff] [blame] | 508 | // compute the intersection point between the diagonal and the ellipse in the |
| 509 | // lower right corner |
| 510 | static SkPoint intersection(SkScalar w, SkScalar h) { |
| 511 | SkASSERT(w > 0.0f || h > 0.0f); |
| 512 | |
| 513 | return SkPoint::Make(w / SK_ScalarSqrt2, h / SK_ScalarSqrt2); |
| 514 | } |
| 515 | |
| 516 | // Use the intersection of the corners' diagonals with their ellipses to shrink |
| 517 | // the bounding rect |
| 518 | SkRect compute_central_occluder(const SkRRect& rr) { |
| 519 | const SkRect r = rr.getBounds(); |
| 520 | |
| 521 | SkScalar newL = r.fLeft, newT = r.fTop, newR = r.fRight, newB = r.fBottom; |
| 522 | |
| 523 | SkVector radii = rr.radii(SkRRect::kUpperLeft_Corner); |
| 524 | if (!radii.isZero()) { |
| 525 | SkPoint p = intersection(radii.fX, radii.fY); |
| 526 | |
| 527 | newL = SkTMax(newL, r.fLeft + radii.fX - p.fX); |
| 528 | newT = SkTMax(newT, r.fTop + radii.fY - p.fY); |
| 529 | } |
| 530 | |
| 531 | radii = rr.radii(SkRRect::kUpperRight_Corner); |
| 532 | if (!radii.isZero()) { |
| 533 | SkPoint p = intersection(radii.fX, radii.fY); |
| 534 | |
| 535 | newR = SkTMin(newR, r.fRight + p.fX - radii.fX); |
| 536 | newT = SkTMax(newT, r.fTop + radii.fY - p.fY); |
| 537 | } |
| 538 | |
| 539 | radii = rr.radii(SkRRect::kLowerRight_Corner); |
| 540 | if (!radii.isZero()) { |
| 541 | SkPoint p = intersection(radii.fX, radii.fY); |
| 542 | |
| 543 | newR = SkTMin(newR, r.fRight + p.fX - radii.fX); |
| 544 | newB = SkTMin(newB, r.fBottom - radii.fY + p.fY); |
| 545 | } |
| 546 | |
| 547 | radii = rr.radii(SkRRect::kLowerLeft_Corner); |
| 548 | if (!radii.isZero()) { |
| 549 | SkPoint p = intersection(radii.fX, radii.fY); |
| 550 | |
| 551 | newL = SkTMax(newL, r.fLeft + radii.fX - p.fX); |
| 552 | newB = SkTMin(newB, r.fBottom - radii.fY + p.fY); |
| 553 | } |
| 554 | |
| 555 | return SkRect::MakeLTRB(newL, newT, newR, newB); |
| 556 | } |
| 557 | |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 558 | // The widest inset rect |
| 559 | SkRect compute_widest_occluder(const SkRRect& rr) { |
| 560 | const SkRect& r = rr.getBounds(); |
| 561 | |
| 562 | const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner); |
| 563 | const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner); |
| 564 | const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner); |
| 565 | const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner); |
| 566 | |
| 567 | SkScalar maxT = SkTMax(ul.fY, ur.fY); |
| 568 | SkScalar maxB = SkTMax(ll.fY, lr.fY); |
| 569 | |
| 570 | return SkRect::MakeLTRB(r.fLeft, r.fTop + maxT, r.fRight, r.fBottom - maxB); |
| 571 | |
| 572 | } |
| 573 | |
| 574 | // The tallest inset rect |
| 575 | SkRect compute_tallest_occluder(const SkRRect& rr) { |
| 576 | const SkRect& r = rr.getBounds(); |
| 577 | |
| 578 | const SkVector& ul = rr.radii(SkRRect::kUpperLeft_Corner); |
| 579 | const SkVector& ur = rr.radii(SkRRect::kUpperRight_Corner); |
| 580 | const SkVector& lr = rr.radii(SkRRect::kLowerRight_Corner); |
| 581 | const SkVector& ll = rr.radii(SkRRect::kLowerLeft_Corner); |
| 582 | |
| 583 | SkScalar maxL = SkTMax(ul.fX, ll.fX); |
| 584 | SkScalar maxR = SkTMax(ur.fX, lr.fX); |
| 585 | |
| 586 | return SkRect::MakeLTRB(r.fLeft + maxL, r.fTop, r.fRight - maxR, r.fBottom); |
| 587 | } |
| 588 | |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 589 | void copy_to_g8(SkBitmap* dst, const SkBitmap& src) { |
| 590 | SkASSERT(kBGRA_8888_SkColorType == src.colorType() || |
| 591 | kRGBA_8888_SkColorType == src.colorType()); |
| 592 | |
| 593 | SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType); |
| 594 | dst->allocPixels(grayInfo); |
| 595 | uint8_t* dst8 = (uint8_t*)dst->getPixels(); |
| 596 | const uint32_t* src32 = (const uint32_t*)src.getPixels(); |
| 597 | |
| 598 | const int w = src.width(); |
| 599 | const int h = src.height(); |
| 600 | const bool isBGRA = (kBGRA_8888_SkColorType == src.colorType()); |
| 601 | for (int y = 0; y < h; ++y) { |
| 602 | if (isBGRA) { |
| 603 | // BGRA |
| 604 | for (int x = 0; x < w; ++x) { |
| 605 | uint32_t s = src32[x]; |
| 606 | dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF); |
| 607 | } |
| 608 | } else { |
| 609 | // RGBA |
| 610 | for (int x = 0; x < w; ++x) { |
| 611 | uint32_t s = src32[x]; |
| 612 | dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF); |
| 613 | } |
| 614 | } |
| 615 | src32 = (const uint32_t*)((const char*)src32 + src.rowBytes()); |
| 616 | dst8 += dst->rowBytes(); |
| 617 | } |
| 618 | } |
robertphillips | 401c196 | 2016-08-04 12:35:46 -0700 | [diff] [blame] | 619 | |
Brian Osman | 964dec3 | 2017-01-26 09:32:33 -0500 | [diff] [blame] | 620 | #if SK_SUPPORT_GPU |
| 621 | sk_sp<SkColorFilter> MakeLinearToSRGBColorFilter() { |
| 622 | return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kLinearToSRGB); |
| 623 | } |
| 624 | |
| 625 | sk_sp<SkColorFilter> MakeSRGBToLinearColorFilter() { |
| 626 | return SkSRGBColorFilter::Make(GrSRGBEffect::Mode::kSRGBToLinear); |
| 627 | } |
| 628 | #endif |
| 629 | |
tfarina | 2010891 | 2014-06-21 10:54:17 -0700 | [diff] [blame] | 630 | } // namespace sk_tool_utils |