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