blob: c0d2be7787c1baf4aa15d5498e7d6f83e114e855 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/effects/SkGradientShader.h"
halcanarya7976be2015-01-07 13:23:09 -080022
reed1a9b9642016-03-13 14:13:58 -070023static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) {
halcanarya7976be2015-01-07 13:23:09 -080024 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 / SK_ARRAY_COUNT(kColors);
39 for (size_t i = 0; i < SK_ARRAY_COUNT(kColors); ++i) {
40 SkColor shaderColors[2];
41 shaderColors[0] = SK_AlphaTRANSPARENT;
42 shaderColors[1] = kColors[i];
halcanarya7976be2015-01-07 13:23:09 -080043 SkPaint p;
Mike Reedfae8fce2019-04-03 10:27:45 -040044 p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2, SkTileMode::kClamp));
reed1a9b9642016-03-13 14:13:58 -070045 canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
halcanarya7976be2015-01-07 13:23:09 -080046 }
47}
48
49// http://crrev.com/834303005
reed1a9b9642016-03-13 14:13:58 -070050static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
halcanarya7976be2015-01-07 13:23:09 -080051 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);
Mike Reed50acf8f2019-04-08 13:20:23 -040056 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
halcanarya7976be2015-01-07 13:23:09 -080057}
58
59// http://crrev.com/834303005
60static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
61 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070062 paint.setShader(create_checkerboard_shader(c1, c2, size));
halcanarya7976be2015-01-07 13:23:09 -080063 canvas->drawPaint(paint);
64}
65
66/**
67 * This GM verifies that a transparent bitmap drawn over a
68 * checkerboard pattern looks correct.
69 */
halcanaryda9170f2015-01-08 12:11:42 -080070DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
Mike Kleind46dce32018-08-16 10:17:03 -040071 checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
halcanarya7976be2015-01-07 13:23:09 -080072 {
73 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
reede8f30622016-03-23 18:59:25 -070074 auto surface(SkSurface::MakeRasterN32Premul(256, 9));
halcanarya7976be2015-01-07 13:23:09 -080075 make_transparency(surface->getCanvas(), 256.0f, 9.0f);
halcanaryda9170f2015-01-08 12:11:42 -080076 canvas->scale(7.0f, 120.0f);
halcanary96fcdcc2015-08-27 07:41:13 -070077 surface->draw(canvas, 0, 0, nullptr);
halcanarya7976be2015-01-07 13:23:09 -080078 }
79}