scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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/SkColorPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkPoint.h" |
| 19 | #include "include/core/SkRect.h" |
| 20 | #include "include/core/SkRefCnt.h" |
| 21 | #include "include/core/SkScalar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/core/SkShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "include/core/SkSize.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/core/SkString.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "include/core/SkTileMode.h" |
| 26 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/effects/SkGradientShader.h" |
| 28 | #include "include/private/SkTDArray.h" |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 29 | #include "src/core/SkTLazy.h" |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 30 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 33 | static sk_sp<SkShader> make_shader(SkBlendMode mode) { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 34 | SkPoint pts[2]; |
| 35 | SkColor colors[2]; |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 37 | pts[0].set(0, 0); |
| 38 | pts[1].set(SkIntToScalar(100), 0); |
| 39 | colors[0] = SK_ColorRED; |
| 40 | colors[1] = SK_ColorBLUE; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 41 | auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 43 | pts[0].set(0, 0); |
| 44 | pts[1].set(0, SkIntToScalar(100)); |
| 45 | colors[0] = SK_ColorBLACK; |
| 46 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 47 | auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 48 | |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 49 | return SkShaders::Blend(mode, std::move(shaderA), std::move(shaderB)); |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 50 | } |
| 51 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 52 | class ComposeShaderGM : public skiagm::GM { |
John Stiles | e6a05a4 | 2020-11-05 10:05:19 -0500 | [diff] [blame] | 53 | protected: |
| 54 | void onOnceBeforeDraw() override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 55 | fShader = make_shader(SkBlendMode::kDstIn); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 56 | } |
| 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | SkString onShortName() override { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 59 | return SkString("composeshader"); |
| 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | SkISize onISize() override { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 63 | return SkISize::Make(120, 120); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | void onDraw(SkCanvas* canvas) override { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 67 | SkPaint paint; |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 68 | paint.setColor(SK_ColorGREEN); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 69 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 70 | paint.setShader(fShader); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 71 | canvas->drawRect(SkRect::MakeWH(100, 100), paint); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 72 | } |
| 73 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 74 | protected: |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 75 | sk_sp<SkShader> fShader; |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 78 | typedef GM INHERITED ; |
| 79 | }; |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 80 | DEF_GM( return new ComposeShaderGM; ) |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 82 | class ComposeShaderAlphaGM : public skiagm::GM { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 83 | public: |
| 84 | ComposeShaderAlphaGM() {} |
| 85 | |
| 86 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | SkString onShortName() override { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 88 | return SkString("composeshader_alpha"); |
| 89 | } |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 90 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 91 | SkISize onISize() override { |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 92 | return SkISize::Make(750, 220); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 93 | } |
reed@google.com | 775bf66 | 2014-05-05 19:37:22 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | void onDraw(SkCanvas* canvas) override { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 96 | sk_sp<SkShader> shaders[] = { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 97 | make_shader(SkBlendMode::kDstIn), |
| 98 | make_shader(SkBlendMode::kSrcOver), |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 99 | }; |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 100 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 101 | SkPaint paint; |
| 102 | paint.setColor(SK_ColorGREEN); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 104 | const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 105 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 106 | for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 107 | canvas->save(); |
| 108 | for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 109 | paint.setAlphaf(1.0f); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | paint.setShader(nullptr); |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 111 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 112 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 113 | paint.setAlpha(alpha); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 114 | paint.setShader(shaders[y]); |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 115 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 116 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 117 | canvas->translate(r.width() + 5, 0); |
| 118 | } |
| 119 | canvas->restore(); |
| 120 | canvas->translate(0, r.height() + 5); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | typedef GM INHERITED ; |
| 126 | }; |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 127 | DEF_GM( return new ComposeShaderAlphaGM; ) |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 128 | |
| 129 | // creates a square bitmap with red background and a green circle in the center |
| 130 | static void draw_color_bm(SkBitmap* bm, int length) { |
| 131 | SkPaint paint; |
| 132 | paint.setColor(SK_ColorGREEN); |
| 133 | |
| 134 | bm->allocN32Pixels(length, length); |
| 135 | bm->eraseColor(SK_ColorRED); |
| 136 | |
| 137 | SkCanvas canvas(*bm); |
| 138 | canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2), |
| 139 | paint); |
| 140 | } |
| 141 | |
| 142 | // creates a square alpha8 bitmap with transparent background and an opaque circle in the center |
| 143 | static void draw_alpha8_bm(SkBitmap* bm, int length) { |
| 144 | SkPaint circlePaint; |
| 145 | circlePaint.setColor(SK_ColorBLACK); |
| 146 | |
| 147 | bm->allocPixels(SkImageInfo::MakeA8(length, length)); |
| 148 | bm->eraseColor(SK_ColorTRANSPARENT); |
| 149 | |
| 150 | SkCanvas canvas(*bm); |
| 151 | canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4), |
| 152 | circlePaint); |
| 153 | } |
| 154 | |
| 155 | // creates a linear gradient shader |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 156 | static sk_sp<SkShader> make_linear_gradient_shader(int length) { |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 157 | SkPoint pts[2]; |
| 158 | SkColor colors[2]; |
| 159 | pts[0].set(0, 0); |
| 160 | pts[1].set(SkIntToScalar(length), 0); |
| 161 | colors[0] = SK_ColorBLUE; |
| 162 | colors[1] = SkColorSetARGB(0, 0, 0, 0xFF); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 163 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | class ComposeShaderBitmapGM : public skiagm::GM { |
| 168 | public: |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 169 | ComposeShaderBitmapGM(bool use_lm) : fUseLocalMatrix(use_lm) {} |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 170 | |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 171 | protected: |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 172 | void onOnceBeforeDraw() override { |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 173 | draw_color_bm(&fColorBitmap, squareLength); |
| 174 | draw_alpha8_bm(&fAlpha8Bitmap, squareLength); |
| 175 | SkMatrix s; |
| 176 | s.reset(); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 177 | fColorBitmapShader = fColorBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 178 | SkSamplingOptions(), s); |
| 179 | fAlpha8BitmapShader = fAlpha8Bitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 180 | SkSamplingOptions(), s); |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 181 | fLinearGradientShader = make_linear_gradient_shader(squareLength); |
| 182 | } |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 183 | |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 184 | SkString onShortName() override { |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 185 | return SkStringPrintf("composeshader_bitmap%s", fUseLocalMatrix ? "_lm" : ""); |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | SkISize onISize() override { |
| 189 | return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5)); |
| 190 | } |
| 191 | |
| 192 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 193 | SkBlendMode mode = SkBlendMode::kDstOver; |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 194 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 195 | SkMatrix lm = SkMatrix::Translate(0, squareLength * 0.5f); |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 196 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 197 | sk_sp<SkShader> shaders[] = { |
| 198 | // gradient should appear over color bitmap |
Kevin Lubick | 988ce04 | 2020-03-25 13:13:20 -0400 | [diff] [blame] | 199 | SkShaders::Blend(mode, fLinearGradientShader, fColorBitmapShader), |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 200 | // gradient should appear over alpha8 bitmap colorized by the paint color |
Kevin Lubick | 988ce04 | 2020-03-25 13:13:20 -0400 | [diff] [blame] | 201 | SkShaders::Blend(mode, fLinearGradientShader, fAlpha8BitmapShader), |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 202 | }; |
Kevin Lubick | 988ce04 | 2020-03-25 13:13:20 -0400 | [diff] [blame] | 203 | if (fUseLocalMatrix) { |
| 204 | for (unsigned i = 0; i < SK_ARRAY_COUNT(shaders); ++i) { |
| 205 | shaders[i] = shaders[i]->makeWithLocalMatrix(lm); |
| 206 | } |
| 207 | } |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 208 | |
| 209 | SkPaint paint; |
| 210 | paint.setColor(SK_ColorYELLOW); |
| 211 | |
Kevin Lubick | 988ce04 | 2020-03-25 13:13:20 -0400 | [diff] [blame] | 212 | const SkRect r = SkRect::MakeIWH(squareLength, squareLength); |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 213 | |
| 214 | for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 215 | canvas->save(); |
| 216 | for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 217 | paint.setAlpha(alpha); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 218 | paint.setShader(shaders[y]); |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 219 | canvas->drawRect(r, paint); |
| 220 | |
| 221 | canvas->translate(r.width() + 5, 0); |
| 222 | } |
| 223 | canvas->restore(); |
| 224 | canvas->translate(0, r.height() + 5); |
| 225 | } |
| 226 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 227 | |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 228 | private: |
reed | 8681e60 | 2016-02-15 08:27:14 -0800 | [diff] [blame] | 229 | /** This determines the length and width of the bitmaps used in the ComposeShaders. Values |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 230 | * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will |
| 231 | * work in a release build. You can change this parameter and then compile a release build |
| 232 | * to have this GM draw larger bitmaps for easier visual inspection. |
| 233 | */ |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 234 | static constexpr int squareLength = 20; |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 235 | |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 236 | const bool fUseLocalMatrix; |
| 237 | |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 238 | SkBitmap fColorBitmap; |
| 239 | SkBitmap fAlpha8Bitmap; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 240 | sk_sp<SkShader> fColorBitmapShader; |
| 241 | sk_sp<SkShader> fAlpha8BitmapShader; |
| 242 | sk_sp<SkShader> fLinearGradientShader; |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 243 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 244 | using INHERITED = GM; |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 245 | }; |
Florin Malita | 9ab449a | 2019-07-09 13:22:50 -0400 | [diff] [blame] | 246 | DEF_GM( return new ComposeShaderBitmapGM(false); ) |
| 247 | DEF_GM( return new ComposeShaderBitmapGM(true); ) |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 248 | |
caryclark | c2ad65e | 2016-08-15 12:03:33 -0700 | [diff] [blame] | 249 | DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) { |
| 250 | int width = 255; |
| 251 | int height = 255; |
| 252 | SkTDArray<uint8_t> dst8Storage; |
| 253 | dst8Storage.setCount(width * height); |
| 254 | SkTDArray<uint32_t> dst32Storage; |
| 255 | dst32Storage.setCount(width * height * sizeof(int32_t)); |
| 256 | for (int y = 0; y < height; ++y) { |
| 257 | for (int x = 0; x < width; ++x) { |
| 258 | dst8Storage[y * width + x] = (y + x) / 2; |
| 259 | dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0); |
| 260 | } |
| 261 | } |
| 262 | SkPaint paint; |
| 263 | paint.setAntiAlias(true); |
| 264 | paint.setColor(SK_ColorBLUE); |
| 265 | SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)}; |
| 266 | canvas->drawRect(r, paint); |
| 267 | SkBitmap skBitmap, skMask; |
| 268 | SkImageInfo imageInfo = SkImageInfo::Make(width, height, |
| 269 | SkColorType::kN32_SkColorType, kPremul_SkAlphaType); |
| 270 | skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t), |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 271 | nullptr, nullptr); |
caryclark | c2ad65e | 2016-08-15 12:03:33 -0700 | [diff] [blame] | 272 | imageInfo = SkImageInfo::Make(width, height, |
| 273 | SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType); |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 274 | skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr); |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 275 | sk_sp<SkImage> skSrc = skBitmap.asImage(); |
| 276 | sk_sp<SkImage> skMaskImage = skMask.asImage(); |
caryclark | c2ad65e | 2016-08-15 12:03:33 -0700 | [diff] [blame] | 277 | paint.setShader( |
Mike Reed | b612b6c | 2020-12-08 21:58:35 -0500 | [diff] [blame] | 278 | SkShaders::Blend(SkBlendMode::kSrcIn, |
| 279 | skMaskImage->makeShader(SkSamplingOptions()), |
| 280 | skSrc->makeShader(SkSamplingOptions()))); |
caryclark | c2ad65e | 2016-08-15 12:03:33 -0700 | [diff] [blame] | 281 | canvas->drawRect(r, paint); |
| 282 | } |
| 283 | |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 284 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 285 | |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 286 | static sk_sp<SkShader> make_src_shader(SkScalar size) { |
| 287 | const SkPoint pts[] = { { 0, 0 }, { 0, size } }; |
| 288 | const SkColor colors[] = { 0xFF0000FF, 0x000000FF }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 289 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static sk_sp<SkShader> make_dst_shader(SkScalar size) { |
| 293 | const SkPoint pts[] = { { 0, 0 }, { size, 0 } }; |
| 294 | const SkColor colors[] = { SK_ColorRED, 0x00FF0000 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 295 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | const SkScalar gCellSize = 100; |
| 299 | |
| 300 | static void draw_cell(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst, |
| 301 | SkBlendMode mode, SkAlpha alpha) { |
| 302 | const SkRect r = SkRect::MakeWH(gCellSize, gCellSize); |
| 303 | SkPaint p; |
| 304 | p.setAlpha(alpha); |
| 305 | |
| 306 | SkAutoCanvasRestore acr(canvas, false); |
| 307 | canvas->saveLayer(&r, &p); |
| 308 | p.setAlpha(0xFF); |
| 309 | |
| 310 | p.setShader(dst); |
| 311 | p.setBlendMode(SkBlendMode::kSrc); |
| 312 | canvas->drawRect(r, p); |
| 313 | |
| 314 | p.setShader(src); |
| 315 | p.setBlendMode(mode); |
| 316 | canvas->drawRect(r, p); |
| 317 | } |
| 318 | |
| 319 | static void draw_composed(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst, |
| 320 | SkBlendMode mode, SkAlpha alpha) { |
| 321 | SkPaint p; |
| 322 | p.setAlpha(alpha); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 323 | p.setShader(SkShaders::Blend(mode, dst, src)); |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 324 | canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p); |
| 325 | } |
| 326 | |
| 327 | static void draw_pair(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst, |
| 328 | SkBlendMode mode) { |
| 329 | SkAutoCanvasRestore acr(canvas, true); |
| 330 | |
| 331 | const SkScalar gap = 4; |
| 332 | SkRect r = SkRect::MakeWH(2 * gCellSize + gap, 2 * gCellSize + gap); |
Mike Reed | 1d08d72 | 2018-02-02 16:15:49 -0500 | [diff] [blame] | 333 | r.outset(gap + 1.5f, gap + 1.5f); |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 334 | SkPaint p; |
| 335 | p.setStyle(SkPaint::kStroke_Style); |
| 336 | canvas->drawRect(r, p); // border |
| 337 | |
| 338 | SkAlpha alpha = 0xFF; |
| 339 | for (int y = 0; y < 2; ++y) { |
| 340 | draw_cell(canvas, src, dst, mode, alpha); |
| 341 | canvas->save(); |
| 342 | canvas->translate(gCellSize + gap, 0); |
| 343 | draw_composed(canvas, src, dst, mode, alpha); |
| 344 | canvas->restore(); |
| 345 | |
| 346 | canvas->translate(0, gCellSize + gap); |
| 347 | alpha = 0x80; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) { |
| 352 | auto src = make_src_shader(gCellSize); |
| 353 | auto dst = make_dst_shader(gCellSize); |
| 354 | |
Mike Reed | 1d08d72 | 2018-02-02 16:15:49 -0500 | [diff] [blame] | 355 | const SkScalar margin = 15; |
Mike Reed | 27738e0 | 2018-02-02 14:36:05 -0500 | [diff] [blame] | 356 | const SkScalar dx = 2*gCellSize + margin; |
| 357 | const SkScalar dy = 2*gCellSize + margin; |
| 358 | |
| 359 | canvas->translate(margin, margin); |
| 360 | canvas->save(); |
| 361 | for (int m = 0; m < 16; ++m) { |
| 362 | SkBlendMode mode = static_cast<SkBlendMode>(m); |
| 363 | draw_pair(canvas, src, dst, mode); |
| 364 | if ((m % 4) == 3) { |
| 365 | canvas->restore(); |
| 366 | canvas->translate(0, dy); |
| 367 | canvas->save(); |
| 368 | } else { |
| 369 | canvas->translate(dx, 0); |
| 370 | } |
| 371 | } |
| 372 | canvas->restore(); |
| 373 | } |