76ff8edfc42a9e6ca4a651c152710fd02c9c711a
[platform/upstream/libSkiaSharp.git] / gm / composeshader.cpp
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
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:
43     ComposeShaderGM() {
44         fShader = make_shader(SkXfermode::kDstIn_Mode);
45     }
46
47     virtual ~ComposeShaderGM() {
48         SkSafeUnref(fShader);
49     }
50
51 protected:
52     SkString onShortName() SK_OVERRIDE {
53         return SkString("composeshader");
54     }
55
56     SkISize onISize() SK_OVERRIDE {
57         return SkISize::Make(120, 120);
58     }
59
60     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
61
62         SkPaint paint;
63
64         paint.setColor(SK_ColorGREEN);
65         canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
66         paint.setShader(fShader);
67         canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
68     }
69
70 protected:
71     SkShader*   fShader;
72
73 private:
74     typedef GM INHERITED ;
75 };
76
77 class ComposeShaderAlphaGM : public skiagm::GM {
78 public:
79     ComposeShaderAlphaGM() {}
80
81 protected:
82     SkString onShortName() SK_OVERRIDE {
83         return SkString("composeshader_alpha");
84     }
85
86     SkISize onISize() SK_OVERRIDE {
87         return SkISize::Make(220, 750);
88     }
89
90     void onDraw(SkCanvas* canvas) SK_OVERRIDE {
91         SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
92         SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
93         SkShader* shaders[] = { shader0.get(), shader1.get() };
94
95         SkPaint paint;
96         paint.setColor(SK_ColorGREEN);
97
98         const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
99
100         for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
101             SkShader* shader = shaders[y];
102             canvas->save();
103             for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
104                 paint.setAlpha(0xFF);
105                 paint.setShader(NULL);
106                 canvas->drawRect(r, paint);
107
108                 paint.setAlpha(alpha);
109                 paint.setShader(shader);
110                 canvas->drawRect(r, paint);
111
112                 canvas->translate(r.width() + 5, 0);
113             }
114             canvas->restore();
115             canvas->translate(0, r.height() + 5);
116         }
117     }
118 };
119
120 //////////////////////////////////////////////////////////////////////////////
121
122 DEF_GM( return new ComposeShaderGM; )
123 DEF_GM( return new ComposeShaderAlphaGM; )