xref: /aosp_15_r20/external/skia/gm/bigblurs.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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/gm.h"
9 #include "include/core/SkBlurTypes.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMaskFilter.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPathBuilder.h"
15 #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"
21 #include "src/core/SkBlurMask.h"
22 
23 namespace 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.
27 // It creates a large blurred rect/rectori then renders the 4 corners and the
28 // middle.
29 class BigBlursGM : public GM {
30 public:
BigBlursGM()31     BigBlursGM() {
32         this->setBGColor(0xFFDDDDDD);
33     }
34 
35 protected:
getName() const36     SkString getName() const override { return SkString("bigblurs"); }
37 
getISize()38     SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
39 
onDraw(SkCanvas * canvas)40     void onDraw(SkCanvas* canvas) override {
41         constexpr int kBig = 65536;
42         const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4));
43 
44         const SkRect bigRect = SkRect::MakeWH(SkIntToScalar(kBig), SkIntToScalar(kBig));
45         SkRect insetRect = bigRect;
46         insetRect.inset(20, 20);
47 
48         SkPath rectori = SkPathBuilder().addRect(bigRect)
49                                         .addRect(insetRect, SkPathDirection::kCCW)
50                                         .detach();
51 
52         // The blur extends 3*kSigma out from the big rect.
53         // Offset the close-up windows so we get the entire blur
54         const SkScalar kLeftTopPad  = 3*kSigma;   // use on left & up of big rect
55         const SkScalar kRightBotPad = kCloseUpSize-3*kSigma; // use on right and bot sides
56 
57         // UL hand corners of the rendered closeups
58         const SkPoint origins[] = {
59             { -kLeftTopPad,          -kLeftTopPad           }, // UL
60             {  kBig-kRightBotPad,    -kLeftTopPad           }, // UR
61             {  kBig-kRightBotPad,     kBig-kRightBotPad     }, // LR
62             { -kLeftTopPad,           kBig-kRightBotPad     }, // LL
63             {  kBig/2-kCloseUpSize/2, kBig/2-kCloseUpSize/2 }, // center
64         };
65 
66         SkPaint outlinePaint;
67         outlinePaint.setColor(SK_ColorRED);
68         outlinePaint.setStyle(SkPaint::kStroke_Style);
69 
70         SkPaint blurPaint;
71         blurPaint.setAntiAlias(true);
72         blurPaint.setColor(SK_ColorBLACK);
73 
74         int desiredX = 0, desiredY = 0;
75 
76         for (int i = 0; i < 2; ++i) {
77             for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) {
78                 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur((SkBlurStyle)j, kSigma));
79 
80                 for (int k = 0; k < (int)std::size(origins); ++k) {
81                     canvas->save();
82 
83                     SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX),
84                                                        SkIntToScalar(desiredY),
85                                                        SkIntToScalar(kCloseUpSize),
86                                                        SkIntToScalar(kCloseUpSize));
87 
88                     canvas->clipRect(clipRect);
89 
90                     canvas->translate(desiredX-origins[k].fX,
91                                       desiredY-origins[k].fY);
92 
93                     if (0 == i) {
94                         canvas->drawRect(bigRect, blurPaint);
95                     } else {
96                         canvas->drawPath(rectori, blurPaint);
97                     }
98                     canvas->restore();
99                     canvas->drawRect(clipRect, outlinePaint);
100 
101                     desiredX += kCloseUpSize;
102                 }
103 
104                 desiredX = 0;
105                 desiredY += kCloseUpSize;
106             }
107         }
108     }
109 
110 private:
111     inline static constexpr int kCloseUpSize = 64;
112     inline static constexpr int kWidth = 5 * kCloseUpSize;
113     inline static constexpr int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize;
114 
115     using INHERITED = GM;
116 };
117 
118 DEF_GM(return new BigBlursGM;)
119 }  // namespace skiagm
120