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