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