blob: 8033ff739d31c2124164564344bc956acf746a7a [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
Mike Reed5dd202d2018-02-06 23:05:36 +000010#include "SkColorPriv.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000011#include "SkCanvas.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
Florin Malitaab244f02017-05-03 19:16:58 +000014#include "SkImage.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000015#include "SkShader.h"
16#include "SkString.h"
Mike Klein33d20552017-03-22 13:47:51 -040017#include "SkTDArray.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000018
Mike Reed7d954ad2016-10-28 15:42:34 -040019static sk_sp<SkShader> make_shader(SkBlendMode mode) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000020 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;
reed2ad1aa62016-03-09 09:50:50 -080027 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, 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);
reed2ad1aa62016-03-09 09:50:50 -080033 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000034
Mike Reed7d954ad2016-10-28 15:42:34 -040035 return SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB), mode);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000036}
37
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000038class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000039public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000040 ComposeShaderGM() {
Mike Reed7d954ad2016-10-28 15:42:34 -040041 fShader = make_shader(SkBlendMode::kDstIn);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000042 }
43
scroggo@google.com32f20eb2012-06-29 17:28:50 +000044protected:
mtklein36352bf2015-03-25 18:17:31 -070045 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000046 return SkString("composeshader");
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000050 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000051 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000054 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000055 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050056 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000057 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050058 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000059 }
60
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000061protected:
reed2ad1aa62016-03-09 09:50:50 -080062 sk_sp<SkShader> fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000063
64private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000065 typedef GM INHERITED ;
66};
Mike Reed27738e02018-02-02 14:36:05 -050067DEF_GM( return new ComposeShaderGM; )
scroggo@google.com32f20eb2012-06-29 17:28:50 +000068
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000069class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000070public:
71 ComposeShaderAlphaGM() {}
72
73protected:
mtklein36352bf2015-03-25 18:17:31 -070074 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000075 return SkString("composeshader_alpha");
76 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000077
mtklein36352bf2015-03-25 18:17:31 -070078 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070079 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000080 }
reed@google.com775bf662014-05-05 19:37:22 +000081
mtklein36352bf2015-03-25 18:17:31 -070082 void onDraw(SkCanvas* canvas) override {
reed2ad1aa62016-03-09 09:50:50 -080083 sk_sp<SkShader> shaders[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040084 make_shader(SkBlendMode::kDstIn),
85 make_shader(SkBlendMode::kSrcOver),
reed2ad1aa62016-03-09 09:50:50 -080086 };
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000087
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000088 SkPaint paint;
89 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000090
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000091 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000092
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000093 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000094 canvas->save();
95 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
96 paint.setAlpha(0xFF);
halcanary96fcdcc2015-08-27 07:41:13 -070097 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000098 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000099
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000100 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800101 paint.setShader(shaders[y]);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000102 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000103
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000104 canvas->translate(r.width() + 5, 0);
105 }
106 canvas->restore();
107 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000108 }
109 }
wangyixd7059582015-09-03 08:32:22 -0700110
111private:
112 typedef GM INHERITED ;
113};
Mike Reed27738e02018-02-02 14:36:05 -0500114DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700115
116// creates a square bitmap with red background and a green circle in the center
117static void draw_color_bm(SkBitmap* bm, int length) {
118 SkPaint paint;
119 paint.setColor(SK_ColorGREEN);
120
121 bm->allocN32Pixels(length, length);
122 bm->eraseColor(SK_ColorRED);
123
124 SkCanvas canvas(*bm);
125 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
126 paint);
127}
128
129// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
130static void draw_alpha8_bm(SkBitmap* bm, int length) {
131 SkPaint circlePaint;
132 circlePaint.setColor(SK_ColorBLACK);
133
134 bm->allocPixels(SkImageInfo::MakeA8(length, length));
135 bm->eraseColor(SK_ColorTRANSPARENT);
136
137 SkCanvas canvas(*bm);
138 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
139 circlePaint);
140}
141
142// creates a linear gradient shader
reed2ad1aa62016-03-09 09:50:50 -0800143static sk_sp<SkShader> make_linear_gradient_shader(int length) {
wangyixd7059582015-09-03 08:32:22 -0700144 SkPoint pts[2];
145 SkColor colors[2];
146 pts[0].set(0, 0);
147 pts[1].set(SkIntToScalar(length), 0);
148 colors[0] = SK_ColorBLUE;
149 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
reed2ad1aa62016-03-09 09:50:50 -0800150 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
wangyixd7059582015-09-03 08:32:22 -0700151}
152
153
154class ComposeShaderBitmapGM : public skiagm::GM {
155public:
herbb10fe492016-01-08 13:48:43 -0800156 ComposeShaderBitmapGM() {}
caryclark6df61152016-01-04 14:17:47 -0800157
herbb10fe492016-01-08 13:48:43 -0800158protected:
caryclark6df61152016-01-04 14:17:47 -0800159 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700160 draw_color_bm(&fColorBitmap, squareLength);
161 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
162 SkMatrix s;
163 s.reset();
reed2ad1aa62016-03-09 09:50:50 -0800164 fColorBitmapShader = SkShader::MakeBitmapShader(fColorBitmap, SkShader::kRepeat_TileMode,
165 SkShader::kRepeat_TileMode, &s);
166 fAlpha8BitmapShader = SkShader::MakeBitmapShader(fAlpha8Bitmap, SkShader::kRepeat_TileMode,
167 SkShader::kRepeat_TileMode, &s);
wangyixd7059582015-09-03 08:32:22 -0700168 fLinearGradientShader = make_linear_gradient_shader(squareLength);
169 }
caryclark6df61152016-01-04 14:17:47 -0800170
wangyixd7059582015-09-03 08:32:22 -0700171 SkString onShortName() override {
172 return SkString("composeshader_bitmap");
173 }
174
175 SkISize onISize() override {
176 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
177 }
178
179 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400180 SkBlendMode mode = SkBlendMode::kDstOver;
wangyixd7059582015-09-03 08:32:22 -0700181
reed2ad1aa62016-03-09 09:50:50 -0800182 sk_sp<SkShader> shaders[] = {
183 // gradient should appear over color bitmap
Mike Reed7d954ad2016-10-28 15:42:34 -0400184 SkShader::MakeComposeShader(fLinearGradientShader, fColorBitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800185 // gradient should appear over alpha8 bitmap colorized by the paint color
Mike Reed7d954ad2016-10-28 15:42:34 -0400186 SkShader::MakeComposeShader(fLinearGradientShader, fAlpha8BitmapShader, mode),
reed2ad1aa62016-03-09 09:50:50 -0800187 };
wangyixd7059582015-09-03 08:32:22 -0700188
189 SkPaint paint;
190 paint.setColor(SK_ColorYELLOW);
191
192 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
193 SkIntToScalar(squareLength));
194
195 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
wangyixd7059582015-09-03 08:32:22 -0700196 canvas->save();
197 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
198 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800199 paint.setShader(shaders[y]);
wangyixd7059582015-09-03 08:32:22 -0700200 canvas->drawRect(r, paint);
201
202 canvas->translate(r.width() + 5, 0);
203 }
204 canvas->restore();
205 canvas->translate(0, r.height() + 5);
206 }
207 }
halcanary9d524f22016-03-29 09:03:52 -0700208
wangyixd7059582015-09-03 08:32:22 -0700209private:
reed8681e602016-02-15 08:27:14 -0800210 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700211 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
212 * work in a release build. You can change this parameter and then compile a release build
213 * to have this GM draw larger bitmaps for easier visual inspection.
214 */
mtkleindbfd7ab2016-09-01 11:24:54 -0700215 static constexpr int squareLength = 20;
wangyixd7059582015-09-03 08:32:22 -0700216
217 SkBitmap fColorBitmap;
218 SkBitmap fAlpha8Bitmap;
reed2ad1aa62016-03-09 09:50:50 -0800219 sk_sp<SkShader> fColorBitmapShader;
220 sk_sp<SkShader> fAlpha8BitmapShader;
221 sk_sp<SkShader> fLinearGradientShader;
wangyixd7059582015-09-03 08:32:22 -0700222
223 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000224};
Mike Reed27738e02018-02-02 14:36:05 -0500225DEF_GM( return new ComposeShaderBitmapGM; )
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000226
caryclarkc2ad65e2016-08-15 12:03:33 -0700227DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) {
228 int width = 255;
229 int height = 255;
230 SkTDArray<uint8_t> dst8Storage;
231 dst8Storage.setCount(width * height);
232 SkTDArray<uint32_t> dst32Storage;
233 dst32Storage.setCount(width * height * sizeof(int32_t));
234 for (int y = 0; y < height; ++y) {
235 for (int x = 0; x < width; ++x) {
236 dst8Storage[y * width + x] = (y + x) / 2;
237 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
238 }
239 }
240 SkPaint paint;
241 paint.setAntiAlias(true);
242 paint.setColor(SK_ColorBLUE);
243 SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)};
244 canvas->drawRect(r, paint);
245 SkBitmap skBitmap, skMask;
246 SkImageInfo imageInfo = SkImageInfo::Make(width, height,
247 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
248 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
Mike Reed086a4272017-07-18 10:53:11 -0400249 nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700250 imageInfo = SkImageInfo::Make(width, height,
251 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
Mike Reed086a4272017-07-18 10:53:11 -0400252 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700253 sk_sp<SkImage> skSrc = SkImage::MakeFromBitmap(skBitmap);
caryclarkc2ad65e2016-08-15 12:03:33 -0700254 sk_sp<SkImage> skMaskImage = SkImage::MakeFromBitmap(skMask);
caryclarkc2ad65e2016-08-15 12:03:33 -0700255 paint.setShader(
Mike Reed0acd7952017-04-28 11:12:19 -0400256 SkShader::MakeComposeShader(skMaskImage->makeShader(), skSrc->makeShader(),
257 SkBlendMode::kSrcIn));
caryclarkc2ad65e2016-08-15 12:03:33 -0700258 canvas->drawRect(r, paint);
259}
260
Mike Reed27738e02018-02-02 14:36:05 -0500261///////////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000262
Mike Reed27738e02018-02-02 14:36:05 -0500263static sk_sp<SkShader> make_src_shader(SkScalar size) {
264 const SkPoint pts[] = { { 0, 0 }, { 0, size } };
265 const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
266 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
267}
268
269static sk_sp<SkShader> make_dst_shader(SkScalar size) {
270 const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
271 const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
272 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
273}
274
275const SkScalar gCellSize = 100;
276
277static void draw_cell(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
278 SkBlendMode mode, SkAlpha alpha) {
279 const SkRect r = SkRect::MakeWH(gCellSize, gCellSize);
280 SkPaint p;
281 p.setAlpha(alpha);
282
283 SkAutoCanvasRestore acr(canvas, false);
284 canvas->saveLayer(&r, &p);
285 p.setAlpha(0xFF);
286
287 p.setShader(dst);
288 p.setBlendMode(SkBlendMode::kSrc);
289 canvas->drawRect(r, p);
290
291 p.setShader(src);
292 p.setBlendMode(mode);
293 canvas->drawRect(r, p);
294}
295
296static void draw_composed(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
297 SkBlendMode mode, SkAlpha alpha) {
298 SkPaint p;
299 p.setAlpha(alpha);
300 p.setShader(SkShader::MakeCompose(dst, src, mode));
301 canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p);
302}
303
304static void draw_pair(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
305 SkBlendMode mode) {
306 SkAutoCanvasRestore acr(canvas, true);
307
308 const SkScalar gap = 4;
309 SkRect r = SkRect::MakeWH(2 * gCellSize + gap, 2 * gCellSize + gap);
Mike Reed1d08d722018-02-02 16:15:49 -0500310 r.outset(gap + 1.5f, gap + 1.5f);
Mike Reed27738e02018-02-02 14:36:05 -0500311 SkPaint p;
312 p.setStyle(SkPaint::kStroke_Style);
313 canvas->drawRect(r, p); // border
314
315 SkAlpha alpha = 0xFF;
316 for (int y = 0; y < 2; ++y) {
317 draw_cell(canvas, src, dst, mode, alpha);
318 canvas->save();
319 canvas->translate(gCellSize + gap, 0);
320 draw_composed(canvas, src, dst, mode, alpha);
321 canvas->restore();
322
323 canvas->translate(0, gCellSize + gap);
324 alpha = 0x80;
325 }
326}
327
328DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) {
329 auto src = make_src_shader(gCellSize);
330 auto dst = make_dst_shader(gCellSize);
331
Mike Reed1d08d722018-02-02 16:15:49 -0500332 const SkScalar margin = 15;
Mike Reed27738e02018-02-02 14:36:05 -0500333 const SkScalar dx = 2*gCellSize + margin;
334 const SkScalar dy = 2*gCellSize + margin;
335
336 canvas->translate(margin, margin);
337 canvas->save();
338 for (int m = 0; m < 16; ++m) {
339 SkBlendMode mode = static_cast<SkBlendMode>(m);
340 draw_pair(canvas, src, dst, mode);
341 if ((m % 4) == 3) {
342 canvas->restore();
343 canvas->translate(0, dy);
344 canvas->save();
345 } else {
346 canvas->translate(dx, 0);
347 }
348 }
349 canvas->restore();
350}