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