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