blob: 45b6478a84ca39c5cb82bff8110c6e2a2a5e964d [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"
12#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkMaskFilter.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040014#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 Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkBlurMask.h"
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000021
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.
29class BlurQuickRejectGM : public skiagm::GM {
30public:
31 BlurQuickRejectGM() {}
32
33protected:
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000035 return SkString("blurquickreject");
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000039 return SkISize::Make(kWidth, kHeight);
40 }
41
mtklein36352bf2015-03-25 18:17:31 -070042 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070043 constexpr SkScalar kBlurRadius = SkIntToScalar(20);
44 constexpr SkScalar kBoxSize = SkIntToScalar(100);
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000045
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;
reed93a12152015-03-16 10:08:34 -070067 blurPaint.setFilterQuality(kLow_SkFilterQuality);
Mike Reed1be1f8d2018-03-14 13:01:17 -040068 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070069 SkBlurMask::ConvertRadiusToSigma(kBlurRadius)));
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000070
71 canvas->clear(SK_ColorBLACK);
72 canvas->save();
73 canvas->translate(kBoxSize, kBoxSize);
74 canvas->drawRect(clipRect, hairlinePaint);
75 canvas->clipRect(clipRect);
robertphillips@google.com073a32e2013-07-30 12:29:20 +000076 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRects); ++i) {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000077 blurPaint.setColor(colors[i]);
78 canvas->drawRect(blurRects[i], blurPaint);
79 canvas->drawRect(blurRects[i], hairlinePaint);
80 }
81 canvas->restore();
82 }
83
84private:
mtkleindbfd7ab2016-09-01 11:24:54 -070085 static constexpr int kWidth = 300;
86 static constexpr int kHeight = 300;
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000087
88 typedef GM INHERITED;
89};
90
91DEF_GM( return new BlurQuickRejectGM(); )