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" |
| 11 | #include "SkComposeShader.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
| 14 | #include "SkShader.h" |
| 15 | #include "SkString.h" |
| 16 | #include "SkXfermode.h" |
| 17 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 18 | static SkShader* make_shader(SkXfermode::Mode mode) { |
| 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; |
| 26 | SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 27 | 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); |
| 33 | SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 34 | SkShader::kClamp_TileMode)); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 36 | SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode)); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 38 | return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer)); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 41 | class ComposeShaderGM : public skiagm::GM { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 42 | public: |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 43 | ComposeShaderGM() { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 44 | fShader = make_shader(SkXfermode::kDstIn_Mode); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 47 | virtual ~ComposeShaderGM() { |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 48 | SkSafeUnref(fShader); |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | virtual SkString onShortName() SK_OVERRIDE { |
| 53 | return SkString("composeshader"); |
| 54 | } |
| 55 | |
| 56 | virtual SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 57 | return SkISize::Make(120, 120); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 58 | } |
| 59 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 60 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 61 | |
| 62 | SkPaint paint; |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 63 | |
| 64 | paint.setColor(SK_ColorGREEN); |
| 65 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 66 | paint.setShader(fShader); |
| 67 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 68 | } |
| 69 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 70 | protected: |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 71 | SkShader* fShader; |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 72 | |
| 73 | private: |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 74 | typedef GM INHERITED ; |
| 75 | }; |
| 76 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 77 | class ComposeShaderAlphaGM : public skiagm::GM { |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 78 | public: |
| 79 | ComposeShaderAlphaGM() {} |
| 80 | |
| 81 | protected: |
| 82 | virtual SkString onShortName() SK_OVERRIDE { |
| 83 | return SkString("composeshader_alpha"); |
| 84 | } |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 85 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 86 | virtual SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 87 | return SkISize::Make(220, 750); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 88 | } |
reed@google.com | 775bf66 | 2014-05-05 19:37:22 +0000 | [diff] [blame] | 89 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 90 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 91 | SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); |
| 92 | SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); |
| 93 | SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 94 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 95 | SkPaint paint; |
| 96 | paint.setColor(SK_ColorGREEN); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 98 | const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); |
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 | for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
| 101 | SkShader* shader = shaders[y]; |
| 102 | canvas->save(); |
| 103 | for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
| 104 | paint.setAlpha(0xFF); |
| 105 | paint.setShader(NULL); |
| 106 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 108 | paint.setAlpha(alpha); |
| 109 | paint.setShader(shader); |
| 110 | canvas->drawRect(r, paint); |
skia.committer@gmail.com | edda70e | 2014-05-06 03:06:53 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | 58e4287 | 2014-05-05 18:23:54 +0000 | [diff] [blame] | 112 | canvas->translate(r.width() + 5, 0); |
| 113 | } |
| 114 | canvas->restore(); |
| 115 | canvas->translate(0, r.height() + 5); |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
commit-bot@chromium.org | ec7d673 | 2014-05-05 16:19:08 +0000 | [diff] [blame] | 122 | DEF_GM( return new ComposeShaderGM; ) |
| 123 | DEF_GM( return new ComposeShaderAlphaGM; ) |