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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkBlurMask.h" |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 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: |
| 30 | BlurQuickRejectGM() {} |
| 31 | |
| 32 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 33 | SkString getName() const override { return SkString("blurquickreject"); } |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 34 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 35 | SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 38 | constexpr SkScalar kBlurRadius = SkIntToScalar(20); |
| 39 | constexpr SkScalar kBoxSize = SkIntToScalar(100); |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 40 | |
| 41 | SkRect clipRect = SkRect::MakeXYWH(0, 0, kBoxSize, kBoxSize); |
| 42 | SkRect blurRects[] = { |
| 43 | { -kBoxSize - (kBlurRadius+1), 0, -(kBlurRadius+1), kBoxSize }, |
| 44 | { 0, -kBoxSize - (kBlurRadius+1), kBoxSize, -(kBlurRadius+1) }, |
| 45 | { kBoxSize+kBlurRadius+1, 0, 2*kBoxSize+kBlurRadius+1, kBoxSize }, |
| 46 | { 0, kBoxSize+kBlurRadius+1, kBoxSize, 2*kBoxSize+kBlurRadius+1 } |
| 47 | }; |
| 48 | SkColor colors[] = { |
| 49 | SK_ColorRED, |
| 50 | SK_ColorGREEN, |
| 51 | SK_ColorBLUE, |
| 52 | SK_ColorYELLOW, |
| 53 | }; |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 54 | SkASSERT(std::size(colors) == std::size(blurRects)); |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 55 | |
| 56 | SkPaint hairlinePaint; |
| 57 | hairlinePaint.setStyle(SkPaint::kStroke_Style); |
| 58 | hairlinePaint.setColor(SK_ColorWHITE); |
| 59 | hairlinePaint.setStrokeWidth(0); |
| 60 | |
| 61 | SkPaint blurPaint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 62 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 63 | SkBlurMask::ConvertRadiusToSigma(kBlurRadius))); |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 64 | |
| 65 | canvas->clear(SK_ColorBLACK); |
| 66 | canvas->save(); |
| 67 | canvas->translate(kBoxSize, kBoxSize); |
| 68 | canvas->drawRect(clipRect, hairlinePaint); |
| 69 | canvas->clipRect(clipRect); |
Herb Derby | c37b386 | 2022-06-21 09:49:17 -0400 | [diff] [blame] | 70 | for (size_t i = 0; i < std::size(blurRects); ++i) { |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 71 | blurPaint.setColor(colors[i]); |
| 72 | canvas->drawRect(blurRects[i], blurPaint); |
| 73 | canvas->drawRect(blurRects[i], hairlinePaint); |
| 74 | } |
| 75 | canvas->restore(); |
| 76 | } |
| 77 | |
| 78 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 79 | inline static constexpr int kWidth = 300; |
| 80 | inline static constexpr int kHeight = 300; |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 81 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 82 | using INHERITED = GM; |
robertphillips@google.com | 17ad2bd | 2013-07-30 12:15:19 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | DEF_GM( return new BlurQuickRejectGM(); ) |