blob: 6a7f9842f4e7e3ea630ab2c992c52013ef488e43 [file] [log] [blame]
reed3601f282016-02-05 11:18:39 -08001/*
2 * Copyright 2016 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"
robertphillipsc5035e72016-03-17 06:58:39 -07009#include "SkAutoPixmapStorage.h"
reed3601f282016-02-05 11:18:39 -080010#include "SkColor.h"
11#include "SkHalf.h"
mtkleina525cb12016-02-09 08:18:10 -080012#include "SkOpts.h"
reed3601f282016-02-05 11:18:39 -080013#include "SkPixmap.h"
reeddd9ffea2016-02-18 12:39:14 -080014#include "SkPM4f.h"
mtkleinfff055c2016-02-11 06:30:03 -080015#include "SkRandom.h"
reed3601f282016-02-05 11:18:39 -080016
17static bool eq_within_half_float(float a, float b) {
18 const float kTolerance = 1.0f / (1 << (8 + 10));
19
20 SkHalf ha = SkFloatToHalf(a);
21 SkHalf hb = SkFloatToHalf(b);
22 float a2 = SkHalfToFloat(ha);
23 float b2 = SkHalfToFloat(hb);
24 return fabsf(a2 - b2) <= kTolerance;
25}
26
27static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
28 for (int i = 0; i < 4; ++i) {
29 if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
30 return false;
31 }
32 }
33 return true;
34}
35
36DEF_TEST(color_half_float, reporter) {
37 const int w = 100;
38 const int h = 100;
39
40 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
41
42 SkAutoPixmapStorage pm;
43 pm.alloc(info);
44 REPORTER_ASSERT(reporter, pm.getSafeSize() == SkToSizeT(w * h * sizeof(uint64_t)));
45
46 SkColor4f c4 { 0.5f, 1, 0.5f, 0.25f };
47 pm.erase(c4);
48
49 SkPM4f origpm4 = c4.premul();
50 for (int y = 0; y < pm.height(); ++y) {
51 for (int x = 0; x < pm.width(); ++x) {
52 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
53 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
54 }
55 }
56}
mtkleina525cb12016-02-09 08:18:10 -080057
58DEF_TEST(float_to_half, reporter) {
59 const float fs[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
60 const uint16_t hs[] = { 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500, 0x4600, 0x4700 };
61
62 uint16_t hscratch[7];
63 SkOpts::float_to_half(hscratch, fs, 7);
64 REPORTER_ASSERT(reporter, 0 == memcmp(hscratch, hs, sizeof(hs)));
65
66 float fscratch[7];
67 SkOpts::half_to_float(fscratch, hs, 7);
68 REPORTER_ASSERT(reporter, 0 == memcmp(fscratch, fs, sizeof(fs)));
69}
mtkleinfff055c2016-02-11 06:30:03 -080070
mtkleinddb64c82016-02-11 12:48:23 -080071static uint32_t u(float f) {
72 uint32_t x;
73 memcpy(&x, &f, 4);
74 return x;
75}
76
mtkleinfff055c2016-02-11 06:30:03 -080077DEF_TEST(HalfToFloat_01, r) {
78 for (uint16_t h = 0; h < 0x8000; h++) {
79 float f = SkHalfToFloat(h);
80 if (f >= 0 && f <= 1) {
mtkleinddb64c82016-02-11 12:48:23 -080081 float got = SkHalfToFloat_01(h)[0];
82 if (got != f) {
83 SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n",
84 h,
85 u(got), got,
86 u(f), f);
87 }
mtkleinfff055c2016-02-11 06:30:03 -080088 REPORTER_ASSERT(r, SkHalfToFloat_01(h)[0] == f);
89 REPORTER_ASSERT(r, SkFloatToHalf_01(SkHalfToFloat_01(h)) == h);
90 }
91 }
92}
93
94DEF_TEST(FloatToHalf_01, r) {
95#if 0
96 for (uint32_t bits = 0; bits < 0x80000000; bits++) {
97#else
98 SkRandom rand;
99 for (int i = 0; i < 1000000; i++) {
100 uint32_t bits = rand.nextU();
101#endif
102 float f;
103 memcpy(&f, &bits, 4);
104 if (f >= 0 && f <= 1) {
105 uint16_t h1 = (uint16_t)SkFloatToHalf_01(Sk4f(f,0,0,0)),
106 h2 = SkFloatToHalf(f);
107 bool ok = (h1 == h2 || h1 == h2-1);
108 REPORTER_ASSERT(r, ok);
109 if (!ok) {
110 SkDebugf("%08x (%d) -> %04x (%d), want %04x (%d)\n",
111 bits, bits>>23, h1, h1>>10, h2, h2>>10);
112 break;
113 }
114 }
115 }
116}