1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(UnpremulBugs, 400, 400, false, 6) {
fill(SkBitmap & b,SkColor c,int n)5 void fill(SkBitmap & b, SkColor c, int n) {
6 for (int y = 0; y < n; y++) {
7 for (int x = 0; x < n; x++) {
8 *(b.getAddr32(x, y)) = c;
9 }
10 }
11 }
12
doCell(SkCanvas * canvas,float x,float y,SkAlphaType at,SkColor c,int n)13 void doCell(SkCanvas* canvas, float x, float y, SkAlphaType at, SkColor c, int n) {
14 SkImageInfo info = SkImageInfo::MakeS32(n, n, at);
15 SkBitmap bmp;
16 bmp.allocPixels(info);
17 fill(bmp, c, n);
18 sk_sp<SkImage> img = bmp.asImage();
19
20 SkPaint paint;
21 const SkTileMode tile = SkTileMode::kRepeat;
22
23 paint.setShader(img->makeShader(tile, tile, SkSamplingOptions()));
24 canvas->drawRect(SkRect::MakeXYWH(x, y, 50, 50), paint);
25 }
26
draw(SkCanvas * canvas)27 void draw(SkCanvas* canvas) {
28 canvas->clear(SK_ColorBLACK);
29
30 doCell(canvas, 0, 0, kPremul_SkAlphaType, 0x80404040, 2);
31 doCell(canvas, 0, 50, kUnpremul_SkAlphaType, 0x80808080, 2);
32 doCell(canvas, 50, 0, kPremul_SkAlphaType, 0x80808080, 2);
33 doCell(canvas, 50, 50, kUnpremul_SkAlphaType, 0x80FFFFFF, 2);
34
35 doCell(canvas, 110, 0, kPremul_SkAlphaType, 0x80404040, 1);
36 doCell(canvas, 110, 50, kUnpremul_SkAlphaType, 0x80808080, 1);
37 doCell(canvas, 160, 0, kPremul_SkAlphaType, 0x80808080, 1);
38 doCell(canvas, 160, 50, kUnpremul_SkAlphaType, 0x80FFFFFF, 1);
39
40 SkFont font(nullptr, 12);
41 SkPaint paint;
42 paint.setColor(SK_ColorWHITE);
43 canvas->drawString("Premul", 220, 20, font, paint);
44 canvas->drawString("Unpremul", 220, 70, font, paint);
45 canvas->drawString("Gray", 5, 120, font, paint);
46 canvas->drawString("White", 55, 120, font, paint);
47 canvas->drawString("Gray", 115, 120, font, paint);
48 canvas->drawString("White", 165, 120, font, paint);
49
50 canvas->drawString("2x2", 40, 150, font, paint);
51 canvas->drawString("1x1", 150, 150, font, paint);
52 }
53 } // END FIDDLE
54