Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(759)

Side by Side Diff: src/effects/SkBlurMaskFilter.cpp

Issue 48623006: Add ability to ninepatch blurred rounded rectangle (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Respond to comments. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "SkBlurMaskFilter.h" 9 #include "SkBlurMaskFilter.h"
10 #include "SkBlurMask.h" 10 #include "SkBlurMask.h"
11 #include "SkGpuBlurUtils.h" 11 #include "SkGpuBlurUtils.h"
12 #include "SkFlattenableBuffers.h" 12 #include "SkFlattenableBuffers.h"
13 #include "SkMaskFilter.h" 13 #include "SkMaskFilter.h"
14 #include "SkRRect.h"
14 #include "SkRTConf.h" 15 #include "SkRTConf.h"
15 #include "SkStringUtils.h" 16 #include "SkStringUtils.h"
16 #include "SkStrokeRec.h" 17 #include "SkStrokeRec.h"
17 18
18 #if SK_SUPPORT_GPU 19 #if SK_SUPPORT_GPU
19 #include "GrContext.h" 20 #include "GrContext.h"
20 #include "GrTexture.h" 21 #include "GrTexture.h"
21 #include "effects/GrSimpleTextureEffect.h" 22 #include "effects/GrSimpleTextureEffect.h"
22 #include "SkGrPixelRef.h" 23 #include "SkGrPixelRef.h"
23 #endif 24 #endif
(...skipping 21 matching lines...) Expand all
45 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE; 46 virtual void computeFastBounds(const SkRect&, SkRect*) const SK_OVERRIDE;
46 47
47 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;) 48 SkDEVCODE(virtual void toString(SkString* str) const SK_OVERRIDE;)
48 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl) 49 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBlurMaskFilterImpl)
49 50
50 protected: 51 protected:
51 virtual FilterReturn filterRectsToNine(const SkRect[], int count, const SkMa trix&, 52 virtual FilterReturn filterRectsToNine(const SkRect[], int count, const SkMa trix&,
52 const SkIRect& clipBounds, 53 const SkIRect& clipBounds,
53 NinePatch*) const SK_OVERRIDE; 54 NinePatch*) const SK_OVERRIDE;
54 55
56 virtual FilterReturn filterRRectToNine(const SkRRect&, const SkMatrix&,
57 const SkIRect& clipBounds,
58 NinePatch*) const SK_OVERRIDE;
59
55 bool filterRectMask(SkMask* dstM, const SkRect& r, const SkMatrix& matrix, 60 bool filterRectMask(SkMask* dstM, const SkRect& r, const SkMatrix& matrix,
56 SkIPoint* margin, SkMask::CreateMode createMode) const; 61 SkIPoint* margin, SkMask::CreateMode createMode) const;
57 62
58 private: 63 private:
59 // To avoid unseemly allocation requests (esp. for finite platforms like 64 // To avoid unseemly allocation requests (esp. for finite platforms like
60 // handset) we limit the radius so something manageable. (as opposed to 65 // handset) we limit the radius so something manageable. (as opposed to
61 // a request like 10,000) 66 // a request like 10,000)
62 static const SkScalar kMAX_BLUR_SIGMA; 67 static const SkScalar kMAX_BLUR_SIGMA;
63 68
64 SkScalar fSigma; 69 SkScalar fSigma;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 const SkMatrix& matrix, 153 const SkMatrix& matrix,
149 SkIPoint* margin, SkMask::CreateMode c reateMode) const{ 154 SkIPoint* margin, SkMask::CreateMode c reateMode) const{
150 SkScalar sigma = computeXformedSigma(matrix); 155 SkScalar sigma = computeXformedSigma(matrix);
151 156
152 return SkBlurMask::BlurRect(sigma, dst, r, (SkBlurMask::Style)fBlurStyle, 157 return SkBlurMask::BlurRect(sigma, dst, r, (SkBlurMask::Style)fBlurStyle,
153 margin, createMode); 158 margin, createMode);
154 } 159 }
155 160
156 #include "SkCanvas.h" 161 #include "SkCanvas.h"
157 162
163 // Common code used by both draw_rrect_into_mask and drawRectsIntoMask.
reed1 2013/11/04 17:46:52 errrrrrr, a giant macro with an embedded return? D
scroggo 2013/11/04 19:52:54 Ideally, I'd like to share common code for creatin
164 // After this macro, mask has been set to bounds, its fImage has been
165 // allocated, and there is a new SkCanvas (called 'canvas') with its
166 // translation set to draw into, and a new SkPaint with antialias exists
167 // for drawing into it.
168 #define PREPARE_TO_DRAW_INTO_MASK(bounds, mask) \
169 SkASSERT(mask != NULL); \
170 \
171 bounds.roundOut(&mask->fBounds); \
172 mask->fRowBytes = SkAlign4(mask->fBounds.width()); \
173 mask->fFormat = SkMask::kA8_Format; \
174 const size_t size = mask->computeImageSize(); \
175 mask->fImage = SkMask::AllocImage(size); \
176 if (NULL == mask->fImage) { \
177 return false; \
178 } \
179 \
180 sk_bzero(mask->fImage, size); \
181 SkBitmap bitmap; \
182 bitmap.setConfig(SkBitmap::kA8_Config, \
183 mask->fBounds.width(), mask->fBounds.height(), \
184 mask->fRowBytes); \
185 bitmap.setPixels(mask->fImage); \
186 \
187 SkCanvas canvas(bitmap); \
188 canvas.translate(-SkIntToScalar(mask->fBounds.left()), \
189 -SkIntToScalar(mask->fBounds.top())); \
190 SkPaint paint; \
191 paint.setAntiAlias(true)
192
193 static bool draw_rrect_into_mask(const SkRRect rrect, SkMask* mask) {
194 PREPARE_TO_DRAW_INTO_MASK(rrect.rect(), mask);
195 canvas.drawRRect(rrect, paint);
196 return true;
197 }
198
158 static bool drawRectsIntoMask(const SkRect rects[], int count, SkMask* mask) { 199 static bool drawRectsIntoMask(const SkRect rects[], int count, SkMask* mask) {
159 rects[0].roundOut(&mask->fBounds); 200 PREPARE_TO_DRAW_INTO_MASK(rects[0], mask);
160 mask->fRowBytes = SkAlign4(mask->fBounds.width());
161 mask->fFormat = SkMask::kA8_Format;
162 size_t size = mask->computeImageSize();
163 mask->fImage = SkMask::AllocImage(size);
164 if (NULL == mask->fImage) {
165 return false;
166 }
167 sk_bzero(mask->fImage, size);
168
169 SkBitmap bitmap;
170 bitmap.setConfig(SkBitmap::kA8_Config,
171 mask->fBounds.width(), mask->fBounds.height(),
172 mask->fRowBytes);
173 bitmap.setPixels(mask->fImage);
174
175 SkCanvas canvas(bitmap);
176 canvas.translate(-SkIntToScalar(mask->fBounds.left()),
177 -SkIntToScalar(mask->fBounds.top()));
178
179 SkPaint paint;
180 paint.setAntiAlias(true);
181 201
182 if (1 == count) { 202 if (1 == count) {
183 canvas.drawRect(rects[0], paint); 203 canvas.drawRect(rects[0], paint);
184 } else { 204 } else {
185 // todo: do I need a fast way to do this? 205 // todo: do I need a fast way to do this?
186 SkPath path; 206 SkPath path;
187 path.addRect(rects[0]); 207 path.addRect(rects[0]);
188 path.addRect(rects[1]); 208 path.addRect(rects[1]);
189 path.setFillType(SkPath::kEvenOdd_FillType); 209 path.setFillType(SkPath::kEvenOdd_FillType);
190 canvas.drawPath(path, paint); 210 canvas.drawPath(path, paint);
191 } 211 }
192 return true; 212 return true;
193 } 213 }
194 214
195 static bool rect_exceeds(const SkRect& r, SkScalar v) { 215 static bool rect_exceeds(const SkRect& r, SkScalar v) {
196 return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v || 216 return r.fLeft < -v || r.fTop < -v || r.fRight > v || r.fBottom > v ||
197 r.width() > v || r.height() > v; 217 r.width() > v || r.height() > v;
198 } 218 }
199 219
220 SkMaskFilter::FilterReturn
221 SkBlurMaskFilterImpl::filterRRectToNine(const SkRRect& rrect, const SkMatrix& ma trix,
222 const SkIRect& clipBounds,
223 NinePatch* patch) const {
224 SkASSERT(patch != NULL);
225 switch (rrect.getType()) {
226 case SkRRect::kUnknown_Type:
227 // Unknown should never be returned.
228 SkASSERT(false);
229 // Fall through.
230 case SkRRect::kEmpty_Type:
231 // Nothing to draw.
232 return kFalse_FilterReturn;
233
234 case SkRRect::kRect_Type:
235 // We should have caught this earlier.
236 SkASSERT(false);
237 // Fall through.
238 case SkRRect::kOval_Type:
239 // The nine patch special case does not handle ovals, and we
240 // already have code for rectangles.
241 return kUnimplemented_FilterReturn;
242
243 case SkRRect::kSimple_Type:
244 // Fall through.
245 case SkRRect::kComplex_Type:
246 // These can take advantage of this fast path.
247 break;
248 }
249
250 // TODO: report correct metrics for innerstyle, where we do not grow the
251 // total bounds, but we do need an inset the size of our blur-radius
252 if (SkBlurMaskFilter::kInner_BlurStyle == fBlurStyle) {
253 return kUnimplemented_FilterReturn;
254 }
255
256 // TODO: take clipBounds into account to limit our coordinates up front
257 // for now, just skip too-large src rects (to take the old code path).
258 if (rect_exceeds(rrect.rect(), SkIntToScalar(32767))) {
259 return kUnimplemented_FilterReturn;
260 }
261
262 SkIPoint margin;
263 SkMask srcM, dstM;
264 rrect.rect().roundOut(&srcM.fBounds);
265 srcM.fImage = NULL;
266 srcM.fFormat = SkMask::kA8_Format;
267 srcM.fRowBytes = 0;
268
269 if (!this->filterMask(&dstM, srcM, matrix, &margin)) {
270 return kFalse_FilterReturn;
271 }
272
273 // Now figure out the appropriate width and height of the smaller round rect angle
274 // to stretch. It will take into account the larger radius per side as well as double
275 // the margin, to account for inner and outer blur.
276 const SkVector& UL = rrect.radii(SkRRect::kUpperLeft_Corner);
reed1 2013/11/04 17:46:52 we're not calling the new allRadii API here...? (w
scroggo 2013/11/04 19:52:54 Really getAllRadii() is just a convenience for my
277 const SkVector& UR = rrect.radii(SkRRect::kUpperRight_Corner);
278 const SkVector& LR = rrect.radii(SkRRect::kLowerRight_Corner);
279 const SkVector& LL = rrect.radii(SkRRect::kLowerLeft_Corner);
280
281 const SkScalar leftUnstretched = SkTMax(UL.fX, LL.fX) + SkIntToScalar(2 * ma rgin.fX);
282 const SkScalar rightUnstretched = SkTMax(UR.fX, LR.fX) + SkIntToScalar(2 * m argin.fX);
283
284 // Extra space in the middle to ensure an unchanging piece for stretching. U se 3 to cover
285 // any fractional space on either side plus 1 for the part to stretch.
286 const SkScalar stretchSize = SkIntToScalar(3);
287
288 const SkScalar totalSmallWidth = leftUnstretched + rightUnstretched + stretc hSize;
289 if (totalSmallWidth >= rrect.rect().width()) {
290 // There is no valid piece to stretch.
291 return kUnimplemented_FilterReturn;
292 }
293
294 const SkScalar topUnstretched = SkTMax(UL.fY, UR.fY) + SkIntToScalar(2 * mar gin.fY);
295 const SkScalar bottomUnstretched = SkTMax(LL.fY, LR.fY) + SkIntToScalar(2 * margin.fY);
296
297 const SkScalar totalSmallHeight = topUnstretched + bottomUnstretched + stret chSize;
298 if (totalSmallHeight >= rrect.rect().height()) {
299 // There is no valid piece to stretch.
300 return kUnimplemented_FilterReturn;
301 }
302
303 SkRect smallR = SkRect::MakeWH(totalSmallWidth, totalSmallHeight);
304
305 SkRRect smallRR;
306 smallRR.setRectRadii(smallR, rrect.getAllRadii());
307
308 if (!draw_rrect_into_mask(smallRR, &srcM)) {
309 return kFalse_FilterReturn;
310 }
311
312 if (!this->filterMask(&patch->fMask, srcM, matrix, &margin)) {
313 return kFalse_FilterReturn;
314 }
315
316 patch->fMask.fBounds.offsetTo(0, 0);
317 patch->fOuterRect = dstM.fBounds;
318 patch->fCenter.fX = SkScalarCeilToInt(leftUnstretched) + 1;
319 patch->fCenter.fY = SkScalarCeilToInt(topUnstretched) + 1;
320 return kTrue_FilterReturn;
321 }
322
200 #ifdef SK_IGNORE_FAST_RECT_BLUR 323 #ifdef SK_IGNORE_FAST_RECT_BLUR
201 SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", false, "Use the faster analytic blur approach for ninepatch rects" ); 324 SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", false, "Use the faster analytic blur approach for ninepatch rects" );
202 #else 325 #else
203 SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", true, "Use the faster analytic blur approach for ninepatch rects" ); 326 SK_CONF_DECLARE( bool, c_analyticBlurNinepatch, "mask.filter.analyticNinePatch", true, "Use the faster analytic blur approach for ninepatch rects" );
204 #endif 327 #endif
205 328
206 SkMaskFilter::FilterReturn 329 SkMaskFilter::FilterReturn
207 SkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count, 330 SkBlurMaskFilterImpl::filterRectsToNine(const SkRect rects[], int count,
208 const SkMatrix& matrix, 331 const SkMatrix& matrix,
209 const SkIRect& clipBounds, 332 const SkIRect& clipBounds,
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 } else { 596 } else {
474 str->append("None"); 597 str->append("None");
475 } 598 }
476 str->append("))"); 599 str->append("))");
477 } 600 }
478 #endif 601 #endif
479 602
480 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter) 603 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkBlurMaskFilter)
481 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl) 604 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkBlurMaskFilterImpl)
482 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 605 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698