• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public:
ComposeShaderGM()54     ComposeShaderGM() {
55         fShader = make_shader(SkBlendMode::kDstIn);
56     }
57 
58 protected:
onShortName()59     SkString onShortName() override {
60         return SkString("composeshader");
61     }
62 
onISize()63     SkISize onISize() override {
64         return SkISize::Make(120, 120);
65     }
66 
onDraw(SkCanvas * canvas)67     void onDraw(SkCanvas* canvas) override {
68         SkPaint paint;
69         paint.setColor(SK_ColorGREEN);
70         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
71         paint.setShader(fShader);
72         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
73     }
74 
75 protected:
76     sk_sp<SkShader> fShader;
77 
78 private:
79     typedef GM INHERITED ;
80 };
81 DEF_GM( return new ComposeShaderGM; )
82 
83 class ComposeShaderAlphaGM : public skiagm::GM {
84 public:
ComposeShaderAlphaGM()85     ComposeShaderAlphaGM() {}
86 
87 protected:
onShortName()88     SkString onShortName() override {
89         return SkString("composeshader_alpha");
90     }
91 
onISize()92     SkISize onISize() override {
93         return SkISize::Make(750, 220);
94     }
95 
onDraw(SkCanvas * canvas)96     void onDraw(SkCanvas* canvas) override {
97         sk_sp<SkShader> shaders[] = {
98             make_shader(SkBlendMode::kDstIn),
99             make_shader(SkBlendMode::kSrcOver),
100         };
101 
102         SkPaint paint;
103         paint.setColor(SK_ColorGREEN);
104 
105         const SkRect r = SkRect::MakeXYWH(5, 5, 100, 100);
106 
107         for (size_t y = 0; y < SK_ARRAY_COUNT(shaders); ++y) {
108             canvas->save();
109             for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
110                 paint.setAlphaf(1.0f);
111                 paint.setShader(nullptr);
112                 canvas->drawRect(r, paint);
113 
114                 paint.setAlpha(alpha);
115                 paint.setShader(shaders[y]);
116                 canvas->drawRect(r, paint);
117 
118                 canvas->translate(r.width() + 5, 0);
119             }
120             canvas->restore();
121             canvas->translate(0, r.height() + 5);
122         }
123     }
124 
125 private:
126     typedef GM INHERITED ;
127 };
DEF_GM(return new ComposeShaderAlphaGM;)128 DEF_GM( return new ComposeShaderAlphaGM; )
129 
130 // creates a square bitmap with red background and a green circle in the center
131 static 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
draw_alpha8_bm(SkBitmap * bm,int length)144 static 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
make_linear_gradient_shader(int length)157 static sk_sp<SkShader> make_linear_gradient_shader(int length) {
158     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);
164     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
165 }
166 
167 
168 class ComposeShaderBitmapGM : public skiagm::GM {
169 public:
ComposeShaderBitmapGM(bool use_lm)170     ComposeShaderBitmapGM(bool use_lm) : fUseLocalMatrix(use_lm) {}
171 
172 protected:
onOnceBeforeDraw()173     void onOnceBeforeDraw() override {
174         draw_color_bm(&fColorBitmap, squareLength);
175         draw_alpha8_bm(&fAlpha8Bitmap, squareLength);
176         SkMatrix s;
177         s.reset();
178         fColorBitmapShader = fColorBitmap.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &s);
179         fAlpha8BitmapShader = fAlpha8Bitmap.makeShader(SkTileMode::kRepeat,
180                                                        SkTileMode::kRepeat, &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         SkTLazy<SkMatrix> lm;
196         if (fUseLocalMatrix) {
197             lm.set(SkMatrix::MakeTrans(0, squareLength * 0.5f));
198         }
199 
200         sk_sp<SkShader> shaders[] = {
201             // gradient should appear over color bitmap
202             SkShaders::Blend(mode, fLinearGradientShader, fColorBitmapShader, lm.getMaybeNull()),
203             // gradient should appear over alpha8 bitmap colorized by the paint color
204             SkShaders::Blend(mode, fLinearGradientShader, fAlpha8BitmapShader, lm.getMaybeNull()),
205         };
206 
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) {
214             canvas->save();
215             for (int alpha = 0xFF; alpha > 0; alpha -= 0x28) {
216                 paint.setAlpha(alpha);
217                 paint.setShader(shaders[y]);
218                 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     }
226 
227 private:
228     /** This determines the length and width of the bitmaps used in the ComposeShaders.  Values
229      *  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      */
233     static constexpr int squareLength = 20;
234 
235     const bool fUseLocalMatrix;
236 
237     SkBitmap fColorBitmap;
238     SkBitmap fAlpha8Bitmap;
239     sk_sp<SkShader> fColorBitmapShader;
240     sk_sp<SkShader> fAlpha8BitmapShader;
241     sk_sp<SkShader> fLinearGradientShader;
242 
243     typedef GM INHERITED;
244 };
245 DEF_GM( return new ComposeShaderBitmapGM(false); )
DEF_GM(return new ComposeShaderBitmapGM (true);)246 DEF_GM( return new ComposeShaderBitmapGM(true); )
247 
248 DEF_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),
270                            nullptr, nullptr);
271     imageInfo = SkImageInfo::Make(width, height,
272             SkColorType::kAlpha_8_SkColorType, kPremul_SkAlphaType);
273     skMask.installPixels(imageInfo, dst8Storage.begin(), width, nullptr, nullptr);
274     sk_sp<SkImage> skSrc = SkImage::MakeFromBitmap(skBitmap);
275     sk_sp<SkImage> skMaskImage = SkImage::MakeFromBitmap(skMask);
276     paint.setShader(
277         SkShaders::Blend(SkBlendMode::kSrcIn, skMaskImage->makeShader(), skSrc->makeShader()));
278     canvas->drawRect(r, paint);
279 }
280 
281 ///////////////////////////////////////////////////////////////////////////////////////////////////
282 
make_src_shader(SkScalar size)283 static sk_sp<SkShader> make_src_shader(SkScalar size) {
284     const SkPoint pts[] = { { 0, 0 }, { 0, size } };
285     const SkColor colors[] = { 0xFF0000FF, 0x000000FF };
286     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
287 }
288 
make_dst_shader(SkScalar size)289 static sk_sp<SkShader> make_dst_shader(SkScalar size) {
290     const SkPoint pts[] = { { 0, 0 }, { size, 0 } };
291     const SkColor colors[] = { SK_ColorRED, 0x00FF0000 };
292     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
293 }
294 
295 const SkScalar gCellSize = 100;
296 
draw_cell(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode,SkAlpha alpha)297 static 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 
draw_composed(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode,SkAlpha alpha)316 static 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);
320     p.setShader(SkShaders::Blend(mode, dst, src));
321     canvas->drawRect(SkRect::MakeWH(gCellSize, gCellSize), p);
322 }
323 
draw_pair(SkCanvas * canvas,sk_sp<SkShader> src,sk_sp<SkShader> dst,SkBlendMode mode)324 static 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);
330     r.outset(gap + 1.5f, gap + 1.5f);
331     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 
348 DEF_SIMPLE_GM(composeshader_grid, canvas, 882, 882) {
349     auto src = make_src_shader(gCellSize);
350     auto dst = make_dst_shader(gCellSize);
351 
352     const SkScalar margin = 15;
353     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 }
371