1 /* 2 * Copyright 2013 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 "gm/gm.h" 9 #include "include/core/SkBlurTypes.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkFilterQuality.h" 13 #include "include/core/SkMaskFilter.h" 14 #include "include/core/SkPaint.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkSize.h" 18 #include "include/core/SkString.h" 19 #include "include/core/SkTypes.h" 20 #include "src/core/SkBlurMask.h" 21 22 // This GM tests out the quick reject bounds of the blur mask filter. It draws 23 // four blurred rects around a central clip. The blurred rect geometry outset 24 // by the blur radius does not overlap the clip rect so, if the blur clipping 25 // just uses the radius, they will be clipped out (and the result will differ 26 // from the result if quick reject were disabled. If the blur clipping uses 27 // the correct 3 sigma bound then the images with and without quick rejecting 28 // will be the same. 29 class BlurQuickRejectGM : public skiagm::GM { 30 public: BlurQuickRejectGM()31 BlurQuickRejectGM() {} 32 33 protected: onShortName()34 SkString onShortName() override { 35 return SkString("blurquickreject"); 36 } 37 onISize()38 SkISize onISize() override { 39 return SkISize::Make(kWidth, kHeight); 40 } 41 onDraw(SkCanvas * canvas)42 void onDraw(SkCanvas* canvas) override { 43 constexpr SkScalar kBlurRadius = SkIntToScalar(20); 44 constexpr SkScalar kBoxSize = SkIntToScalar(100); 45 46 SkRect clipRect = SkRect::MakeXYWH(0, 0, kBoxSize, kBoxSize); 47 SkRect blurRects[] = { 48 { -kBoxSize - (kBlurRadius+1), 0, -(kBlurRadius+1), kBoxSize }, 49 { 0, -kBoxSize - (kBlurRadius+1), kBoxSize, -(kBlurRadius+1) }, 50 { kBoxSize+kBlurRadius+1, 0, 2*kBoxSize+kBlurRadius+1, kBoxSize }, 51 { 0, kBoxSize+kBlurRadius+1, kBoxSize, 2*kBoxSize+kBlurRadius+1 } 52 }; 53 SkColor colors[] = { 54 SK_ColorRED, 55 SK_ColorGREEN, 56 SK_ColorBLUE, 57 SK_ColorYELLOW, 58 }; 59 SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(blurRects)); 60 61 SkPaint hairlinePaint; 62 hairlinePaint.setStyle(SkPaint::kStroke_Style); 63 hairlinePaint.setColor(SK_ColorWHITE); 64 hairlinePaint.setStrokeWidth(0); 65 66 SkPaint blurPaint; 67 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 68 SkBlurMask::ConvertRadiusToSigma(kBlurRadius))); 69 70 canvas->clear(SK_ColorBLACK); 71 canvas->save(); 72 canvas->translate(kBoxSize, kBoxSize); 73 canvas->drawRect(clipRect, hairlinePaint); 74 canvas->clipRect(clipRect); 75 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRects); ++i) { 76 blurPaint.setColor(colors[i]); 77 canvas->drawRect(blurRects[i], blurPaint); 78 canvas->drawRect(blurRects[i], hairlinePaint); 79 } 80 canvas->restore(); 81 } 82 83 private: 84 static constexpr int kWidth = 300; 85 static constexpr int kHeight = 300; 86 87 using INHERITED = GM; 88 }; 89 90 DEF_GM( return new BlurQuickRejectGM(); ) 91