BlurTest.cpp revision 96fcdcc219d2a0d3579719b84b28bede76efba64
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 30static const int outset = 100; 31static const SkColor bgColor = SK_ColorWHITE; 32static const int strokeWidth = 4; 33 34static void create(SkBitmap* bm, const SkIRect& bound) { 35 bm->allocN32Pixels(bound.width(), bound.height()); 36} 37 38static void drawBG(SkCanvas* canvas) { 39 canvas->drawColor(bgColor); 40} 41 42 43struct 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. 51static void draw50x50Rect(SkPath* path) { 52 path->addRect(0, 0, SkIntToScalar(50), SkIntToScalar(50)); 53} 54 55//Tests 56static 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 */ 72static 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 102static void test_blur_drawing(skiatest::Reporter* reporter) { 103 104 SkPaint paint; 105 paint.setColor(SK_ColorGRAY); 106 paint.setStyle(SkPaint::kStroke_Style); 107 paint.setStrokeWidth(SkIntToScalar(strokeWidth)); 108 109 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)); 110 for (int style = 0; style <= kLastEnum_SkBlurStyle; ++style) { 111 SkBlurStyle blurStyle = static_cast<SkBlurStyle>(style); 112 113 const uint32_t flagPermutations = SkBlurMaskFilter::kAll_BlurFlag; 114 for (uint32_t flags = 0; flags < flagPermutations; ++flags) { 115 SkMaskFilter* filter; 116 filter = SkBlurMaskFilter::Create(blurStyle, sigma, flags); 117 118 paint.setMaskFilter(filter); 119 filter->unref(); 120 121 for (size_t test = 0; test < SK_ARRAY_COUNT(tests); ++test) { 122 SkPath path; 123 tests[test].addPath(&path); 124 SkPath strokedPath; 125 paint.getFillPath(path, &strokedPath); 126 SkRect refBound = strokedPath.getBounds(); 127 SkIRect iref; 128 refBound.roundOut(&iref); 129 iref.inset(-outset, -outset); 130 SkBitmap refBitmap; 131 create(&refBitmap, iref); 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; 142 create(&testBitmap, itest); 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 } 155 } 156} 157 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'. 162static void ground_truth_2d(int width, int height, 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 174 dst.fImage = nullptr; 175 SkBlurMask::BlurGroundTruth(sigma, &dst, src, kNormal_SkBlurStyle); 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. 195static 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'. 203static 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' 211static 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 227static 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 247static 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'. 265static 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 279static bool gpu_blur_path(GrContextFactory* factory, const SkPath& path, 280 SkScalar gaussianSigma, 281 int* result, int resultCount) { 282 283 GrContext* grContext = factory->get(GrContextFactory::kNative_GLContextType); 284 if (nullptr == grContext) { 285 return false; 286 } 287 288 GrSurfaceDesc desc; 289 desc.fConfig = kSkia8888_GrPixelConfig; 290 desc.fFlags = kRenderTarget_GrSurfaceFlag; 291 desc.fWidth = resultCount; 292 desc.fHeight = 30; 293 desc.fSampleCnt = 0; 294 295 SkAutoTUnref<GrTexture> texture(grContext->createTexture(desc, false, nullptr, 0)); 296 SkAutoTUnref<SkGpuDevice> device(new SkGpuDevice (grContext, texture.get())); 297 SkCanvas canvas(device.get()); 298 299 blur_path(&canvas, path, gaussianSigma); 300 readback(&canvas, result, resultCount); 301 return true; 302} 303#endif 304#endif 305 306#if WRITE_CSV 307static 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 316static 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 329static 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]; 350 int groundTruthResult[kSize]; 351 int bruteForce1DResult[kSize]; 352 353 SkScalar sigma = 10.0f; 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); 359 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 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. 371 if (haveGPUResult) { 372 // 1 works everywhere but: Ubuntu13 & Nexus4 373 REPORTER_ASSERT(reporter, match(gpuResult, bruteForce1DResult, kSize, 10)); 374 } 375#endif 376#endif 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 391/////////////////////////////////////////////////////////////////////////////////////////// 392 393static SkBlurQuality blurMaskFilterFlags_as_quality(uint32_t blurMaskFilterFlags) { 394 return (blurMaskFilterFlags & SkBlurMaskFilter::kHighQuality_BlurFlag) ? 395 kHigh_SkBlurQuality : kLow_SkBlurQuality; 396} 397 398static 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 416static 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 433 if (nullptr == lp.get()) { 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 450static 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 466static 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 474static 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 483static 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 510static 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)); 529 if (nullptr == mf.get()) { 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 570DEF_GPUTEST(Blur, reporter, factory) { 571 test_blur_drawing(reporter); 572 test_sigma_range(reporter, factory); 573 test_asABlur(reporter); 574} 575