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 | |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 10 | #include "SkBitmapProcShader.h" |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkComposeShader.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
| 15 | #include "SkShader.h" |
| 16 | #include "SkString.h" |
| 17 | #include "SkXfermode.h" |
| 18 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 19 | static SkShader* make_shader(SkXfermode::Mode mode) { |
| 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; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, nullptr, 2, |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 28 | SkShader::kClamp_TileMode)); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 30 | pts[0].set(0, 0); |
| 31 | pts[1].set(0, SkIntToScalar(100)); |
| 32 | colors[0] = SK_ColorBLACK; |
| 33 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, nullptr, 2, |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 35 | SkShader::kClamp_TileMode)); |
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 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode)); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 38 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 39 | return new SkComposeShader(shaderA, shaderB, xfer); |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 40 | } |
| 41 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 42 | class ComposeShaderGM : public skiagm::GM { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 43 | public: |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 44 | ComposeShaderGM() { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 45 | fShader = make_shader(SkXfermode::kDstIn_Mode); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 46 | } |
| 47 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 48 | virtual ~ComposeShaderGM() { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 49 | SkSafeUnref(fShader); |
| 50 | } |
| 51 | |
| 52 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | SkString onShortName() override { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 54 | return SkString("composeshader"); |
| 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | SkISize onISize() override { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 58 | return SkISize::Make(120, 120); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) override { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 62 | |
| 63 | SkPaint paint; |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 64 | |
| 65 | paint.setColor(SK_ColorGREEN); |
| 66 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 67 | paint.setShader(fShader); |
| 68 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 69 | } |
| 70 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 71 | protected: |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 72 | SkShader* fShader; |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 75 | typedef GM INHERITED ; |
| 76 | }; |
| 77 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 78 | class ComposeShaderAlphaGM : public skiagm::GM { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 79 | public: |
| 80 | ComposeShaderAlphaGM() {} |
| 81 | |
| 82 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | SkString onShortName() override { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 84 | return SkString("composeshader_alpha"); |
| 85 | } |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 86 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | SkISize onISize() override { |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 88 | return SkISize::Make(750, 220); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 89 | } |
reed@google.com | 775bf66 | 2014-05-05 19:37:22 +0000 | [diff] [blame] | 90 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 91 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 92 | SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); |
| 93 | SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); |
| 94 | SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 95 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 96 | SkPaint paint; |
| 97 | paint.setColor(SK_ColorGREEN); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 99 | const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); |
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 | for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
| 102 | SkShader* shader = shaders[y]; |
| 103 | canvas->save(); |
| 104 | for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 105 | paint.setAlpha(0xFF); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | paint.setShader(nullptr); |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 107 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 109 | paint.setAlpha(alpha); |
| 110 | paint.setShader(shader); |
| 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 | canvas->translate(r.width() + 5, 0); |
| 114 | } |
| 115 | canvas->restore(); |
| 116 | canvas->translate(0, r.height() + 5); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | typedef GM INHERITED ; |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | // creates a square bitmap with red background and a green circle in the center |
| 126 | static void draw_color_bm(SkBitmap* bm, int length) { |
| 127 | SkPaint paint; |
| 128 | paint.setColor(SK_ColorGREEN); |
| 129 | |
| 130 | bm->allocN32Pixels(length, length); |
| 131 | bm->eraseColor(SK_ColorRED); |
| 132 | |
| 133 | SkCanvas canvas(*bm); |
| 134 | canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2), |
| 135 | paint); |
| 136 | } |
| 137 | |
| 138 | // creates a square alpha8 bitmap with transparent background and an opaque circle in the center |
| 139 | static void draw_alpha8_bm(SkBitmap* bm, int length) { |
| 140 | SkPaint circlePaint; |
| 141 | circlePaint.setColor(SK_ColorBLACK); |
| 142 | |
| 143 | bm->allocPixels(SkImageInfo::MakeA8(length, length)); |
| 144 | bm->eraseColor(SK_ColorTRANSPARENT); |
| 145 | |
| 146 | SkCanvas canvas(*bm); |
| 147 | canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4), |
| 148 | circlePaint); |
| 149 | } |
| 150 | |
| 151 | // creates a linear gradient shader |
| 152 | static SkShader* make_linear_gradient_shader(int length) { |
| 153 | SkPoint pts[2]; |
| 154 | SkColor colors[2]; |
| 155 | pts[0].set(0, 0); |
| 156 | pts[1].set(SkIntToScalar(length), 0); |
| 157 | colors[0] = SK_ColorBLUE; |
| 158 | colors[1] = SkColorSetARGB(0, 0, 0, 0xFF); |
| 159 | return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | class ComposeShaderBitmapGM : public skiagm::GM { |
| 164 | public: |
| 165 | ComposeShaderBitmapGM() { |
| 166 | draw_color_bm(&fColorBitmap, squareLength); |
| 167 | draw_alpha8_bm(&fAlpha8Bitmap, squareLength); |
| 168 | SkMatrix s; |
| 169 | s.reset(); |
| 170 | fColorBitmapShader = new SkBitmapProcShader(fColorBitmap, SkShader::kRepeat_TileMode, |
| 171 | SkShader::kRepeat_TileMode, &s); |
| 172 | fAlpha8BitmapShader = new SkBitmapProcShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode, |
| 173 | SkShader::kRepeat_TileMode, &s); |
| 174 | fLinearGradientShader = make_linear_gradient_shader(squareLength); |
| 175 | } |
| 176 | ~ComposeShaderBitmapGM() { |
| 177 | SkSafeUnref(fColorBitmapShader); |
| 178 | SkSafeUnref(fAlpha8BitmapShader); |
| 179 | SkSafeUnref(fLinearGradientShader); |
| 180 | } |
| 181 | |
| 182 | protected: |
| 183 | SkString onShortName() override { |
| 184 | return SkString("composeshader_bitmap"); |
| 185 | } |
| 186 | |
| 187 | SkISize onISize() override { |
| 188 | return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5)); |
| 189 | } |
| 190 | |
| 191 | void onDraw(SkCanvas* canvas) override { |
| 192 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode)); |
| 193 | |
| 194 | // gradient should appear over color bitmap |
| 195 | SkAutoTUnref<SkShader> shader0(new SkComposeShader(fLinearGradientShader, |
| 196 | fColorBitmapShader, |
| 197 | xfer)); |
| 198 | // gradient should appear over alpha8 bitmap colorized by the paint color |
| 199 | SkAutoTUnref<SkShader> shader1(new SkComposeShader(fLinearGradientShader, |
| 200 | fAlpha8BitmapShader, |
| 201 | xfer)); |
| 202 | |
| 203 | SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 204 | |
| 205 | SkPaint paint; |
| 206 | paint.setColor(SK_ColorYELLOW); |
| 207 | |
| 208 | const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength), |
| 209 | SkIntToScalar(squareLength)); |
| 210 | |
| 211 | for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
| 212 | SkShader* shader = shaders[y]; |
| 213 | canvas->save(); |
| 214 | for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 215 | paint.setAlpha(alpha); |
| 216 | paint.setShader(shader); |
| 217 | canvas->drawRect(r, paint); |
| 218 | |
| 219 | canvas->translate(r.width() + 5, 0); |
| 220 | } |
| 221 | canvas->restore(); |
| 222 | canvas->translate(0, r.height() + 5); |
| 223 | } |
| 224 | } |
| 225 | private: |
| 226 | /** This determines the length and width of the bitmaps used in the SkComposeShaders. Values |
| 227 | * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will |
| 228 | * work in a release build. You can change this parameter and then compile a release build |
| 229 | * to have this GM draw larger bitmaps for easier visual inspection. |
| 230 | */ |
| 231 | static const int squareLength = 20; |
| 232 | |
| 233 | SkBitmap fColorBitmap; |
| 234 | SkBitmap fAlpha8Bitmap; |
| 235 | SkShader* fColorBitmapShader; |
| 236 | SkShader* fAlpha8BitmapShader; |
| 237 | SkShader* fLinearGradientShader; |
| 238 | |
| 239 | typedef GM INHERITED; |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 242 | ////////////////////////////////////////////////////////////////////////////// |
| 243 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 244 | DEF_GM( return new ComposeShaderGM; ) |
| 245 | DEF_GM( return new ComposeShaderAlphaGM; ) |
wangyix | d705958 | 2015-09-03 08:32:22 -0700 | [diff] [blame] | 246 | DEF_GM( return new ComposeShaderBitmapGM; ) |