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, SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(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 + SK_ARRAY_COUNT(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 Arithmode2GM : public skiagm::GM {
176 float fK1, fK2, fK3, fK4;
177 sk_sp<SkImage> fSrc, fDst, fChecker;
178
onShortName()179 SkString onShortName() override { return SkString("arithmode_blender"); }
180
onISize()181 SkISize onISize() override { return {430, 430}; }
182
183 enum {
184 W = 200,
185 H = 200,
186 };
187
onOnceBeforeDraw()188 void onOnceBeforeDraw() override {
189 // need something interesting, in case we're drawn w/o calling animate()
190 fK1 = -0.25f;
191 fK2 = 0.25f;
192 fK3 = 0.25f;
193 fK4 = 0;
194
195 fSrc = make_src(W, H);
196 fDst = make_dst(W, H);
197
198 fChecker = ToolUtils::create_checkerboard_image(W, H, 0xFFBBBBBB, 0xFFEEEEEE, 8);
199 }
200
onAnimate(double nanos)201 bool onAnimate(double nanos) override {
202 double theta = nanos * 1e-6 * 0.001;
203 fK1 = sin(theta + 0) * 0.25;
204 fK2 = cos(theta + 1) * 0.25;
205 fK3 = sin(theta + 2) * 0.25;
206 fK4 = 0.5;
207 return true;
208 }
209
onDraw(SkCanvas * canvas)210 void onDraw(SkCanvas* canvas) override {
211 const SkRect rect = SkRect::MakeWH(W, H);
212
213 canvas->drawImage(fSrc, 10, 10);
214 canvas->drawImage(fDst, 10, 10 + fSrc->height() + 10);
215
216 auto sampling = SkSamplingOptions();
217 auto blender = SkBlenders::Arithmetic(fK1, fK2, fK3, fK4, true);
218
219 SkPaint paint;
220
221 canvas->translate(10 + fSrc->width() + 10, 10);
222 canvas->drawImage(fChecker, 0, 0);
223
224 // Draw via blend step
225 canvas->saveLayer(&rect, nullptr);
226 canvas->drawImage(fDst, 0, 0);
227 paint.setBlender(blender);
228 canvas->drawImage(fSrc, 0, 0, sampling, &paint);
229 canvas->restore();
230
231 canvas->translate(0, 10 + fSrc->height());
232 canvas->drawImage(fChecker, 0, 0);
233
234 // Draw via imagefilter (should appear the same as above)
235 paint.setBlender(nullptr);
236 paint.setImageFilter(SkImageFilters::Blend(blender,
237 /* dst imagefilter */nullptr,
238 SkImageFilters::Image(fSrc, sampling)));
239 canvas->drawImage(fDst, 0, 0, sampling, &paint);
240 }
241
242 private:
243 using INHERITED = GM;
244 };
245 DEF_GM( return new Arithmode2GM; )
246