epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkColorPriv.h" |
| 14 | #include "include/core/SkDrawLooper.h" |
| 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMaskFilter.h" |
| 17 | #include "include/core/SkMath.h" |
| 18 | #include "include/core/SkPaint.h" |
| 19 | #include "include/core/SkPath.h" |
| 20 | #include "include/core/SkPixmap.h" |
| 21 | #include "include/core/SkPoint.h" |
| 22 | #include "include/core/SkRRect.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 23 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/core/SkRefCnt.h" |
| 25 | #include "include/core/SkScalar.h" |
| 26 | #include "include/core/SkShader.h" |
| 27 | #include "include/core/SkSize.h" |
| 28 | #include "include/core/SkSurface.h" |
| 29 | #include "include/core/SkTypes.h" |
| 30 | #include "include/effects/SkBlurDrawLooper.h" |
| 31 | #include "include/effects/SkLayerDrawLooper.h" |
| 32 | #include "include/effects/SkPerlinNoiseShader.h" |
| 33 | #include "include/private/SkFloatBits.h" |
| 34 | #include "src/core/SkBlurMask.h" |
| 35 | #include "src/core/SkBlurPriv.h" |
| 36 | #include "src/core/SkMask.h" |
| 37 | #include "src/core/SkMaskFilterBase.h" |
| 38 | #include "src/core/SkMathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | #include "src/effects/SkEmbossMaskFilter.h" |
| 40 | #include "tests/Test.h" |
| 41 | #include "tools/ToolUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 42 | #include "tools/gpu/GrContextFactory.h" |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 43 | |
Ben Wagner | e6b04a1 | 2018-03-09 14:41:36 -0500 | [diff] [blame] | 44 | #include <math.h> |
| 45 | #include <string.h> |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 46 | #include <initializer_list> |
Ben Wagner | e6b04a1 | 2018-03-09 14:41:36 -0500 | [diff] [blame] | 47 | #include <utility> |
| 48 | |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 49 | class GrContext; |
| 50 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 51 | #define WRITE_CSV 0 |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | |
| 55 | #define ILLEGAL_MODE ((SkXfermode::Mode)-1) |
| 56 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 57 | static const int outset = 100; |
| 58 | static const SkColor bgColor = SK_ColorWHITE; |
| 59 | static const int strokeWidth = 4; |
| 60 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 61 | static void create(SkBitmap* bm, const SkIRect& bound) { |
| 62 | bm->allocN32Pixels(bound.width(), bound.height()); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void drawBG(SkCanvas* canvas) { |
| 66 | canvas->drawColor(bgColor); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | struct BlurTest { |
| 71 | void (*addPath)(SkPath*); |
| 72 | int viewLen; |
| 73 | SkIRect views[9]; |
| 74 | }; |
| 75 | |
| 76 | //Path Draw Procs |
| 77 | //Beware that paths themselves my draw differently depending on the clip. |
| 78 | static void draw50x50Rect(SkPath* path) { |
| 79 | path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50)); |
| 80 | } |
| 81 | |
| 82 | //Tests |
| 83 | static BlurTest tests[] = { |
| 84 | { draw50x50Rect, 3, { |
| 85 | //inner half of blur |
| 86 | { 0, 0, 50, 50 }, |
| 87 | //blur, but no path. |
| 88 | { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 }, |
| 89 | //just an edge |
| 90 | { 40, strokeWidth, 60, 50 - strokeWidth }, |
| 91 | }}, |
| 92 | }; |
| 93 | |
| 94 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 95 | implies that everything outside is "bgColor". |
| 96 | Checks that all overlap is the same and that all non-overlap on the |
| 97 | ref is "bgColor". |
| 98 | */ |
| 99 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 100 | const SkBitmap& test, const SkIRect& itest) |
| 101 | { |
| 102 | const int xOff = itest.fLeft - iref.fLeft; |
| 103 | const int yOff = itest.fTop - iref.fTop; |
| 104 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 105 | for (int y = 0; y < test.height(); ++y) { |
| 106 | for (int x = 0; x < test.width(); ++x) { |
| 107 | SkColor testColor = test.getColor(x, y); |
| 108 | int refX = x + xOff; |
| 109 | int refY = y + yOff; |
| 110 | SkColor refColor; |
| 111 | if (refX >= 0 && refX < ref.width() && |
| 112 | refY >= 0 && refY < ref.height()) |
| 113 | { |
| 114 | refColor = ref.getColor(refX, refY); |
| 115 | } else { |
| 116 | refColor = bgColor; |
| 117 | } |
| 118 | if (refColor != testColor) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 126 | DEF_TEST(BlurDrawing, reporter) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 127 | SkPaint paint; |
| 128 | paint.setColor(SK_ColorGRAY); |
| 129 | paint.setStyle(SkPaint::kStroke_Style); |
| 130 | paint.setStrokeWidth(SkIntToScalar(strokeWidth)); |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 131 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 132 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)); |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 133 | for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) { |
| 134 | SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style); |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 135 | |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 136 | for (bool respectCTM : { false, true }) { |
| 137 | paint.setMaskFilter(SkMaskFilter::MakeBlur(blurStyle, sigma, respectCTM)); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 138 | |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 139 | for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 140 | SkPath path; |
| 141 | tests[test].addPath(&path); |
bungeman@google.com | d16872c | 2011-09-02 15:46:17 +0000 | [diff] [blame] | 142 | SkPath strokedPath; |
| 143 | paint.getFillPath(path, &strokedPath); |
| 144 | SkRect refBound = strokedPath.getBounds(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 145 | SkIRect iref; |
| 146 | refBound.roundOut(&iref); |
bungeman@google.com | d16872c | 2011-09-02 15:46:17 +0000 | [diff] [blame] | 147 | iref.inset(-outset, -outset); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 148 | SkBitmap refBitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 149 | create(&refBitmap, iref); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 150 | |
| 151 | SkCanvas refCanvas(refBitmap); |
| 152 | refCanvas.translate(SkIntToScalar(-iref.fLeft), |
| 153 | SkIntToScalar(-iref.fTop)); |
| 154 | drawBG(&refCanvas); |
| 155 | refCanvas.drawPath(path, paint); |
| 156 | |
| 157 | for (int view = 0; view < tests[test].viewLen; ++view) { |
| 158 | SkIRect itest = tests[test].views[view]; |
| 159 | SkBitmap testBitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 160 | create(&testBitmap, itest); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 161 | |
| 162 | SkCanvas testCanvas(testBitmap); |
| 163 | testCanvas.translate(SkIntToScalar(-itest.fLeft), |
| 164 | SkIntToScalar(-itest.fTop)); |
| 165 | drawBG(&testCanvas); |
| 166 | testCanvas.drawPath(path, paint); |
| 167 | |
| 168 | REPORTER_ASSERT(reporter, |
| 169 | compare(refBitmap, iref, testBitmap, itest)); |
| 170 | } |
| 171 | } |
| 172 | } |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 176 | /////////////////////////////////////////////////////////////////////////////// |
| 177 | |
| 178 | // Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid |
| 179 | // white rect. Return the right half of the middle row in 'result'. |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 180 | static void ground_truth_2d(int width, int height, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 181 | SkScalar sigma, |
| 182 | int* result, int resultCount) { |
| 183 | SkMask src, dst; |
| 184 | |
| 185 | src.fBounds.set(0, 0, width, height); |
| 186 | src.fFormat = SkMask::kA8_Format; |
| 187 | src.fRowBytes = src.fBounds.width(); |
| 188 | src.fImage = SkMask::AllocImage(src.computeTotalImageSize()); |
| 189 | |
| 190 | memset(src.fImage, 0xff, src.computeTotalImageSize()); |
| 191 | |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 192 | if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) { |
| 193 | return; |
| 194 | } |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 195 | |
Mike Reed | 1f27585 | 2018-04-11 14:30:17 -0400 | [diff] [blame] | 196 | int midX = dst.fBounds.x() + dst.fBounds.width()/2; |
| 197 | int midY = dst.fBounds.y() + dst.fBounds.height()/2; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 198 | uint8_t* bytes = dst.getAddr8(midX, midY); |
| 199 | int i; |
| 200 | for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) { |
| 201 | if (i < resultCount) { |
| 202 | result[i] = bytes[i]; |
| 203 | } |
| 204 | } |
| 205 | for ( ; i < resultCount; ++i) { |
| 206 | result[i] = 0; |
| 207 | } |
robertphillips@google.com | 6d837aa | 2013-10-11 14:38:46 +0000 | [diff] [blame] | 208 | |
| 209 | SkMask::FreeImage(src.fImage); |
| 210 | SkMask::FreeImage(dst.fImage); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Implement a step function that is 255 between min and max; 0 elsewhere. |
| 214 | static int step(int x, SkScalar min, SkScalar max) { |
| 215 | if (min < x && x < max) { |
| 216 | return 255; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | // Implement a Gaussian function with 0 mean and std.dev. of 'sigma'. |
| 222 | static float gaussian(int x, SkScalar sigma) { |
robertphillips@google.com | b85564a | 2013-09-05 17:53:34 +0000 | [diff] [blame] | 223 | float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI)); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 224 | float exponent = -(x * x) / (2 * sigma * sigma); |
robertphillips@google.com | b85564a | 2013-09-05 17:53:34 +0000 | [diff] [blame] | 225 | return k * expf(exponent); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Perform a brute force convolution of a step function with a Gaussian. |
| 229 | // Return the right half in 'result' |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 230 | static void brute_force_1d(SkScalar stepMin, SkScalar stepMax, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 231 | SkScalar gaussianSigma, |
| 232 | int* result, int resultCount) { |
| 233 | |
| 234 | int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma); |
| 235 | |
| 236 | for (int i = 0; i < resultCount; ++i) { |
| 237 | SkScalar sum = 0.0f; |
| 238 | for (int j = -gaussianRange; j < gaussianRange; ++j) { |
| 239 | sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax); |
| 240 | } |
| 241 | |
| 242 | result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255); |
| 243 | } |
| 244 | } |
| 245 | |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 246 | static void blur_path(SkCanvas* canvas, const SkPath& path, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 247 | SkScalar gaussianSigma) { |
| 248 | |
| 249 | SkScalar midX = path.getBounds().centerX(); |
| 250 | SkScalar midY = path.getBounds().centerY(); |
| 251 | |
| 252 | canvas->translate(-midX, -midY); |
| 253 | |
| 254 | SkPaint blurPaint; |
| 255 | blurPaint.setColor(SK_ColorWHITE); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 256 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, gaussianSigma)); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 257 | |
| 258 | canvas->drawColor(SK_ColorBLACK); |
| 259 | canvas->drawPath(path, blurPaint); |
| 260 | } |
| 261 | |
| 262 | // Readback the blurred draw results from the canvas |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 263 | static void readback(const SkBitmap& src, int* result, int resultCount) { |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 264 | SkBitmap readback; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 265 | readback.allocN32Pixels(resultCount, 30); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 266 | SkPixmap pm; |
| 267 | readback.peekPixels(&pm); |
| 268 | src.readPixels(pm, 0, 0); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 269 | |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 270 | const SkPMColor* pixels = pm.addr32(0, 15); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 271 | |
| 272 | for (int i = 0; i < resultCount; ++i) { |
| 273 | result[i] = SkColorGetR(pixels[i]); |
| 274 | } |
| 275 | } |
| 276 | |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 277 | // Draw a blurred version of the provided path. |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 278 | // Return the right half of the middle row in 'result'. |
| 279 | static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma, |
| 280 | int* result, int resultCount) { |
| 281 | |
| 282 | SkBitmap bitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 283 | bitmap.allocN32Pixels(resultCount, 30); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 284 | SkCanvas canvas(bitmap); |
| 285 | |
| 286 | blur_path(&canvas, path, gaussianSigma); |
Mike Reed | f194219 | 2017-07-21 14:24:29 -0400 | [diff] [blame] | 287 | readback(bitmap, result, resultCount); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 288 | } |
| 289 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 290 | #if WRITE_CSV |
| 291 | static void write_as_csv(const char* label, SkScalar scale, int* data, int count) { |
| 292 | SkDebugf("%s_%.2f,", label, scale); |
| 293 | for (int i = 0; i < count-1; ++i) { |
| 294 | SkDebugf("%d,", data[i]); |
| 295 | } |
| 296 | SkDebugf("%d\n", data[count-1]); |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | static bool match(int* first, int* second, int count, int tol) { |
| 301 | int delta; |
| 302 | for (int i = 0; i < count; ++i) { |
| 303 | delta = first[i] - second[i]; |
| 304 | if (delta > tol || delta < -tol) { |
| 305 | return false; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | // Test out the normal blur style with a wide range of sigmas |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 313 | DEF_TEST(BlurSigmaRange, reporter) { |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 314 | static const int kSize = 100; |
| 315 | |
| 316 | // The geometry is offset a smidge to trigger: |
| 317 | // https://code.google.com/p/chromium/issues/detail?id=282418 |
| 318 | SkPath rectPath; |
| 319 | rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f); |
| 320 | |
| 321 | SkPoint polyPts[] = { |
| 322 | { 0.3f, 0.3f }, |
| 323 | { 100.3f, 0.3f }, |
| 324 | { 100.3f, 100.3f }, |
| 325 | { 0.3f, 100.3f }, |
| 326 | { 2.3f, 50.3f } // a little divet to throw off the rect special case |
| 327 | }; |
| 328 | SkPath polyPath; |
| 329 | polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true); |
| 330 | |
| 331 | int rectSpecialCaseResult[kSize]; |
| 332 | int generalCaseResult[kSize]; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 333 | int groundTruthResult[kSize]; |
| 334 | int bruteForce1DResult[kSize]; |
| 335 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 336 | SkScalar sigma = 10.0f; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 337 | |
| 338 | for (int i = 0; i < 4; ++i, sigma /= 10) { |
| 339 | |
| 340 | cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize); |
| 341 | cpu_blur_path(polyPath, sigma, generalCaseResult, kSize); |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 342 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 343 | ground_truth_2d(100, 100, sigma, groundTruthResult, kSize); |
| 344 | brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize); |
| 345 | |
| 346 | REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5)); |
| 347 | REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15)); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 348 | REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1)); |
| 349 | |
| 350 | #if WRITE_CSV |
| 351 | write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize); |
| 352 | write_as_csv("GeneralCase", sigma, generalCaseResult, kSize); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 353 | write_as_csv("GPU", sigma, gpuResult, kSize); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 354 | write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize); |
| 355 | write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize); |
| 356 | #endif |
| 357 | } |
| 358 | } |
| 359 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 360 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 361 | |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 362 | static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, SkBlurStyle style) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 363 | if (kNormal_SkBlurStyle != style) { |
| 364 | return; // blurdrawlooper only supports normal |
| 365 | } |
| 366 | |
| 367 | const SkColor color = 0xFF335577; |
| 368 | const SkScalar dx = 10; |
| 369 | const SkScalar dy = -5; |
Matt Sarett | f160ad4 | 2017-03-23 16:23:38 -0400 | [diff] [blame] | 370 | sk_sp<SkDrawLooper> lp(SkBlurDrawLooper::Make(color, sigma, dx, dy)); |
| 371 | const bool expectSuccess = sigma > 0; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 372 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 373 | if (nullptr == lp) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 374 | REPORTER_ASSERT(reporter, sigma <= 0); |
| 375 | } else { |
| 376 | SkDrawLooper::BlurShadowRec rec; |
| 377 | bool success = lp->asABlurShadow(&rec); |
| 378 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 379 | if (success) { |
| 380 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 381 | REPORTER_ASSERT(reporter, rec.fOffset.x() == dx); |
| 382 | REPORTER_ASSERT(reporter, rec.fOffset.y() == dy); |
| 383 | REPORTER_ASSERT(reporter, rec.fColor == color); |
| 384 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 389 | static void test_looper(skiatest::Reporter* reporter, sk_sp<SkDrawLooper> lp, SkScalar sigma, |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 390 | SkBlurStyle style, bool expectSuccess) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 391 | SkDrawLooper::BlurShadowRec rec; |
| 392 | bool success = lp->asABlurShadow(&rec); |
| 393 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 394 | if (success != expectSuccess) { |
| 395 | lp->asABlurShadow(&rec); |
| 396 | } |
| 397 | if (success) { |
| 398 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 399 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 400 | } |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void make_noop_layer(SkLayerDrawLooper::Builder* builder) { |
| 404 | SkLayerDrawLooper::LayerInfo info; |
| 405 | |
| 406 | info.fPaintBits = 0; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 407 | info.fColorMode = SkBlendMode::kDst; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 408 | builder->addLayer(info); |
| 409 | } |
| 410 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 411 | static void make_blur_layer(SkLayerDrawLooper::Builder* builder, sk_sp<SkMaskFilter> mf) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 412 | SkLayerDrawLooper::LayerInfo info; |
| 413 | |
| 414 | info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 415 | info.fColorMode = SkBlendMode::kSrc; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 416 | SkPaint* paint = builder->addLayer(info); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 417 | paint->setMaskFilter(std::move(mf)); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 418 | } |
| 419 | |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 420 | static void test_layerDrawLooper(skiatest::Reporter* reporter, sk_sp<SkMaskFilter> mf, |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 421 | SkScalar sigma, SkBlurStyle style, bool expectSuccess) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 422 | |
| 423 | SkLayerDrawLooper::LayerInfo info; |
| 424 | SkLayerDrawLooper::Builder builder; |
| 425 | |
| 426 | // 1 layer is too few |
| 427 | make_noop_layer(&builder); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 428 | test_looper(reporter, builder.detach(), sigma, style, false); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 429 | |
| 430 | // 2 layers is good, but need blur |
| 431 | make_noop_layer(&builder); |
| 432 | make_noop_layer(&builder); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 433 | test_looper(reporter, builder.detach(), sigma, style, false); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 434 | |
| 435 | // 2 layers is just right |
| 436 | make_noop_layer(&builder); |
| 437 | make_blur_layer(&builder, mf); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 438 | test_looper(reporter, builder.detach(), sigma, style, expectSuccess); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 439 | |
| 440 | // 3 layers is too many |
| 441 | make_noop_layer(&builder); |
| 442 | make_blur_layer(&builder, mf); |
| 443 | make_noop_layer(&builder); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 444 | test_looper(reporter, builder.detach(), sigma, style, false); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 447 | DEF_TEST(BlurAsABlur, reporter) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 448 | const SkBlurStyle styles[] = { |
| 449 | kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle |
| 450 | }; |
| 451 | const SkScalar sigmas[] = { |
| 452 | // values <= 0 should not success for a blur |
| 453 | -1, 0, 0.5f, 2 |
| 454 | }; |
| 455 | |
| 456 | // Test asABlur for SkBlurMaskFilter |
| 457 | // |
| 458 | for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) { |
Robert Phillips | 98624d2 | 2016-12-19 11:37:37 -0500 | [diff] [blame] | 459 | const SkBlurStyle style = styles[i]; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 460 | for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { |
| 461 | const SkScalar sigma = sigmas[j]; |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 462 | for (bool respectCTM : { false, true }) { |
| 463 | sk_sp<SkMaskFilter> mf(SkMaskFilter::MakeBlur(style, sigma, respectCTM)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 464 | if (nullptr == mf.get()) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 465 | REPORTER_ASSERT(reporter, sigma <= 0); |
| 466 | } else { |
| 467 | REPORTER_ASSERT(reporter, sigma > 0); |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 468 | SkMaskFilterBase::BlurRec rec; |
| 469 | bool success = as_MFB(mf)->asABlur(&rec); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 470 | if (respectCTM) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 471 | REPORTER_ASSERT(reporter, success); |
| 472 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 473 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 474 | } else { |
| 475 | REPORTER_ASSERT(reporter, !success); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 476 | } |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 477 | test_layerDrawLooper(reporter, std::move(mf), sigma, style, success); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 478 | } |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 479 | test_blurDrawLooper(reporter, sigma, style); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // Test asABlur for SkEmbossMaskFilter -- should never succeed |
| 485 | // |
| 486 | { |
| 487 | SkEmbossMaskFilter::Light light = { |
| 488 | { 1, 1, 1 }, 0, 127, 127 |
| 489 | }; |
| 490 | for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { |
| 491 | const SkScalar sigma = sigmas[j]; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 492 | auto mf(SkEmbossMaskFilter::Make(sigma, light)); |
| 493 | if (mf) { |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 494 | SkMaskFilterBase::BlurRec rec; |
| 495 | bool success = as_MFB(mf)->asABlur(&rec); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 496 | REPORTER_ASSERT(reporter, !success); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 502 | // This exercises the problem discovered in crbug.com/570232. The return value from |
robertphillips | 4abb0c1 | 2016-01-04 11:20:25 -0800 | [diff] [blame] | 503 | // SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 504 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctxInfo) { |
robertphillips | 4abb0c1 | 2016-01-04 11:20:25 -0800 | [diff] [blame] | 505 | |
| 506 | SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 507 | auto surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info)); |
robertphillips | 4abb0c1 | 2016-01-04 11:20:25 -0800 | [diff] [blame] | 508 | SkCanvas* canvas = surface->getCanvas(); |
| 509 | |
| 510 | SkRect r = SkRect::MakeXYWH(10, 10, 100, 100); |
| 511 | SkRRect rr = SkRRect::MakeRectXY(r, 10, 10); |
| 512 | |
| 513 | SkPaint p; |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 514 | p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 0.01f)); |
robertphillips | 4abb0c1 | 2016-01-04 11:20:25 -0800 | [diff] [blame] | 515 | |
| 516 | canvas->drawRRect(rr, p); |
| 517 | } |
| 518 | |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 519 | DEF_TEST(BlurredRRectNinePatchComputation, reporter) { |
| 520 | const SkRect r = SkRect::MakeXYWH(10, 10, 100, 100); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 521 | static const SkScalar kBlurRad = 3.0f; |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 522 | |
| 523 | bool ninePatchable; |
| 524 | SkRRect rrectToDraw; |
| 525 | SkISize size; |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 526 | SkScalar rectXs[kSkBlurRRectMaxDivisions], |
| 527 | rectYs[kSkBlurRRectMaxDivisions]; |
| 528 | SkScalar texXs[kSkBlurRRectMaxDivisions], |
| 529 | texYs[kSkBlurRRectMaxDivisions]; |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 530 | int numX, numY; |
| 531 | uint32_t skipMask; |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 532 | |
| 533 | // not nine-patchable |
| 534 | { |
| 535 | SkVector radii[4] = { { 100, 100 }, { 0, 0 }, { 100, 100 }, { 0, 0 } }; |
| 536 | |
| 537 | SkRRect rr; |
| 538 | rr.setRectRadii(r, radii); |
| 539 | |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 540 | ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(), |
| 541 | kBlurRad, kBlurRad, |
| 542 | &rrectToDraw, &size, |
| 543 | rectXs, rectYs, texXs, texYs, |
| 544 | &numX, &numY, &skipMask); |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, !ninePatchable); |
| 546 | } |
| 547 | |
| 548 | // simple circular |
| 549 | { |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 550 | static const SkScalar kCornerRad = 10.0f; |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 551 | SkRRect rr; |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 552 | rr.setRectXY(r, kCornerRad, kCornerRad); |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 553 | |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 554 | ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(), |
| 555 | kBlurRad, kBlurRad, |
| 556 | &rrectToDraw, &size, |
| 557 | rectXs, rectYs, texXs, texYs, |
| 558 | &numX, &numY, &skipMask); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 559 | |
| 560 | static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f; |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 561 | REPORTER_ASSERT(reporter, ninePatchable); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 562 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns)); |
| 563 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns)); |
| 564 | REPORTER_ASSERT(reporter, 4 == numX && 4 == numY); |
| 565 | REPORTER_ASSERT(reporter, !skipMask); |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // simple elliptical |
| 569 | { |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 570 | static const SkScalar kXCornerRad = 2.0f; |
| 571 | static const SkScalar kYCornerRad = 10.0f; |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 572 | SkRRect rr; |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 573 | rr.setRectXY(r, kXCornerRad, kYCornerRad); |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 574 | |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 575 | ninePatchable = SkComputeBlurredRRectParams(rr, rr, SkRect::MakeEmpty(), |
| 576 | kBlurRad, kBlurRad, |
| 577 | &rrectToDraw, &size, |
| 578 | rectXs, rectYs, texXs, texYs, |
| 579 | &numX, &numY, &skipMask); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 580 | |
| 581 | static const SkScalar kXAns = 12.0f * kBlurRad + 2.0f * kXCornerRad + 1.0f; |
| 582 | static const SkScalar kYAns = 12.0f * kBlurRad + 2.0f * kYCornerRad + 1.0f; |
| 583 | |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 584 | REPORTER_ASSERT(reporter, ninePatchable); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 585 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kXAns)); |
| 586 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kYAns)); |
| 587 | REPORTER_ASSERT(reporter, 4 == numX && 4 == numY); |
| 588 | REPORTER_ASSERT(reporter, !skipMask); |
| 589 | } |
| 590 | |
| 591 | // test-out occlusion |
| 592 | { |
| 593 | static const SkScalar kCornerRad = 10.0f; |
| 594 | SkRRect rr; |
| 595 | rr.setRectXY(r, kCornerRad, kCornerRad); |
| 596 | |
| 597 | // The rectXs & rectYs should be { 1, 29, 91, 119 }. Add two more points around each. |
| 598 | SkScalar testLocs[] = { |
| 599 | -18.0f, -9.0f, |
| 600 | 1.0f, |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 601 | 9.0f, 18.0f, |
| 602 | 29.0f, |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 603 | 39.0f, 49.0f, |
| 604 | 91.0f, |
| 605 | 109.0f, 118.0f, |
| 606 | 119.0f, |
| 607 | 139.0f, 149.0f |
| 608 | }; |
| 609 | |
| 610 | for (int minY = 0; minY < (int)SK_ARRAY_COUNT(testLocs); ++minY) { |
| 611 | for (int maxY = minY+1; maxY < (int)SK_ARRAY_COUNT(testLocs); ++maxY) { |
| 612 | for (int minX = 0; minX < (int)SK_ARRAY_COUNT(testLocs); ++minX) { |
| 613 | for (int maxX = minX+1; maxX < (int)SK_ARRAY_COUNT(testLocs); ++maxX) { |
| 614 | SkRect occluder = SkRect::MakeLTRB(testLocs[minX], testLocs[minY], |
| 615 | testLocs[maxX], testLocs[maxY]); |
| 616 | if (occluder.isEmpty()) { |
| 617 | continue; |
| 618 | } |
| 619 | |
Mike Reed | 28d4773 | 2018-03-05 16:56:52 -0500 | [diff] [blame] | 620 | ninePatchable = SkComputeBlurredRRectParams(rr, rr, occluder, |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 621 | kBlurRad, kBlurRad, |
| 622 | &rrectToDraw, &size, |
| 623 | rectXs, rectYs, texXs, texYs, |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 624 | &numX, &numY, &skipMask); |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 625 | |
| 626 | static const SkScalar kAns = 12.0f * kBlurRad + 2.0f * kCornerRad + 1.0f; |
| 627 | REPORTER_ASSERT(reporter, ninePatchable); |
| 628 | REPORTER_ASSERT(reporter, |
| 629 | SkScalarNearlyEqual(SkIntToScalar(size.fWidth), kAns)); |
| 630 | REPORTER_ASSERT(reporter, |
| 631 | SkScalarNearlyEqual(SkIntToScalar(size.fHeight), kAns)); |
| 632 | |
| 633 | int checkBit = 0x1; |
| 634 | for (int y = 0; y < numY-1; ++y) { |
| 635 | for (int x = 0; x < numX-1; ++x) { |
| 636 | SkRect cell = SkRect::MakeLTRB(rectXs[x], rectYs[y], |
| 637 | rectXs[x+1], rectYs[y+1]); |
| 638 | REPORTER_ASSERT(reporter, |
| 639 | SkToBool(skipMask & checkBit) == |
| 640 | (cell.isEmpty() || occluder.contains(cell))); |
| 641 | |
| 642 | REPORTER_ASSERT(reporter, texXs[x] >= 0 && |
| 643 | texXs[x] <= size.fWidth); |
| 644 | REPORTER_ASSERT(reporter, texYs[y] >= 0 && |
| 645 | texXs[y] <= size.fHeight); |
| 646 | |
| 647 | checkBit <<= 1; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 653 | } |
robertphillips | c4d2f90 | 2016-08-16 09:30:03 -0700 | [diff] [blame] | 654 | |
| 655 | |
robertphillips | f5a83e8 | 2016-08-10 12:00:09 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | } |
| 659 | |
Florin Malita | 1ba5bfe | 2017-11-28 17:54:23 -0500 | [diff] [blame] | 660 | // https://crbugs.com/787712 |
| 661 | DEF_TEST(EmbossPerlinCrash, reporter) { |
| 662 | SkPaint p; |
| 663 | |
| 664 | static constexpr SkEmbossMaskFilter::Light light = { |
| 665 | { 1, 1, 1 }, 0, 127, 127 |
| 666 | }; |
| 667 | p.setMaskFilter(SkEmbossMaskFilter::Make(1, light)); |
| 668 | p.setShader(SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f)); |
| 669 | |
| 670 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 671 | surface->getCanvas()->drawPaint(p); |
| 672 | } |
| 673 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 674 | /////////////////////////////////////////////////////////////////////////////////////////// |
Mike Reed | f221b49 | 2018-02-15 11:38:20 -0500 | [diff] [blame] | 675 | |
| 676 | DEF_TEST(BlurZeroSigma, reporter) { |
| 677 | auto surf = SkSurface::MakeRasterN32Premul(20, 20); |
| 678 | SkPaint paint; |
| 679 | paint.setAntiAlias(true); |
| 680 | |
| 681 | const SkIRect ir = { 5, 5, 15, 15 }; |
| 682 | const SkRect r = SkRect::Make(ir); |
| 683 | |
| 684 | const SkScalar sigmas[] = { 0, SkBits2Float(1) }; |
| 685 | // if sigma is zero (or nearly so), we need to draw correctly (unblurred) and not crash |
| 686 | // or assert. |
| 687 | for (auto sigma : sigmas) { |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 688 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma)); |
Mike Reed | f221b49 | 2018-02-15 11:38:20 -0500 | [diff] [blame] | 689 | surf->getCanvas()->drawRect(r, paint); |
| 690 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 691 | ToolUtils::PixelIter iter(surf.get()); |
Mike Reed | f221b49 | 2018-02-15 11:38:20 -0500 | [diff] [blame] | 692 | SkIPoint loc; |
| 693 | while (const SkPMColor* p = (const SkPMColor*)iter.next(&loc)) { |
| 694 | if (ir.contains(loc.fX, loc.fY)) { |
| 695 | // inside the rect we draw (opaque black) |
| 696 | REPORTER_ASSERT(reporter, *p == SkPackARGB32(0xFF, 0, 0, 0)); |
| 697 | } else { |
| 698 | // outside the rect we didn't draw at all, no blurred edges |
| 699 | REPORTER_ASSERT(reporter, *p == 0); |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Robert Phillips | c90b4db | 2018-04-23 15:12:00 +0000 | [diff] [blame] | 705 | |
| 706 | /////////////////////////////////////////////////////////////////////////////////////////// |
Robert Phillips | c90b4db | 2018-04-23 15:12:00 +0000 | [diff] [blame] | 707 | |
| 708 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurMaskBiggerThanDest, reporter, ctxInfo) { |
| 709 | GrContext* context = ctxInfo.grContext(); |
| 710 | |
| 711 | SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 712 | |
| 713 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
| 714 | if (!dst) { |
| 715 | ERRORF(reporter, "Could not create surface for test."); |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | SkPaint p; |
| 720 | p.setColor(SK_ColorRED); |
| 721 | p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 3)); |
| 722 | |
| 723 | SkCanvas* canvas = dst->getCanvas(); |
| 724 | |
| 725 | canvas->clear(SK_ColorBLACK); |
| 726 | canvas->drawCircle(SkPoint::Make(16, 16), 8, p); |
| 727 | |
| 728 | SkBitmap readback; |
| 729 | SkAssertResult(readback.tryAllocPixels(ii)); |
| 730 | |
| 731 | canvas->readPixels(readback, 0, 0); |
| 732 | REPORTER_ASSERT(reporter, SkColorGetR(readback.getColor(15, 15)) > 128); |
| 733 | REPORTER_ASSERT(reporter, SkColorGetG(readback.getColor(15, 15)) == 0); |
| 734 | REPORTER_ASSERT(reporter, SkColorGetB(readback.getColor(15, 15)) == 0); |
| 735 | REPORTER_ASSERT(reporter, readback.getColor(31, 31) == SK_ColorBLACK); |
| 736 | } |
Mike Reed | 840debe | 2018-09-15 12:15:27 -0400 | [diff] [blame] | 737 | |
| 738 | DEF_TEST(zero_blur, reporter) { |
| 739 | SkBitmap alpha, bitmap; |
| 740 | |
| 741 | SkPaint paint; |
| 742 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, 3)); |
| 743 | SkIPoint offset; |
| 744 | bitmap.extractAlpha(&alpha, &paint, nullptr, &offset); |
| 745 | } |
| 746 | |