reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlendMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorFilter.h" |
| 14 | #include "include/core/SkFont.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkMaskFilter.h" |
| 16 | #include "include/core/SkPaint.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkShader.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 21 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/core/SkBlurMask.h" |
| 24 | #include "src/effects/SkEmbossMaskFilter.h" |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 25 | |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 26 | static sk_sp<SkImage> make_bm() { |
| 27 | auto surf = SkSurface::MakeRasterN32Premul(100, 100); |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 28 | |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 29 | SkPaint paint; |
| 30 | paint.setAntiAlias(true); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 31 | surf->getCanvas()->drawCircle(50, 50, 50, paint); |
| 32 | return surf->makeImageSnapshot(); |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | class EmbossGM : public skiagm::GM { |
| 36 | public: |
| 37 | EmbossGM() { |
| 38 | } |
| 39 | |
| 40 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | SkString onShortName() override { |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 42 | return SkString("emboss"); |
| 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | SkISize onISize() override { |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 46 | return SkISize::Make(600, 120); |
| 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | void onDraw(SkCanvas* canvas) override { |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 50 | SkPaint paint; |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 51 | auto img = make_bm(); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 52 | canvas->drawImage(img, 10, 10); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 53 | canvas->translate(img->width() + SkIntToScalar(10), 0); |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 54 | |
| 55 | paint.setMaskFilter(SkEmbossMaskFilter::Make( |
| 56 | SkBlurMask::ConvertRadiusToSigma(3), |
| 57 | { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 })); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 58 | canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 59 | canvas->translate(img->width() + SkIntToScalar(10), 0); |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 60 | |
| 61 | // this combination of emboss+colorfilter used to crash -- so we exercise it to |
| 62 | // confirm that we have a fix. |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 63 | paint.setColorFilter(SkColorFilters::Blend(0xFFFF0000, SkBlendMode::kSrcATop)); |
Mike Reed | 039f136 | 2021-01-27 21:21:08 -0500 | [diff] [blame] | 64 | canvas->drawImage(img, 10, 10, SkSamplingOptions(), &paint); |
Mike Reed | 34a0c97 | 2021-01-25 17:49:32 -0500 | [diff] [blame] | 65 | canvas->translate(img->width() + SkIntToScalar(10), 0); |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 66 | |
| 67 | paint.setAntiAlias(true); |
| 68 | paint.setStyle(SkPaint::kStroke_Style); |
| 69 | paint.setStrokeWidth(SkIntToScalar(10)); |
| 70 | paint.setMaskFilter(SkEmbossMaskFilter::Make( |
| 71 | SkBlurMask::ConvertRadiusToSigma(4), |
| 72 | { { SK_Scalar1, SK_Scalar1, SK_Scalar1 }, 0, 128, 16*2 })); |
| 73 | paint.setColorFilter(nullptr); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 74 | paint.setShader(SkShaders::Color(SK_ColorBLUE)); |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 75 | paint.setDither(true); |
| 76 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 77 | SkIntToScalar(30), paint); |
| 78 | canvas->translate(SkIntToScalar(100), 0); |
| 79 | |
| 80 | paint.setStyle(SkPaint::kFill_Style); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 81 | canvas->drawString("Hello", 0, 50, SkFont(nullptr, 50), paint); |
Mike Klein | 278615a | 2019-10-29 14:15:19 -0500 | [diff] [blame] | 82 | |
| 83 | paint.setShader(nullptr); |
| 84 | paint.setColor(SK_ColorGREEN); |
| 85 | canvas->drawString("World", 0, 100, SkFont(nullptr, 50), paint); |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 89 | using INHERITED = skiagm::GM; |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 92 | DEF_GM(return new EmbossGM;) |