reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 9 | #include "SkAutoPixmapStorage.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 10 | #include "SkColor.h" |
| 11 | #include "SkHalf.h" |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 12 | #include "SkOpts.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 13 | #include "SkPixmap.h" |
reed | dd9ffea | 2016-02-18 12:39:14 -0800 | [diff] [blame] | 14 | #include "SkPM4f.h" |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 15 | #include "SkRandom.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 16 | |
| 17 | static 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 | |
| 27 | static 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 | |
| 36 | DEF_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 | |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 46 | SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f }; |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 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 | } |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 57 | |
mtklein | ddb64c8 | 2016-02-11 12:48:23 -0800 | [diff] [blame] | 58 | static uint32_t u(float f) { |
| 59 | uint32_t x; |
| 60 | memcpy(&x, &f, 4); |
| 61 | return x; |
| 62 | } |
| 63 | |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 64 | DEF_TEST(HalfToFloat_finite, r) { |
| 65 | for (uint32_t h = 0; h <= 0xffff; h++) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 66 | float f = SkHalfToFloat(h); |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 67 | if (isfinite(f)) { |
| 68 | float got = SkHalfToFloat_finite(h)[0]; |
mtklein | ddb64c8 | 2016-02-11 12:48:23 -0800 | [diff] [blame] | 69 | if (got != f) { |
| 70 | SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n", |
| 71 | h, |
| 72 | u(got), got, |
| 73 | u(f), f); |
| 74 | } |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 75 | REPORTER_ASSERT(r, SkHalfToFloat_finite(h)[0] == f); |
msarett | 6bdbf44 | 2016-07-19 09:07:55 -0700 | [diff] [blame] | 76 | uint64_t result; |
| 77 | SkFloatToHalf_finite(SkHalfToFloat_finite(h)).store(&result); |
| 78 | REPORTER_ASSERT(r, result == h); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 83 | DEF_TEST(FloatToHalf_finite, r) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 84 | #if 0 |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 85 | for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 86 | #else |
| 87 | SkRandom rand; |
| 88 | for (int i = 0; i < 1000000; i++) { |
| 89 | uint32_t bits = rand.nextU(); |
| 90 | #endif |
| 91 | float f; |
| 92 | memcpy(&f, &bits, 4); |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 93 | if (isfinite(f) && isfinite(SkHalfToFloat(SkFloatToHalf(f)))) { |
msarett | 6bdbf44 | 2016-07-19 09:07:55 -0700 | [diff] [blame] | 94 | uint16_t h1 = SkFloatToHalf_finite(Sk4f(f,0,0,0))[0], |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 95 | h2 = SkFloatToHalf(f); |
| 96 | bool ok = (h1 == h2 || h1 == h2-1); |
| 97 | REPORTER_ASSERT(r, ok); |
| 98 | if (!ok) { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 99 | SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n", |
| 100 | bits, f, h1, h2, SkHalfToFloat(h2)); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |