blob: 410d02d170cb440859361d3fddf50e7858a452ab [file] [log] [blame]
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040011#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040013#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 Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkBlurMask.h"
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000020
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.
28class BlurQuickRejectGM : public skiagm::GM {
29public:
30 BlurQuickRejectGM() {}
31
32protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000033 SkString getName() const override { return SkString("blurquickreject"); }
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000034
Leandro Lovisolo8f023882023-08-15 21:13:52 +000035 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000036
mtklein36352bf2015-03-25 18:17:31 -070037 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070038 constexpr SkScalar kBlurRadius = SkIntToScalar(20);
39 constexpr SkScalar kBoxSize = SkIntToScalar(100);
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000040
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 Derbyc37b3862022-06-21 09:49:17 -040054 SkASSERT(std::size(colors) == std::size(blurRects));
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000055
56 SkPaint hairlinePaint;
57 hairlinePaint.setStyle(SkPaint::kStroke_Style);
58 hairlinePaint.setColor(SK_ColorWHITE);
59 hairlinePaint.setStrokeWidth(0);
60
61 SkPaint blurPaint;
Mike Reed1be1f8d2018-03-14 13:01:17 -040062 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070063 SkBlurMask::ConvertRadiusToSigma(kBlurRadius)));
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000064
65 canvas->clear(SK_ColorBLACK);
66 canvas->save();
67 canvas->translate(kBoxSize, kBoxSize);
68 canvas->drawRect(clipRect, hairlinePaint);
69 canvas->clipRect(clipRect);
Herb Derbyc37b3862022-06-21 09:49:17 -040070 for (size_t i = 0; i < std::size(blurRects); ++i) {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000071 blurPaint.setColor(colors[i]);
72 canvas->drawRect(blurRects[i], blurPaint);
73 canvas->drawRect(blurRects[i], hairlinePaint);
74 }
75 canvas->restore();
76 }
77
78private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -040079 inline static constexpr int kWidth = 300;
80 inline static constexpr int kHeight = 300;
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000081
John Stiles7571f9e2020-09-02 22:42:33 -040082 using INHERITED = GM;
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000083};
84
85DEF_GM( return new BlurQuickRejectGM(); )