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