aboutsummaryrefslogtreecommitdiff
path: root/gm/transparency.cpp
blob: d3d22baf6893339857f2bfd604f1ab73e35b2fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkGradientShader.h"

static void make_transparency(SkCanvas* canvas, SkScalar width, SkScalar height) {
    SkPoint pts[2];
    pts[0] = SkPoint::Make(0, 0);
    pts[1] = SkPoint::Make(width, 0);
    const SkColor kColors[] = {
            SK_ColorBLACK,
            SK_ColorGRAY,
            SK_ColorWHITE,
            SK_ColorRED,
            SK_ColorYELLOW,
            SK_ColorGREEN,
            SK_ColorCYAN,
            SK_ColorBLUE,
            SK_ColorMAGENTA,
    };
    const SkScalar kRowHeight = height / std::size(kColors);
    for (size_t i = 0; i < std::size(kColors); ++i) {
        SkColor shaderColors[2];
        shaderColors[0] = SK_AlphaTRANSPARENT;
        shaderColors[1] = kColors[i];
        SkPaint p;
        p.setShader(SkGradientShader::MakeLinear(pts, shaderColors, nullptr, 2, SkTileMode::kClamp));
        canvas->drawRect(SkRect::MakeXYWH(0, i * kRowHeight, width, kRowHeight), p);
    }
}

// http://crrev.com/834303005
static sk_sp<SkShader> create_checkerboard_shader(SkColor c1, SkColor c2, int size) {
    SkBitmap bm;
    bm.allocN32Pixels(2 * size, 2 * size);
    bm.eraseColor(c1);
    bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
    bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
    return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions());
}

// http://crrev.com/834303005
static void checkerboard(SkCanvas* canvas, SkColor c1, SkColor c2, int size) {
    SkPaint paint;
    paint.setShader(create_checkerboard_shader(c1, c2, size));
    canvas->drawPaint(paint);
}

/**
 *  This GM verifies that a transparent bitmap drawn over a
 *  checkerboard pattern looks correct.
 */
DEF_SIMPLE_GM(transparency_check, canvas, 1792, 1080) {
    checkerboard(canvas, 0xFF999999, 0xFF666666, 8);
    {
        SkAutoCanvasRestore autoCanvasRestore(canvas, true);
        auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(256, 9)));
        make_transparency(surface->getCanvas(), 256.0f, 9.0f);
        canvas->scale(7.0f, 120.0f);
        surface->draw(canvas, 0, 0);
    }
}