Debian Codegraph @

gm/transparency.cpp

raw
1 /*
2 * Copyright 2015 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/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTileMode.h"
20 #include "include/core/SkTypes.h"
21 #include "include/effects/SkGradient.h"
22
23 static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) {
24 SkPoint pts[2];
25 pts[0] = SkPoint::Make(0, 0);
26 pts[1] = SkPoint::Make(width, 0);
27 const SkColor kColors[] = {
28 SK_ColorBLACK,
29 SK_ColorGRAY,
30 SK_ColorWHITE,
31 SK_ColorRED,
32 SK_ColorYELLOW,
33 SK_ColorGREEN,
34 SK_ColorCYAN,
35 SK_ColorBLUE,
36 SK_ColorMAGENTA,
37 };
38 const SkScalar kRowHeight = height / std::size(kColors);
39 for (size_t i = 0; i < std::size(kColors); ++i) {
40 SkColor4f shaderColors[] = {
41 {0, 0, 0, 0},
42 SkColor4f::FromColor(kColors[i])
43 };
44 SkPaint p;
45 p.setShader(SkShaders::LinearGradient(pts, {{shaderColors, {}, SkTileMode::kClamp}, {}}));
46 canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
47 }
48 }
49
50 // http://crrev.com/834303005
51 static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
52 SkBitmap bm;
53 bm.allocN32Pixels(2 * size, 2 * size);
54 bm.eraseColor(c1);
55 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
56 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
57 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
58 }
59
60 // http://crrev.com/834303005
61 static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
62 SkPaint paint;
63 paint.setShader(create_checkerboard_shader(c1, c2, size));
64 canvas->drawPaint(paint);
65 }
66
67 /**
68 * This GM verifies that a transparent bitmap drawn over a
69 * checkerboard pattern looks correct.
70 */
71 DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
72 checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
73 {
74 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
75 auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(256, 9)));
76 make_transparency(surface->getCanvas(), 256.0f, 9.0f);
77 canvas->scale(7.0f, 120.0f);
78 surface->draw(canvas, 0, 0);
79 }
80 }