blob: b40e2263d60e23650e2d1fb3aa35967adb820534 [file] [log] [blame]
scroggo@google.com32f20eb2012-06-29 17:28:50 +00001
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
19namespace skiagm {
20
21class ShaderGM : public GM {
22public:
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
53protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000054 virtual uint32_t onGetFlags() const SK_OVERRIDE {
55 return kSkipTiled_Flag;
56 }
57
scroggo@google.com32f20eb2012-06-29 17:28:50 +000058 virtual SkString onShortName() SK_OVERRIDE {
59 return SkString("composeshader");
60 }
61
62 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +000063 return make_isize(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000064 }
65
scroggo@google.com32f20eb2012-06-29 17:28:50 +000066 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
67
68 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000069
70 paint.setColor(SK_ColorGREEN);
71 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
72 paint.setShader(fShader);
73 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
74 }
75
76private:
77 SkShader* fShader;
78 typedef GM INHERITED ;
79};
80
81//////////////////////////////////////////////////////////////////////////////
82
83static GM* MyFactory(void*) { return new ShaderGM; }
84static GMRegistry reg(MyFactory);
85
86} // namespace