blob: 0a753ddc06a594b60cd86b04699e28d2c0e9a0cc [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
8#include "gm.h"
9#include "SkBlurMask.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040010#include "SkMaskFilter.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000012
13namespace skiagm {
14
15// This GM exercises the blurred rect nine-patching special cases when the
16// blurred rect is very large and/or very far from the origin.
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000017// It creates a large blurred rect/rectori then renders the 4 corners and the
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000018// middle.
19class BigBlursGM : public GM {
20public:
21 BigBlursGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040022 this->setBGColor(0xFFDDDDDD);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000023 }
24
25protected:
mtklein36352bf2015-03-25 18:17:31 -070026 SkString onShortName() override {
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000027 return SkString("bigblurs");
28 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070031 return SkISize::Make(kWidth, kHeight);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000032 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070035 constexpr int kBig = 65536;
36 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000037
38 const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig));
39 SkRect insetRect = bigRect;
40 insetRect.inset(20, 20);
41
42 SkPath rectori;
43
44 rectori.addRect(bigRect);
45 rectori.addRect(insetRect, SkPath::kCCW_Direction);
46
47 // The blur extends 3*kSigma out from the big rect.
48 // Offset the close-up windows so we get the entire blur
mtkleindbfd7ab2016-09-01 11:24:54 -070049 const SkScalar kLeftTopPad = 3*kSigma; // use on left & up of big rect
50 const SkScalar kRightBotPad = kCloseUpSize-3*kSigma; // use on right and bot sides
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000051
52 // UL hand corners of the rendered closeups
53 const SkPoint origins[] = {
54 { -kLeftTopPad, -kLeftTopPad }, // UL
55 { kBig-kRightBotPad, -kLeftTopPad }, // UR
56 { kBig-kRightBotPad, kBig-kRightBotPad }, // LR
57 { -kLeftTopPad, kBig-kRightBotPad }, // LL
58 { kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center
59 };
60
61 SkPaint outlinePaint;
62 outlinePaint.setColor(SK_ColorRED);
63 outlinePaint.setStyle(SkPaint::kStroke_Style);
64
65 SkPaint blurPaint;
66 blurPaint.setAntiAlias(true);
67 blurPaint.setColor(SK_ColorBLACK);
68
69 int desiredX = 0, desiredY = 0;
70
71 for (int i = 0; i < 2; ++i) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000072 for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040073 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur((SkBlurStyle)j, kSigma));
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000074
75 for (int k = 0; k < (int)SK_ARRAY_COUNT(origins); ++k) {
76 canvas->save();
77
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000078 SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX),
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000079 SkIntToScalar(desiredY),
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000080 SkIntToScalar(kCloseUpSize),
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000081 SkIntToScalar(kCloseUpSize));
82
reed66998382016-09-21 11:15:07 -070083 canvas->clipRect(clipRect);
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000084
skia.committer@gmail.comab7442c2013-11-08 07:01:56 +000085 canvas->translate(desiredX-origins[k].fX,
robertphillips@google.com96ac2f62013-11-07 22:25:21 +000086 desiredY-origins[k].fY);
87
88 if (0 == i) {
89 canvas->drawRect(bigRect, blurPaint);
90 } else {
91 canvas->drawPath(rectori, blurPaint);
92 }
93 canvas->restore();
94 canvas->drawRect(clipRect, outlinePaint);
95
96 desiredX += kCloseUpSize;
97 }
98
99 desiredX = 0;
100 desiredY += kCloseUpSize;
101 }
102 }
103 }
104
105private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700106 static constexpr int kCloseUpSize = 64;
107 static constexpr int kWidth = 5 * kCloseUpSize;
108 static constexpr int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize;
robertphillips@google.com96ac2f62013-11-07 22:25:21 +0000109
110 typedef GM INHERITED;
111};
112
halcanary385fe4d2015-08-26 13:07:48 -0700113DEF_GM(return new BigBlursGM;)
robertphillips@google.comf3db5462013-11-07 22:43:04 +0000114}