blob: 13033334ef2315b74e7b0bf1ba43625af757994e [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 {
John Stilese6a05a42020-11-05 10:05:19 -050053protected:
54 void onOnceBeforeDraw() override {
Mike Reed7d954ad2016-10-28 15:42:34 -040055 fShader = make_shader(SkBlendMode::kDstIn);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000056 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 SkString onShortName() override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000059 return SkString("composeshader");
60 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 SkISize onISize() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000063 return SkISize::Make(120, 120);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000064 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 void onDraw(SkCanvas* canvas) override {
scroggo@google.com32f20eb2012-06-29 17:28:50 +000067 SkPaint paint;
scroggo@google.com32f20eb2012-06-29 17:28:50 +000068 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050069 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000070 paint.setShader(fShader);
Mike Reed3661bc92017-02-22 13:21:42 -050071 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
scroggo@google.com32f20eb2012-06-29 17:28:50 +000072 }
73
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000074protected:
reed2ad1aa62016-03-09 09:50:50 -080075 sk_sp<SkShader> fShader;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000076
77private:
scroggo@google.com32f20eb2012-06-29 17:28:50 +000078 typedef GM INHERITED ;
79};
Mike Reed27738e02018-02-02 14:36:05 -050080DEF_GM( return new ComposeShaderGM; )
scroggo@google.com32f20eb2012-06-29 17:28:50 +000081
commit-bot@chromium.org58e42872014-05-05 18:23:54 +000082class ComposeShaderAlphaGM : public skiagm::GM {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000083public:
84 ComposeShaderAlphaGM() {}
85
86protected:
mtklein36352bf2015-03-25 18:17:31 -070087 SkString onShortName() override {
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000088 return SkString("composeshader_alpha");
89 }
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +000090
mtklein36352bf2015-03-25 18:17:31 -070091 SkISize onISize() override {
wangyixd7059582015-09-03 08:32:22 -070092 return SkISize::Make(750, 220);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +000093 }
reed@google.com775bf662014-05-05 19:37:22 +000094
mtklein36352bf2015-03-25 18:17:31 -070095 void onDraw(SkCanvas* canvas) override {
reed2ad1aa62016-03-09 09:50:50 -080096 sk_sp<SkShader> shaders[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040097 make_shader(SkBlendMode::kDstIn),
98 make_shader(SkBlendMode::kSrcOver),
reed2ad1aa62016-03-09 09:50:50 -080099 };
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000100
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000101 SkPaint paint;
102 paint.setColor(SK_ColorGREEN);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000103
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000104 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000105
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000106 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000107 canvas->save();
108 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
Mike Reed9407e242019-02-15 16:13:57 -0500109 paint.setAlphaf(1.0f);
halcanary96fcdcc2015-08-27 07:41:13 -0700110 paint.setShader(nullptr);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000111 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000112
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000113 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800114 paint.setShader(shaders[y]);
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000115 canvas->drawRect(r, paint);
skia.committer@gmail.comedda70e2014-05-06 03:06:53 +0000116
commit-bot@chromium.org58e42872014-05-05 18:23:54 +0000117 canvas->translate(r.width() + 5, 0);
118 }
119 canvas->restore();
120 canvas->translate(0, r.height() + 5);
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000121 }
122 }
wangyixd7059582015-09-03 08:32:22 -0700123
124private:
125 typedef GM INHERITED ;
126};
Mike Reed27738e02018-02-02 14:36:05 -0500127DEF_GM( return new ComposeShaderAlphaGM; )
wangyixd7059582015-09-03 08:32:22 -0700128
129// creates a square bitmap with red background and a green circle in the center
130static void draw_color_bm(SkBitmap* bm, int length) {
131 SkPaint paint;
132 paint.setColor(SK_ColorGREEN);
133
134 bm->allocN32Pixels(length, length);
135 bm->eraseColor(SK_ColorRED);
136
137 SkCanvas canvas(*bm);
138 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/2),
139 paint);
140}
141
142// creates a square alpha8 bitmap with transparent background and an opaque circle in the center
143static void draw_alpha8_bm(SkBitmap* bm, int length) {
144 SkPaint circlePaint;
145 circlePaint.setColor(SK_ColorBLACK);
146
147 bm->allocPixels(SkImageInfo::MakeA8(length, length));
148 bm->eraseColor(SK_ColorTRANSPARENT);
149
150 SkCanvas canvas(*bm);
151 canvas.drawCircle(SkIntToScalar(length/2), SkIntToScalar(length/2), SkIntToScalar(length/4),
152 circlePaint);
153}
154
155// creates a linear gradient shader
reed2ad1aa62016-03-09 09:50:50 -0800156static sk_sp<SkShader> make_linear_gradient_shader(int length) {
wangyixd7059582015-09-03 08:32:22 -0700157 SkPoint pts[2];
158 SkColor colors[2];
159 pts[0].set(0, 0);
160 pts[1].set(SkIntToScalar(length), 0);
161 colors[0] = SK_ColorBLUE;
162 colors[1] = SkColorSetARGB(0, 0, 0, 0xFF);
Mike Reedfae8fce2019-04-03 10:27:45 -0400163 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
wangyixd7059582015-09-03 08:32:22 -0700164}
165
166
167class ComposeShaderBitmapGM : public skiagm::GM {
168public:
Florin Malita9ab449a2019-07-09 13:22:50 -0400169 ComposeShaderBitmapGM(bool use_lm) : fUseLocalMatrix(use_lm) {}
caryclark6df61152016-01-04 14:17:47 -0800170
herbb10fe492016-01-08 13:48:43 -0800171protected:
caryclark6df61152016-01-04 14:17:47 -0800172 void onOnceBeforeDraw() override {
wangyixd7059582015-09-03 08:32:22 -0700173 draw_color_bm(&fColorBitmap, squareLength);
174 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
175 SkMatrix s;
176 s.reset();
Mike Reedb41bd152020-12-12 11:18:31 -0500177 fColorBitmapShader = fColorBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
178 SkSamplingOptions(), s);
179 fAlpha8BitmapShader = fAlpha8Bitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
180 SkSamplingOptions(), 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
Mike Reed1f607332020-05-21 12:11:27 -0400195 SkMatrix lm = SkMatrix::Translate(0, squareLength * 0.5f);
Florin Malita9ab449a2019-07-09 13:22:50 -0400196
reed2ad1aa62016-03-09 09:50:50 -0800197 sk_sp<SkShader> shaders[] = {
198 // gradient should appear over color bitmap
Kevin Lubick988ce042020-03-25 13:13:20 -0400199 SkShaders::Blend(mode, fLinearGradientShader, fColorBitmapShader),
reed2ad1aa62016-03-09 09:50:50 -0800200 // gradient should appear over alpha8 bitmap colorized by the paint color
Kevin Lubick988ce042020-03-25 13:13:20 -0400201 SkShaders::Blend(mode, fLinearGradientShader, fAlpha8BitmapShader),
reed2ad1aa62016-03-09 09:50:50 -0800202 };
Kevin Lubick988ce042020-03-25 13:13:20 -0400203 if (fUseLocalMatrix) {
204 for (unsigned i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
205 shaders[i] = shaders[i]->makeWithLocalMatrix(lm);
206 }
207 }
wangyixd7059582015-09-03 08:32:22 -0700208
209 SkPaint paint;
210 paint.setColor(SK_ColorYELLOW);
211
Kevin Lubick988ce042020-03-25 13:13:20 -0400212 const SkRect r = SkRect::MakeIWH(squareLength, squareLength);
wangyixd7059582015-09-03 08:32:22 -0700213
214 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
wangyixd7059582015-09-03 08:32:22 -0700215 canvas->save();
216 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
217 paint.setAlpha(alpha);
reed2ad1aa62016-03-09 09:50:50 -0800218 paint.setShader(shaders[y]);
wangyixd7059582015-09-03 08:32:22 -0700219 canvas->drawRect(r, paint);
220
221 canvas->translate(r.width() + 5, 0);
222 }
223 canvas->restore();
224 canvas->translate(0, r.height() + 5);
225 }
226 }
halcanary9d524f22016-03-29 09:03:52 -0700227
wangyixd7059582015-09-03 08:32:22 -0700228private:
reed8681e602016-02-15 08:27:14 -0800229 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
wangyixd7059582015-09-03 08:32:22 -0700230 * above 20 may cause an SkASSERT to fail in SkSmallAllocator. However, larger values will
231 * work in a release build. You can change this parameter and then compile a release build
232 * to have this GM draw larger bitmaps for easier visual inspection.
233 */
mtkleindbfd7ab2016-09-01 11:24:54 -0700234 static constexpr int squareLength = 20;
wangyixd7059582015-09-03 08:32:22 -0700235
Florin Malita9ab449a2019-07-09 13:22:50 -0400236 const bool fUseLocalMatrix;
237
wangyixd7059582015-09-03 08:32:22 -0700238 SkBitmap fColorBitmap;
239 SkBitmap fAlpha8Bitmap;
reed2ad1aa62016-03-09 09:50:50 -0800240 sk_sp<SkShader> fColorBitmapShader;
241 sk_sp<SkShader> fAlpha8BitmapShader;
242 sk_sp<SkShader> fLinearGradientShader;
wangyixd7059582015-09-03 08:32:22 -0700243
John Stiles7571f9e2020-09-02 22:42:33 -0400244 using INHERITED = GM;
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000245};
Florin Malita9ab449a2019-07-09 13:22:50 -0400246DEF_GM( return new ComposeShaderBitmapGM(false); )
247DEF_GM( return new ComposeShaderBitmapGM(true); )
commit-bot@chromium.orgec7d6732014-05-05 16:19:08 +0000248
caryclarkc2ad65e2016-08-15 12:03:33 -0700249DEF_SIMPLE_GM(composeshader_bitmap2, canvas, 200, 200) {
250 int width = 255;
251 int height = 255;
252 SkTDArray<uint8_t> dst8Storage;
253 dst8Storage.setCount(width * height);
254 SkTDArray<uint32_t> dst32Storage;
255 dst32Storage.setCount(width * height * sizeof(int32_t));
256 for (int y = 0; y < height; ++y) {
257 for (int x = 0; x < width; ++x) {
258 dst8Storage[y * width + x] = (y + x) / 2;
259 dst32Storage[y * width + x] = SkPackARGB32(0xFF, x, y, 0);
260 }
261 }
262 SkPaint paint;
263 paint.setAntiAlias(true);
264 paint.setColor(SK_ColorBLUE);
265 SkRect r = {0, 0, SkIntToScalar(width), SkIntToScalar(height)};
266 canvas->drawRect(r, paint);
267 SkBitmap skBitmap, skMask;
268 SkImageInfo imageInfo = SkImageInfo::Make(width, height,
269 SkColorType::kN32_SkColorType, kPremul_SkAlphaType);
270 skBitmap.installPixels(imageInfo, dst32Storage.begin(), width * sizeof(int32_t),
Mike Reed086a4272017-07-18 10:53:11 -0400271 nullptr, nullptr);
caryclarkc2ad65e2016-08-15 12:03:33 -0700272 imageInfo = SkImageInfo::Make(width, height,
273 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
Mike Reed086a4272017-07-18 10:53:11 -0400274 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
Mike Reedac9f0c92020-12-23 10:11:33 -0500275 sk_sp<SkImage> skSrc = skBitmap.asImage();
276 sk_sp<SkImage> skMaskImage = skMask.asImage();
caryclarkc2ad65e2016-08-15 12:03:33 -0700277 paint.setShader(
Mike Reedb612b6c2020-12-08 21:58:35 -0500278 SkShaders::Blend(SkBlendMode::kSrcIn,
279 skMaskImage->makeShader(SkSamplingOptions()),
280 skSrc->makeShader(SkSamplingOptions())));
caryclarkc2ad65e2016-08-15 12:03:33 -0700281 canvas->drawRect(r, paint);
282}
283
Mike Reed27738e02018-02-02 14:36:05 -0500284///////////////////////////////////////////////////////////////////////////////////////////////////
scroggo@google.com32f20eb2012-06-29 17:28:50 +0000285
Mike Reed27738e02018-02-02 14:36:05 -0500286static sk_sp<SkShader> make_src_shader(SkScalar size) {
287 const SkPoint pts[] = { { 0, 0 }, { 0, size } };
288 const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
Mike Reedfae8fce2019-04-03 10:27:45 -0400289 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed27738e02018-02-02 14:36:05 -0500290}
291
292static sk_sp<SkShader> make_dst_shader(SkScalar size) {
293 const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
294 const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
Mike Reedfae8fce2019-04-03 10:27:45 -0400295 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
Mike Reed27738e02018-02-02 14:36:05 -0500296}
297
298const SkScalar gCellSize = 100;
299
300static void draw_cell(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
301 SkBlendMode mode, SkAlpha alpha) {
302 const SkRect r = SkRect::MakeWH(gCellSize, gCellSize);
303 SkPaint p;
304 p.setAlpha(alpha);
305
306 SkAutoCanvasRestore acr(canvas, false);
307 canvas->saveLayer(&r, &p);
308 p.setAlpha(0xFF);
309
310 p.setShader(dst);
311 p.setBlendMode(SkBlendMode::kSrc);
312 canvas->drawRect(r, p);
313
314 p.setShader(src);
315 p.setBlendMode(mode);
316 canvas->drawRect(r, p);
317}
318
319static void draw_composed(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
320 SkBlendMode mode, SkAlpha alpha) {
321 SkPaint p;
322 p.setAlpha(alpha);
Mike Reedc8bea7d2019-04-09 13:55:36 -0400323 p.setShader(SkShaders::Blend(mode, dst, src));
Mike Reed27738e02018-02-02 14:36:05 -0500324 canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p);
325}
326
327static void draw_pair(SkCanvas* canvas, sk_sp<SkShader> src, sk_sp<SkShader> dst,
328 SkBlendMode mode) {
329 SkAutoCanvasRestore acr(canvas, true);
330
331 const SkScalar gap = 4;
332 SkRect r = SkRect::MakeWH(2 * gCellSize + gap, 2 * gCellSize + gap);
Mike Reed1d08d722018-02-02 16:15:49 -0500333 r.outset(gap + 1.5f, gap + 1.5f);
Mike Reed27738e02018-02-02 14:36:05 -0500334 SkPaint p;
335 p.setStyle(SkPaint::kStroke_Style);
336 canvas->drawRect(r, p); // border
337
338 SkAlpha alpha = 0xFF;
339 for (int y = 0; y < 2; ++y) {
340 draw_cell(canvas, src, dst, mode, alpha);
341 canvas->save();
342 canvas->translate(gCellSize + gap, 0);
343 draw_composed(canvas, src, dst, mode, alpha);
344 canvas->restore();
345
346 canvas->translate(0, gCellSize + gap);
347 alpha = 0x80;
348 }
349}
350
351DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) {
352 auto src = make_src_shader(gCellSize);
353 auto dst = make_dst_shader(gCellSize);
354
Mike Reed1d08d722018-02-02 16:15:49 -0500355 const SkScalar margin = 15;
Mike Reed27738e02018-02-02 14:36:05 -0500356 const SkScalar dx = 2*gCellSize + margin;
357 const SkScalar dy = 2*gCellSize + margin;
358
359 canvas->translate(margin, margin);
360 canvas->save();
361 for (int m = 0; m < 16; ++m) {
362 SkBlendMode mode = static_cast<SkBlendMode>(m);
363 draw_pair(canvas, src, dst, mode);
364 if ((m % 4) == 3) {
365 canvas->restore();
366 canvas->translate(0, dy);
367 canvas->save();
368 } else {
369 canvas->translate(dx, 0);
370 }
371 }
372 canvas->restore();
373}