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