1 /*
2 * Copyright 2018 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/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPathEffect.h"
19 #include "include/core/SkPixmap.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkDashPathEffect.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "include/private/base/SkTPin.h"
28 #include "src/core/SkColorSpaceXformSteps.h"
29 #include "src/core/SkImageInfoPriv.h"
30 #include "tools/fonts/FontToolUtils.h"
31
32 #include <math.h>
33 #include <string.h>
34
nearly_equal(SkColor4f x,SkColor4f y)35 static bool nearly_equal(SkColor4f x, SkColor4f y) {
36 const float K = 0.01f;
37 return fabsf(x.fR - y.fR) < K
38 && fabsf(x.fG - y.fG) < K
39 && fabsf(x.fB - y.fB) < K
40 && fabsf(x.fA - y.fA) < K;
41 }
42
fmt(SkColor4f c)43 static SkString fmt(SkColor4f c) {
44 return SkStringPrintf("%.2g %.2g %.2g %.2g", c.fR, c.fG, c.fB, c.fA);
45 }
46
transform(SkColor4f c,SkColorSpace * src,SkColorSpace * dst)47 static SkColor4f transform(SkColor4f c, SkColorSpace* src, SkColorSpace* dst) {
48 SkColorSpaceXformSteps(src, kUnpremul_SkAlphaType,
49 dst, kUnpremul_SkAlphaType).apply(c.vec());
50 return c;
51 }
52
compare_pixel(const char * label,SkCanvas * canvas,int x,int y,SkColor4f color,SkColorSpace * cs)53 static void compare_pixel(const char* label,
54 SkCanvas* canvas, int x, int y,
55 SkColor4f color, SkColorSpace* cs) {
56 SkPaint paint;
57 SkFont font = ToolUtils::DefaultPortableFont();
58 auto canvas_cs = canvas->imageInfo().refColorSpace();
59
60 // I'm not really sure if this makes things easier or harder to follow,
61 // but we sniff the canvas to grab its current y-translate, so that (x,y)
62 // can be written in sort of chunk-relative terms.
63 const SkMatrix& m = canvas->getTotalMatrix();
64 SkASSERT(m.isScaleTranslate());
65 SkScalar dy = m.getTranslateY();
66 SkASSERT(dy == (int)dy);
67 y += (int)dy;
68
69 SkBitmap bm;
70 bm.allocPixels(SkImageInfo::Make(1,1, kRGBA_F32_SkColorType, kUnpremul_SkAlphaType, canvas_cs));
71 if (!canvas->readPixels(bm, x,y)) {
72 MarkGMGood(canvas, 140,40);
73 canvas->drawString("can't readPixels() on this canvas :(", 100,20, font, paint);
74 return;
75 }
76
77 SkColor4f pixel;
78 memcpy(&pixel, bm.getAddr(0,0), sizeof(pixel));
79
80 SkColor4f expected = transform(color,cs, canvas_cs.get());
81 if (SkColorTypeIsNormalized(canvas->imageInfo().colorType())) {
82 // We can't expect normalized formats to hold values outside [0,1].
83 for (int i = 0; i < 4; ++i) {
84 expected[i] = SkTPin(expected[i], 0.0f, 1.0f);
85 }
86 }
87 if (canvas->imageInfo().colorType() == kGray_8_SkColorType) {
88 // Drawing into Gray8 is known to be maybe-totally broken.
89 // TODO: update expectation here to be {lum,lum,lum,1} if we fix Gray8.
90 expected = SkColor4f{NAN, NAN, NAN, 1};
91 }
92
93 if (nearly_equal(pixel, expected)) {
94 MarkGMGood(canvas, 140,40);
95 } else {
96 MarkGMBad(canvas, 140,40);
97 }
98
99 struct {
100 const char* label;
101 SkColor4f color;
102 } lines[] = {
103 {"Pixel:" , pixel },
104 {"Expected:", expected},
105 };
106
107 SkAutoCanvasRestore saveRestore(canvas, true);
108 canvas->drawString(label, 80,20, font, paint);
109 for (auto l : lines) {
110 canvas->translate(0,20);
111 canvas->drawString(l.label, 80,20, font, paint);
112 canvas->drawString(fmt(l.color).c_str(), 140,20, font, paint);
113 }
114 }
115
116 DEF_SIMPLE_GM(p3, canvas, 450, 1300) {
117 auto p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
118 auto srgb = SkColorSpace::MakeSRGB();
119
__anonc7db44f50202(SkColor4f c) 120 auto p3_to_srgb = [&](SkColor4f c) {
121 SkPaint p;
122 p.setColor4f(c, p3.get());
123 return p.getColor4f();
124 };
125
126 // Draw a P3 red rectangle and check the corner.
127 {
128 SkPaint paint;
129 paint.setColor4f({1,0,0,1}, p3.get());
130
131 canvas->drawRect({10,10,70,70}, paint);
132 compare_pixel("drawRect P3 red ",
133 canvas, 10,10,
134 {1,0,0,1}, p3.get());
135 }
136
137 canvas->translate(0,80);
138
139 // Draw a P3 red bitmap, using a draw.
140 {
141 SkBitmap bm;
142 bm.allocPixels(SkImageInfo::Make(60,60, kRGBA_F16_SkColorType, kPremul_SkAlphaType, p3));
143
144 SkPaint paint;
145 paint.setColor4f({1,0,0,1}, p3.get());
146 SkCanvas{bm}.drawPaint(paint);
147
148 canvas->drawImage(bm.asImage(), 10,10);
149 compare_pixel("drawBitmap P3 red, from drawPaint",
150 canvas, 10,10,
151 {1,0,0,1}, p3.get());
152 }
153
154 canvas->translate(0,80);
155
156 // TODO(mtklein): sample and check the middle points of these gradients too.
157
158 // Draw a gradient from P3 red to P3 green interpolating in unpremul P3, checking the corners.
159 {
160
161 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
162 SkColor4f colors[] = {{1,0,0,1}, {0,1,0,1}};
163
164 SkPaint paint;
165 paint.setShader(SkGradientShader::MakeLinear(points, colors, p3,
166 nullptr, std::size(colors),
167 SkTileMode::kClamp));
168 canvas->drawRect({10,10,70,70}, paint);
169 canvas->save();
170 compare_pixel("UPM P3 gradient, P3 red",
171 canvas, 10,10,
172 {1,0,0,1}, p3.get());
173
174 canvas->translate(180, 0);
175
176 compare_pixel("UPM P3 gradient, P3 green",
177 canvas, 69,69,
178 {0,1,0,1}, p3.get());
179 canvas->restore();
180 }
181
182 canvas->translate(0,80);
183
184 // Draw a gradient from P3 red to P3 green interpolating in premul P3, checking the corners.
185 {
186
187 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
188 SkColor4f colors[] = {{1,0,0,1}, {0,1,0,1}};
189
190 SkPaint paint;
191 paint.setShader(
192 SkGradientShader::MakeLinear(points, colors, p3,
193 nullptr, std::size(colors),
194 SkTileMode::kClamp,
195 SkGradientShader::kInterpolateColorsInPremul_Flag,
196 nullptr/*local matrix*/));
197 canvas->drawRect({10,10,70,70}, paint);
198 canvas->save();
199 compare_pixel("PM P3 gradient, P3 red",
200 canvas, 10,10,
201 {1,0,0,1}, p3.get());
202
203 canvas->translate(180, 0);
204
205 compare_pixel("PM P3 gradient, P3 green",
206 canvas, 69,69,
207 {0,1,0,1}, p3.get());
208 canvas->restore();
209 }
210
211 canvas->translate(0,80);
212
213 // Draw a gradient from P3 red to P3 green interpolating in unpremul sRGB, checking the corners.
214 {
215
216 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
217 SkColor4f colors[] = {p3_to_srgb({1,0,0,1}), p3_to_srgb({0,1,0,1})};
218
219 SkPaint paint;
220 paint.setShader(SkGradientShader::MakeLinear(points, colors, srgb,
221 nullptr, std::size(colors),
222 SkTileMode::kClamp));
223 canvas->drawRect({10,10,70,70}, paint);
224 canvas->save();
225 compare_pixel("UPM sRGB gradient, P3 red",
226 canvas, 10,10,
227 {1,0,0,1}, p3.get());
228
229 canvas->translate(180, 0);
230
231 compare_pixel("UPM sRGB gradient, P3 green",
232 canvas, 69,69,
233 {0,1,0,1}, p3.get());
234 canvas->restore();
235 }
236
237 canvas->translate(0,80);
238
239 // Draw a gradient from P3 red to P3 green interpolating in premul sRGB, checking the corners.
240 {
241
242 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
243 SkColor4f colors[] = {p3_to_srgb({1,0,0,1}), p3_to_srgb({0,1,0,1})};
244
245 SkPaint paint;
246 paint.setShader(
247 SkGradientShader::MakeLinear(points, colors, srgb,
248 nullptr, std::size(colors),
249 SkTileMode::kClamp,
250 SkGradientShader::kInterpolateColorsInPremul_Flag,
251 nullptr/*local matrix*/));
252 canvas->drawRect({10,10,70,70}, paint);
253 canvas->save();
254 compare_pixel("PM sRGB gradient, P3 red",
255 canvas, 10,10,
256 {1,0,0,1}, p3.get());
257
258 canvas->translate(180, 0);
259
260 compare_pixel("PM sRGB gradient, P3 green",
261 canvas, 69,69,
262 {0,1,0,1}, p3.get());
263 canvas->restore();
264 }
265
266 canvas->translate(0,80);
267
268 // Leon's blue -> green -> red gradient, interpolating in premul.
269 {
270 SkPoint points[] = {{10.5,10.5}, {10.5,69.5}};
271 SkColor4f colors[] = { {0,0,1,1}, {0,1,0,1}, {1,0,0,1} };
272
273 SkPaint paint;
274 paint.setShader(
275 SkGradientShader::MakeLinear(points, colors, p3,
276 nullptr, std::size(colors),
277 SkTileMode::kClamp,
278 SkGradientShader::kInterpolateColorsInPremul_Flag,
279 nullptr/*local matrix*/));
280 canvas->drawRect({10,10,70,70}, paint);
281 canvas->save();
282 compare_pixel("Leon's gradient, P3 blue",
283 canvas, 10,10,
284 {0,0,1,1}, p3.get());
285
286 canvas->translate(180, 0);
287
288 compare_pixel("Leon's gradient, P3 red",
289 canvas, 10,69,
290 {1,0,0,1}, p3.get());
291 canvas->restore();
292 }
293
294 canvas->translate(0,80);
295
296 // Draw an A8 image with a P3 red, scaled and not, as a shader or bitmap.
297 {
298 uint8_t mask[256];
299 for (int i = 0; i < 256; i++) {
300 mask[i] = 255-i;
301 }
302
303 SkBitmap bm;
304 bm.installPixels(SkImageInfo::MakeA8(16,16), mask, 16);
305
306 SkPaint as_bitmap;
307 as_bitmap.setColor4f({1,0,0,1}, p3.get());
308 SkSamplingOptions sampling(SkFilterMode::kLinear);
309
310 SkPaint as_shader;
311 as_shader.setColor4f({1,0,0,1}, p3.get());
312 as_shader.setShader(bm.makeShader(sampling));
313
314 canvas->drawImage(bm.asImage(), 10,10, sampling, &as_bitmap);
315 compare_pixel("A8 sprite bitmap P3 red",
316 canvas, 10,10,
317 {1,0,0,1}, p3.get());
318
319 canvas->translate(0, 80);
320
321 canvas->save();
322 canvas->translate(10,10);
323 canvas->drawRect({0,0,16,16}, as_shader);
324 canvas->restore();
325 compare_pixel("A8 sprite shader P3 red",
326 canvas, 10,10,
327 {1,0,0,1}, p3.get());
328
329 canvas->translate(0,80);
330
331 canvas->drawImageRect(bm.asImage(), {10,10,70,70}, sampling, &as_bitmap);
332 compare_pixel("A8 scaled bitmap P3 red",
333 canvas, 10,10,
334 {1,0,0,1}, p3.get());
335
336 canvas->translate(0,80);
337
338 canvas->save();
339 canvas->translate(10,10);
340 canvas->scale(3.75,3.75);
341 canvas->drawRect({0,0,16,16}, as_shader);
342 canvas->restore();
343 compare_pixel("A8 scaled shader P3 red",
344 canvas, 10,10,
345 {1,0,0,1}, p3.get());
346 }
347
348 // TODO: draw P3 colors more ways
349 }
350
351 DEF_SIMPLE_GM(p3_ovals, canvas, 450, 320) {
352 auto p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
353
354 // Test cases that exercise each Op in GrOvalOpFactory.cpp
355
356 // Draw a circle and check the center (CircleOp)
357 {
358 SkPaint paint;
359 paint.setAntiAlias(true);
360 paint.setColor4f({ 1,0,0,1 }, p3.get());
361
362 canvas->drawCircle(40, 40, 30, paint);
363 compare_pixel("drawCircle P3 red ",
364 canvas, 40, 40,
365 { 1,0,0,1 }, p3.get());
366 }
367
368 canvas->translate(0, 80);
369
370 // Draw an oval and check the center (EllipseOp)
371 {
372 SkPaint paint;
373 paint.setAntiAlias(true);
374 paint.setColor4f({ 1,0,0,1 }, p3.get());
375
376 canvas->drawOval({ 20,10,60,70 }, paint);
377 compare_pixel("drawOval P3 red ",
378 canvas, 40, 40,
379 { 1,0,0,1 }, p3.get());
380 }
381
382 canvas->translate(0, 80);
383
384 // Draw a butt-capped dashed circle and check the top of the stroke (ButtCappedDashedCircleOp)
385 {
386 SkPaint paint;
387 paint.setAntiAlias(true);
388 paint.setColor4f({ 1,0,0,1 }, p3.get());
389 paint.setStyle(SkPaint::kStroke_Style);
390 float intervals[] = { 70, 10 };
391 paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 0));
392 paint.setStrokeWidth(10);
393
394 canvas->drawCircle(40, 40, 30, paint);
395 compare_pixel("drawDashedCircle P3 red ",
396 canvas, 40, 10,
397 { 1,0,0,1 }, p3.get());
398 }
399
400 canvas->translate(0, 80);
401
402 // Draw an oval with rotation and check the center (DIEllipseOp)
403 {
404 SkPaint paint;
405 paint.setAntiAlias(true);
406 paint.setColor4f({ 1,0,0,1 }, p3.get());
407
408 canvas->save();
409 canvas->translate(40, 40);
410 canvas->rotate(45);
411 canvas->drawOval({ -20,-30,20,30 }, paint);
412 canvas->restore();
413 compare_pixel("drawRotatedOval P3 red ",
414 canvas, 40, 40,
415 { 1,0,0,1 }, p3.get());
416 }
417
418 canvas->translate(0, 80);
419 }
420