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