1 /* 2 * Copyright 2011 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.h" 9 #include "SkBlurMask.h" 10 #include "SkBlurMaskFilter.h" 11 #include "SkPath.h" 12 13 DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, sk_tool_utils::color_to_565(0xFFDDDDDD)) { 14 SkBlurStyle NONE = SkBlurStyle(-999); 15 static const struct { 16 SkBlurStyle fStyle; 17 int fCx, fCy; 18 } gRecs[] = { 19 { NONE, 0, 0 }, 20 { kInner_SkBlurStyle, -1, 0 }, 21 { kNormal_SkBlurStyle, 0, 1 }, 22 { kSolid_SkBlurStyle, 0, -1 }, 23 { kOuter_SkBlurStyle, 1, 0 }, 24 }; 25 26 SkPaint paint; 27 paint.setAntiAlias(true); 28 sk_tool_utils::set_portable_typeface(&paint); 29 paint.setTextSize(SkIntToScalar(25)); 30 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0)); 31 32 SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag; 33 for (int j = 0; j < 2; j++) { 34 canvas->save(); 35 paint.setColor(SK_ColorBLUE); 36 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { 37 if (gRecs[i].fStyle != NONE) { 38 SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle, 39 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), 40 flags); 41 paint.setMaskFilter(mf)->unref(); 42 } else { 43 paint.setMaskFilter(nullptr); 44 } 45 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100), 46 SkIntToScalar(200 + gRecs[i].fCy*100), 47 SkIntToScalar(50), 48 paint); 49 } 50 // draw text 51 { 52 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 53 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)), 54 flags); 55 paint.setMaskFilter(mf)->unref(); 56 SkScalar x = SkIntToScalar(70); 57 SkScalar y = SkIntToScalar(400); 58 paint.setColor(SK_ColorBLACK); 59 canvas->drawText("Hamburgefons Style", 18, x, y, paint); 60 canvas->drawText("Hamburgefons Style", 18, 61 x, y + SkIntToScalar(50), paint); 62 paint.setMaskFilter(nullptr); 63 paint.setColor(SK_ColorWHITE); 64 x -= SkIntToScalar(2); 65 y -= SkIntToScalar(2); 66 canvas->drawText("Hamburgefons Style", 18, x, y, paint); 67 } 68 canvas->restore(); 69 flags = SkBlurMaskFilter::kHighQuality_BlurFlag; 70 canvas->translate(SkIntToScalar(350), SkIntToScalar(0)); 71 } 72 } 73 74 ////////////////////////////////////////////////////////////////////////////////////////////// 75 76 // exercise a special-case of blurs, which is two nested rects. These are drawn specially, 77 // and possibly cached. 78 // 79 // in particular, we want to notice that the 2nd rect draws slightly differently, since it 80 // is translated a fractional amount. 81 // 82 DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) { 83 SkPaint paint; 84 85 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 86 2.3f))->unref(); 87 88 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); 89 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); 90 SkPath path; 91 path.addRect(outer, SkPath::kCW_Direction); 92 path.addRect(inner, SkPath::kCCW_Direction); 93 94 canvas->drawPath(path, paint); 95 // important to translate by a factional amount to exercise a different "phase" 96 // of the same path w.r.t. the pixel grid 97 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; 98 canvas->translate(dx, 0); 99 canvas->drawPath(path, paint); 100 } 101 102 DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) { 103 SkPaint paint; 104 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, 105 4.3f))->unref(); 106 107 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100); 108 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10); 109 SkPath path; 110 path.addRect(outer, SkPath::kCW_Direction); 111 path.addRect(inner, SkPath::kCW_Direction); 112 canvas->drawPath(path, paint); 113 114 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f; 115 canvas->translate(dx, 0); 116 canvas->drawPath(path, paint); 117 118 // Translate to outside of clip bounds. 119 canvas->translate(-dx, 0); 120 canvas->translate(-30, -150); 121 canvas->drawPath(path, paint); 122 } 123