blob: 10c12aa280d047468be6ce4e412193bbcd74a0a0 [file] [log] [blame]
Mike Kleince4cf722018-05-10 11:29:15 -04001/*
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 "Test.h"
9#include "SkHalf.h"
10#include "SkSurface.h"
11#include "SkCanvas.h"
12
13DEF_TEST(NonlinearBlending, r) {
14
15 // First check our familiar basics with linear F16.
16 {
17 auto info = SkImageInfo::Make(1,1, kRGBA_F16_SkColorType, kPremul_SkAlphaType,
18 SkColorSpace::MakeSRGBLinear());
19
20 auto surface = SkSurface::MakeRaster(info);
21 surface->getCanvas()->clear(0xff808080);
22 uint64_t pix;
23 REPORTER_ASSERT(r, surface->readPixels(info, &pix, sizeof(pix),0,0));
24
25 // 0x80 in sRGB is ≈ 0.22 linear.
26 REPORTER_ASSERT(r, SkHalfToFloat(pix & 0xffff) < 0.25f);
27 }
28
29 // Test that we support sRGB-encoded F16. This is somewhat new.
30 {
31 auto info = SkImageInfo::Make(1,1, kRGBA_F16_SkColorType, kPremul_SkAlphaType,
32 SkColorSpace::MakeSRGB());
33
34 auto surface = SkSurface::MakeRaster(info);
35 surface->getCanvas()->clear(0xff808080);
36 uint64_t pix;
37 REPORTER_ASSERT(r, surface->readPixels(info, &pix, sizeof(pix),0,0));
38
39 // 0x80 sRGB is ≈ 0.501.
40 REPORTER_ASSERT(r, SkHalfToFloat(pix & 0xffff) >= 0.5f);
41 }
42
43 // Since we're only clear()ing, this should work the same as the last block.
44 {
45 auto info = SkImageInfo::Make(1,1, kRGBA_F16_SkColorType, kPremul_SkAlphaType,
46 SkColorSpace::MakeSRGB()->makeNonlinearBlending());
47
48 auto surface = SkSurface::MakeRaster(info);
49 surface->getCanvas()->clear(0xff808080);
50 uint64_t pix;
51 REPORTER_ASSERT(r, surface->readPixels(info, &pix, sizeof(pix),0,0));
52
53 // 0x80 sRGB is ≈ 0.501.
54 REPORTER_ASSERT(r, SkHalfToFloat(pix & 0xffff) >= 0.5f);
55 }
56
57 // This won't work until we actually support color spaces with non-linear blending.
58 if (0) {
59 auto info = SkImageInfo::Make(1,1, kRGBA_F16_SkColorType, kPremul_SkAlphaType,
60 SkColorSpace::MakeSRGB()->makeNonlinearBlending());
61
62 auto surface = SkSurface::MakeRaster(info);
63
64 surface->getCanvas()->clear(SK_ColorWHITE);
65 SkPaint p;
66 p.setColor(0x80000000);
67 surface->getCanvas()->drawPaint(p);
68
69 uint64_t pix;
70 REPORTER_ASSERT(r, surface->readPixels(info, &pix, sizeof(pix),0,0));
71
72 // 0x80 sRGB is ≈ 0.501. A likely failure here is ~0.75, linear blending.
73 REPORTER_ASSERT(r, SkHalfToFloat(pix & 0xffff) >= 0.45f &&
74 SkHalfToFloat(pix & 0xffff) <= 0.55f);
75 }
76}