blob: 6436c85322e31e60da169049ed392b6c1658399f [file] [log] [blame]
robertphillips@google.com96ac2f62013-11-07 22:25:21 +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"
10#include "include/core/SkCanvas.h"
11#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkPath.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkBlurMask.h"
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000022
23namespace skiagm {
24
25// This GM exercises the blurred rect nine-patching special cases when the
26// blurred rect is very large and/or very far from the origin.
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000027// It creates a large blurred rect/rectori then renders the 4 corners and the
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000028// middle.
29class BigBlursGM : public GM {
30public:
31 BigBlursGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040032 this->setBGColor(0xFFDDDDDD);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000033 }
34
35protected:
mtklein36352bf2015-03-25 18:17:31 -070036 SkString onShortName() override {
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000037 return SkString("bigblurs");
38 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070041 return SkISize::Make(kWidth, kHeight);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000042 }
43
mtklein36352bf2015-03-25 18:17:31 -070044 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070045 constexpr int kBig = 65536;
46 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000047
48 const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig));
49 SkRect insetRect = bigRect;
50 insetRect.inset(20, 20);
51
52 SkPath rectori;
53
54 rectori.addRect(bigRect);
Mike Reed30bc5272019-11-22 18:34:02 +000055 rectori.addRect(insetRect, SkPathDirection::kCCW);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000056
57 // The blur extends 3*kSigma out from the big rect.
58 // Offset the close-up windows so we get the entire blur
mtkleindbfd7ab2016-09-01 11:24:54 -070059 const SkScalar kLeftTopPad = 3*kSigma; // use on left & up of big rect
60 const SkScalar kRightBotPad = kCloseUpSize-3*kSigma; // use on right and bot sides
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000061
62 // UL hand corners of the rendered closeups
63 const SkPoint origins[] = {
64 { -kLeftTopPad, -kLeftTopPad }, // UL
65 { kBig-kRightBotPad, -kLeftTopPad }, // UR
66 { kBig-kRightBotPad, kBig-kRightBotPad }, // LR
67 { -kLeftTopPad, kBig-kRightBotPad }, // LL
68 { kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center
69 };
70
71 SkPaint outlinePaint;
72 outlinePaint.setColor(SK_ColorRED);
73 outlinePaint.setStyle(SkPaint::kStroke_Style);
74
75 SkPaint blurPaint;
76 blurPaint.setAntiAlias(true);
77 blurPaint.setColor(SK_ColorBLACK);
78
79 int desiredX = 0, desiredY = 0;
80
81 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000082 for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040083 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur((SkBlurStyle)j, kSigma));
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000084
85 for (int k = 0; k < (int)SK_ARRAY_COUNT(origins); ++k) {
86 canvas->save();
87
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000088 SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX),
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000089 SkIntToScalar(desiredY),
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000090 SkIntToScalar(kCloseUpSize),
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000091 SkIntToScalar(kCloseUpSize));
92
reed66998382016-09-21 11:15:07 -070093 canvas->clipRect(clipRect);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000094
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000095 canvas->translate(desiredX-origins[k].fX,
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000096 desiredY-origins[k].fY);
97
98 if (0 == i) {
99 canvas->drawRect(bigRect, blurPaint);
100 } else {
101 canvas->drawPath(rectori, blurPaint);
102 }
103 canvas->restore();
104 canvas->drawRect(clipRect, outlinePaint);
105
106 desiredX += kCloseUpSize;
107 }
108
109 desiredX = 0;
110 desiredY += kCloseUpSize;
111 }
112 }
113 }
114
115private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700116 static constexpr int kCloseUpSize = 64;
117 static constexpr int kWidth = 5 * kCloseUpSize;
118 static constexpr int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize;
robertphillips@google.com96ac2f62013-11-07 22:25:21 +0000119
120 typedef GM INHERITED;
121};
122
halcanary385fe4d2015-08-26 13:07:48 -0700123DEF_GM(return new BigBlursGM;)
robertphillips@google.comf3db5462013-11-07 22:43:04 +0000124}