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: |
| 54 | virtual SkString onShortName() SK_OVERRIDE { |
| 55 | return SkString("composeshader"); |
| 56 | } |
| 57 | |
| 58 | virtual SkISize onISize() SK_OVERRIDE { |
| 59 | return make_isize(640, 480); |
| 60 | } |
| 61 | |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 62 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 63 | |
| 64 | SkPaint paint; |
scroggo@google.com | 32f20eb | 2012-06-29 17:28:50 +0000 | [diff] [blame] | 65 | |
| 66 | paint.setColor(SK_ColorGREEN); |
| 67 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 68 | paint.setShader(fShader); |
| 69 | canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | SkShader* fShader; |
| 74 | typedef GM INHERITED ; |
| 75 | }; |
| 76 | |
| 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | static GM* MyFactory(void*) { return new ShaderGM; } |
| 80 | static GMRegistry reg(MyFactory); |
| 81 | |
| 82 | } // namespace |