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