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