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