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