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
make_shader(SkXfermode::Mode mode)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, NULL, 2,
27 SkShader::kClamp_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, NULL, 2,
34 SkShader::kClamp_TileMode));
35
36 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
37
38 return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer));
39 }
40
41 class ComposeShaderGM : public skiagm::GM {
42 public:
ComposeShaderGM()43 ComposeShaderGM() {
44 fShader = make_shader(SkXfermode::kDstIn_Mode);
45 }
46
~ComposeShaderGM()47 virtual ~ComposeShaderGM() {
48 SkSafeUnref(fShader);
49 }
50
51 protected:
onGetFlags() const52 virtual uint32_t onGetFlags() const SK_OVERRIDE {
53 return kSkipTiled_Flag;
54 }
55
onShortName()56 virtual SkString onShortName() SK_OVERRIDE {
57 return SkString("composeshader");
58 }
59
onISize()60 virtual SkISize onISize() SK_OVERRIDE {
61 return SkISize::Make(120, 120);
62 }
63
onDraw(SkCanvas * canvas)64 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
65
66 SkPaint paint;
67
68 paint.setColor(SK_ColorGREEN);
69 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
70 paint.setShader(fShader);
71 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
72 }
73
74 protected:
75 SkShader* fShader;
76
77 private:
78 typedef GM INHERITED ;
79 };
80
81 class ComposeShaderAlphaGM : public skiagm::GM {
82 public:
ComposeShaderAlphaGM()83 ComposeShaderAlphaGM() {}
84
85 protected:
onShortName()86 virtual SkString onShortName() SK_OVERRIDE {
87 return SkString("composeshader_alpha");
88 }
89
onISize()90 virtual SkISize onISize() SK_OVERRIDE {
91 return SkISize::Make(220, 750);
92 }
93
onGetFlags() const94 virtual uint32_t onGetFlags() const SK_OVERRIDE {
95 // we're only off by 1 bit per-component
96 return kSkipTiled_Flag;
97 }
98
onDraw(SkCanvas * canvas)99 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
100 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
101 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
102 SkShader* shaders[] = { shader0.get(), shader1.get() };
103
104 SkPaint paint;
105 paint.setColor(SK_ColorGREEN);
106
107 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
108
109 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
110 SkShader* shader = shaders[y];
111 canvas->save();
112 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
113 paint.setAlpha(0xFF);
114 paint.setShader(NULL);
115 canvas->drawRect(r, paint);
116
117 paint.setAlpha(alpha);
118 paint.setShader(shader);
119 canvas->drawRect(r, paint);
120
121 canvas->translate(r.width() + 5, 0);
122 }
123 canvas->restore();
124 canvas->translate(0, r.height() + 5);
125 }
126 }
127 };
128
129 //////////////////////////////////////////////////////////////////////////////
130
131 DEF_GM( return new ComposeShaderGM; )
132 DEF_GM( return new ComposeShaderAlphaGM; )
133