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/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontTypes.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.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/SkSurface.h"
24 #include "include/core/SkTileMode.h"
25 #include "include/core/SkTypeface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkGradientShader.h"
28 #include "include/effects/SkImageFilters.h"
29 #include "tools/ToolUtils.h"
30
31 #include <utility>
32
make_src(int w,int h)33 static sk_sp<SkImage> make_src(int w, int h) {
34 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h));
35 SkCanvas* canvas = surface->getCanvas();
36
37 SkPaint paint;
38 SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
39 SkColor colors[] = {
40 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
41 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
42 };
43 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
44 SkTileMode::kClamp));
45 canvas->drawPaint(paint);
46 return surface->makeImageSnapshot();
47 }
48
make_dst(int w,int h)49 static sk_sp<SkImage> make_dst(int w, int h) {
50 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(w, h));
51 SkCanvas* canvas = surface->getCanvas();
52
53 SkPaint paint;
54 SkPoint pts[] = { {0, SkIntToScalar(h)}, {SkIntToScalar(w), 0} };
55 SkColor colors[] = {
56 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN,
57 SK_ColorGRAY,
58 };
59 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
60 SkTileMode::kClamp));
61 canvas->drawPaint(paint);
62 return surface->makeImageSnapshot();
63 }
64
show_k_text(SkCanvas * canvas,SkScalar x,SkScalar y,const SkScalar k[])65 static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
66 SkFont font(ToolUtils::create_portable_typeface(), 24);
67 font.setEdging(SkFont::Edging::kAntiAlias);
68 SkPaint paint;
69 paint.setAntiAlias(true);
70 for (int i = 0; i < 4; ++i) {
71 SkString str;
72 str.appendScalar(k[i]);
73 SkScalar width = font.measureText(str.c_str(), str.size(), SkTextEncoding::kUTF8);
74 canvas->drawString(str, x, y + font.getSize(), font, paint);
75 x += width + SkIntToScalar(10);
76 }
77 }
78
79 class ArithmodeGM : public skiagm::GM {
onShortName()80 SkString onShortName() override { return SkString("arithmode"); }
81
onISize()82 SkISize onISize() override { return {640, 572}; }
83
onDraw(SkCanvas * canvas)84 void onDraw(SkCanvas* canvas) override {
85 constexpr int WW = 100,
86 HH = 32;
87
88 sk_sp<SkImage> src = make_src(WW, HH);
89 sk_sp<SkImage> dst = make_dst(WW, HH);
90 sk_sp<SkImageFilter> srcFilter = SkImageFilters::Image(src);
91 sk_sp<SkImageFilter> dstFilter = SkImageFilters::Image(dst);
92
93 constexpr SkScalar one = SK_Scalar1;
94 constexpr SkScalar K[] = {
95 0, 0, 0, 0,
96 0, 0, 0, one,
97 0, one, 0, 0,
98 0, 0, one, 0,
99 0, one, one, 0,
100 0, one, -one, 0,
101 0, one/2, one/2, 0,
102 0, one/2, one/2, one/4,
103 0, one/2, one/2, -one/4,
104 one/4, one/2, one/2, 0,
105 -one/4, one/2, one/2, 0,
106 };
107
108 const SkScalar* k = K;
109 const SkScalar* stop = k + std::size(K);
110 const SkRect rect = SkRect::MakeWH(WW, HH);
111 SkScalar gap = SkIntToScalar(WW + 20);
112 while (k < stop) {
113 {
114 SkAutoCanvasRestore acr(canvas, true);
115 canvas->drawImage(src, 0, 0);
116 canvas->translate(gap, 0);
117 canvas->drawImage(dst, 0, 0);
118 canvas->translate(gap, 0);
119 SkPaint paint;
120 paint.setImageFilter(SkImageFilters::Arithmetic(k[0], k[1], k[2], k[3], true,
121 dstFilter, srcFilter, nullptr));
122 canvas->saveLayer(&rect, &paint);
123 canvas->restore();
124
125 canvas->translate(gap, 0);
126 show_k_text(canvas, 0, 0, k);
127 }
128
129 k += 4;
130 canvas->translate(0, HH + 12);
131 }
132
133 // Draw two special cases to test enforcePMColor. In these cases, we
134 // draw the dst bitmap twice, the first time it is halved and inverted,
135 // leading to invalid premultiplied colors. If we enforcePMColor, these
136 // invalid values should be clamped, and will not contribute to the
137 // second draw.
138 for (int i = 0; i < 2; i++) {
139 const bool enforcePMColor = (i == 0);
140
141 {
142 SkAutoCanvasRestore acr(canvas, true);
143 canvas->translate(gap, 0);
144 canvas->drawImage(dst, 0, 0);
145 canvas->translate(gap, 0);
146
147 sk_sp<SkImageFilter> bg =
148 SkImageFilters::Arithmetic(0, 0, -one / 2, 1, enforcePMColor, dstFilter,
149 nullptr, nullptr);
150 SkPaint p;
151 p.setImageFilter(SkImageFilters::Arithmetic(0, one / 2, -one, 1, true,
152 std::move(bg), dstFilter, nullptr));
153 canvas->saveLayer(&rect, &p);
154 canvas->restore();
155 canvas->translate(gap, 0);
156
157 // Label
158 SkFont font(ToolUtils::create_portable_typeface(), 24);
159 SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
160 canvas->drawString(str, 0, font.getSize(), font, SkPaint());
161 }
162 canvas->translate(0, HH + 12);
163 }
164 }
165
166 private:
167 using INHERITED = GM;
168 };
169 DEF_GM( return new ArithmodeGM; )
170
171 ///////////////////////////////////////////////////////////////////////////////
172
173 #include "include/effects/SkBlenders.h"
174
175 class ArithmodeBlenderGM : public skiagm::GM {
176 float fK1, fK2, fK3, fK4;
177 sk_sp<SkImage> fSrc, fDst, fChecker;
178 sk_sp<SkShader> fSrcShader, fDstShader;
179 sk_sp<SkRuntimeEffect> fRuntimeEffect;
180
onShortName()181 SkString onShortName() override { return SkString("arithmode_blender"); }
182
183 static constexpr int W = 200;
184 static constexpr int H = 200;
185
onISize()186 SkISize onISize() override { return {(W + 30) * 2, (H + 30) * 4}; }
187
onOnceBeforeDraw()188 void onOnceBeforeDraw() override {
189 // Prepare a runtime effect for this blend.
190 static constexpr char kShader[] = R"(
191 uniform shader srcImage;
192 uniform shader dstImage;
193 uniform blender arithBlend;
194 half4 main(float2 xy) {
195 return arithBlend.eval(srcImage.eval(xy), dstImage.eval(xy));
196 }
197 )";
198 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kShader));
199 SkASSERT(effect);
200 fRuntimeEffect = effect;
201
202 // Start with interesting K-values, in case we're drawn without calling onAnimate().
203 fK1 = -0.25f;
204 fK2 = 0.25f;
205 fK3 = 0.25f;
206 fK4 = 0;
207
208 fSrc = make_src(W, H);
209 fDst = make_dst(W, H);
210 fSrcShader = fSrc->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, SkSamplingOptions());
211 fDstShader = fDst->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, SkSamplingOptions());
212
213 fChecker = ToolUtils::create_checkerboard_image(W, H, 0xFFBBBBBB, 0xFFEEEEEE, 8);
214 }
215
onAnimate(double nanos)216 bool onAnimate(double nanos) override {
217 double theta = nanos * 1e-6 * 0.001;
218 fK1 = sin(theta + 0) * 0.25;
219 fK2 = cos(theta + 1) * 0.25;
220 fK3 = sin(theta + 2) * 0.25;
221 fK4 = 0.5;
222 return true;
223 }
224
onDraw(SkCanvas * canvas)225 void onDraw(SkCanvas* canvas) override {
226 const SkRect rect = SkRect::MakeWH(W, H);
227
228 canvas->drawImage(fSrc, 10, 10);
229 canvas->drawImage(fDst, 10, 10 + H + 10);
230
231 SkSamplingOptions sampling;
232 sk_sp<SkBlender> blender = SkBlenders::Arithmetic(fK1, fK2, fK3, fK4,
233 /*enforcePremul=*/true);
234 canvas->translate(10 + W + 10, 10);
235
236 // All three images drawn below should appear identical.
237 // Draw via blend step
238 SkPaint blenderPaint;
239 canvas->drawImage(fChecker, 0, 0);
240 canvas->saveLayer(&rect, nullptr);
241 canvas->drawImage(fDst, 0, 0);
242 blenderPaint.setBlender(blender);
243 canvas->drawImage(fSrc, 0, 0, sampling, &blenderPaint);
244 canvas->restore();
245
246 canvas->translate(0, 10 + H);
247
248 // Draw via SkImageFilters::Blend (should appear the same as above)
249 SkPaint imageFilterPaint;
250 canvas->drawImage(fChecker, 0, 0);
251 imageFilterPaint.setImageFilter(
252 SkImageFilters::Blend(blender,
253 /*background=*/nullptr,
254 /*foreground=*/SkImageFilters::Image(fSrc, sampling)));
255 canvas->drawImage(fDst, 0, 0, sampling, &imageFilterPaint);
256
257 canvas->translate(0, 10 + H);
258
259 // Draw via SkShaders::Blend (should still appear the same as above)
260 SkPaint shaderBlendPaint;
261 canvas->drawImage(fChecker, 0, 0);
262 shaderBlendPaint.setShader(SkShaders::Blend(blender, fDstShader, fSrcShader));
263 canvas->drawRect(rect, shaderBlendPaint);
264
265 canvas->translate(0, 10 + H);
266
267 // Draw via runtime effect (should still appear the same as above)
268 SkPaint runtimePaint;
269 canvas->drawImage(fChecker, 0, 0);
270 SkRuntimeEffect::ChildPtr children[] = {fSrcShader, fDstShader, blender};
271 runtimePaint.setShader(fRuntimeEffect->makeShader(/*uniforms=*/{}, children));
272 canvas->drawRect(rect, runtimePaint);
273 }
274
275 private:
276 using INHERITED = GM;
277 };
278 DEF_GM( return new ArithmodeBlenderGM; )
279