2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkComposeShader.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
16 #include "SkXfermode.h"
18 static SkShader* make_shader(SkXfermode::Mode mode) {
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));
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));
36 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
38 return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer));
41 class ComposeShaderGM : public skiagm::GM {
44 fShader = make_shader(SkXfermode::kDstIn_Mode);
47 virtual ~ComposeShaderGM() {
52 SkString onShortName() override {
53 return SkString("composeshader");
56 SkISize onISize() override {
57 return SkISize::Make(120, 120);
60 void onDraw(SkCanvas* canvas) override {
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);
74 typedef GM INHERITED ;
77 class ComposeShaderAlphaGM : public skiagm::GM {
79 ComposeShaderAlphaGM() {}
82 SkString onShortName() override {
83 return SkString("composeshader_alpha");
86 SkISize onISize() override {
87 return SkISize::Make(220, 750);
90 void onDraw(SkCanvas* canvas) 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() };
96 paint.setColor(SK_ColorGREEN);
98 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
100 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
101 SkShader* shader = shaders[y];
103 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
104 paint.setAlpha(0xFF);
105 paint.setShader(NULL);
106 canvas->drawRect(r, paint);
108 paint.setAlpha(alpha);
109 paint.setShader(shader);
110 canvas->drawRect(r, paint);
112 canvas->translate(r.width() + 5, 0);
115 canvas->translate(0, r.height() + 5);
120 //////////////////////////////////////////////////////////////////////////////
122 DEF_GM( return new ComposeShaderGM; )
123 DEF_GM( return new ComposeShaderAlphaGM; )