OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkBlurMask.h" | 9 #include "SkBlurMask.h" |
10 #include "SkBlurMaskFilter.h" | 10 #include "SkBlurMaskFilter.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkColorFilter.h" | 12 #include "SkColorFilter.h" |
13 #include "SkLayerDrawLooper.h" | 13 #include "SkLayerDrawLooper.h" |
14 #include "SkPaint.h" | 14 #include "SkPaint.h" |
15 #include "SkPath.h" | 15 #include "SkPath.h" |
16 #include "SkPoint.h" | 16 #include "SkPoint.h" |
17 #include "SkRect.h" | 17 #include "SkRect.h" |
18 #include "SkRRect.h" | 18 #include "SkRRect.h" |
19 #include "SkString.h" | 19 #include "SkString.h" |
20 #include "SkXfermode.h" | 20 #include "SkXfermode.h" |
21 | 21 |
| 22 // This GM mimics a blurred RR seen in the wild. |
22 class BlurRoundRectGM : public skiagm::GM { | 23 class BlurRoundRectGM : public skiagm::GM { |
23 public: | 24 public: |
24 BlurRoundRectGM(int width, int height, | 25 BlurRoundRectGM(int width, int height, int radius) |
25 // X and Y radii for the upper left corner | |
26 int ulX, int ulY, | |
27 // X and Y radii for the upper right corner | |
28 int urX, int urY, | |
29 // X and Y radii for the lower right corner | |
30 int lrX, int lrY, | |
31 // X and Y radii for the lower left corner | |
32 int llX, int llY, | |
33 int scaleX, int scaleY) | |
34 : fName("blurroundrect") | 26 : fName("blurroundrect") |
35 , fWidth(width) | 27 { |
36 , fHeight(height) | 28 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
37 , fScaleX(SkIntToScalar(scaleX)) | 29 fRRect.setRectXY(r, SkIntToScalar(radius), SkIntToScalar(radius)); |
38 , fScaleY(SkIntToScalar(scaleY)) { | 30 fName.appendf("-WH[%ix%i]-corner[%i]", width, height, radius); |
39 fName.appendf("-WH[%ix%i]-UL[%ix%i]-UR[%ix%i]-LR[%ix%i]-LL[%ix%i]-scale[
%ix%i]", | 31 } |
40 width, height, | 32 |
41 ulX, ulY, | 33 BlurRoundRectGM(int width, int height) |
42 urX, urY, | 34 : fName("blurroundrect") { |
43 lrX, lrY, | 35 fName.appendf("-WH[%ix%i]-unevenCorners", |
44 llX, llY, | 36 width, height); |
45 scaleX, scaleY); | |
46 SkVector radii[4]; | 37 SkVector radii[4]; |
47 radii[0].set(SkIntToScalar(ulX), SkIntToScalar(ulY)); | 38 radii[0].set(SkIntToScalar(30), SkIntToScalar(30)); |
48 radii[1].set(SkIntToScalar(urX), SkIntToScalar(urY)); | 39 radii[1].set(SkIntToScalar(10), SkIntToScalar(10)); |
49 radii[2].set(SkIntToScalar(lrX), SkIntToScalar(lrY)); | 40 radii[2].set(SkIntToScalar(30), SkIntToScalar(30)); |
50 radii[3].set(SkIntToScalar(llX), SkIntToScalar(llY)); | 41 radii[3].set(SkIntToScalar(10), SkIntToScalar(10)); |
51 SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight))
; | 42 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
52 fRRect.setRectRadii(r, radii); | 43 fRRect.setRectRadii(r, radii); |
53 } | 44 } |
54 | 45 |
55 virtual SkString onShortName() SK_OVERRIDE { | 46 virtual SkString onShortName() SK_OVERRIDE { |
56 return fName; | 47 return fName; |
57 } | 48 } |
58 | 49 |
59 virtual SkISize onISize() SK_OVERRIDE { | 50 virtual SkISize onISize() SK_OVERRIDE { |
60 SkISize size = this->getUnscaledSize(); | 51 return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()), |
61 return SkISize::Make(SkScalarCeilToInt(SkScalarMul(size.fWidth, fScaleX)
), | 52 SkScalarCeilToInt(fRRect.rect().height())); |
62 SkScalarCeilToInt(SkScalarMul(size.fHeight, fScaleY
))); | |
63 } | 53 } |
64 | 54 |
65 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 55 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
66 canvas->scale(fScaleX, fScaleY); | |
67 } | |
68 | |
69 const SkRRect& getRRect() const { | |
70 return fRRect; | |
71 } | |
72 | |
73 // The subclass will implement this to inform us how big they | |
74 // draw before scaling. | |
75 virtual SkISize getUnscaledSize() const = 0; | |
76 | |
77 // So subclasses can modify the name. | |
78 SkString* getName() { | |
79 return &fName; | |
80 } | |
81 | |
82 private: | |
83 SkString fName; | |
84 const int fWidth; | |
85 const int fHeight; | |
86 const SkScalar fScaleX; | |
87 const SkScalar fScaleY; | |
88 SkRRect fRRect; | |
89 typedef skiagm::GM INHERITED; | |
90 }; | |
91 | |
92 class SKPBlurRoundRectGM : public BlurRoundRectGM { | |
93 public: | |
94 SKPBlurRoundRectGM(int width, int height, | |
95 int ulX, int ulY, | |
96 int urX, int urY, | |
97 int lrX, int lrY, | |
98 int llX, int llY, | |
99 int scaleX, int scaleY) | |
100 : INHERITED(width, height, ulX, ulY, urX, urY, lrX, lrY, llX, llY, scale
X, scaleY) { | |
101 this->getName()->prepend("skp-"); | |
102 } | |
103 | |
104 protected: | |
105 virtual SkISize getUnscaledSize() const SK_OVERRIDE { | |
106 return SkISize::Make(SkScalarCeilToInt(this->getRRect().rect().width()), | |
107 SkScalarCeilToInt(this->getRRect().rect().height())
); | |
108 } | |
109 | |
110 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
111 this->INHERITED::onDraw(canvas); | |
112 SkLayerDrawLooper* looper = new SkLayerDrawLooper; | 56 SkLayerDrawLooper* looper = new SkLayerDrawLooper; |
113 { | 57 { |
114 SkLayerDrawLooper::LayerInfo info; | 58 SkLayerDrawLooper::LayerInfo info; |
115 info.fFlagsMask = 0; | 59 info.fFlagsMask = 0; |
116 info.fPaintBits = 40; | 60 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit |
| 61 | SkLayerDrawLooper::kColorFilter_Bit; |
117 info.fColorMode = SkXfermode::kSrc_Mode; | 62 info.fColorMode = SkXfermode::kSrc_Mode; |
118 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); | 63 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); |
119 info.fPostTranslate = false; | 64 info.fPostTranslate = false; |
120 SkPaint* paint = looper->addLayerOnTop(info); | 65 SkPaint* paint = looper->addLayerOnTop(info); |
121 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(SK_ScalarHalf, | 66 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(SK_ScalarHalf, |
122 SkBlurMaskFilter::kNormal_BlurStyle, | 67 SkBlurMaskFilter::kNormal_BlurStyle, |
123 SkBlurMaskFilter::kHighQuality_BlurFlag); | 68 SkBlurMaskFilter::kHighQuality_BlurFlag); |
124 paint->setMaskFilter(maskFilter)->unref(); | 69 paint->setMaskFilter(maskFilter)->unref(); |
125 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_Colo
rLTGRAY, | 70 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_Colo
rLTGRAY, |
126 SkXfermode::kSrcIn_Mode); | 71 SkXfermode::kSrcIn_Mode); |
127 paint->setColorFilter(colorFilter)->unref(); | 72 paint->setColorFilter(colorFilter)->unref(); |
128 paint->setColor(SK_ColorGRAY); | 73 paint->setColor(SK_ColorGRAY); |
129 } | 74 } |
130 { | 75 { |
131 SkLayerDrawLooper::LayerInfo info; | 76 SkLayerDrawLooper::LayerInfo info; |
132 looper->addLayerOnTop(info); | 77 looper->addLayerOnTop(info); |
133 } | 78 } |
134 SkPaint paint; | 79 SkPaint paint; |
135 canvas->drawRect(this->getRRect().rect(), paint); | 80 canvas->drawRect(fRRect.rect(), paint); |
136 | 81 |
137 paint.setLooper(looper)->unref(); | 82 paint.setLooper(looper)->unref(); |
138 paint.setColor(SK_ColorCYAN); | 83 paint.setColor(SK_ColorCYAN); |
139 paint.setAntiAlias(true); | 84 paint.setAntiAlias(true); |
140 | 85 |
141 canvas->drawRRect(this->getRRect(), paint); | 86 canvas->drawRRect(fRRect, paint); |
142 } | 87 } |
143 | 88 |
144 private: | 89 private: |
145 typedef BlurRoundRectGM INHERITED; | 90 SkString fName; |
| 91 SkRRect fRRect; |
| 92 |
| 93 typedef skiagm::GM INHERITED; |
146 }; | 94 }; |
147 | 95 |
148 class SimpleBlurRoundRectGM : public BlurRoundRectGM { | 96 // Simpler blurred RR test cases where all the radii are the same. |
| 97 class SimpleBlurRoundRectGM : public skiagm::GM { |
149 public: | 98 public: |
150 SimpleBlurRoundRectGM(int width, int height, | 99 SimpleBlurRoundRectGM() |
151 int blurRadius, int cornerRadius, | 100 : fName("simpleblurroundrect") { |
152 int scaleX = 1, int scaleY = 1) | |
153 : INHERITED(width, height, cornerRadius, cornerRadius, | |
154 cornerRadius, cornerRadius, cornerRadius, | |
155 cornerRadius, cornerRadius, cornerRadius, scaleX, scaleY) | |
156 , fBlurRadius(blurRadius) { | |
157 // For now at least, change the name to reflect only the | |
158 // variables that are changing. | |
159 this->getName()->printf("blurround-blur[%i]-corner[%i]-scale[%ix%i]", fB
lurRadius, cornerRadius, scaleX, scaleY); | |
160 } | 101 } |
161 | 102 |
162 protected: | 103 protected: |
163 virtual SkISize getUnscaledSize() const SK_OVERRIDE { | 104 virtual SkString onShortName() SK_OVERRIDE { |
164 return SkISize::Make(SkScalarCeilToInt(this->getRRect().rect().width() +
20), | 105 return fName; |
165 SkScalarCeilToInt(this->getRRect().rect().height()
+ 20)); | 106 } |
| 107 |
| 108 virtual SkISize onISize() SK_OVERRIDE { |
| 109 return SkISize::Make(SkIntToScalar(750), SkIntToScalar(750)); |
166 } | 110 } |
167 | 111 |
168 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 112 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
169 // Handle the scaling. | 113 canvas->scale(SkFloatToScalar(1.5f), SkFloatToScalar(1.5f)); |
170 this->INHERITED::onDraw(canvas); | 114 |
171 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); | 115 const int blurRadii[] = { 1, 3, 6, 10 }; |
172 SkMaskFilter* filter = SkBlurMaskFilter::Create(SkIntToScalar(fBlurRadiu
s), | 116 const int cornerRadii[] = { 1, 3, 6, 10 }; |
173 SkBlurMaskFilter::kNormal_BlurStyle); | 117 const SkRect r = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
174 SkPaint paint; | 118 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) { |
175 paint.setColor(SK_ColorBLUE); | 119 SkAutoCanvasRestore autoRestore(canvas, true); |
176 paint.setMaskFilter(filter)->unref(); | 120 canvas->translate(0, (r.height() + SkIntToScalar(20)) * i); |
177 canvas->drawRRect(this->getRRect(), paint); | 121 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) { |
| 122 SkMaskFilter* filter = SkBlurMaskFilter::Create(SkIntToScalar(bl
urRadii[i]), |
| 123 SkBlurMaskFilter::kNormal_BlurStyle)
; |
| 124 SkPaint paint; |
| 125 paint.setColor(SK_ColorBLUE); |
| 126 paint.setMaskFilter(filter)->unref(); |
| 127 |
| 128 SkRRect rrect; |
| 129 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), SkIntToScalar(
cornerRadii[j])); |
| 130 canvas->drawRRect(rrect, paint); |
| 131 canvas->translate(r.width() + SkIntToScalar(10), 0); |
| 132 } |
| 133 } |
178 } | 134 } |
179 private: | 135 private: |
180 const int fBlurRadius; | 136 const SkString fName; |
181 | 137 |
182 typedef BlurRoundRectGM INHERITED; | 138 typedef skiagm::GM INHERITED; |
183 }; | 139 }; |
184 | 140 |
185 // Create one with dimensions/rounded corners based on the skp | 141 // Create one with dimensions/rounded corners based on the skp |
186 DEF_GM(return new SKPBlurRoundRectGM(600, 5514, 6, 6, 6, 6, 6, 6, 6, 6, 1, 1);) | 142 DEF_GM(return new BlurRoundRectGM(600, 5514, 6);) |
187 // Same radii, much smaller rectangle | |
188 DEF_GM(return new SKPBlurRoundRectGM(100, 100, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2);) | |
189 // Rounded rect with two opposite corners with large radii, the other two | 143 // Rounded rect with two opposite corners with large radii, the other two |
190 // small. | 144 // small. |
191 DEF_GM(return new SKPBlurRoundRectGM(100, 100, 30, 30, 10, 10, 30, 30, 10, 10, 3
, 4);) | 145 DEF_GM(return new BlurRoundRectGM(100, 100);) |
192 DEF_GM(return new SKPBlurRoundRectGM(100, 100, 90, 90, 90, 90, 90, 90, 90, 90, 2
, 3);) | |
193 | 146 |
194 // Try a few blur values with a small corner radius | 147 DEF_GM(return new SimpleBlurRoundRectGM();) |
195 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 1, 1)); | |
196 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 3, 1, 2, 2)); | |
197 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 6, 1)); | |
198 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 10, 1, 3, 3)); | |
199 | 148 |
200 // Now a few blur values with a larger corner radius | |
201 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 1, 3, 2, 2)); | |
202 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 3, 3)); | |
203 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 6, 3, 3, 3)); | |
204 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 10, 3)); | |
205 | |
206 // Even larger corner radius | |
207 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 1, 6, 2, 4)); | |
208 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 3, 6)); | |
209 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 6, 6)); | |
210 DEF_GM(return new SimpleBlurRoundRectGM(100, 100, 10, 6, 1, 3)); | |
211 | |
OLD | NEW |