OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 #include "gm.h" | 8 #include "gm.h" |
10 | 9 |
11 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
12 #include "SkComposeShader.h" | 11 #include "SkComposeShader.h" |
13 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
14 #include "SkGraphics.h" | 13 #include "SkGraphics.h" |
15 #include "SkShader.h" | 14 #include "SkShader.h" |
16 #include "SkString.h" | 15 #include "SkString.h" |
17 #include "SkXfermode.h" | 16 #include "SkXfermode.h" |
18 | 17 |
19 namespace skiagm { | 18 class ComposeShaderGM : public skiagm::GM { |
20 | |
21 class ShaderGM : public GM { | |
22 public: | 19 public: |
23 ShaderGM() { | 20 ComposeShaderGM() { |
24 SkPoint pts[2]; | 21 SkPoint pts[2]; |
25 SkColor colors[2]; | 22 SkColor colors[2]; |
26 | 23 |
27 pts[0].set(0, 0); | 24 pts[0].set(0, 0); |
28 pts[1].set(SkIntToScalar(100), 0); | 25 pts[1].set(SkIntToScalar(100), 0); |
29 colors[0] = SK_ColorRED; | 26 colors[0] = SK_ColorRED; |
30 colors[1] = SK_ColorBLUE; | 27 colors[1] = SK_ColorBLUE; |
31 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, | 28 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
32 SkShader::kClamp_Tile
Mode); | 29 SkShader::kClamp_Tile
Mode); |
33 | 30 |
34 pts[0].set(0, 0); | 31 pts[0].set(0, 0); |
35 pts[1].set(0, SkIntToScalar(100)); | 32 pts[1].set(0, SkIntToScalar(100)); |
36 colors[0] = SK_ColorBLACK; | 33 colors[0] = SK_ColorBLACK; |
37 colors[1] = SkColorSetARGB(0x80, 0, 0, 0); | 34 colors[1] = SkColorSetARGB(0x80, 0, 0, 0); |
38 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, | 35 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, |
39 SkShader::kClamp_Tile
Mode); | 36 SkShader::kClamp_Tile
Mode); |
40 | 37 |
41 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); | 38 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); |
42 | 39 |
43 fShader = new SkComposeShader(shaderA, shaderB, mode); | 40 fShader = new SkComposeShader(shaderA, shaderB, mode); |
44 shaderA->unref(); | 41 shaderA->unref(); |
45 shaderB->unref(); | 42 shaderB->unref(); |
46 mode->unref(); | 43 mode->unref(); |
47 } | 44 } |
48 | 45 |
49 virtual ~ShaderGM() { | 46 virtual ~ComposeShaderGM() { |
50 SkSafeUnref(fShader); | 47 SkSafeUnref(fShader); |
51 } | 48 } |
52 | 49 |
53 protected: | 50 protected: |
54 virtual uint32_t onGetFlags() const SK_OVERRIDE { | 51 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
55 return kSkipTiled_Flag; | 52 return kSkipTiled_Flag; |
56 } | 53 } |
57 | 54 |
58 virtual SkString onShortName() SK_OVERRIDE { | 55 virtual SkString onShortName() SK_OVERRIDE { |
59 return SkString("composeshader"); | 56 return SkString("composeshader"); |
60 } | 57 } |
61 | 58 |
62 virtual SkISize onISize() SK_OVERRIDE { | 59 virtual SkISize onISize() SK_OVERRIDE { |
63 return make_isize(120, 120); | 60 return SkISize::Make(120, 120); |
64 } | 61 } |
65 | 62 |
66 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 63 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
67 | 64 |
68 SkPaint paint; | 65 SkPaint paint; |
69 | 66 |
70 paint.setColor(SK_ColorGREEN); | 67 paint.setColor(SK_ColorGREEN); |
71 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); | 68 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); |
72 paint.setShader(fShader); | 69 paint.setShader(fShader); |
73 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); | 70 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); |
74 } | 71 } |
75 | 72 |
| 73 protected: |
| 74 SkShader* fShader; |
| 75 |
76 private: | 76 private: |
77 SkShader* fShader; | |
78 typedef GM INHERITED ; | 77 typedef GM INHERITED ; |
79 }; | 78 }; |
80 | 79 |
| 80 class ComposeShaderAlphaGM : public ComposeShaderGM { |
| 81 public: |
| 82 ComposeShaderAlphaGM() {} |
| 83 |
| 84 protected: |
| 85 virtual SkString onShortName() SK_OVERRIDE { |
| 86 return SkString("composeshader_alpha"); |
| 87 } |
| 88 |
| 89 virtual SkISize onISize() SK_OVERRIDE { |
| 90 return SkISize::Make(120, 1024); |
| 91 } |
| 92 |
| 93 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 94 SkPaint paint; |
| 95 paint.setColor(SK_ColorGREEN); |
| 96 |
| 97 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); |
| 98 for (int alpha = 0xFF; alpha > 0; alpha -= 0x20) { |
| 99 paint.setAlpha(0xFF); |
| 100 paint.setShader(NULL); |
| 101 canvas->drawRect(r, paint); |
| 102 |
| 103 paint.setAlpha(alpha); |
| 104 paint.setShader(fShader); |
| 105 canvas->drawRect(r, paint); |
| 106 |
| 107 canvas->translate(r.width() + 5, 0); |
| 108 } |
| 109 } |
| 110 }; |
| 111 |
81 ////////////////////////////////////////////////////////////////////////////// | 112 ////////////////////////////////////////////////////////////////////////////// |
82 | 113 |
83 static GM* MyFactory(void*) { return new ShaderGM; } | 114 DEF_GM( return new ComposeShaderGM; ) |
84 static GMRegistry reg(MyFactory); | 115 DEF_GM( return new ComposeShaderAlphaGM; ) |
85 | 116 |
86 } // namespace | |
OLD | NEW |