1/*
2 * Copyright 2011 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.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
11#include "SkShader.h"
12#include "SkSurface.h"
13
14#include "SkColorMatrixFilter.h"
15#include "SkGradientShader.h"
16
17static sk_sp<SkShader> make_opaque_color() {
18    return SkShader::MakeColorShader(0xFFFF0000);
19}
20
21static sk_sp<SkShader> make_alpha_color() {
22    return SkShader::MakeColorShader(0x80FF0000);
23}
24
25static sk_sp<SkColorFilter> make_cf_null() {
26    return nullptr;
27}
28
29static sk_sp<SkColorFilter> make_cf0() {
30    SkColorMatrix cm;
31    cm.setSaturation(0.75f);
32    return SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat);
33}
34
35static sk_sp<SkColorFilter> make_cf1() {
36    SkColorMatrix cm;
37    cm.setSaturation(0.75f);
38    auto a = SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat);
39    // CreateComposedFilter will try to concat these two matrices, resulting in a single
40    // filter (which is good for speed). For this test, we want to force a real compose of
41    // these two, so our inner filter has a scale-up, which disables the optimization of
42    // combining the two matrices.
43    cm.setScale(1.1f, 0.9f, 1);
44    return a->makeComposed(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
45}
46
47static sk_sp<SkColorFilter> make_cf2() {
48    return SkColorFilter::MakeModeFilter(0x8044CC88, SkBlendMode::kSrcATop);
49}
50
51static void draw_into_canvas(SkCanvas* canvas) {
52    const SkRect r = SkRect::MakeWH(50, 100);
53    sk_sp<SkShader> (*shaders[])() { make_opaque_color, make_alpha_color };
54    sk_sp<SkColorFilter> (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
55
56    SkPaint paint;
57    for (auto shProc : shaders) {
58        paint.setShader(shProc());
59        for (auto cfProc : filters) {
60            paint.setColorFilter(cfProc());
61            canvas->drawRect(r, paint);
62            canvas->translate(60, 0);
63        }
64    }
65}
66
67DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
68    canvas->translate(10, 10);
69
70    SkPaint bg;
71    // need the target to be opaque, so we can draw it to the screen
72    // even if it holds sRGB values.
73    bg.setColor(0xFFFFFFFF);
74
75    sk_sp<SkColorSpace> colorSpaces[]{
76        nullptr,
77        SkColorSpace::MakeSRGB()
78    };
79    for (auto colorSpace : colorSpaces) {
80        const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
81                                                   colorSpace);
82        auto surface(SkSurface::MakeRaster(info));
83        surface->getCanvas()->drawPaint(bg);
84        draw_into_canvas(surface->getCanvas());
85        surface->draw(canvas, 0, 0, nullptr);
86        canvas->translate(0, 120);
87    }
88}
89
90///////////////////////////////////////////////////////////////////////////////////////////////////
91#include "SkColorSpace.h"
92
93DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
94    canvas->translate(10, 10);
95
96    auto srgb = SkColorSpace::MakeSRGB();
97    auto spin = srgb->makeColorSpin(); // RGB -> GBR
98
99    const SkColor4f colors[] {
100        { 1, 0, 0, 1 },
101        { 0, 1, 0, 1 },
102        { 0, 0, 1, 1 },
103        { 0.5, 0.5, 0.5, 1 },
104    };
105
106    SkPaint paint;
107    SkRect r = SkRect::MakeWH(100, 100);
108
109    for (const auto& c4 : colors) {
110        sk_sp<SkShader> shaders[] {
111            SkShader::MakeColorShader(c4, nullptr),
112            SkShader::MakeColorShader(c4, srgb),
113            SkShader::MakeColorShader(c4, spin),
114        };
115
116        canvas->save();
117        for (const auto& s : shaders) {
118            paint.setShader(s);
119            canvas->drawRect(r, paint);
120            canvas->translate(r.width() * 6 / 5, 0);
121        }
122        canvas->restore();
123        canvas->translate(0, r.height() * 6 / 5);
124    }
125}
126