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/SkPath.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: onShortName()36 SkString onShortName() override { 37 return SkString("bigblurs"); 38 } 39 onISize()40 SkISize onISize() override { 41 return SkISize::Make(kWidth, kHeight); 42 } 43 onDraw(SkCanvas * canvas)44 void onDraw(SkCanvas* canvas) override { 45 constexpr int kBig = 65536; 46 const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)); 47 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); 55 rectori.addRect(insetRect, SkPathDirection::kCCW); 56 57 // The blur extends 3*kSigma out from the big rect. 58 // Offset the close-up windows so we get the entire blur 59 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 61 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) { 82 for (int j = 0; j <= kLastEnum_SkBlurStyle; ++j) { 83 blurPaint.setMaskFilter(SkMaskFilter::MakeBlur((SkBlurStyle)j, kSigma)); 84 85 for (int k = 0; k < (int)SK_ARRAY_COUNT(origins); ++k) { 86 canvas->save(); 87 88 SkRect clipRect = SkRect::MakeXYWH(SkIntToScalar(desiredX), 89 SkIntToScalar(desiredY), 90 SkIntToScalar(kCloseUpSize), 91 SkIntToScalar(kCloseUpSize)); 92 93 canvas->clipRect(clipRect); 94 95 canvas->translate(desiredX-origins[k].fX, 96 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 115 private: 116 static constexpr int kCloseUpSize = 64; 117 static constexpr int kWidth = 5 * kCloseUpSize; 118 static constexpr int kHeight = 2 * (kLastEnum_SkBlurStyle + 1) * kCloseUpSize; 119 120 typedef GM INHERITED; 121 }; 122 123 DEF_GM(return new BigBlursGM;) 124 } 125