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