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 | |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 10 | #include "Resources.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 11 | #include "SkBlurMask.h" |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 12 | #include "SkImage.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 13 | #include "SkMaskFilter.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 14 | #include "SkPath.h" |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 15 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 16 | DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, sk_tool_utils::color_to_565(0xFFDDDDDD)) { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 17 | SkBlurStyle NONE = SkBlurStyle(-999); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 18 | const struct { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 19 | SkBlurStyle fStyle; |
| 20 | int fCx, fCy; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 21 | } gRecs[] = { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 22 | { NONE, 0, 0 }, |
| 23 | { kInner_SkBlurStyle, -1, 0 }, |
| 24 | { kNormal_SkBlurStyle, 0, 1 }, |
| 25 | { kSolid_SkBlurStyle, 0, -1 }, |
| 26 | { kOuter_SkBlurStyle, 1, 0 }, |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | SkPaint paint; |
| 30 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 31 | sk_tool_utils::set_portable_typeface(&paint); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 32 | paint.setTextSize(SkIntToScalar(25)); |
| 33 | canvas->translate(SkIntToScalar(-40), SkIntToScalar(0)); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 34 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 35 | paint.setColor(SK_ColorBLUE); |
| 36 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 37 | if (gRecs[i].fStyle != NONE) { |
| 38 | paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle, |
| 39 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)))); |
| 40 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | paint.setMaskFilter(nullptr); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 42 | } |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 43 | canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100), |
| 44 | SkIntToScalar(200 + gRecs[i].fCy*100), |
| 45 | SkIntToScalar(50), |
| 46 | paint); |
| 47 | } |
| 48 | // draw text |
| 49 | { |
| 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, paint); |
| 56 | canvas->drawString("Hamburgefons Style", |
| 57 | x, y + SkIntToScalar(50), 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, paint); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 63 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 64 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 65 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 66 | ////////////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 67 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 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 | // |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 74 | DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 77 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 2.3f)); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 78 | |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 79 | SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); |
| 80 | SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 81 | SkPath path; |
| 82 | path.addRect(outer, SkPath::kCW_Direction); |
| 83 | path.addRect(inner, SkPath::kCCW_Direction); |
| 84 | |
| 85 | canvas->drawPath(path, paint); |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 86 | // important to translate by a factional amount to exercise a different "phase" |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 87 | // of the same path w.r.t. the pixel grid |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 88 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 89 | canvas->translate(dx, 0); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 90 | canvas->drawPath(path, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 91 | } |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 92 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 93 | DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 94 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 95 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4.3f)); |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 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); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 112 | } |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 113 | |
| 114 | DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) { |
| 115 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 116 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10)); |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 117 | canvas->clear(0xFF88FF88); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 118 | if (auto image = GetResourceAsImage("images/mandrill_512_q075.jpg")) { |
Hal Canary | 9982c4e | 2017-06-07 15:23:49 -0400 | [diff] [blame] | 119 | canvas->scale(0.25, 0.25); |
| 120 | canvas->drawImage(image, 256, 256, &paint); |
| 121 | } |
| 122 | } |