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 */ 7 8 #include "SkBlurMask.h" 9 #include "SkBlurMaskFilter.h" 10 #include "SkBlurDrawLooper.h" 11 #include "SkCanvas.h" 12 #include "SkEmbossMaskFilter.h" 13 #include "SkLayerDrawLooper.h" 14 #include "SkMath.h" 15 #include "SkPaint.h" 16 #include "SkPath.h" 17 #include "Test.h" 18 19 #if SK_SUPPORT_GPU 20 #include "GrContextFactory.h" 21 #include "SkGpuDevice.h" 22 #endif 23 24 #define WRITE_CSV 0 25 26 /////////////////////////////////////////////////////////////////////////////// 27 28 #define ILLEGAL_MODE ((SkXfermode::Mode)-1) 29 30 static const int outset = 100; 31 static const SkColor bgColor = SK_ColorWHITE; 32 static const int strokeWidth = 4; 33 34 static void create(SkBitmap* bm, const SkIRect& bound) { 35 bm->allocN32Pixels(bound.width(), bound.height()); 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 102 DEF_TEST(BlurDrawing, reporter) { 103 SkPaint paint; 104 paint.setColor(SK_ColorGRAY); 105 paint.setStyle(SkPaint::kStroke_Style); 106 paint.setStrokeWidth(SkIntToScalar(strokeWidth)); 107 108 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)); 109 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) { 110 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style); 111 112 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag; 113 for (uint32_t flags = 0; flags < flagPermutations; ++flags) { 114 SkMaskFilter* filter; 115 filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags); 116 117 paint.setMaskFilter(filter); 118 filter->unref(); 119 120 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) { 121 SkPath path; 122 tests[test].addPath(&path); 123 SkPath strokedPath; 124 paint.getFillPath(path, &strokedPath); 125 SkRect refBound = strokedPath.getBounds(); 126 SkIRect iref; 127 refBound.roundOut(&iref); 128 iref.inset(-outset, -outset); 129 SkBitmap refBitmap; 130 create(&refBitmap, iref); 131 132 SkCanvas refCanvas(refBitmap); 133 refCanvas.translate(SkIntToScalar(-iref.fLeft), 134 SkIntToScalar(-iref.fTop)); 135 drawBG(&refCanvas); 136 refCanvas.drawPath(path, paint); 137 138 for (int view = 0; view < tests[test].viewLen; ++view) { 139 SkIRect itest = tests[test].views[view]; 140 SkBitmap testBitmap; 141 create(&testBitmap, itest); 142 143 SkCanvas testCanvas(testBitmap); 144 testCanvas.translate(SkIntToScalar(-itest.fLeft), 145 SkIntToScalar(-itest.fTop)); 146 drawBG(&testCanvas); 147 testCanvas.drawPath(path, paint); 148 149 REPORTER_ASSERT(reporter, 150 compare(refBitmap, iref, testBitmap, itest)); 151 } 152 } 153 } 154 } 155 } 156 157 /////////////////////////////////////////////////////////////////////////////// 158 159 // Use SkBlurMask::BlurGroundTruth to blur a 'width' x 'height' solid 160 // white rect. Return the right half of the middle row in 'result'. 161 static void ground_truth_2d(int width, int height, 162 SkScalar sigma, 163 int* result, int resultCount) { 164 SkMask src, dst; 165 166 src.fBounds.set(0, 0, width, height); 167 src.fFormat = SkMask::kA8_Format; 168 src.fRowBytes = src.fBounds.width(); 169 src.fImage = SkMask::AllocImage(src.computeTotalImageSize()); 170 171 memset(src.fImage, 0xff, src.computeTotalImageSize()); 172 173 if (!SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle)) { 174 return; 175 } 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 } 189 190 SkMask::FreeImage(src.fImage); 191 SkMask::FreeImage(dst.fImage); 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) { 204 float k = SK_Scalar1/(sigma * sqrtf(2.0f*SK_ScalarPI)); 205 float exponent = -(x * x) / (2 * sigma * sigma); 206 return k * expf(exponent); 207 } 208 209 // Perform a brute force convolution of a step function with a Gaussian. 210 // Return the right half in 'result' 211 static void brute_force_1d(SkScalar stepMin, SkScalar stepMax, 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 227 static void blur_path(SkCanvas* canvas, const SkPath& path, 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); 237 SkMaskFilter* filter = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 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; 249 readback.allocN32Pixels(resultCount, 30); 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 263 // Draw a blurred version of the provided path. 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; 269 bitmap.allocN32Pixels(resultCount, 30); 270 SkCanvas canvas(bitmap); 271 272 blur_path(&canvas, path, gaussianSigma); 273 readback(&canvas, result, resultCount); 274 } 275 276 #if SK_SUPPORT_GPU 277 #if 0 278 // temporary disable; see below for explanation 279 static bool gpu_blur_path(GrContext* context, const SkPath& path, 280 SkScalar gaussianSigma, 281 int* result, int resultCount) { 282 GrSurfaceDesc desc; 283 desc.fConfig = kSkia8888_GrPixelConfig; 284 desc.fFlags = kRenderTarget_GrSurfaceFlag; 285 desc.fWidth = resultCount; 286 desc.fHeight = 30; 287 desc.fSampleCnt = 0; 288 289 SkAutoTUnref<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0)); 290 SkAutoTUnref<SkGpuDevice> device(new SkGpuDevice (grContext, texture.get())); 291 SkCanvas canvas(device.get()); 292 293 blur_path(&canvas, path, gaussianSigma); 294 readback(&canvas, result, resultCount); 295 return true; 296 } 297 #endif 298 #endif 299 300 #if WRITE_CSV 301 static void write_as_csv(const char* label, SkScalar scale, int* data, int count) { 302 SkDebugf("%s_%.2f,", label, scale); 303 for (int i = 0; i < count-1; ++i) { 304 SkDebugf("%d,", data[i]); 305 } 306 SkDebugf("%d\n", data[count-1]); 307 } 308 #endif 309 310 static bool match(int* first, int* second, int count, int tol) { 311 int delta; 312 for (int i = 0; i < count; ++i) { 313 delta = first[i] - second[i]; 314 if (delta > tol || delta < -tol) { 315 return false; 316 } 317 } 318 319 return true; 320 } 321 322 // Test out the normal blur style with a wide range of sigmas 323 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BlurSigmaRange, reporter, context) { 324 static const int kSize = 100; 325 326 // The geometry is offset a smidge to trigger: 327 // https://code.google.com/p/chromium/issues/detail?id=282418 328 SkPath rectPath; 329 rectPath.addRect(0.3f, 0.3f, 100.3f, 100.3f); 330 331 SkPoint polyPts[] = { 332 { 0.3f, 0.3f }, 333 { 100.3f, 0.3f }, 334 { 100.3f, 100.3f }, 335 { 0.3f, 100.3f }, 336 { 2.3f, 50.3f } // a little divet to throw off the rect special case 337 }; 338 SkPath polyPath; 339 polyPath.addPoly(polyPts, SK_ARRAY_COUNT(polyPts), true); 340 341 int rectSpecialCaseResult[kSize]; 342 int generalCaseResult[kSize]; 343 int groundTruthResult[kSize]; 344 int bruteForce1DResult[kSize]; 345 346 SkScalar sigma = 10.0f; 347 348 for (int i = 0; i < 4; ++i, sigma /= 10) { 349 350 cpu_blur_path(rectPath, sigma, rectSpecialCaseResult, kSize); 351 cpu_blur_path(polyPath, sigma, generalCaseResult, kSize); 352 353 ground_truth_2d(100, 100, sigma, groundTruthResult, kSize); 354 brute_force_1d(-50.0f, 50.0f, sigma, bruteForce1DResult, kSize); 355 356 REPORTER_ASSERT(reporter, match(rectSpecialCaseResult, bruteForce1DResult, kSize, 5)); 357 REPORTER_ASSERT(reporter, match(generalCaseResult, bruteForce1DResult, kSize, 15)); 358 #if SK_SUPPORT_GPU 359 #if 0 360 int gpuResult[kSize]; 361 bool haveGPUResult = gpu_blur_path(context, rectPath, sigma, gpuResult, kSize); 362 // Disabling this test for now -- I don't think it's a legit comparison. 363 // Will continue to investigate this. 364 if (haveGPUResult) { 365 // 1 works everywhere but: Ubuntu13 & Nexus4 366 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10)); 367 } 368 #endif 369 #endif 370 REPORTER_ASSERT(reporter, match(groundTruthResult, bruteForce1DResult, kSize, 1)); 371 372 #if WRITE_CSV 373 write_as_csv("RectSpecialCase", sigma, rectSpecialCaseResult, kSize); 374 write_as_csv("GeneralCase", sigma, generalCaseResult, kSize); 375 #if SK_SUPPORT_GPU 376 write_as_csv("GPU", sigma, gpuResult, kSize); 377 #endif 378 write_as_csv("GroundTruth2D", sigma, groundTruthResult, kSize); 379 write_as_csv("BruteForce1D", sigma, bruteForce1DResult, kSize); 380 #endif 381 } 382 } 383 384 /////////////////////////////////////////////////////////////////////////////////////////// 385 386 static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) { 387 return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ? 388 kHigh_SkBlurQuality : kLow_SkBlurQuality; 389 } 390 391 static uint32_t blurMaskFilterFlags_to_blurDrawLooperFlags(uint32_t bmf) { 392 const struct { 393 uint32_t fBlurMaskFilterFlag; 394 uint32_t fBlurDrawLooperFlag; 395 } pairs[] = { 396 { SkBlurMaskFilter::kIgnoreTransform_BlurFlag, SkBlurDrawLooper::kIgnoreTransform_BlurFlag }, 397 { SkBlurMaskFilter::kHighQuality_BlurFlag, SkBlurDrawLooper::kHighQuality_BlurFlag }, 398 }; 399 400 uint32_t bdl = 0; 401 for (size_t i = 0; i < SK_ARRAY_COUNT(pairs); ++i) { 402 if (bmf & pairs[i].fBlurMaskFilterFlag) { 403 bdl |= pairs[i].fBlurDrawLooperFlag; 404 } 405 } 406 return bdl; 407 } 408 409 static void test_blurDrawLooper(skiatest::Reporter* reporter, SkScalar sigma, 410 SkBlurStyle style, uint32_t blurMaskFilterFlags) { 411 if (kNormal_SkBlurStyle != style) { 412 return; // blurdrawlooper only supports normal 413 } 414 415 const SkColor color = 0xFF335577; 416 const SkScalar dx = 10; 417 const SkScalar dy = -5; 418 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(blurMaskFilterFlags); 419 uint32_t flags = blurMaskFilterFlags_to_blurDrawLooperFlags(blurMaskFilterFlags); 420 421 SkAutoTUnref<SkDrawLooper> lp(SkBlurDrawLooper::Create(color, sigma, dx, dy, flags)); 422 423 const bool expectSuccess = sigma > 0 && 424 0 == (flags & SkBlurDrawLooper::kIgnoreTransform_BlurFlag); 425 426 if (nullptr == lp.get()) { 427 REPORTER_ASSERT(reporter, sigma <= 0); 428 } else { 429 SkDrawLooper::BlurShadowRec rec; 430 bool success = lp->asABlurShadow(&rec); 431 REPORTER_ASSERT(reporter, success == expectSuccess); 432 if (success) { 433 REPORTER_ASSERT(reporter, rec.fSigma == sigma); 434 REPORTER_ASSERT(reporter, rec.fOffset.x() == dx); 435 REPORTER_ASSERT(reporter, rec.fOffset.y() == dy); 436 REPORTER_ASSERT(reporter, rec.fColor == color); 437 REPORTER_ASSERT(reporter, rec.fStyle == style); 438 REPORTER_ASSERT(reporter, rec.fQuality == quality); 439 } 440 } 441 } 442 443 static void test_delete_looper(skiatest::Reporter* reporter, SkDrawLooper* lp, SkScalar sigma, 444 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) { 445 SkDrawLooper::BlurShadowRec rec; 446 bool success = lp->asABlurShadow(&rec); 447 REPORTER_ASSERT(reporter, success == expectSuccess); 448 if (success != expectSuccess) { 449 lp->asABlurShadow(&rec); 450 } 451 if (success) { 452 REPORTER_ASSERT(reporter, rec.fSigma == sigma); 453 REPORTER_ASSERT(reporter, rec.fStyle == style); 454 REPORTER_ASSERT(reporter, rec.fQuality == quality); 455 } 456 lp->unref(); 457 } 458 459 static void make_noop_layer(SkLayerDrawLooper::Builder* builder) { 460 SkLayerDrawLooper::LayerInfo info; 461 462 info.fPaintBits = 0; 463 info.fColorMode = SkXfermode::kDst_Mode; 464 builder->addLayer(info); 465 } 466 467 static void make_blur_layer(SkLayerDrawLooper::Builder* builder, SkMaskFilter* mf) { 468 SkLayerDrawLooper::LayerInfo info; 469 470 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit; 471 info.fColorMode = SkXfermode::kSrc_Mode; 472 SkPaint* paint = builder->addLayer(info); 473 paint->setMaskFilter(mf); 474 } 475 476 static void test_layerDrawLooper(skiatest::Reporter* reporter, SkMaskFilter* mf, SkScalar sigma, 477 SkBlurStyle style, SkBlurQuality quality, bool expectSuccess) { 478 479 SkLayerDrawLooper::LayerInfo info; 480 SkLayerDrawLooper::Builder builder; 481 482 // 1 layer is too few 483 make_noop_layer(&builder); 484 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); 485 486 // 2 layers is good, but need blur 487 make_noop_layer(&builder); 488 make_noop_layer(&builder); 489 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); 490 491 // 2 layers is just right 492 make_noop_layer(&builder); 493 make_blur_layer(&builder, mf); 494 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, expectSuccess); 495 496 // 3 layers is too many 497 make_noop_layer(&builder); 498 make_blur_layer(&builder, mf); 499 make_noop_layer(&builder); 500 test_delete_looper(reporter, builder.detachLooper(), sigma, style, quality, false); 501 } 502 503 DEF_TEST(BlurAsABlur, reporter) { 504 const SkBlurStyle styles[] = { 505 kNormal_SkBlurStyle, kSolid_SkBlurStyle, kOuter_SkBlurStyle, kInner_SkBlurStyle 506 }; 507 const SkScalar sigmas[] = { 508 // values <= 0 should not success for a blur 509 -1, 0, 0.5f, 2 510 }; 511 512 // Test asABlur for SkBlurMaskFilter 513 // 514 for (size_t i = 0; i < SK_ARRAY_COUNT(styles); ++i) { 515 const SkBlurStyle style = (SkBlurStyle)styles[i]; 516 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { 517 const SkScalar sigma = sigmas[j]; 518 for (int flags = 0; flags <= SkBlurMaskFilter::kAll_BlurFlag; ++flags) { 519 const SkBlurQuality quality = blurMaskFilterFlags_as_quality(flags); 520 521 SkAutoTUnref<SkMaskFilter> mf(SkBlurMaskFilter::Create(style, sigma, flags)); 522 if (nullptr == mf.get()) { 523 REPORTER_ASSERT(reporter, sigma <= 0); 524 } else { 525 REPORTER_ASSERT(reporter, sigma > 0); 526 SkMaskFilter::BlurRec rec; 527 bool success = mf->asABlur(&rec); 528 if (flags & SkBlurMaskFilter::kIgnoreTransform_BlurFlag) { 529 REPORTER_ASSERT(reporter, !success); 530 } else { 531 REPORTER_ASSERT(reporter, success); 532 REPORTER_ASSERT(reporter, rec.fSigma == sigma); 533 REPORTER_ASSERT(reporter, rec.fStyle == style); 534 REPORTER_ASSERT(reporter, rec.fQuality == quality); 535 } 536 test_layerDrawLooper(reporter, mf, sigma, style, quality, success); 537 } 538 test_blurDrawLooper(reporter, sigma, style, flags); 539 } 540 } 541 } 542 543 // Test asABlur for SkEmbossMaskFilter -- should never succeed 544 // 545 { 546 SkEmbossMaskFilter::Light light = { 547 { 1, 1, 1 }, 0, 127, 127 548 }; 549 for (size_t j = 0; j < SK_ARRAY_COUNT(sigmas); ++j) { 550 const SkScalar sigma = sigmas[j]; 551 SkAutoTUnref<SkMaskFilter> mf(SkEmbossMaskFilter::Create(sigma, light)); 552 if (mf.get()) { 553 SkMaskFilter::BlurRec rec; 554 bool success = mf->asABlur(&rec); 555 REPORTER_ASSERT(reporter, !success); 556 } 557 } 558 } 559 } 560 561 #if SK_SUPPORT_GPU 562 563 // This exercises the problem discovered in crbug.com/570232. The return value from 564 // SkBlurMask::BoxBlur wasn't being checked in SkBlurMaskFilter.cpp::GrRRectBlurEffect::Create 565 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SmallBoxBlurBug, reporter, ctx) { 566 567 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128); 568 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkBudgeted::kNo, info)); 569 SkCanvas* canvas = surface->getCanvas(); 570 571 SkRect r = SkRect::MakeXYWH(10, 10, 100, 100); 572 SkRRect rr = SkRRect::MakeRectXY(r, 10, 10); 573 574 SkPaint p; 575 p.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 0.01f))->unref(); 576 577 canvas->drawRRect(rr, p); 578 } 579 580 #endif 581 582 /////////////////////////////////////////////////////////////////////////////////////////// 583