Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 8 | #include "SkBlurImageFilter.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 9 | #include "SkMaskFilter.h" |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 10 | #include "gm.h" |
| 11 | #include "sk_tool_utils.h" |
| 12 | |
| 13 | |
| 14 | DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) { |
| 15 | SkPaint paint; |
| 16 | paint.setAntiAlias(true); |
| 17 | paint.setColor(SK_ColorBLACK); |
| 18 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 19 | SkFont font(sk_tool_utils::create_portable_typeface(), 25); |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 20 | |
| 21 | const double sigmas[] = {3.0, 8.0, 16.0, 24.0, 32.0}; |
| 22 | |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 23 | canvas->drawString("mask blur", 285, 50, font, paint); |
| 24 | canvas->drawString("image blur", 285 + 250, 50, font, paint); |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 25 | |
| 26 | |
| 27 | SkRect r = {35, 100, 135, 200}; |
| 28 | for (auto sigma:sigmas) { |
| 29 | |
| 30 | canvas->drawRect(r, paint); |
| 31 | |
| 32 | char out[100]; |
| 33 | sprintf(out, "Sigma: %g", sigma); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 34 | canvas->drawString(out, r.left(), r.bottom() + 35, font, paint); |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 35 | |
| 36 | r.offset(250, 0); |
| 37 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 38 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma)); |
Herb Derby | a48ae6e | 2017-07-10 13:49:05 -0400 | [diff] [blame] | 39 | canvas->drawRect(r, paint); |
| 40 | paint.setMaskFilter(nullptr); |
| 41 | |
| 42 | SkPaint imageBlurPaint; |
| 43 | r.offset(250, 0); |
| 44 | imageBlurPaint.setImageFilter(SkBlurImageFilter::Make(sigma, sigma, nullptr)); |
| 45 | canvas->saveLayer(nullptr, &imageBlurPaint); |
| 46 | |
| 47 | canvas->drawRect(r, paint); |
| 48 | canvas->restore(); |
| 49 | r.offset(-500, 200); |
| 50 | } |
| 51 | |
| 52 | } |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 53 | |
| 54 | #include "Resources.h" |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 55 | DEF_SIMPLE_GM_CAN_FAIL(blur_image, canvas, errorMsg, 500, 500) { |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 56 | auto image = GetResourceAsImage("images/mandrill_128.png"); |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 57 | if (!image) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 58 | *errorMsg = "Could not load mandrill_128.png. Did you forget to set the resourcePath?"; |
| 59 | return skiagm::DrawResult::kFail; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 60 | } |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 61 | |
| 62 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 63 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 4)); |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 64 | |
| 65 | // both of these should draw with the blur, but (formerally) we had a bug where the unscaled |
| 66 | // version (taking the spriteblitter code path) ignore the maskfilter. |
| 67 | |
| 68 | canvas->drawImage(image, 10, 10, &paint); |
| 69 | canvas->scale(1.01f, 1.01f); |
| 70 | canvas->drawImage(image, 10 + image->width() + 10.f, 10, &paint); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 71 | return skiagm::DrawResult::kOk; |
Mike Reed | 2826627 | 2018-01-17 23:04:04 -0500 | [diff] [blame] | 72 | } |