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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/core/SkBlurMask.h" |
| 22 | #include "tools/Resources.h" |
| 23 | #include "tools/ToolUtils.h" |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 24 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 25 | DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) { |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 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 | }; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 37 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | paint.setColor(SK_ColorBLUE); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 41 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 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 { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 49 | paint.setMaskFilter(nullptr); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 50 | } |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 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 | { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 58 | SkFont font(ToolUtils::create_portable_typeface(), 25); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 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 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 73 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 74 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 75 | ////////////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 76 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 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 | // |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 83 | DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 84 | SkPaint paint; |
| 85 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 86 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 2.3f)); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 87 | |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 88 | SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); |
| 89 | SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 90 | SkPath path; |
| 91 | path.addRect(outer, SkPath::kCW_Direction); |
| 92 | path.addRect(inner, SkPath::kCCW_Direction); |
| 93 | |
| 94 | canvas->drawPath(path, paint); |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 95 | // important to translate by a factional amount to exercise a different "phase" |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 96 | // of the same path w.r.t. the pixel grid |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 97 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 98 | canvas->translate(dx, 0); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 99 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 100 | } |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 101 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 102 | DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 103 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 104 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4.3f)); |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 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); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 121 | } |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 122 | |
| 123 | DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) { |
| 124 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 125 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10)); |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 126 | canvas->clear(0xFF88FF88); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 127 | if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) { |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 128 | canvas->scale(0.25, 0.25); |
| 129 | canvas->drawImage(image, 256, 256, &paint); |
| 130 | } |
| 131 | } |