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 
17 static sk_sp<SkShader> make_opaque_color() {
18     return SkShader::MakeColorShader(0xFFFF0000);
19 }
20 
21 static sk_sp<SkShader> make_alpha_color() {
22     return SkShader::MakeColorShader(0x80FF0000);
23 }
24 
25 static sk_sp<SkColorFilter> make_cf_null() {
26     return nullptr;
27 }
28 
29 static sk_sp<SkColorFilter> make_cf0() {
30     SkColorMatrix cm;
31     cm.setSaturation(0.75f);
32     return SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat);
33 }
34 
35 static 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     auto b(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat));
45     return SkColorFilter::MakeComposeFilter(a, b);
46 }
47 
48 static sk_sp<SkColorFilter> make_cf2() {
49     return SkColorFilter::MakeModeFilter(0x8044CC88, SkBlendMode::kSrcATop);
50 }
51 
52 static void draw_into_canvas(SkCanvas* canvas) {
53     const SkRect r = SkRect::MakeWH(50, 100);
54     sk_sp<SkShader> (*shaders[])() { make_opaque_color, make_alpha_color };
55     sk_sp<SkColorFilter> (*filters[])() { make_cf_null, make_cf0, make_cf1, make_cf2 };
56 
57     SkPaint paint;
58     for (auto shProc : shaders) {
59         paint.setShader(shProc());
60         for (auto cfProc : filters) {
61             paint.setColorFilter(cfProc());
62             canvas->drawRect(r, paint);
63             canvas->translate(60, 0);
64         }
65     }
66 }
67 
68 DEF_SIMPLE_GM(color4f, canvas, 1024, 260) {
69     canvas->translate(10, 10);
70 
71     SkPaint bg;
72     // need the target to be opaque, so we can draw it to the screen
73     // even if it holds sRGB values.
74     bg.setColor(0xFFFFFFFF);
75 
76     sk_sp<SkColorSpace> colorSpaces[]{
77         nullptr,
78         SkColorSpace::MakeSRGB()
79     };
80     for (auto colorSpace : colorSpaces) {
81         const SkImageInfo info = SkImageInfo::Make(1024, 100, kN32_SkColorType, kPremul_SkAlphaType,
82                                                    colorSpace);
83         auto surface(SkSurface::MakeRaster(info));
84         surface->getCanvas()->drawPaint(bg);
85         draw_into_canvas(surface->getCanvas());
86         surface->draw(canvas, 0, 0, nullptr);
87         canvas->translate(0, 120);
88     }
89 }
90 
91 ///////////////////////////////////////////////////////////////////////////////////////////////////
92 #include "SkColorSpace.h"
93 
94 DEF_SIMPLE_GM(color4shader, canvas, 360, 480) {
95     canvas->translate(10, 10);
96 
97     auto srgb = SkColorSpace::MakeSRGB();
98     auto spin = srgb->makeColorSpin(); // RGB -> GBR
99 
100     const SkColor4f colors[] {
101         { 1, 0, 0, 1 },
102         { 0, 1, 0, 1 },
103         { 0, 0, 1, 1 },
104         { 0.5, 0.5, 0.5, 1 },
105     };
106 
107     SkPaint paint;
108     SkRect r = SkRect::MakeWH(100, 100);
109 
110     for (const auto& c4 : colors) {
111         sk_sp<SkShader> shaders[] {
112             SkShader::MakeColorShader(c4, nullptr),
113             SkShader::MakeColorShader(c4, srgb),
114             SkShader::MakeColorShader(c4, spin),
115         };
116 
117         canvas->save();
118         for (const auto& s : shaders) {
119             paint.setShader(s);
120             canvas->drawRect(r, paint);
121             canvas->translate(r.width() * 6 / 5, 0);
122         }
123         canvas->restore();
124         canvas->translate(0, r.height() * 6 / 5);
125     }
126 }
127