• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkGradientShader.h"
22 
make_transparency(SkCanvas * canvas,SkScalar width,SkScalar height)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         SkColor shaderColors[2];
41         shaderColors[0] = SK_AlphaTRANSPARENT;
42         shaderColors[1] = kColors[i];
43         SkPaint p;
44         p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2, SkTileMode::kClamp));
45         canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
46     }
47 }
48 
49 // http://crrev.com/834303005
create_checkerboard_shader(SkColor c1,SkColor c2,int size)50 static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
51     SkBitmap bm;
52     bm.allocN32Pixels(2 * size, 2 * size);
53     bm.eraseColor(c1);
54     bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
55     bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
56     return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
57 }
58 
59 // http://crrev.com/834303005
checkerboard(SkCanvas * canvas,SkColor c1,SkColor c2,int size)60 static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
61     SkPaint paint;
62     paint.setShader(create_checkerboard_shader(c1, c2, size));
63     canvas->drawPaint(paint);
64 }
65 
66 /**
67  *  This GM verifies that a transparent bitmap drawn over a
68  *  checkerboard pattern looks correct.
69  */
70 DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
71     checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
72     {
73         SkAutoCanvasRestore autoCanvasRestore(canvas, true);
74         auto surface(SkSurface::MakeRasterN32Premul(256, 9));
75         make_transparency(surface->getCanvas(), 256.0f, 9.0f);
76         canvas->scale(7.0f, 120.0f);
77         surface->draw(canvas, 0, 0);
78     }
79 }
80