Debian Codegraph @

gm/p3.cpp

raw
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/SkGradient.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
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
43 static SkString fmt(SkColor4f c) {
44 return SkStringPrintf("%.2g %.2g %.2g %.2g", c.fR, c.fG, c.fB, c.fA);
45 }
46
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
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
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(SkShaders::LinearGradient(points,
166 {{colors, {}, SkTileMode::kClamp, p3}, {}}));
167 canvas->drawRect({10,10,70,70}, paint);
168 canvas->save();
169 compare_pixel("UPM P3 gradient, P3 red",
170 canvas, 10,10,
171 {1,0,0,1}, p3.get());
172
173 canvas->translate(180, 0);
174
175 compare_pixel("UPM P3 gradient, P3 green",
176 canvas, 69,69,
177 {0,1,0,1}, p3.get());
178 canvas->restore();
179 }
180
181 canvas->translate(0,80);
182
183 // Draw a gradient from P3 red to P3 green interpolating in premul P3, checking the corners.
184 {
185
186 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
187 SkColor4f colors[] = {{1,0,0,1}, {0,1,0,1}};
188
189 SkPaint paint;
190 paint.setShader(
191 SkShaders::LinearGradient(points,
192 {{colors, {}, SkTileMode::kClamp, p3},
193 {SkGradient::Interpolation::InPremul::kYes}}));
194 canvas->drawRect({10,10,70,70}, paint);
195 canvas->save();
196 compare_pixel("PM P3 gradient, P3 red",
197 canvas, 10,10,
198 {1,0,0,1}, p3.get());
199
200 canvas->translate(180, 0);
201
202 compare_pixel("PM P3 gradient, P3 green",
203 canvas, 69,69,
204 {0,1,0,1}, p3.get());
205 canvas->restore();
206 }
207
208 canvas->translate(0,80);
209
210 // Draw a gradient from P3 red to P3 green interpolating in unpremul sRGB, checking the corners.
211 {
212
213 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
214 SkColor4f colors[] = {p3_to_srgb({1,0,0,1}), p3_to_srgb({0,1,0,1})};
215
216 SkPaint paint;
217 paint.setShader(SkShaders::LinearGradient(points,
218 {{colors, {}, SkTileMode::kClamp, srgb}, {}}));
219 canvas->drawRect({10,10,70,70}, paint);
220 canvas->save();
221 compare_pixel("UPM sRGB gradient, P3 red",
222 canvas, 10,10,
223 {1,0,0,1}, p3.get());
224
225 canvas->translate(180, 0);
226
227 compare_pixel("UPM sRGB gradient, P3 green",
228 canvas, 69,69,
229 {0,1,0,1}, p3.get());
230 canvas->restore();
231 }
232
233 canvas->translate(0,80);
234
235 // Draw a gradient from P3 red to P3 green interpolating in premul sRGB, checking the corners.
236 {
237
238 SkPoint points[] = {{10.5,10.5}, {69.5,69.5}};
239 SkColor4f colors[] = {p3_to_srgb({1,0,0,1}), p3_to_srgb({0,1,0,1})};
240
241 SkPaint paint;
242 paint.setShader(SkShaders::LinearGradient(points,
243 {{colors, {}, SkTileMode::kClamp, srgb},
244 {SkGradient::Interpolation::InPremul::kYes}}));
245 canvas->drawRect({10,10,70,70}, paint);
246 canvas->save();
247 compare_pixel("PM sRGB gradient, P3 red",
248 canvas, 10,10,
249 {1,0,0,1}, p3.get());
250
251 canvas->translate(180, 0);
252
253 compare_pixel("PM sRGB gradient, P3 green",
254 canvas, 69,69,
255 {0,1,0,1}, p3.get());
256 canvas->restore();
257 }
258
259 canvas->translate(0,80);
260
261 // Leon's blue -> green -> red gradient, interpolating in premul.
262 {
263 SkPoint points[] = {{10.5,10.5}, {10.5,69.5}};
264 SkColor4f colors[] = { {0,0,1,1}, {0,1,0,1}, {1,0,0,1} };
265
266 SkPaint paint;
267 paint.setShader(SkShaders::LinearGradient(points,
268 {{colors, {}, SkTileMode::kClamp, p3},
269 {SkGradient::Interpolation::InPremul::kYes}}));
270 canvas->drawRect({10,10,70,70}, paint);
271 canvas->save();
272 compare_pixel("Leon's gradient, P3 blue",
273 canvas, 10,10,
274 {0,0,1,1}, p3.get());
275
276 canvas->translate(180, 0);
277
278 compare_pixel("Leon's gradient, P3 red",
279 canvas, 10,69,
280 {1,0,0,1}, p3.get());
281 canvas->restore();
282 }
283
284 canvas->translate(0,80);
285
286 // Draw an A8 image with a P3 red, scaled and not, as a shader or bitmap.
287 {
288 uint8_t mask[256];
289 for (int i = 0; i < 256; i++) {
290 mask[i] = 255-i;
291 }
292
293 SkBitmap bm;
294 bm.installPixels(SkImageInfo::MakeA8(16,16), mask, 16);
295
296 SkPaint as_bitmap;
297 as_bitmap.setColor4f({1,0,0,1}, p3.get());
298 SkSamplingOptions sampling(SkFilterMode::kLinear);
299
300 SkPaint as_shader;
301 as_shader.setColor4f({1,0,0,1}, p3.get());
302 as_shader.setShader(bm.makeShader(sampling));
303
304 canvas->drawImage(bm.asImage(), 10,10, sampling, &as_bitmap);
305 compare_pixel("A8 sprite bitmap P3 red",
306 canvas, 10,10,
307 {1,0,0,1}, p3.get());
308
309 canvas->translate(0, 80);
310
311 canvas->save();
312 canvas->translate(10,10);
313 canvas->drawRect({0,0,16,16}, as_shader);
314 canvas->restore();
315 compare_pixel("A8 sprite shader P3 red",
316 canvas, 10,10,
317 {1,0,0,1}, p3.get());
318
319 canvas->translate(0,80);
320
321 canvas->drawImageRect(bm.asImage(), {10,10,70,70}, sampling, &as_bitmap);
322 compare_pixel("A8 scaled bitmap P3 red",
323 canvas, 10,10,
324 {1,0,0,1}, p3.get());
325
326 canvas->translate(0,80);
327
328 canvas->save();
329 canvas->translate(10,10);
330 canvas->scale(3.75,3.75);
331 canvas->drawRect({0,0,16,16}, as_shader);
332 canvas->restore();
333 compare_pixel("A8 scaled shader P3 red",
334 canvas, 10,10,
335 {1,0,0,1}, p3.get());
336 }
337
338 // TODO: draw P3 colors more ways
339 }
340
341 DEF_SIMPLE_GM(p3_ovals, canvas, 450, 320) {
342 auto p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
343
344 // Test cases that exercise each Op in GrOvalOpFactory.cpp
345
346 // Draw a circle and check the center (CircleOp)
347 {
348 SkPaint paint;
349 paint.setAntiAlias(true);
350 paint.setColor4f({ 1,0,0,1 }, p3.get());
351
352 canvas->drawCircle(40, 40, 30, paint);
353 compare_pixel("drawCircle P3 red ",
354 canvas, 40, 40,
355 { 1,0,0,1 }, p3.get());
356 }
357
358 canvas->translate(0, 80);
359
360 // Draw an oval and check the center (EllipseOp)
361 {
362 SkPaint paint;
363 paint.setAntiAlias(true);
364 paint.setColor4f({ 1,0,0,1 }, p3.get());
365
366 canvas->drawOval({ 20,10,60,70 }, paint);
367 compare_pixel("drawOval P3 red ",
368 canvas, 40, 40,
369 { 1,0,0,1 }, p3.get());
370 }
371
372 canvas->translate(0, 80);
373
374 // Draw a butt-capped dashed circle and check the top of the stroke (ButtCappedDashedCircleOp)
375 {
376 SkPaint paint;
377 paint.setAntiAlias(true);
378 paint.setColor4f({ 1,0,0,1 }, p3.get());
379 paint.setStyle(SkPaint::kStroke_Style);
380 float intervals[] = { 70, 10 };
381 paint.setPathEffect(SkDashPathEffect::Make(intervals, 0));
382 paint.setStrokeWidth(10);
383
384 canvas->drawCircle(40, 40, 30, paint);
385 compare_pixel("drawDashedCircle P3 red ",
386 canvas, 40, 10,
387 { 1,0,0,1 }, p3.get());
388 }
389
390 canvas->translate(0, 80);
391
392 // Draw an oval with rotation and check the center (DIEllipseOp)
393 {
394 SkPaint paint;
395 paint.setAntiAlias(true);
396 paint.setColor4f({ 1,0,0,1 }, p3.get());
397
398 canvas->save();
399 canvas->translate(40, 40);
400 canvas->rotate(45);
401 canvas->drawOval({ -20,-30,20,30 }, paint);
402 canvas->restore();
403 compare_pixel("drawRotatedOval P3 red ",
404 canvas, 40, 40,
405 { 1,0,0,1 }, p3.get());
406 }
407
408 canvas->translate(0, 80);
409 }