epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 14 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkPath.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Herb Derby | f07a363 | 2019-09-23 16:46:09 -0400 | [diff] [blame] | 21 | #include "include/effects/SkBlurImageFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkBlurMask.h" |
| 23 | #include "tools/Resources.h" |
| 24 | #include "tools/ToolUtils.h" |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 25 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 26 | DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) { |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 27 | SkBlurStyle NONE = SkBlurStyle(-999); |
| 28 | const struct { |
| 29 | SkBlurStyle fStyle; |
| 30 | int fCx, fCy; |
| 31 | } gRecs[] = { |
| 32 | { NONE, 0, 0 }, |
| 33 | { kInner_SkBlurStyle, -1, 0 }, |
| 34 | { kNormal_SkBlurStyle, 0, 1 }, |
| 35 | { kSolid_SkBlurStyle, 0, -1 }, |
| 36 | { kOuter_SkBlurStyle, 1, 0 }, |
| 37 | }; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 38 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 39 | SkPaint paint; |
| 40 | paint.setAntiAlias(true); |
| 41 | paint.setColor(SK_ColorBLUE); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 42 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 43 | canvas->translate(SkIntToScalar(-40), SkIntToScalar(0)); |
| 44 | |
| 45 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 46 | if (gRecs[i].fStyle != NONE) { |
| 47 | paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle, |
| 48 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)))); |
| 49 | } else { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 50 | paint.setMaskFilter(nullptr); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 51 | } |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 52 | canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100), |
| 53 | SkIntToScalar(200 + gRecs[i].fCy*100), |
| 54 | SkIntToScalar(50), |
| 55 | paint); |
| 56 | } |
| 57 | // draw text |
| 58 | { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 59 | SkFont font(ToolUtils::create_portable_typeface(), 25); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 60 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 61 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)))); |
| 62 | SkScalar x = SkIntToScalar(70); |
| 63 | SkScalar y = SkIntToScalar(400); |
| 64 | paint.setColor(SK_ColorBLACK); |
| 65 | canvas->drawString("Hamburgefons Style", x, y, font, paint); |
| 66 | canvas->drawString("Hamburgefons Style", |
| 67 | x, y + SkIntToScalar(50), font, paint); |
| 68 | paint.setMaskFilter(nullptr); |
| 69 | paint.setColor(SK_ColorWHITE); |
| 70 | x -= SkIntToScalar(2); |
| 71 | y -= SkIntToScalar(2); |
| 72 | canvas->drawString("Hamburgefons Style", x, y, font, paint); |
| 73 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 74 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 75 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 76 | ////////////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 77 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 78 | // exercise a special-case of blurs, which is two nested rects. These are drawn specially, |
| 79 | // and possibly cached. |
| 80 | // |
| 81 | // in particular, we want to notice that the 2nd rect draws slightly differently, since it |
| 82 | // is translated a fractional amount. |
| 83 | // |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 84 | DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 85 | SkPaint paint; |
| 86 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 87 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 2.3f)); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 88 | |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 89 | SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); |
| 90 | SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 91 | SkPath path; |
| 92 | path.addRect(outer, SkPath::kCW_Direction); |
| 93 | path.addRect(inner, SkPath::kCCW_Direction); |
| 94 | |
| 95 | canvas->drawPath(path, paint); |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 96 | // important to translate by a factional amount to exercise a different "phase" |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 97 | // of the same path w.r.t. the pixel grid |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 98 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 99 | canvas->translate(dx, 0); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 100 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 101 | } |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 102 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 103 | DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 104 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 105 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4.3f)); |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 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); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 122 | } |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 123 | |
| 124 | DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) { |
| 125 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 126 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10)); |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 127 | canvas->clear(0xFF88FF88); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 128 | if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) { |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 129 | canvas->scale(0.25, 0.25); |
| 130 | canvas->drawImage(image, 256, 256, &paint); |
| 131 | } |
| 132 | } |
Herb Derby | f07a363 | 2019-09-23 16:46:09 -0400 | [diff] [blame] | 133 | |
| 134 | DEF_SIMPLE_GM(BlurBigSigma, canvas, 1024, 1024) { |
| 135 | SkPaint layerPaint, p; |
| 136 | |
Herb Derby | 4e8f63a | 2019-09-25 18:44:35 -0400 | [diff] [blame] | 137 | p.setImageFilter(SkBlurImageFilter::Make(500, 500, nullptr)); |
Herb Derby | f07a363 | 2019-09-23 16:46:09 -0400 | [diff] [blame] | 138 | |
| 139 | canvas->drawRect(SkRect::MakeWH(700, 800), p); |
| 140 | } |