blob: 7c74fdf38053fba8c1139a101d04c5b17231458a [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
8#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkCanvas.h"
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000012
13// This GM tests out the quick reject bounds of the blur mask filter. It draws
14// four blurred rects around a central clip. The blurred rect geometry outset
15// by the blur radius does not overlap the clip rect so, if the blur clipping
16// just uses the radius, they will be clipped out (and the result will differ
17// from the result if quick reject were disabled. If the blur clipping uses
18// the correct 3 sigma bound then the images with and without quick rejecting
19// will be the same.
20class BlurQuickRejectGM : public skiagm::GM {
21public:
22 BlurQuickRejectGM() {}
23
24protected:
mtklein72c9faa2015-01-09 10:06:39 -080025 SkString onShortName() SK_OVERRIDE {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000026 return SkString("blurquickreject");
27 }
28
mtklein72c9faa2015-01-09 10:06:39 -080029 SkISize onISize() SK_OVERRIDE {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000030 return SkISize::Make(kWidth, kHeight);
31 }
32
mtklein72c9faa2015-01-09 10:06:39 -080033 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000034 static const SkScalar kBlurRadius = SkIntToScalar(20);
35 static const SkScalar kBoxSize = SkIntToScalar(100);
36
37 SkRect clipRect = SkRect::MakeXYWH(0, 0, kBoxSize, kBoxSize);
38 SkRect blurRects[] = {
39 { -kBoxSize - (kBlurRadius+1), 0, -(kBlurRadius+1), kBoxSize },
40 { 0, -kBoxSize - (kBlurRadius+1), kBoxSize, -(kBlurRadius+1) },
41 { kBoxSize+kBlurRadius+1, 0, 2*kBoxSize+kBlurRadius+1, kBoxSize },
42 { 0, kBoxSize+kBlurRadius+1, kBoxSize, 2*kBoxSize+kBlurRadius+1 }
43 };
44 SkColor colors[] = {
45 SK_ColorRED,
46 SK_ColorGREEN,
47 SK_ColorBLUE,
48 SK_ColorYELLOW,
49 };
50 SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(blurRects));
51
52 SkPaint hairlinePaint;
53 hairlinePaint.setStyle(SkPaint::kStroke_Style);
54 hairlinePaint.setColor(SK_ColorWHITE);
55 hairlinePaint.setStrokeWidth(0);
56
57 SkPaint blurPaint;
reed@google.com44699382013-10-31 17:28:30 +000058 blurPaint.setFilterLevel(SkPaint::kLow_FilterLevel);
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000059 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000060 SkBlurMask::ConvertRadiusToSigma(kBlurRadius));
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000061 blurPaint.setMaskFilter(mf)->unref();
62
63 canvas->clear(SK_ColorBLACK);
64 canvas->save();
65 canvas->translate(kBoxSize, kBoxSize);
66 canvas->drawRect(clipRect, hairlinePaint);
67 canvas->clipRect(clipRect);
robertphillips@google.com073a32e2013-07-30 12:29:20 +000068 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRects); ++i) {
robertphillips@google.com17ad2bd2013-07-30 12:15:19 +000069 blurPaint.setColor(colors[i]);
70 canvas->drawRect(blurRects[i], blurPaint);
71 canvas->drawRect(blurRects[i], hairlinePaint);
72 }
73 canvas->restore();
74 }
75
76private:
77 static const int kWidth = 300;
78 static const int kHeight = 300;
79
80 typedef GM INHERITED;
81};
82
83DEF_GM( return new BlurQuickRejectGM(); )