robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/core/SkBlurMask.h" |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 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: |
| 31 | BlurQuickRejectGM() {} |
| 32 | |
| 33 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkString onShortName() override { |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 35 | return SkString("blurquickreject"); |
| 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | SkISize onISize() override { |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 39 | return SkISize::Make(kWidth, kHeight); |
| 40 | } |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 43 | constexpr SkScalar kBlurRadius = SkIntToScalar(20); |
| 44 | constexpr SkScalar kBoxSize = SkIntToScalar(100); |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 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; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 67 | blurPaint.setFilterQuality(kLow_SkFilterQuality); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 68 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 69 | SkBlurMask::ConvertRadiusToSigma(kBlurRadius))); |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 70 | |
| 71 | canvas->clear(SK_ColorBLACK); |
| 72 | canvas->save(); |
| 73 | canvas->translate(kBoxSize, kBoxSize); |
| 74 | canvas->drawRect(clipRect, hairlinePaint); |
| 75 | canvas->clipRect(clipRect); |
robertphillips@google.com | 073a32e | 2013-07-30 12:29:20 +0000 | [diff] [blame] | 76 | for (size_t i = 0; i < SK_ARRAY_COUNT(blurRects); ++i) { |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 77 | blurPaint.setColor(colors[i]); |
| 78 | canvas->drawRect(blurRects[i], blurPaint); |
| 79 | canvas->drawRect(blurRects[i], hairlinePaint); |
| 80 | } |
| 81 | canvas->restore(); |
| 82 | } |
| 83 | |
| 84 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 85 | static constexpr int kWidth = 300; |
| 86 | static constexpr int kHeight = 300; |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 87 | |
| 88 | typedef GM INHERITED; |
| 89 | }; |
| 90 | |
| 91 | DEF_GM( return new BlurQuickRejectGM(); ) |