blob: 8933cf39653d956d684ee4256cc0ff3623f60721 [file] [log] [blame]
scroggo@google.com32f20eb2012-06-29 17:28:50 +00001/*
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
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000018static SkShader* make_shader(SkXfermode::Mode mode) {
19 SkPoint pts[2];
20 SkColor colors[2];
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000021
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000022 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));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000028
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000029 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));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000035
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000036 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000037
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000038 return SkNEW_ARGS(SkComposeShader, (shaderA, shaderB, xfer));
39}
40
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000041class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000042public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000043 ComposeShaderGM() {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000044 fShader = make_shader(SkXfermode::kDstIn_Mode);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000045 }
46
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000047 virtual ~ComposeShaderGM() {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000048 SkSafeUnref(fShader);
49 }
50
51protected:
mtklein36352bf2015-03-25 18:17:31 -070052 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000053 return SkString("composeshader");
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000057 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000058 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000061
62 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000063
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
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000070protected:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000071 SkShader* fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000072
73private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000074 typedef GM INHERITED ;
75};
76
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000077class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000078public:
79 ComposeShaderAlphaGM() {}
80
81protected:
mtklein36352bf2015-03-25 18:17:31 -070082 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000083 return SkString("composeshader_alpha");
84 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000085
mtklein36352bf2015-03-25 18:17:31 -070086 SkISize onISize() override {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000087 return SkISize::Make(220, 750);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000088 }
reed@google.com775bf662014-05-05 19:37:22 +000089
mtklein36352bf2015-03-25 18:17:31 -070090 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000091 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
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000095 SkPaint paint;
96 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000097
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000098 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000099
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000100 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);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000107
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000108 paint.setAlpha(alpha);
109 paint.setShader(shader);
110 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000111
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000112 canvas->translate(r.width() + 5, 0);
113 }
114 canvas->restore();
115 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000116 }
117 }
118};
119
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000120//////////////////////////////////////////////////////////////////////////////
121
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000122DEF_GM( return new ComposeShaderGM; )
123DEF_GM( return new ComposeShaderAlphaGM; )