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