blob: 1b008fefd21505258d9d58e6212851a2503ccd53 [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
reed1a9b9642016-03-13 14:13:58 -070012static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) {
halcanarya7976be2015-01-07 13:23:09 -080013 SkPoint pts[2];
14 pts[0] = SkPoint::Make(0, 0);
15 pts[1] = SkPoint::Make(width, 0);
16 const SkColor kColors[] = {
17 SK_ColorBLACK,
18 SK_ColorGRAY,
19 SK_ColorWHITE,
20 SK_ColorRED,
21 SK_ColorYELLOW,
22 SK_ColorGREEN,
23 SK_ColorCYAN,
24 SK_ColorBLUE,
25 SK_ColorMAGENTA,
26 };
27 const SkScalar kRowHeight = height / SK_ARRAY_COUNT(kColors);
28 for (size_t i = 0; i < SK_ARRAY_COUNT(kColors); ++i) {
29 SkColor shaderColors[2];
30 shaderColors[0] = SK_AlphaTRANSPARENT;
31 shaderColors[1] = kColors[i];
halcanarya7976be2015-01-07 13:23:09 -080032 SkPaint p;
reed1a9b9642016-03-13 14:13:58 -070033 p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2,
34 SkShader::kClamp_TileMode));
35 canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
halcanarya7976be2015-01-07 13:23:09 -080036 }
37}
38
39// http://crrev.com/834303005
reed1a9b9642016-03-13 14:13:58 -070040static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanarya7976be2015-01-07 13:23:09 -080041 SkBitmap bm;
42 bm.allocN32Pixels(2 * size, 2 * size);
43 bm.eraseColor(c1);
44 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
45 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
reed1a9b9642016-03-13 14:13:58 -070046 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
halcanarya7976be2015-01-07 13:23:09 -080047}
48
49// http://crrev.com/834303005
50static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
51 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070052 paint.setShader(create_checkerboard_shader(c1, c2, size));
halcanarya7976be2015-01-07 13:23:09 -080053 canvas->drawPaint(paint);
54}
55
56/**
57 * This GM verifies that a transparent bitmap drawn over a
58 * checkerboard pattern looks correct.
59 */
halcanaryda9170f2015-01-08 12:11:42 -080060DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
Mike Kleind46dce32018-08-16 10:17:03 -040061 checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
halcanarya7976be2015-01-07 13:23:09 -080062 {
63 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
reede8f30622016-03-23 18:59:25 -070064 auto surface(SkSurface::MakeRasterN32Premul(256, 9));
halcanarya7976be2015-01-07 13:23:09 -080065 make_transparency(surface->getCanvas(), 256.0f, 9.0f);
halcanaryda9170f2015-01-08 12:11:42 -080066 canvas->scale(7.0f, 120.0f);
halcanary96fcdcc2015-08-27 07:41:13 -070067 surface->draw(canvas, 0, 0, nullptr);
halcanarya7976be2015-01-07 13:23:09 -080068 }
69}