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