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/SkFilterQuality.h"
14 #include "include/core/SkFont.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/SkRefCnt.h"
22 #include "include/core/SkScalar.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkDashPathEffect.h"
28 #include "include/effects/SkGradientShader.h"
29 #include "include/private/SkTPin.h"
30 #include "src/core/SkColorSpaceXformSteps.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;
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.isTranslate());
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 
__anon16ef4e4c0202(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->drawBitmap(bm, 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     // Draw a P3 red bitmap, using SkPixmap::erase().
157     {
158         SkBitmap bm;
159         bm.allocPixels(SkImageInfo::Make(60,60, kRGBA_F16_SkColorType, kPremul_SkAlphaType, p3));
160 
161         // At the moment only SkPixmap has an erase() that takes an SkColor4f.
162         SkPixmap pm;
163         SkAssertResult(bm.peekPixels(&pm));
164         SkAssertResult(pm.erase({1,0,0,1}, p3.get()));
165 
166         canvas->drawBitmap(bm, 10,10);
167         compare_pixel("drawBitmap P3 red, from SkPixmap::erase",
168                       canvas, 10,10,
169                       {1,0,0,1}, p3.get());
170     }
171 
172     canvas->translate(0,80);
173 
174     // Draw a P3 red bitmap wrapped in a shader, using SkPixmap::erase().
175     {
176         SkBitmap bm;
177         bm.allocPixels(SkImageInfo::Make(60,60, kRGBA_F16_SkColorType, kPremul_SkAlphaType, p3));
178 
179         // At the moment only SkPixmap has an erase() that takes an SkColor4f.
180         SkPixmap pm;
181         SkAssertResult(bm.peekPixels(&pm));
182         SkAssertResult(pm.erase({1,0,0,1}, p3.get()));
183 
184         SkPaint paint;
185         paint.setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat));
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, SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(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         SkBitmap bm;
342         bm.installPixels(SkImageInfo::MakeA8(16,16), mask, 16);
343 
344         SkPaint as_bitmap;
345         as_bitmap.setColor4f({1,0,0,1}, p3.get());
346         as_bitmap.setFilterQuality(kLow_SkFilterQuality);
347 
348         SkPaint as_shader;
349         as_shader.setColor4f({1,0,0,1}, p3.get());
350         as_shader.setFilterQuality(kLow_SkFilterQuality);
351         as_shader.setShader(bm.makeShader());
352 
353         canvas->drawBitmap(bm, 10,10, &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->drawBitmapRect(bm, {10,10,70,70}, &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