2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
14 #define STROKE_WIDTH SkIntToScalar(10)
16 typedef void (*Proc)(SkCanvas*, const SkRect&, const SkPaint&);
18 static void fill_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
19 canvas->drawRect(r, p);
22 static void draw_donut(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
27 rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
30 rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
33 path.setFillType(SkPath::kEvenOdd_FillType);
35 canvas->drawPath(path, p);
38 static void draw_donut_skewed(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
43 rect.outset(STROKE_WIDTH/2, STROKE_WIDTH/2);
46 rect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
51 path.setFillType(SkPath::kEvenOdd_FillType);
53 canvas->drawPath(path, p);
56 #include "SkGradientShader.h"
59 * Spits out a dummy gradient to test blur with shader on paint
61 static SkShader* MakeRadial() {
64 { SkIntToScalar(100), SkIntToScalar(100) }
66 SkShader::TileMode tm = SkShader::kClamp_TileMode;
67 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
68 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
70 scale.setScale(0.5f, 0.5f);
71 scale.postTranslate(25.f, 25.f);
72 SkPoint center0, center1;
73 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
74 SkScalarAve(pts[0].fY, pts[1].fY));
75 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
76 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
77 return SkGradientShader::CreateTwoPointRadial(center1, (pts[1].fX - pts[0].fX) / 7,
78 center0, (pts[1].fX - pts[0].fX) / 2,
79 colors, pos, SK_ARRAY_COUNT(colors), tm,
83 typedef void (*PaintProc)(SkPaint*, SkScalar width);
85 class BlurRectGM : public skiagm::GM {
86 SkAutoTUnref<SkMaskFilter> fMaskFilters[kLastEnum_SkBlurStyle + 1];
90 BlurRectGM(const char name[], U8CPU alpha)
92 , fAlpha(SkToU8(alpha)) {
96 void onOnceBeforeDraw() SK_OVERRIDE {
97 for (int i = 0; i <= kLastEnum_SkBlurStyle; ++i) {
98 fMaskFilters[i].reset(SkBlurMaskFilter::Create((SkBlurStyle)i,
99 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(STROKE_WIDTH/2)),
100 SkBlurMaskFilter::kHighQuality_BlurFlag));
104 SkString onShortName() SK_OVERRIDE {
108 SkISize onISize() SK_OVERRIDE {
109 return SkISize::Make(860, 820);
112 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
113 canvas->translate(STROKE_WIDTH*3/2, STROKE_WIDTH*3/2);
115 SkRect r = { 0, 0, 100, 50 };
116 SkScalar scales[] = { SK_Scalar1, 0.6f };
118 for (size_t s = 0; s < SK_ARRAY_COUNT(scales); ++s) {
120 for (size_t f = 0; f < SK_ARRAY_COUNT(fMaskFilters); ++f) {
122 paint.setMaskFilter(fMaskFilters[f]);
123 paint.setAlpha(fAlpha);
125 SkPaint paintWithRadial = paint;
126 paintWithRadial.setShader(MakeRadial())->unref();
128 static const Proc procs[] = {
129 fill_rect, draw_donut, draw_donut_skewed
133 canvas->scale(scales[s], scales[s]);
134 this->drawProcs(canvas, r, paint, false, procs, SK_ARRAY_COUNT(procs));
135 canvas->translate(r.width() * 4/3, 0);
136 this->drawProcs(canvas, r, paintWithRadial, false, procs, SK_ARRAY_COUNT(procs));
137 canvas->translate(r.width() * 4/3, 0);
138 this->drawProcs(canvas, r, paint, true, procs, SK_ARRAY_COUNT(procs));
139 canvas->translate(r.width() * 4/3, 0);
140 this->drawProcs(canvas, r, paintWithRadial, true, procs, SK_ARRAY_COUNT(procs));
143 canvas->translate(0, SK_ARRAY_COUNT(procs) * r.height() * 4/3 * scales[s]);
146 canvas->translate(4 * r.width() * 4/3 * scales[s], 0);
151 void drawProcs(SkCanvas* canvas, const SkRect& r, const SkPaint& paint,
152 bool doClip, const Proc procs[], size_t procsCount) {
153 SkAutoCanvasRestore acr(canvas, true);
154 for (size_t i = 0; i < procsCount; ++i) {
157 clipRect.inset(STROKE_WIDTH/2, STROKE_WIDTH/2);
161 procs[i](canvas, r, paint);
165 canvas->translate(0, r.height() * 4/3);
169 typedef GM INHERITED;
173 class BlurRectDirectGM : public skiagm::GM {
175 int fGMWidth, fGMHeight;
176 int fPadding, fMargin;
178 BlurRectDirectGM(const char name[])
188 virtual SkString onShortName() {
192 virtual SkISize onISize() {
193 return SkISize::Make(fGMWidth, fGMHeight);
196 virtual void onDraw(SkCanvas* canvas) {
197 const int widths[] = {25, 5, 5, 100, 150, 25};
198 const int heights[] = {100, 100, 5, 25, 150, 25};
199 const SkBlurStyle styles[] = {kNormal_SkBlurStyle, kInner_SkBlurStyle, kOuter_SkBlurStyle};
200 const float radii[] = {20, 5, 10};
202 canvas->translate(50,20);
209 for (size_t i = 0 ; i < SK_ARRAY_COUNT(widths) ; i++) {
210 int width = widths[i];
211 int height = heights[i];
213 r.setWH(SkIntToScalar(width), SkIntToScalar(height));
214 SkAutoCanvasRestore autoRestore(canvas, true);
216 for (size_t j = 0 ; j < SK_ARRAY_COUNT(radii) ; j++) {
217 float radius = radii[j];
218 for (size_t k = 0 ; k < SK_ARRAY_COUNT(styles) ; k++) {
219 SkBlurStyle style = styles[k];
222 SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(radius), &mask, r, style);
224 SkAutoMaskFreeImage amfi(mask.fImage);
227 bm.installMaskPixels(mask);
229 if (cur_x + bm.width() >= fGMWidth - fMargin) {
231 cur_y += max_height + fPadding;
236 canvas->translate((SkScalar)cur_x, (SkScalar)cur_y);
237 canvas->translate(-(bm.width() - r.width())/2, -(bm.height()-r.height())/2);
238 canvas->drawBitmap(bm, 0.f, 0.f, NULL);
241 cur_x += bm.width() + fPadding;
242 if (bm.height() > max_height)
243 max_height = bm.height();
250 typedef GM INHERITED;
253 class BlurRectCompareGM : public skiagm::GM {
255 unsigned int fRectWidth, fRectHeight;
259 BlurRectCompareGM(const char name[], unsigned int rectWidth,
260 unsigned int rectHeight, float radius,
263 , fRectWidth(rectWidth)
264 , fRectHeight(rectHeight)
274 SkScalar radius() const {
277 SkBlurStyle style() const {
282 virtual SkString onShortName() {
286 virtual SkISize onISize() {
287 return SkISize::Make(640, 480);
290 virtual bool makeMask(SkMask *m, const SkRect&) = 0;
292 virtual void onDraw(SkCanvas* canvas) {
294 r.setWH(SkIntToScalar(fRectWidth), SkIntToScalar(fRectHeight));
296 SkISize canvas_size = canvas->getDeviceSize();
297 int center_x = (canvas_size.fWidth - (int)(r.width()))/2;
298 int center_y = (canvas_size.fHeight - (int)(r.height()))/2;
302 if (!this->makeMask(&mask, r)) {
304 r.offset( SkIntToScalar(center_x), SkIntToScalar(center_y) );
305 canvas->drawRect(r,paint);
308 SkAutoMaskFreeImage amfi(mask.fImage);
311 bm.installMaskPixels(mask);
313 center_x = (canvas_size.fWidth - mask.fBounds.width())/2;
314 center_y = (canvas_size.fHeight - mask.fBounds.height())/2;
316 canvas->drawBitmap(bm, SkIntToScalar(center_x), SkIntToScalar(center_y), NULL);
320 typedef GM INHERITED;
323 class BlurRectFastGM: public BlurRectCompareGM {
325 BlurRectFastGM(const char name[], unsigned int rectWidth,
326 unsigned int rectHeight, float blurRadius,
328 INHERITED(name, rectWidth, rectHeight, blurRadius, style) {
332 bool makeMask(SkMask *m, const SkRect& r) SK_OVERRIDE {
333 return SkBlurMask::BlurRect(SkBlurMask::ConvertRadiusToSigma(this->radius()),
334 m, r, this->style());
337 typedef BlurRectCompareGM INHERITED;
340 class BlurRectSlowGM: public BlurRectCompareGM {
342 BlurRectSlowGM(const char name[], unsigned int rectWidth, unsigned int rectHeight,
343 float blurRadius, SkBlurStyle style)
344 : INHERITED(name, rectWidth, rectHeight, blurRadius, style) {
348 bool makeMask(SkMask *m, const SkRect& r) SK_OVERRIDE {
350 r.roundOut(&src.fBounds);
351 src.fBounds.offset(-src.fBounds.fLeft, -src.fBounds.fTop); // move to origin
352 src.fFormat = SkMask::kA8_Format;
353 src.fRowBytes = src.fBounds.width();
354 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
355 SkAutoMaskFreeImage amfi(src.fImage);
357 memset(src.fImage, 0xff, src.computeTotalImageSize());
359 return SkBlurMask::BoxBlur(m, src,
360 SkBlurMask::ConvertRadiusToSigma(this->radius()),
361 this->style(), this->getQuality());
364 virtual SkBlurQuality getQuality() {
365 return kHigh_SkBlurQuality;
368 typedef BlurRectCompareGM INHERITED;
371 class BlurRectSlowLowGM: public BlurRectSlowGM {
373 BlurRectSlowLowGM(const char name[], unsigned int rectWidth, unsigned int rectHeight,
374 float blurRadius, SkBlurStyle style)
375 : INHERITED(name, rectWidth, rectHeight, blurRadius, style) {
379 SkBlurQuality getQuality() SK_OVERRIDE {
380 return kLow_SkBlurQuality;
383 typedef BlurRectSlowGM INHERITED;
386 class BlurRectGroundTruthGM: public BlurRectCompareGM {
388 BlurRectGroundTruthGM(const char name[], unsigned int rectWidth, unsigned int rectHeight,
389 float blurRadius, SkBlurStyle style)
390 : INHERITED(name, rectWidth, rectHeight, blurRadius, style) {
394 bool makeMask(SkMask *m, const SkRect& r) SK_OVERRIDE {
396 r.roundOut(&src.fBounds);
397 src.fBounds.offset(-src.fBounds.fLeft, -src.fBounds.fTop); // move to origin
398 src.fFormat = SkMask::kA8_Format;
399 src.fRowBytes = src.fBounds.width();
400 src.fImage = SkMask::AllocImage(src.computeTotalImageSize());
401 SkAutoMaskFreeImage amfi(src.fImage);
403 memset(src.fImage, 0xff, src.computeTotalImageSize());
405 return SkBlurMask::BlurGroundTruth(SkBlurMask::ConvertRadiusToSigma(this->radius()),
406 m, src, this->style());
409 virtual SkBlurQuality getQuality() {
410 return kHigh_SkBlurQuality;
413 typedef BlurRectCompareGM INHERITED;
417 //////////////////////////////////////////////////////////////////////////////
419 DEF_GM(return new BlurRectGM("blurrects", 0xFF);)
420 DEF_GM(return new BlurRectDirectGM("blurrect_gallery");)