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