1 /*
2 * Copyright 2013 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/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "include/effects/SkLumaColorFilter.h"
28 #include "include/effects/SkRuntimeEffect.h"
29 #include "src/core/SkColorFilterPriv.h"
30 #include "tools/Resources.h"
31 #include "tools/ToolUtils.h"
32
33 #include <string.h>
34
35 static SkScalar kSize = 80;
36 static SkScalar kInset = 10;
37 static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
38 static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
39
draw_label(SkCanvas * canvas,const char * label,const SkPoint & offset)40 static void draw_label(SkCanvas* canvas, const char* label,
41 const SkPoint& offset) {
42 SkFont font(ToolUtils::create_portable_typeface());
43 font.setEdging(SkFont::Edging::kAlias);
44
45 size_t len = strlen(label);
46
47 SkScalar width = font.measureText(label, len, SkTextEncoding::kUTF8);
48 canvas->drawSimpleText(label, len, SkTextEncoding::kUTF8, offset.x() - width / 2, offset.y(),
49 font, SkPaint());
50 }
51
draw_scene(SkCanvas * canvas,const sk_sp<SkColorFilter> & filter,SkBlendMode mode,const sk_sp<SkShader> & s1,const sk_sp<SkShader> & s2)52 static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode,
53 const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) {
54 SkPaint paint;
55 paint.setAntiAlias(true);
56 SkRect r, c, bounds = SkRect::MakeWH(kSize, kSize);
57
58 c = bounds;
59 c.fRight = bounds.centerX();
60 paint.setARGB(0x20, 0, 0, 0xff);
61 canvas->drawRect(bounds, paint);
62
63 canvas->saveLayer(&bounds, nullptr);
64
65 r = bounds;
66 r.inset(kInset, 0);
67 paint.setShader(s1);
68 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
69 canvas->drawOval(r, paint);
70 if (!s1) {
71 canvas->save();
72 canvas->clipRect(c);
73 paint.setColor(kColor1);
74 canvas->drawOval(r, paint);
75 canvas->restore();
76 }
77
78 SkPaint xferPaint;
79 xferPaint.setBlendMode(mode);
80 canvas->saveLayer(&bounds, &xferPaint);
81
82 r = bounds;
83 r.inset(0, kInset);
84 paint.setShader(s2);
85 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
86 paint.setColorFilter(filter);
87 canvas->drawOval(r, paint);
88 if (!s2) {
89 canvas->save();
90 canvas->clipRect(c);
91 paint.setColor(kColor2);
92 canvas->drawOval(r, paint);
93 canvas->restore();
94 }
95
96 canvas->restore();
97 canvas->restore();
98 }
99
100 class LumaFilterGM : public skiagm::GM {
101 protected:
onOnceBeforeDraw()102 void onOnceBeforeDraw() override {
103 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
104 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
105 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
106 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
107 SkScalar pos[] = { 0.2f, 1.0f };
108
109 fFilter = SkLumaColorFilter::Make();
110 fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
111 SkTileMode::kClamp);
112 fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
113 SkTileMode::kClamp);
114 }
115
onShortName()116 SkString onShortName() override {
117 return SkString("lumafilter");
118 }
119
onISize()120 SkISize onISize() override {
121 return SkISize::Make(600, 420);
122 }
123
onDraw(SkCanvas * canvas)124 void onDraw(SkCanvas* canvas) override {
125 SkBlendMode modes[] = {
126 SkBlendMode::kSrcOver,
127 SkBlendMode::kDstOver,
128 SkBlendMode::kSrcATop,
129 SkBlendMode::kDstATop,
130 SkBlendMode::kSrcIn,
131 SkBlendMode::kDstIn,
132 };
133 struct {
134 const sk_sp<SkShader>& fShader1;
135 const sk_sp<SkShader>& fShader2;
136 } shaders[] = {
137 { nullptr, nullptr },
138 { nullptr, fGr2 },
139 { fGr1, nullptr },
140 { fGr1, fGr2 },
141 };
142
143 SkScalar gridStep = kSize + 2 * kInset;
144 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) {
145 draw_label(canvas, SkBlendMode_Name(modes[i]),
146 SkPoint::Make(gridStep * (0.5f + i), 20));
147 }
148
149 for (size_t i = 0; i < SK_ARRAY_COUNT(shaders); ++i) {
150 canvas->save();
151 canvas->translate(kInset, gridStep * i + 30);
152 for (size_t m = 0; m < SK_ARRAY_COUNT(modes); ++m) {
153 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1,
154 shaders[i].fShader2);
155 canvas->translate(gridStep, 0);
156 }
157 canvas->restore();
158 }
159 }
160
161 private:
162 sk_sp<SkColorFilter> fFilter;
163 sk_sp<SkShader> fGr1, fGr2;
164
165 using INHERITED = skiagm::GM;
166 };
167
168 //////////////////////////////////////////////////////////////////////////////
169
170 DEF_GM(return new LumaFilterGM;)
171
172 DEF_SIMPLE_GM(AlternateLuma, canvas, 384,128) {
173 sk_sp<SkImage> img = GetResourceAsImage("images/mandrill_128.png");
174 if (!img) {
175 return;
176 }
177
178 // Normal luma colorfilter on the left.
179 SkPaint paint;
180 paint.setColorFilter(SkLumaColorFilter::Make());
181 canvas->drawImage(img, 0,0, SkSamplingOptions{}, &paint);
182 canvas->translate(128,0);
183
184 // Original image in the middle for reference.
185 canvas->drawImage(img, 0,0);
186 canvas->translate(128,0);
187
188 // Here, RGB holds CIE XYZ. Splatting the G (Y) channel should result in (near) greyscale.
189 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
190 "half4 main(half4 inColor) { return inColor.ggga; }"});
191 SkASSERT(effect && err.isEmpty());
192
193 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
194 SkASSERT(filter);
195
196 SkAlphaType unpremul = kUnpremul_SkAlphaType;
197 paint.setColorFilter(SkColorFilterPriv::WithWorkingFormat(std::move(filter),
198 &SkNamedTransferFn::kLinear,
199 &SkNamedGamut::kXYZ,
200 &unpremul));
201 canvas->drawImage(img, 0,0, SkSamplingOptions{}, &paint);
202 }
203