blob: 8b8392096f0d41eb6f33fbd08a0225517f6a1a6c [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:
mtklein36352bf2015-03-25 18:17:31 -070033 SkString onShortName() override {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000034 return SkString("blurquickreject");
35 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 SkISize onISize() override {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000038 return SkISize::Make(kWidth, kHeight);
39 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070042 constexpr SkScalar kBlurRadius = SkIntToScalar(20);
43 constexpr SkScalar kBoxSize = SkIntToScalar(100);
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000044
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;
Mike Reed1be1f8d2018-03-14 13:01:17 -040066 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070067 SkBlurMask::ConvertRadiusToSigma(kBlurRadius)));
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000068
69 canvas->clear(SK_ColorBLACK);
70 canvas->save();
71 canvas->translate(kBoxSize, kBoxSize);
72 canvas->drawRect(clipRect, hairlinePaint);
73 canvas->clipRect(clipRect);
robertphillips@google.com073a32e2013-07-30 12:29:20 +000074 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRects); ++i) {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000075 blurPaint.setColor(colors[i]);
76 canvas->drawRect(blurRects[i], blurPaint);
77 canvas->drawRect(blurRects[i], hairlinePaint);
78 }
79 canvas->restore();
80 }
81
82private:
mtkleindbfd7ab2016-09-01 11:24:54 -070083 static constexpr int kWidth = 300;
84 static constexpr int kHeight = 300;
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000085
John Stiles7571f9e2020-09-02 22:42:33 -040086 using INHERITED = GM;
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000087};
88
89DEF_GM( return new BlurQuickRejectGM(); )