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