blob: e012bbb28590b21282b59b47a3da5173c4e8603b [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
wangyixd7059582015-09-03 08:32:22 -070010#include "SkBitmapProcShader.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000011#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
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000019static SkShader* make_shader(SkXfermode::Mode mode) {
20 SkPoint pts[2];
21 SkColor colors[2];
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000022
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000023 pts[0].set(0, 0);
24 pts[1].set(SkIntToScalar(100), 0);
25 colors[0] = SK_ColorRED;
26 colors[1] = SK_ColorBLUE;
halcanary96fcdcc2015-08-27 07:41:13 -070027 SkAutoTUnref<SkShader> shaderA(SkGradientShader::CreateLinear(pts, colors, nullptr, 2,
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000028 SkShader::kClamp_TileMode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000029
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000030 pts[0].set(0, 0);
31 pts[1].set(0, SkIntToScalar(100));
32 colors[0] = SK_ColorBLACK;
33 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -070034 SkAutoTUnref<SkShader> shaderB(SkGradientShader::CreateLinear(pts, colors, nullptr, 2,
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000035 SkShader::kClamp_TileMode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000036
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000037 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(mode));
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000038
halcanary385fe4d2015-08-26 13:07:48 -070039 return new SkComposeShader(shaderA, shaderB, xfer);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000040}
41
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000042class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000043public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000044 ComposeShaderGM() {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000045 fShader = make_shader(SkXfermode::kDstIn_Mode);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000046 }
47
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000048 virtual ~ComposeShaderGM() {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000049 SkSafeUnref(fShader);
50 }
51
52protected:
mtklein36352bf2015-03-25 18:17:31 -070053 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000054 return SkString("composeshader");
55 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000058 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000059 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000062
63 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000064
65 paint.setColor(SK_ColorGREEN);
66 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
67 paint.setShader(fShader);
68 canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
69 }
70
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000071protected:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000072 SkShader* fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000073
74private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000075 typedef GM INHERITED ;
76};
77
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000078class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000079public:
80 ComposeShaderAlphaGM() {}
81
82protected:
mtklein36352bf2015-03-25 18:17:31 -070083 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000084 return SkString("composeshader_alpha");
85 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000086
mtklein36352bf2015-03-25 18:17:31 -070087 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070088 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000089 }
reed@google.com775bf662014-05-05 19:37:22 +000090
mtklein36352bf2015-03-25 18:17:31 -070091 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000092 SkAutoTUnref<SkShader> shader0(make_shader(SkXfermode::kDstIn_Mode));
93 SkAutoTUnref<SkShader> shader1(make_shader(SkXfermode::kSrcOver_Mode));
94 SkShader* shaders[] = { shader0.get(), shader1.get() };
95
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000096 SkPaint paint;
97 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000098
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000099 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000100
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000101 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
102 SkShader* shader = shaders[y];
103 canvas->save();
104 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
105 paint.setAlpha(0xFF);
halcanary96fcdcc2015-08-27 07:41:13 -0700106 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000107 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000108
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000109 paint.setAlpha(alpha);
110 paint.setShader(shader);
111 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000112
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000113 canvas->translate(r.width() + 5, 0);
114 }
115 canvas->restore();
116 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000117 }
118 }
wangyixd7059582015-09-03 08:32:22 -0700119
120private:
121 typedef GM INHERITED ;
122};
123
124
125// creates a square bitmap with red background and a green circle in the center
126static void draw_color_bm(SkBitmap* bm, int length) {
127 SkPaint paint;
128 paint.setColor(SK_ColorGREEN);
129
130 bm->allocN32Pixels(length, length);
131 bm->eraseColor(SK_ColorRED);
132
133 SkCanvas canvas(*bm);
134 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
135 paint);
136}
137
138// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
139static void draw_alpha8_bm(SkBitmap* bm, int length) {
140 SkPaint circlePaint;
141 circlePaint.setColor(SK_ColorBLACK);
142
143 bm->allocPixels(SkImageInfo::MakeA8(length, length));
144 bm->eraseColor(SK_ColorTRANSPARENT);
145
146 SkCanvas canvas(*bm);
147 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
148 circlePaint);
149}
150
151// creates a linear gradient shader
152static SkShader* make_linear_gradient_shader(int length) {
153 SkPoint pts[2];
154 SkColor colors[2];
155 pts[0].set(0, 0);
156 pts[1].set(SkIntToScalar(length), 0);
157 colors[0] = SK_ColorBLUE;
158 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
159 return SkGradientShader::CreateLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
160}
161
162
163class ComposeShaderBitmapGM : public skiagm::GM {
164public:
165 ComposeShaderBitmapGM() {
166 draw_color_bm(&fColorBitmap, squareLength);
167 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
168 SkMatrix s;
169 s.reset();
170 fColorBitmapShader = new SkBitmapProcShader(fColorBitmap, SkShader::kRepeat_TileMode,
171 SkShader::kRepeat_TileMode, &s);
172 fAlpha8BitmapShader = new SkBitmapProcShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
173 SkShader::kRepeat_TileMode, &s);
174 fLinearGradientShader = make_linear_gradient_shader(squareLength);
175 }
176 ~ComposeShaderBitmapGM() {
177 SkSafeUnref(fColorBitmapShader);
178 SkSafeUnref(fAlpha8BitmapShader);
179 SkSafeUnref(fLinearGradientShader);
180 }
181
182protected:
183 SkString onShortName() override {
184 return SkString("composeshader_bitmap");
185 }
186
187 SkISize onISize() override {
188 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
189 }
190
191 void onDraw(SkCanvas* canvas) override {
192 SkAutoTUnref<SkXfermode> xfer(SkXfermode::Create(SkXfermode::kDstOver_Mode));
193
194 // gradient should appear over color bitmap
195 SkAutoTUnref<SkShader> shader0(new SkComposeShader(fLinearGradientShader,
196 fColorBitmapShader,
197 xfer));
198 // gradient should appear over alpha8 bitmap colorized by the paint color
199 SkAutoTUnref<SkShader> shader1(new SkComposeShader(fLinearGradientShader,
200 fAlpha8BitmapShader,
201 xfer));
202
203 SkShader* shaders[] = { shader0.get(), shader1.get() };
204
205 SkPaint paint;
206 paint.setColor(SK_ColorYELLOW);
207
208 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
209 SkIntToScalar(squareLength));
210
211 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
212 SkShader* shader = shaders[y];
213 canvas->save();
214 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
215 paint.setAlpha(alpha);
216 paint.setShader(shader);
217 canvas->drawRect(r, paint);
218
219 canvas->translate(r.width() + 5, 0);
220 }
221 canvas->restore();
222 canvas->translate(0, r.height() + 5);
223 }
224 }
225private:
226 /** This determines the length and width of the bitmaps used in the SkComposeShaders. Values
227 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
228 * work in a release build. You can change this parameter and then compile a release build
229 * to have this GM draw larger bitmaps for easier visual inspection.
230 */
231 static const int squareLength = 20;
232
233 SkBitmap fColorBitmap;
234 SkBitmap fAlpha8Bitmap;
235 SkShader* fColorBitmapShader;
236 SkShader* fAlpha8BitmapShader;
237 SkShader* fLinearGradientShader;
238
239 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000240};
241
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000242//////////////////////////////////////////////////////////////////////////////
243
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000244DEF_GM( return new ComposeShaderGM; )
245DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700246DEF_GM( return new ComposeShaderBitmapGM; )