OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkComposeShader.h" | 11 #include "SkComposeShader.h" |
12 #include "SkGradientShader.h" | 12 #include "SkGradientShader.h" |
13 #include "SkGraphics.h" | 13 #include "SkGraphics.h" |
14 #include "SkShader.h" | 14 #include "SkShader.h" |
15 #include "SkString.h" | 15 #include "SkString.h" |
16 #include "SkXfermode.h" | 16 #include "SkXfermode.h" |
17 | 17 |
| 18 static SkShader* make_shader(SkXfermode::Mode mode) { |
| 19 SkPoint pts[2]; |
| 20 SkColor colors[2]; |
| 21 |
| 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, N
ULL, 2, |
| 27 SkShader::kCla
mp_TileMode)); |
| 28 |
| 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, N
ULL, 2, |
| 34 SkShader::kCla
mp_TileMode)); |
| 35 |
| 36 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode)); |
| 37 |
| 38 return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer)); |
| 39 } |
| 40 |
18 class ComposeShaderGM : public skiagm::GM { | 41 class ComposeShaderGM : public skiagm::GM { |
19 public: | 42 public: |
20 ComposeShaderGM() { | 43 ComposeShaderGM() { |
21 SkPoint pts[2]; | 44 fShader = make_shader(SkXfermode::kDstIn_Mode); |
22 SkColor colors[2]; | |
23 | |
24 pts[0].set(0, 0); | |
25 pts[1].set(SkIntToScalar(100), 0); | |
26 colors[0] = SK_ColorRED; | |
27 colors[1] = SK_ColorBLUE; | |
28 SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, | |
29 SkShader::kClamp_Tile
Mode); | |
30 | |
31 pts[0].set(0, 0); | |
32 pts[1].set(0, SkIntToScalar(100)); | |
33 colors[0] = SK_ColorBLACK; | |
34 colors[1] = SkColorSetARGB(0x80, 0, 0, 0); | |
35 SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, | |
36 SkShader::kClamp_Tile
Mode); | |
37 | |
38 SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); | |
39 | |
40 fShader = new SkComposeShader(shaderA, shaderB, mode); | |
41 shaderA->unref(); | |
42 shaderB->unref(); | |
43 mode->unref(); | |
44 } | 45 } |
45 | 46 |
46 virtual ~ComposeShaderGM() { | 47 virtual ~ComposeShaderGM() { |
47 SkSafeUnref(fShader); | 48 SkSafeUnref(fShader); |
48 } | 49 } |
49 | 50 |
50 protected: | 51 protected: |
51 virtual uint32_t onGetFlags() const SK_OVERRIDE { | 52 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
52 return kSkipTiled_Flag; | 53 return kSkipTiled_Flag; |
53 } | 54 } |
(...skipping 16 matching lines...) Expand all Loading... |
70 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); | 71 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), pai
nt); |
71 } | 72 } |
72 | 73 |
73 protected: | 74 protected: |
74 SkShader* fShader; | 75 SkShader* fShader; |
75 | 76 |
76 private: | 77 private: |
77 typedef GM INHERITED ; | 78 typedef GM INHERITED ; |
78 }; | 79 }; |
79 | 80 |
80 class ComposeShaderAlphaGM : public ComposeShaderGM { | 81 class ComposeShaderAlphaGM : public skiagm::GM { |
81 public: | 82 public: |
82 ComposeShaderAlphaGM() {} | 83 ComposeShaderAlphaGM() {} |
83 | 84 |
84 protected: | 85 protected: |
85 virtual SkString onShortName() SK_OVERRIDE { | 86 virtual SkString onShortName() SK_OVERRIDE { |
86 return SkString("composeshader_alpha"); | 87 return SkString("composeshader_alpha"); |
87 } | 88 } |
88 | 89 |
89 virtual SkISize onISize() SK_OVERRIDE { | 90 virtual SkISize onISize() SK_OVERRIDE { |
90 return SkISize::Make(120, 1024); | 91 return SkISize::Make(220, 750); |
91 } | 92 } |
92 | 93 |
93 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | 94 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 95 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode)); |
| 96 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode)); |
| 97 SkShader* shaders[] = { shader0.get(), shader1.get() }; |
| 98 |
94 SkPaint paint; | 99 SkPaint paint; |
95 paint.setColor(SK_ColorGREEN); | 100 paint.setColor(SK_ColorGREEN); |
96 | 101 |
97 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); | 102 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100); |
98 for (int alpha = 0xFF; alpha > 0; alpha -= 0x20) { | 103 |
99 paint.setAlpha(0xFF); | 104 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) { |
100 paint.setShader(NULL); | 105 SkShader* shader = shaders[y]; |
101 canvas->drawRect(r, paint); | 106 canvas->save(); |
102 | 107 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) { |
103 paint.setAlpha(alpha); | 108 paint.setAlpha(0xFF); |
104 paint.setShader(fShader); | 109 paint.setShader(NULL); |
105 canvas->drawRect(r, paint); | 110 canvas->drawRect(r, paint); |
106 | 111 |
107 canvas->translate(r.width() + 5, 0); | 112 paint.setAlpha(alpha); |
| 113 paint.setShader(shader); |
| 114 canvas->drawRect(r, paint); |
| 115 |
| 116 canvas->translate(r.width() + 5, 0); |
| 117 } |
| 118 canvas->restore(); |
| 119 canvas->translate(0, r.height() + 5); |
108 } | 120 } |
109 } | 121 } |
110 }; | 122 }; |
111 | 123 |
112 ////////////////////////////////////////////////////////////////////////////// | 124 ////////////////////////////////////////////////////////////////////////////// |
113 | 125 |
114 DEF_GM( return new ComposeShaderGM; ) | 126 DEF_GM( return new ComposeShaderGM; ) |
115 DEF_GM( return new ComposeShaderAlphaGM; ) | 127 DEF_GM( return new ComposeShaderAlphaGM; ) |
116 | 128 |
OLD | NEW |