blob: 907bf36e93429a30d43a31736cf4d548b20ee9ad [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkImageInfo.h"
16#include "include/core/SkMatrix.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkSize.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkString.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/core/SkTileMode.h"
26#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
28#include "include/private/SkTDArray.h"
Florin Malita9ab449a2019-07-09 13:22:50 -040029#include "src/core/SkTLazy.h"
scroggo@google.com32f20eb2012-06-29 17:28:50 +000030
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include <utility>
32
Mike Reed7d954ad2016-10-28 15:42:34 -040033static sk_sp<SkShader> make_shader(SkBlendMode mode) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000034 SkPoint pts[2];
35 SkColor colors[2];
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000036
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000037 pts[0].set(0, 0);
38 pts[1].set(SkIntToScalar(100), 0);
39 colors[0] = SK_ColorRED;
40 colors[1] = SK_ColorBLUE;
Mike Reedfae8fce2019-04-03 10:27:45 -040041 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000042
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000043 pts[0].set(0, 0);
44 pts[1].set(0, SkIntToScalar(100));
45 colors[0] = SK_ColorBLACK;
46 colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
Mike Reedfae8fce2019-04-03 10:27:45 -040047 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000048
Mike Reedc8bea7d2019-04-09 13:55:36 -040049 return SkShaders::Blend(mode, std::move(shaderA), std::move(shaderB));
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000050}
51
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000052class ComposeShaderGM : public skiagm::GM {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000053public:
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000054 ComposeShaderGM() {
Mike Reed7d954ad2016-10-28 15:42:34 -040055 fShader = make_shader(SkBlendMode::kDstIn);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000056 }
57
scroggo@google.com32f20eb2012-06-29 17:28:50 +000058protected:
mtklein36352bf2015-03-25 18:17:31 -070059 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000060 return SkString("composeshader");
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000064 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000065 }
66
mtklein36352bf2015-03-25 18:17:31 -070067 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000068 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000069 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050070 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000071 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050072 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000073 }
74
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000075protected:
reed2ad1aa62016-03-09 09:50:50 -080076 sk_sp<SkShader> fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000077
78private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000079 typedef GM INHERITED ;
80};
Mike Reed27738e02018-02-02 14:36:05 -050081DEF_GM( return new ComposeShaderGM; )
scroggo@google.com32f20eb2012-06-29 17:28:50 +000082
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000083class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000084public:
85 ComposeShaderAlphaGM() {}
86
87protected:
mtklein36352bf2015-03-25 18:17:31 -070088 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000089 return SkString("composeshader_alpha");
90 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000091
mtklein36352bf2015-03-25 18:17:31 -070092 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070093 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000094 }
reed@google.com775bf662014-05-05 19:37:22 +000095
mtklein36352bf2015-03-25 18:17:31 -070096 void onDraw(SkCanvas* canvas) override {
reed2ad1aa62016-03-09 09:50:50 -080097 sk_sp<SkShader> shaders[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040098 make_shader(SkBlendMode::kDstIn),
99 make_shader(SkBlendMode::kSrcOver),
reed2ad1aa62016-03-09 09:50:50 -0800100 };
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000101
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000102 SkPaint paint;
103 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000104
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000105 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000106
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000107 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000108 canvas->save();
109 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
Mike Reed9407e242019-02-15 16:13:57 -0500110 paint.setAlphaf(1.0f);
halcanary96fcdcc2015-08-27 07:41:13 -0700111 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000112 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000113
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000114 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800115 paint.setShader(shaders[y]);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000116 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000117
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000118 canvas->translate(r.width() + 5, 0);
119 }
120 canvas->restore();
121 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000122 }
123 }
wangyixd7059582015-09-03 08:32:22 -0700124
125private:
126 typedef GM INHERITED ;
127};
Mike Reed27738e02018-02-02 14:36:05 -0500128DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700129
130// creates a square bitmap with red background and a green circle in the center
131static void draw_color_bm(SkBitmap* bm, int length) {
132 SkPaint paint;
133 paint.setColor(SK_ColorGREEN);
134
135 bm->allocN32Pixels(length, length);
136 bm->eraseColor(SK_ColorRED);
137
138 SkCanvas canvas(*bm);
139 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
140 paint);
141}
142
143// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
144static void draw_alpha8_bm(SkBitmap* bm, int length) {
145 SkPaint circlePaint;
146 circlePaint.setColor(SK_ColorBLACK);
147
148 bm->allocPixels(SkImageInfo::MakeA8(length, length));
149 bm->eraseColor(SK_ColorTRANSPARENT);
150
151 SkCanvas canvas(*bm);
152 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
153 circlePaint);
154}
155
156// creates a linear gradient shader
reed2ad1aa62016-03-09 09:50:50 -0800157static sk_sp<SkShader> make_linear_gradient_shader(int length) {
wangyixd7059582015-09-03 08:32:22 -0700158 SkPoint pts[2];
159 SkColor colors[2];
160 pts[0].set(0, 0);
161 pts[1].set(SkIntToScalar(length), 0);
162 colors[0] = SK_ColorBLUE;
163 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
Mike Reedfae8fce2019-04-03 10:27:45 -0400164 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
wangyixd7059582015-09-03 08:32:22 -0700165}
166
167
168class ComposeShaderBitmapGM : public skiagm::GM {
169public:
Florin Malita9ab449a2019-07-09 13:22:50 -0400170 ComposeShaderBitmapGM(bool use_lm) : fUseLocalMatrix(use_lm) {}
caryclark6df61152016-01-04 14:17:47 -0800171
herbb10fe492016-01-08 13:48:43 -0800172protected:
caryclark6df61152016-01-04 14:17:47 -0800173 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700174 draw_color_bm(&fColorBitmap, squareLength);
175 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
176 SkMatrix s;
177 s.reset();
Mike Reed50acf8f2019-04-08 13:20:23 -0400178 fColorBitmapShader = fColorBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s);
179 fAlpha8BitmapShader = fAlpha8Bitmap.makeShader(SkTileMode::kRepeat,
180 SkTileMode::kRepeat, &s);
wangyixd7059582015-09-03 08:32:22 -0700181 fLinearGradientShader = make_linear_gradient_shader(squareLength);
182 }
caryclark6df61152016-01-04 14:17:47 -0800183
wangyixd7059582015-09-03 08:32:22 -0700184 SkString onShortName() override {
Florin Malita9ab449a2019-07-09 13:22:50 -0400185 return SkStringPrintf("composeshader_bitmap%s", fUseLocalMatrix ? "_lm" : "");
wangyixd7059582015-09-03 08:32:22 -0700186 }
187
188 SkISize onISize() override {
189 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
190 }
191
192 void onDraw(SkCanvas* canvas) override {
Mike Reed7d954ad2016-10-28 15:42:34 -0400193 SkBlendMode mode = SkBlendMode::kDstOver;
wangyixd7059582015-09-03 08:32:22 -0700194
Florin Malita9ab449a2019-07-09 13:22:50 -0400195 SkTLazy<SkMatrix> lm;
196 if (fUseLocalMatrix) {
197 lm.set(SkMatrix::MakeTrans(0, squareLength * 0.5f));
198 }
199
reed2ad1aa62016-03-09 09:50:50 -0800200 sk_sp<SkShader> shaders[] = {
201 // gradient should appear over color bitmap
Florin Malita9ab449a2019-07-09 13:22:50 -0400202 SkShaders::Blend(mode, fLinearGradientShader, fColorBitmapShader, lm.getMaybeNull()),
reed2ad1aa62016-03-09 09:50:50 -0800203 // gradient should appear over alpha8 bitmap colorized by the paint color
Florin Malita9ab449a2019-07-09 13:22:50 -0400204 SkShaders::Blend(mode, fLinearGradientShader, fAlpha8BitmapShader, lm.getMaybeNull()),
reed2ad1aa62016-03-09 09:50:50 -0800205 };
wangyixd7059582015-09-03 08:32:22 -0700206
207 SkPaint paint;
208 paint.setColor(SK_ColorYELLOW);
209
210 const SkRect r = SkRect::MakeXYWH(0, 0, SkIntToScalar(squareLength),
211 SkIntToScalar(squareLength));
212
213 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
wangyixd7059582015-09-03 08:32:22 -0700214 canvas->save();
215 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
216 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800217 paint.setShader(shaders[y]);
wangyixd7059582015-09-03 08:32:22 -0700218 canvas->drawRect(r, paint);
219
220 canvas->translate(r.width() + 5, 0);
221 }
222 canvas->restore();
223 canvas->translate(0, r.height() + 5);
224 }
225 }
halcanary9d524f22016-03-29 09:03:52 -0700226
wangyixd7059582015-09-03 08:32:22 -0700227private:
reed8681e602016-02-15 08:27:14 -0800228 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700229 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
230 * work in a release build. You can change this parameter and then compile a release build
231 * to have this GM draw larger bitmaps for easier visual inspection.
232 */
mtkleindbfd7ab2016-09-01 11:24:54 -0700233 static constexpr int squareLength = 20;
wangyixd7059582015-09-03 08:32:22 -0700234
Florin Malita9ab449a2019-07-09 13:22:50 -0400235 const bool fUseLocalMatrix;
236
wangyixd7059582015-09-03 08:32:22 -0700237 SkBitmap fColorBitmap;
238 SkBitmap fAlpha8Bitmap;
reed2ad1aa62016-03-09 09:50:50 -0800239 sk_sp<SkShader> fColorBitmapShader;
240 sk_sp<SkShader> fAlpha8BitmapShader;
241 sk_sp<SkShader> fLinearGradientShader;
wangyixd7059582015-09-03 08:32:22 -0700242
243 typedef GM INHERITED;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000244};
Florin Malita9ab449a2019-07-09 13:22:50 -0400245DEF_GM( return new ComposeShaderBitmapGM(false); )
246DEF_GM( return new ComposeShaderBitmapGM(true); )
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000247
caryclarkc2ad65e2016-08-15 12:03:33 -0700248DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) {
249 int width = 255;
250 int height = 255;
251 SkTDArray<uint8_t> dst8Storage;
252 dst8Storage.setCount(width * height);
253 SkTDArray<uint32_t> dst32Storage;
254 dst32Storage.setCount(width * height * sizeof(int32_t));
255 for (int y = 0; y < height; ++y) {
256 for (int x = 0; x < width; ++x) {
257 dst8Storage[y * width + x] = (y + x) / 2;
258 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
259 }
260 }
261 SkPaint paint;
262 paint.setAntiAlias(true);
263 paint.setColor(SK_ColorBLUE);
264 SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)};
265 canvas->drawRect(r, paint);
266 SkBitmap skBitmap, skMask;
267 SkImageInfo imageInfo = SkImageInfo::Make(width, height,
268 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
269 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
Mike Reed086a4272017-07-18 10:53:11 -0400270 nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700271 imageInfo = SkImageInfo::Make(width, height,
272 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
Mike Reed086a4272017-07-18 10:53:11 -0400273 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700274 sk_sp<SkImage> skSrc = SkImage::MakeFromBitmap(skBitmap);
caryclarkc2ad65e2016-08-15 12:03:33 -0700275 sk_sp<SkImage> skMaskImage = SkImage::MakeFromBitmap(skMask);
caryclarkc2ad65e2016-08-15 12:03:33 -0700276 paint.setShader(
Mike Reedc8bea7d2019-04-09 13:55:36 -0400277 SkShaders::Blend(SkBlendMode::kSrcIn, skMaskImage->makeShader(), skSrc->makeShader()));
caryclarkc2ad65e2016-08-15 12:03:33 -0700278 canvas->drawRect(r, paint);
279}
280
Mike Reed27738e02018-02-02 14:36:05 -0500281///////////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000282
Mike Reed27738e02018-02-02 14:36:05 -0500283static sk_sp<SkShader> make_src_shader(SkScalar size) {
284 const SkPoint pts[] = { { 0, 0 }, { 0, size } };
285 const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
Mike Reedfae8fce2019-04-03 10:27:45 -0400286 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed27738e02018-02-02 14:36:05 -0500287}
288
289static sk_sp<SkShader> make_dst_shader(SkScalar size) {
290 const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
291 const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400292 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed27738e02018-02-02 14:36:05 -0500293}
294
295const SkScalar gCellSize = 100;
296
297static void draw_cell(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
298 SkBlendMode mode, SkAlpha alpha) {
299 const SkRect r = SkRect::MakeWH(gCellSize, gCellSize);
300 SkPaint p;
301 p.setAlpha(alpha);
302
303 SkAutoCanvasRestore acr(canvas, false);
304 canvas->saveLayer(&r, &p);
305 p.setAlpha(0xFF);
306
307 p.setShader(dst);
308 p.setBlendMode(SkBlendMode::kSrc);
309 canvas->drawRect(r, p);
310
311 p.setShader(src);
312 p.setBlendMode(mode);
313 canvas->drawRect(r, p);
314}
315
316static void draw_composed(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
317 SkBlendMode mode, SkAlpha alpha) {
318 SkPaint p;
319 p.setAlpha(alpha);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400320 p.setShader(SkShaders::Blend(mode, dst, src));
Mike Reed27738e02018-02-02 14:36:05 -0500321 canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p);
322}
323
324static void draw_pair(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
325 SkBlendMode mode) {
326 SkAutoCanvasRestore acr(canvas, true);
327
328 const SkScalar gap = 4;
329 SkRect r = SkRect::MakeWH(2 * gCellSize + gap, 2 * gCellSize + gap);
Mike Reed1d08d722018-02-02 16:15:49 -0500330 r.outset(gap + 1.5f, gap + 1.5f);
Mike Reed27738e02018-02-02 14:36:05 -0500331 SkPaint p;
332 p.setStyle(SkPaint::kStroke_Style);
333 canvas->drawRect(r, p); // border
334
335 SkAlpha alpha = 0xFF;
336 for (int y = 0; y < 2; ++y) {
337 draw_cell(canvas, src, dst, mode, alpha);
338 canvas->save();
339 canvas->translate(gCellSize + gap, 0);
340 draw_composed(canvas, src, dst, mode, alpha);
341 canvas->restore();
342
343 canvas->translate(0, gCellSize + gap);
344 alpha = 0x80;
345 }
346}
347
348DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) {
349 auto src = make_src_shader(gCellSize);
350 auto dst = make_dst_shader(gCellSize);
351
Mike Reed1d08d722018-02-02 16:15:49 -0500352 const SkScalar margin = 15;
Mike Reed27738e02018-02-02 14:36:05 -0500353 const SkScalar dx = 2*gCellSize + margin;
354 const SkScalar dy = 2*gCellSize + margin;
355
356 canvas->translate(margin, margin);
357 canvas->save();
358 for (int m = 0; m < 16; ++m) {
359 SkBlendMode mode = static_cast<SkBlendMode>(m);
360 draw_pair(canvas, src, dst, mode);
361 if ((m % 4) == 3) {
362 canvas->restore();
363 canvas->translate(0, dy);
364 canvas->save();
365 } else {
366 canvas->translate(dx, 0);
367 }
368 }
369 canvas->restore();
370}