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