blob: 1a6275e2a22a249da8d31defe06cc2d9672d1ef2 [file] [log] [blame]
halcanarya7976be2015-01-07 13:23:09 -08001/*
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 "SkGradientShader.h"
9#include "SkSurface.h"
10#include "gm.h"
11
12static void make_transparency(SkCanvas* canvas,
13 SkScalar width,
14 SkScalar height) {
15 SkPoint pts[2];
16 pts[0] = SkPoint::Make(0, 0);
17 pts[1] = SkPoint::Make(width, 0);
18 const SkColor kColors[] = {
19 SK_ColorBLACK,
20 SK_ColorGRAY,
21 SK_ColorWHITE,
22 SK_ColorRED,
23 SK_ColorYELLOW,
24 SK_ColorGREEN,
25 SK_ColorCYAN,
26 SK_ColorBLUE,
27 SK_ColorMAGENTA,
28 };
29 const SkScalar kRowHeight = height / SK_ARRAY_COUNT(kColors);
30 for (size_t i = 0; i < SK_ARRAY_COUNT(kColors); ++i) {
31 SkColor shaderColors[2];
32 shaderColors[0] = SK_AlphaTRANSPARENT;
33 shaderColors[1] = kColors[i];
34 SkAutoTUnref<SkShader> shader(SkGradientShader::CreateLinear(
35 pts, shaderColors, NULL, 2, SkShader::kClamp_TileMode));
36 SkRect r = SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight);
37 SkPaint p;
38 p.setShader(shader);
39 canvas->drawRect(r, p);
40 }
41}
42
43// http://crrev.com/834303005
44static SkShader* create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
45 SkBitmap bm;
46 bm.allocN32Pixels(2 * size, 2 * size);
47 bm.eraseColor(c1);
48 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
49 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
50 return SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
51 SkShader::kRepeat_TileMode);
52}
53
54// http://crrev.com/834303005
55static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
56 SkPaint paint;
57 paint.setShader(create_checkerboard_shader(c1, c2, size))->unref();
58 canvas->drawPaint(paint);
59}
60
61/**
62 * This GM verifies that a transparent bitmap drawn over a
63 * checkerboard pattern looks correct.
64 */
halcanaryda9170f2015-01-08 12:11:42 -080065DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
halcanarya7976be2015-01-07 13:23:09 -080066 checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
67 {
68 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
69 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(256, 9));
70 make_transparency(surface->getCanvas(), 256.0f, 9.0f);
halcanaryda9170f2015-01-08 12:11:42 -080071 canvas->scale(7.0f, 120.0f);
halcanarya7976be2015-01-07 13:23:09 -080072 surface->draw(canvas, 0, 0, NULL);
73 }
74}