scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "gm.h" |
| 10 | |
| 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 | |
| 19 | namespace skiagm { |
| 20 | |
| 21 | class ShaderGM : public GM { |
| 22 | public: |
| 23 | ShaderGM() { |
| 24 | SkPoint pts[2]; |
| 25 | SkColor colors[2]; |
| 26 | |
| 27 | pts[0].set(0, 0); |
| 28 | pts[1].set(SkIntToScalar(100), 0); |
| 29 | colors[0] = SK_ColorRED; |
| 30 | colors[1] = SK_ColorBLUE; |
| 31 | SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 32 | SkShader::kClamp_TileMode); |
| 33 | |
| 34 | pts[0].set(0, 0); |
| 35 | pts[1].set(0, SkIntToScalar(100)); |
| 36 | colors[0] = SK_ColorBLACK; |
| 37 | colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
| 38 | SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
| 39 | SkShader::kClamp_TileMode); |
| 40 | |
| 41 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
| 42 | |
| 43 | fShader = new SkComposeShader(shaderA, shaderB, mode); |
| 44 | shaderA->unref(); |
| 45 | shaderB->unref(); |
| 46 | mode->unref(); |
| 47 | } |
| 48 | |
| 49 | virtual ~ShaderGM() { |
| 50 | SkSafeUnref(fShader); |
| 51 | } |
| 52 | |
| 53 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 54 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 55 | return kSkipTiled_Flag; |
| 56 | } |
| 57 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 58 | virtual SkString onShortName() SK_OVERRIDE { |
| 59 | return SkString("composeshader"); |
| 60 | } |
| 61 | |
| 62 | virtual SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | 5d0b150 | 2014-04-14 15:02:19 +0000 | [diff] [blame] | 63 | return make_isize(120, 120); |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 66 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 67 | |
| 68 | SkPaint paint; |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 69 | |
| 70 | paint.setColor(SK_ColorGREEN); |
| 71 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 72 | paint.setShader(fShader); |
| 73 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | SkShader* fShader; |
| 78 | typedef GM INHERITED ; |
| 79 | }; |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | static GM* MyFactory(void*) { return new ShaderGM; } |
| 84 | static GMRegistry reg(MyFactory); |
| 85 | |
| 86 | } // namespace |