1 /*
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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkImage.h"
15 #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"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradientShader.h"
28 #include "include/private/SkTDArray.h"
29 #include "src/core/SkTLazy.h"
30
31 #include <utility>
32
make_shader(SkBlendMode mode)33 static sk_sp<SkShader> make_shader(SkBlendMode mode) {
34 SkPoint pts[2];
35 SkColor colors[2];
36
37 pts[0].set(0, 0);
38 pts[1].set(SkIntToScalar(100), 0);
39 colors[0] = SK_ColorRED;
40 colors[1] = SK_ColorBLUE;
41 auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
42
43 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);
47 auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
48
49 return SkShaders::Blend(mode, std::move(shaderA), std::move(shaderB));
50 }
51
52 class ComposeShaderGM : public skiagm::GM {
53 protected:
onOnceBeforeDraw()54 void onOnceBeforeDraw() override {
55 fShader = make_shader(SkBlendMode::kDstIn);
56 }
57
onShortName()58 SkString onShortName() override {
59 return SkString("composeshader");
60 }
61
onISize()62 SkISize onISize() override {
63 return SkISize::Make(120, 120);
64 }
65
onDraw(SkCanvas * canvas)66 void onDraw(SkCanvas* canvas) override {
67 SkPaint paint;
68 paint.setColor(SK_ColorGREEN);
69 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
70 paint.setShader(fShader);
71 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
72 }
73
74 protected:
75 sk_sp<SkShader> fShader;
76
77 private:
78 typedef GM INHERITED ;
79 };
80 DEF_GM( return new ComposeShaderGM; )
81
82 class ComposeShaderAlphaGM : public skiagm::GM {
83 public:
ComposeShaderAlphaGM()84 ComposeShaderAlphaGM() {}
85
86 protected:
onShortName()87 SkString onShortName() override {
88 return SkString("composeshader_alpha");
89 }
90
onISize()91 SkISize onISize() override {
92 return SkISize::Make(750, 220);
93 }
94
onDraw(SkCanvas * canvas)95 void onDraw(SkCanvas* canvas) override {
96 sk_sp<SkShader> shaders[] = {
97 make_shader(SkBlendMode::kDstIn),
98 make_shader(SkBlendMode::kSrcOver),
99 };
100
101 SkPaint paint;
102 paint.setColor(SK_ColorGREEN);
103
104 const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
105
106 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
107 canvas->save();
108 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
109 paint.setAlphaf(1.0f);
110 paint.setShader(nullptr);
111 canvas->drawRect(r, paint);
112
113 paint.setAlpha(alpha);
114 paint.setShader(shaders[y]);
115 canvas->drawRect(r, paint);
116
117 canvas->translate(r.width() + 5, 0);
118 }
119 canvas->restore();
120 canvas->translate(0, r.height() + 5);
121 }
122 }
123
124 private:
125 typedef GM INHERITED ;
126 };
DEF_GM(return new ComposeShaderAlphaGM;)127 DEF_GM( return new ComposeShaderAlphaGM; )
128
129 // creates a square bitmap with red background and a green circle in the center
130 static 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
draw_alpha8_bm(SkBitmap * bm,int length)143 static 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
make_linear_gradient_shader(int length)156 static sk_sp<SkShader> make_linear_gradient_shader(int length) {
157 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);
163 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
164 }
165
166
167 class ComposeShaderBitmapGM : public skiagm::GM {
168 public:
ComposeShaderBitmapGM(bool use_lm)169 ComposeShaderBitmapGM(bool use_lm) : fUseLocalMatrix(use_lm) {}
170
171 protected:
onOnceBeforeDraw()172 void onOnceBeforeDraw() override {
173 draw_color_bm(&fColorBitmap, squareLength);
174 draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
175 SkMatrix s;
176 s.reset();
177 fColorBitmapShader = fColorBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
178 SkSamplingOptions(), s);
179 fAlpha8BitmapShader = fAlpha8Bitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
180 SkSamplingOptions(), s);
181 fLinearGradientShader = make_linear_gradient_shader(squareLength);
182 }
183
onShortName()184 SkString onShortName() override {
185 return SkStringPrintf("composeshader_bitmap%s", fUseLocalMatrix ? "_lm" : "");
186 }
187
onISize()188 SkISize onISize() override {
189 return SkISize::Make(7 * (squareLength + 5), 2 * (squareLength + 5));
190 }
191
onDraw(SkCanvas * canvas)192 void onDraw(SkCanvas* canvas) override {
193 SkBlendMode mode = SkBlendMode::kDstOver;
194
195 SkMatrix lm = SkMatrix::Translate(0, squareLength * 0.5f);
196
197 sk_sp<SkShader> shaders[] = {
198 // gradient should appear over color bitmap
199 SkShaders::Blend(mode, fLinearGradientShader, fColorBitmapShader),
200 // gradient should appear over alpha8 bitmap colorized by the paint color
201 SkShaders::Blend(mode, fLinearGradientShader, fAlpha8BitmapShader),
202 };
203 if (fUseLocalMatrix) {
204 for (unsigned i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
205 shaders[i] = shaders[i]->makeWithLocalMatrix(lm);
206 }
207 }
208
209 SkPaint paint;
210 paint.setColor(SK_ColorYELLOW);
211
212 const SkRect r = SkRect::MakeIWH(squareLength, squareLength);
213
214 for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
215 canvas->save();
216 for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
217 paint.setAlpha(alpha);
218 paint.setShader(shaders[y]);
219 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 }
227
228 private:
229 /** This determines the length and width of the bitmaps used in the ComposeShaders. Values
230 * 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 */
234 static constexpr int squareLength = 20;
235
236 const bool fUseLocalMatrix;
237
238 SkBitmap fColorBitmap;
239 SkBitmap fAlpha8Bitmap;
240 sk_sp<SkShader> fColorBitmapShader;
241 sk_sp<SkShader> fAlpha8BitmapShader;
242 sk_sp<SkShader> fLinearGradientShader;
243
244 using INHERITED = GM;
245 };
246 DEF_GM( return new ComposeShaderBitmapGM(false); )
DEF_GM(return new ComposeShaderBitmapGM (true);)247 DEF_GM( return new ComposeShaderBitmapGM(true); )
248
249 DEF_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),
271 nullptr, nullptr);
272 imageInfo = SkImageInfo::Make(width, height,
273 SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
274 skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
275 sk_sp<SkImage> skSrc = skBitmap.asImage();
276 sk_sp<SkImage> skMaskImage = skMask.asImage();
277 paint.setShader(
278 SkShaders::Blend(SkBlendMode::kSrcIn,
279 skMaskImage->makeShader(SkSamplingOptions()),
280 skSrc->makeShader(SkSamplingOptions())));
281 canvas->drawRect(r, paint);
282 }
283
284 ///////////////////////////////////////////////////////////////////////////////////////////////////
285
make_src_shader(SkScalar size)286 static sk_sp<SkShader> make_src_shader(SkScalar size) {
287 const SkPoint pts[] = { { 0, 0 }, { 0, size } };
288 const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
289 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
290 }
291
make_dst_shader(SkScalar size)292 static sk_sp<SkShader> make_dst_shader(SkScalar size) {
293 const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
294 const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
295 return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
296 }
297
298 const SkScalar gCellSize = 100;
299
draw_cell(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode,SkAlpha alpha)300 static 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
draw_composed(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode,SkAlpha alpha)319 static 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);
323 p.setShader(SkShaders::Blend(mode, dst, src));
324 canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p);
325 }
326
draw_pair(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode)327 static 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);
333 r.outset(gap + 1.5f, gap + 1.5f);
334 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
351 DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) {
352 auto src = make_src_shader(gCellSize);
353 auto dst = make_dst_shader(gCellSize);
354
355 const SkScalar margin = 15;
356 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 }
374