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