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 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 8 | #include "SkBlurMask.h" |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 9 | #include "SkBlurMaskFilter.h" |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 10 | #include "SkBlurDrawLooper.h" |
reed | fb8c1fc | 2015-08-04 18:44:56 -0700 | [diff] [blame] | 11 | #include "SkCanvas.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 12 | #include "SkEmbossMaskFilter.h" |
| 13 | #include "SkLayerDrawLooper.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 14 | #include "SkMath.h" |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkPath.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 17 | #include "Test.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
| 20 | #include "GrContextFactory.h" |
| 21 | #include "SkGpuDevice.h" |
| 22 | #endif |
| 23 | |
| 24 | #define WRITE_CSV 0 |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | #define ILLEGAL_MODE ((SkXfermode::Mode)-1) |
| 29 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 30 | static const int outset = 100; |
| 31 | static const SkColor bgColor = SK_ColorWHITE; |
| 32 | static const int strokeWidth = 4; |
| 33 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 34 | static void create(SkBitmap* bm, const SkIRect& bound) { |
| 35 | bm->allocN32Pixels(bound.width(), bound.height()); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static void drawBG(SkCanvas* canvas) { |
| 39 | canvas->drawColor(bgColor); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | struct BlurTest { |
| 44 | void (*addPath)(SkPath*); |
| 45 | int viewLen; |
| 46 | SkIRect views[9]; |
| 47 | }; |
| 48 | |
| 49 | //Path Draw Procs |
| 50 | //Beware that paths themselves my draw differently depending on the clip. |
| 51 | static void draw50x50Rect(SkPath* path) { |
| 52 | path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50)); |
| 53 | } |
| 54 | |
| 55 | //Tests |
| 56 | static BlurTest tests[] = { |
| 57 | { draw50x50Rect, 3, { |
| 58 | //inner half of blur |
| 59 | { 0, 0, 50, 50 }, |
| 60 | //blur, but no path. |
| 61 | { 50 + strokeWidth/2, 50 + strokeWidth/2, 100, 100 }, |
| 62 | //just an edge |
| 63 | { 40, strokeWidth, 60, 50 - strokeWidth }, |
| 64 | }}, |
| 65 | }; |
| 66 | |
| 67 | /** Assumes that the ref draw was completely inside ref canvas -- |
| 68 | implies that everything outside is "bgColor". |
| 69 | Checks that all overlap is the same and that all non-overlap on the |
| 70 | ref is "bgColor". |
| 71 | */ |
| 72 | static bool compare(const SkBitmap& ref, const SkIRect& iref, |
| 73 | const SkBitmap& test, const SkIRect& itest) |
| 74 | { |
| 75 | const int xOff = itest.fLeft - iref.fLeft; |
| 76 | const int yOff = itest.fTop - iref.fTop; |
| 77 | |
| 78 | SkAutoLockPixels alpRef(ref); |
| 79 | SkAutoLockPixels alpTest(test); |
| 80 | |
| 81 | for (int y = 0; y < test.height(); ++y) { |
| 82 | for (int x = 0; x < test.width(); ++x) { |
| 83 | SkColor testColor = test.getColor(x, y); |
| 84 | int refX = x + xOff; |
| 85 | int refY = y + yOff; |
| 86 | SkColor refColor; |
| 87 | if (refX >= 0 && refX < ref.width() && |
| 88 | refY >= 0 && refY < ref.height()) |
| 89 | { |
| 90 | refColor = ref.getColor(refX, refY); |
| 91 | } else { |
| 92 | refColor = bgColor; |
| 93 | } |
| 94 | if (refColor != testColor) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 102 | static void test_blur_drawing(skiatest::Reporter* reporter) { |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 103 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 104 | SkPaint paint; |
| 105 | paint.setColor(SK_ColorGRAY); |
| 106 | paint.setStyle(SkPaint::kStroke_Style); |
| 107 | paint.setStrokeWidth(SkIntToScalar(strokeWidth)); |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 108 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 109 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)); |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 110 | for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) { |
| 111 | SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style); |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 112 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 113 | const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag; |
| 114 | for (uint32_t flags = 0; flags < flagPermutations; ++flags) { |
| 115 | SkMaskFilter* filter; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 116 | filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags); |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 117 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 118 | paint.setMaskFilter(filter); |
| 119 | filter->unref(); |
| 120 | |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 121 | for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 122 | SkPath path; |
| 123 | tests[test].addPath(&path); |
bungeman@google.com | d16872c | 2011-09-02 15:46:17 +0000 | [diff] [blame] | 124 | SkPath strokedPath; |
| 125 | paint.getFillPath(path, &strokedPath); |
| 126 | SkRect refBound = strokedPath.getBounds(); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 127 | SkIRect iref; |
| 128 | refBound.roundOut(&iref); |
bungeman@google.com | d16872c | 2011-09-02 15:46:17 +0000 | [diff] [blame] | 129 | iref.inset(-outset, -outset); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 130 | SkBitmap refBitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 131 | create(&refBitmap, iref); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 132 | |
| 133 | SkCanvas refCanvas(refBitmap); |
| 134 | refCanvas.translate(SkIntToScalar(-iref.fLeft), |
| 135 | SkIntToScalar(-iref.fTop)); |
| 136 | drawBG(&refCanvas); |
| 137 | refCanvas.drawPath(path, paint); |
| 138 | |
| 139 | for (int view = 0; view < tests[test].viewLen; ++view) { |
| 140 | SkIRect itest = tests[test].views[view]; |
| 141 | SkBitmap testBitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 142 | create(&testBitmap, itest); |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 143 | |
| 144 | SkCanvas testCanvas(testBitmap); |
| 145 | testCanvas.translate(SkIntToScalar(-itest.fLeft), |
| 146 | SkIntToScalar(-itest.fTop)); |
| 147 | drawBG(&testCanvas); |
| 148 | testCanvas.drawPath(path, paint); |
| 149 | |
| 150 | REPORTER_ASSERT(reporter, |
| 151 | compare(refBitmap, iref, testBitmap, itest)); |
| 152 | } |
| 153 | } |
| 154 | } |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | // Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid |
| 161 | // 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] | 162 | static void ground_truth_2d(int width, int height, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 163 | SkScalar sigma, |
| 164 | int* result, int resultCount) { |
| 165 | SkMask src, dst; |
| 166 | |
| 167 | src.fBounds.set(0, 0, width, height); |
| 168 | src.fFormat = SkMask::kA8_Format; |
| 169 | src.fRowBytes = src.fBounds.width(); |
| 170 | src.fImage = SkMask::AllocImage(src.computeTotalImageSize()); |
| 171 | |
| 172 | memset(src.fImage, 0xff, src.computeTotalImageSize()); |
| 173 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | dst.fImage = nullptr; |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 175 | SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 176 | |
| 177 | int midX = dst.fBounds.centerX(); |
| 178 | int midY = dst.fBounds.centerY(); |
| 179 | uint8_t* bytes = dst.getAddr8(midX, midY); |
| 180 | int i; |
| 181 | for (i = 0; i < dst.fBounds.width()-(midX-dst.fBounds.fLeft); ++i) { |
| 182 | if (i < resultCount) { |
| 183 | result[i] = bytes[i]; |
| 184 | } |
| 185 | } |
| 186 | for ( ; i < resultCount; ++i) { |
| 187 | result[i] = 0; |
| 188 | } |
robertphillips@google.com | 6d837aa | 2013-10-11 14:38:46 +0000 | [diff] [blame] | 189 | |
| 190 | SkMask::FreeImage(src.fImage); |
| 191 | SkMask::FreeImage(dst.fImage); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Implement a step function that is 255 between min and max; 0 elsewhere. |
| 195 | static int step(int x, SkScalar min, SkScalar max) { |
| 196 | if (min < x && x < max) { |
| 197 | return 255; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | // Implement a Gaussian function with 0 mean and std.dev. of 'sigma'. |
| 203 | static float gaussian(int x, SkScalar sigma) { |
robertphillips@google.com | b85564a | 2013-09-05 17:53:34 +0000 | [diff] [blame] | 204 | float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI)); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 205 | float exponent = -(x * x) / (2 * sigma * sigma); |
robertphillips@google.com | b85564a | 2013-09-05 17:53:34 +0000 | [diff] [blame] | 206 | return k * expf(exponent); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Perform a brute force convolution of a step function with a Gaussian. |
| 210 | // Return the right half in 'result' |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 211 | static void brute_force_1d(SkScalar stepMin, SkScalar stepMax, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 212 | SkScalar gaussianSigma, |
| 213 | int* result, int resultCount) { |
| 214 | |
| 215 | int gaussianRange = SkScalarCeilToInt(10 * gaussianSigma); |
| 216 | |
| 217 | for (int i = 0; i < resultCount; ++i) { |
| 218 | SkScalar sum = 0.0f; |
| 219 | for (int j = -gaussianRange; j < gaussianRange; ++j) { |
| 220 | sum += gaussian(j, gaussianSigma) * step(i-j, stepMin, stepMax); |
| 221 | } |
| 222 | |
| 223 | result[i] = SkClampMax(SkClampPos(int(sum + 0.5f)), 255); |
| 224 | } |
| 225 | } |
| 226 | |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 227 | static void blur_path(SkCanvas* canvas, const SkPath& path, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 228 | SkScalar gaussianSigma) { |
| 229 | |
| 230 | SkScalar midX = path.getBounds().centerX(); |
| 231 | SkScalar midY = path.getBounds().centerY(); |
| 232 | |
| 233 | canvas->translate(-midX, -midY); |
| 234 | |
| 235 | SkPaint blurPaint; |
| 236 | blurPaint.setColor(SK_ColorWHITE); |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 237 | SkMaskFilter* filter = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 238 | gaussianSigma, |
| 239 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 240 | blurPaint.setMaskFilter(filter)->unref(); |
| 241 | |
| 242 | canvas->drawColor(SK_ColorBLACK); |
| 243 | canvas->drawPath(path, blurPaint); |
| 244 | } |
| 245 | |
| 246 | // Readback the blurred draw results from the canvas |
| 247 | static void readback(SkCanvas* canvas, int* result, int resultCount) { |
| 248 | SkBitmap readback; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 249 | readback.allocN32Pixels(resultCount, 30); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 250 | |
| 251 | SkIRect readBackRect = { 0, 0, resultCount, 30 }; |
| 252 | |
| 253 | canvas->readPixels(readBackRect, &readback); |
| 254 | |
| 255 | readback.lockPixels(); |
| 256 | SkPMColor* pixels = (SkPMColor*) readback.getAddr32(0, 15); |
| 257 | |
| 258 | for (int i = 0; i < resultCount; ++i) { |
| 259 | result[i] = SkColorGetR(pixels[i]); |
| 260 | } |
| 261 | } |
| 262 | |
skia.committer@gmail.com | 6fc1b49 | 2013-09-06 07:01:45 +0000 | [diff] [blame] | 263 | // Draw a blurred version of the provided path. |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 264 | // Return the right half of the middle row in 'result'. |
| 265 | static void cpu_blur_path(const SkPath& path, SkScalar gaussianSigma, |
| 266 | int* result, int resultCount) { |
| 267 | |
| 268 | SkBitmap bitmap; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 269 | bitmap.allocN32Pixels(resultCount, 30); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 270 | SkCanvas canvas(bitmap); |
| 271 | |
| 272 | blur_path(&canvas, path, gaussianSigma); |
| 273 | readback(&canvas, result, resultCount); |
| 274 | } |
| 275 | |
| 276 | #if SK_SUPPORT_GPU |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 277 | #if 0 |
| 278 | // temporary disable; see below for explanation |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 279 | static bool gpu_blur_path(GrContextFactory* factory, const SkPath& path, |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 280 | SkScalar gaussianSigma, |
| 281 | int* result, int resultCount) { |
| 282 | |
| 283 | GrContext* grContext = factory->get(GrContextFactory::kNative_GLContextType); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 284 | if (nullptr == grContext) { |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 285 | return false; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 286 | } |
| 287 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 288 | GrSurfaceDesc desc; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 289 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 290 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 291 | desc.fWidth = resultCount; |
| 292 | desc.fHeight = 30; |
| 293 | desc.fSampleCnt = 0; |
| 294 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 295 | SkAutoTUnref<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0)); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 296 | SkAutoTUnref<SkGpuDevice> device(new SkGpuDevice (grContext, texture.get())); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 297 | SkCanvas canvas(device.get()); |
| 298 | |
| 299 | blur_path(&canvas, path, gaussianSigma); |
| 300 | readback(&canvas, result, resultCount); |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 301 | return true; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 302 | } |
| 303 | #endif |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 304 | #endif |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 305 | |
| 306 | #if WRITE_CSV |
| 307 | static void write_as_csv(const char* label, SkScalar scale, int* data, int count) { |
| 308 | SkDebugf("%s_%.2f,", label, scale); |
| 309 | for (int i = 0; i < count-1; ++i) { |
| 310 | SkDebugf("%d,", data[i]); |
| 311 | } |
| 312 | SkDebugf("%d\n", data[count-1]); |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | static bool match(int* first, int* second, int count, int tol) { |
| 317 | int delta; |
| 318 | for (int i = 0; i < count; ++i) { |
| 319 | delta = first[i] - second[i]; |
| 320 | if (delta > tol || delta < -tol) { |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | // Test out the normal blur style with a wide range of sigmas |
| 329 | static void test_sigma_range(skiatest::Reporter* reporter, GrContextFactory* factory) { |
| 330 | |
| 331 | static const int kSize = 100; |
| 332 | |
| 333 | // The geometry is offset a smidge to trigger: |
| 334 | // https://code.google.com/p/chromium/issues/detail?id=282418 |
| 335 | SkPath rectPath; |
| 336 | rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f); |
| 337 | |
| 338 | SkPoint polyPts[] = { |
| 339 | { 0.3f, 0.3f }, |
| 340 | { 100.3f, 0.3f }, |
| 341 | { 100.3f, 100.3f }, |
| 342 | { 0.3f, 100.3f }, |
| 343 | { 2.3f, 50.3f } // a little divet to throw off the rect special case |
| 344 | }; |
| 345 | SkPath polyPath; |
| 346 | polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true); |
| 347 | |
| 348 | int rectSpecialCaseResult[kSize]; |
| 349 | int generalCaseResult[kSize]; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 350 | int groundTruthResult[kSize]; |
| 351 | int bruteForce1DResult[kSize]; |
| 352 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 353 | SkScalar sigma = 10.0f; |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 354 | |
| 355 | for (int i = 0; i < 4; ++i, sigma /= 10) { |
| 356 | |
| 357 | cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize); |
| 358 | cpu_blur_path(polyPath, sigma, generalCaseResult, kSize); |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 359 | |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 360 | ground_truth_2d(100, 100, sigma, groundTruthResult, kSize); |
| 361 | brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize); |
| 362 | |
| 363 | REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5)); |
| 364 | REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15)); |
| 365 | #if SK_SUPPORT_GPU |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 366 | #if 0 |
| 367 | int gpuResult[kSize]; |
| 368 | bool haveGPUResult = gpu_blur_path(factory, rectPath, sigma, gpuResult, kSize); |
| 369 | // Disabling this test for now -- I don't think it's a legit comparison. |
| 370 | // Will continue to investigate this. |
robertphillips@google.com | cb3b615 | 2013-11-14 14:47:56 +0000 | [diff] [blame] | 371 | if (haveGPUResult) { |
| 372 | // 1 works everywhere but: Ubuntu13 & Nexus4 |
| 373 | REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10)); |
| 374 | } |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 375 | #endif |
humper | 4a24cd8 | 2014-06-17 13:39:29 -0700 | [diff] [blame] | 376 | #endif |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 377 | REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1)); |
| 378 | |
| 379 | #if WRITE_CSV |
| 380 | write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize); |
| 381 | write_as_csv("GeneralCase", sigma, generalCaseResult, kSize); |
| 382 | #if SK_SUPPORT_GPU |
| 383 | write_as_csv("GPU", sigma, gpuResult, kSize); |
| 384 | #endif |
| 385 | write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize); |
| 386 | write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize); |
| 387 | #endif |
| 388 | } |
| 389 | } |
| 390 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 391 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 392 | |
| 393 | static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) { |
| 394 | return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ? |
| 395 | kHigh_SkBlurQuality : kLow_SkBlurQuality; |
| 396 | } |
| 397 | |
| 398 | static uint32_t blurMaskFilterFlags_to_blurDrawLooperFlags(uint32_t bmf) { |
| 399 | const struct { |
| 400 | uint32_t fBlurMaskFilterFlag; |
| 401 | uint32_t fBlurDrawLooperFlag; |
| 402 | } pairs[] = { |
| 403 | { SkBlurMaskFilter::kIgnoreTransform_BlurFlag, SkBlurDrawLooper::kIgnoreTransform_BlurFlag }, |
| 404 | { SkBlurMaskFilter::kHighQuality_BlurFlag, SkBlurDrawLooper::kHighQuality_BlurFlag }, |
| 405 | }; |
| 406 | |
| 407 | uint32_t bdl = 0; |
| 408 | for (size_t i = 0; i < SK_ARRAY_COUNT(pairs); ++i) { |
| 409 | if (bmf & pairs[i].fBlurMaskFilterFlag) { |
| 410 | bdl |= pairs[i].fBlurDrawLooperFlag; |
| 411 | } |
| 412 | } |
| 413 | return bdl; |
| 414 | } |
| 415 | |
| 416 | static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, |
| 417 | SkBlurStyle style, uint32_t blurMaskFilterFlags) { |
| 418 | if (kNormal_SkBlurStyle != style) { |
| 419 | return; // blurdrawlooper only supports normal |
| 420 | } |
| 421 | |
| 422 | const SkColor color = 0xFF335577; |
| 423 | const SkScalar dx = 10; |
| 424 | const SkScalar dy = -5; |
| 425 | const SkBlurQuality quality = blurMaskFilterFlags_as_quality(blurMaskFilterFlags); |
| 426 | uint32_t flags = blurMaskFilterFlags_to_blurDrawLooperFlags(blurMaskFilterFlags); |
| 427 | |
| 428 | SkAutoTUnref<SkDrawLooper> lp(SkBlurDrawLooper::Create(color, sigma, dx, dy, flags)); |
| 429 | |
| 430 | const bool expectSuccess = sigma > 0 && |
| 431 | 0 == (flags & SkBlurDrawLooper::kIgnoreTransform_BlurFlag); |
| 432 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 433 | if (nullptr == lp.get()) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 434 | REPORTER_ASSERT(reporter, sigma <= 0); |
| 435 | } else { |
| 436 | SkDrawLooper::BlurShadowRec rec; |
| 437 | bool success = lp->asABlurShadow(&rec); |
| 438 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 439 | if (success) { |
| 440 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 441 | REPORTER_ASSERT(reporter, rec.fOffset.x() == dx); |
| 442 | REPORTER_ASSERT(reporter, rec.fOffset.y() == dy); |
| 443 | REPORTER_ASSERT(reporter, rec.fColor == color); |
| 444 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
| 445 | REPORTER_ASSERT(reporter, rec.fQuality == quality); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | static void test_delete_looper(skiatest::Reporter* reporter, SkDrawLooper* lp, SkScalar sigma, |
| 451 | SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) { |
| 452 | SkDrawLooper::BlurShadowRec rec; |
| 453 | bool success = lp->asABlurShadow(&rec); |
| 454 | REPORTER_ASSERT(reporter, success == expectSuccess); |
| 455 | if (success != expectSuccess) { |
| 456 | lp->asABlurShadow(&rec); |
| 457 | } |
| 458 | if (success) { |
| 459 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 460 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
| 461 | REPORTER_ASSERT(reporter, rec.fQuality == quality); |
| 462 | } |
| 463 | lp->unref(); |
| 464 | } |
| 465 | |
| 466 | static void make_noop_layer(SkLayerDrawLooper::Builder* builder) { |
| 467 | SkLayerDrawLooper::LayerInfo info; |
| 468 | |
| 469 | info.fPaintBits = 0; |
| 470 | info.fColorMode = SkXfermode::kDst_Mode; |
| 471 | builder->addLayer(info); |
| 472 | } |
| 473 | |
| 474 | static void make_blur_layer(SkLayerDrawLooper::Builder* builder, SkMaskFilter* mf) { |
| 475 | SkLayerDrawLooper::LayerInfo info; |
| 476 | |
| 477 | info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit; |
| 478 | info.fColorMode = SkXfermode::kSrc_Mode; |
| 479 | SkPaint* paint = builder->addLayer(info); |
| 480 | paint->setMaskFilter(mf); |
| 481 | } |
| 482 | |
| 483 | static void test_layerDrawLooper(skiatest::Reporter* reporter, SkMaskFilter* mf, SkScalar sigma, |
| 484 | SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) { |
| 485 | |
| 486 | SkLayerDrawLooper::LayerInfo info; |
| 487 | SkLayerDrawLooper::Builder builder; |
| 488 | |
| 489 | // 1 layer is too few |
| 490 | make_noop_layer(&builder); |
| 491 | test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); |
| 492 | |
| 493 | // 2 layers is good, but need blur |
| 494 | make_noop_layer(&builder); |
| 495 | make_noop_layer(&builder); |
| 496 | test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); |
| 497 | |
| 498 | // 2 layers is just right |
| 499 | make_noop_layer(&builder); |
| 500 | make_blur_layer(&builder, mf); |
| 501 | test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, expectSuccess); |
| 502 | |
| 503 | // 3 layers is too many |
| 504 | make_noop_layer(&builder); |
| 505 | make_blur_layer(&builder, mf); |
| 506 | make_noop_layer(&builder); |
| 507 | test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); |
| 508 | } |
| 509 | |
| 510 | static void test_asABlur(skiatest::Reporter* reporter) { |
| 511 | const SkBlurStyle styles[] = { |
| 512 | kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle |
| 513 | }; |
| 514 | const SkScalar sigmas[] = { |
| 515 | // values <= 0 should not success for a blur |
| 516 | -1, 0, 0.5f, 2 |
| 517 | }; |
| 518 | |
| 519 | // Test asABlur for SkBlurMaskFilter |
| 520 | // |
| 521 | for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) { |
| 522 | const SkBlurStyle style = (SkBlurStyle)styles[i]; |
| 523 | for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { |
| 524 | const SkScalar sigma = sigmas[j]; |
| 525 | for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) { |
| 526 | const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags); |
| 527 | |
| 528 | SkAutoTUnref<SkMaskFilter> mf(SkBlurMaskFilter::Create(style, sigma, flags)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 529 | if (nullptr == mf.get()) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 530 | REPORTER_ASSERT(reporter, sigma <= 0); |
| 531 | } else { |
| 532 | REPORTER_ASSERT(reporter, sigma > 0); |
| 533 | SkMaskFilter::BlurRec rec; |
| 534 | bool success = mf->asABlur(&rec); |
| 535 | if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) { |
| 536 | REPORTER_ASSERT(reporter, !success); |
| 537 | } else { |
| 538 | REPORTER_ASSERT(reporter, success); |
| 539 | REPORTER_ASSERT(reporter, rec.fSigma == sigma); |
| 540 | REPORTER_ASSERT(reporter, rec.fStyle == style); |
| 541 | REPORTER_ASSERT(reporter, rec.fQuality == quality); |
| 542 | } |
| 543 | test_layerDrawLooper(reporter, mf, sigma, style, quality, success); |
| 544 | } |
| 545 | test_blurDrawLooper(reporter, sigma, style, flags); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // Test asABlur for SkEmbossMaskFilter -- should never succeed |
| 551 | // |
| 552 | { |
| 553 | SkEmbossMaskFilter::Light light = { |
| 554 | { 1, 1, 1 }, 0, 127, 127 |
| 555 | }; |
| 556 | for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { |
| 557 | const SkScalar sigma = sigmas[j]; |
| 558 | SkAutoTUnref<SkMaskFilter> mf(SkEmbossMaskFilter::Create(sigma, light)); |
| 559 | if (mf.get()) { |
| 560 | SkMaskFilter::BlurRec rec; |
| 561 | bool success = mf->asABlur(&rec); |
| 562 | REPORTER_ASSERT(reporter, !success); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 569 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 570 | DEF_GPUTEST(Blur, reporter, factory) { |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 571 | test_blur_drawing(reporter); |
| 572 | test_sigma_range(reporter, factory); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 573 | test_asABlur(reporter); |
robertphillips@google.com | 3dfa4cc | 2013-09-05 16:39:03 +0000 | [diff] [blame] | 574 | } |