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