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 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 8 | #include "SkAutoPixmapStorage.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 9 | #include "SkColor.h" |
| 10 | #include "SkHalf.h" |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 11 | #include "SkOpts.h" |
Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 12 | #include "SkPM4f.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 13 | #include "SkPixmap.h" |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 14 | #include "SkRandom.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 15 | #include "SkTo.h" |
| 16 | #include "Test.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 17 | |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 18 | #include <cmath> |
| 19 | |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 20 | static bool eq_within_half_float(float a, float b) { |
| 21 | const float kTolerance = 1.0f / (1 << (8 + 10)); |
| 22 | |
| 23 | SkHalf ha = SkFloatToHalf(a); |
| 24 | SkHalf hb = SkFloatToHalf(b); |
| 25 | float a2 = SkHalfToFloat(ha); |
| 26 | float b2 = SkHalfToFloat(hb); |
| 27 | return fabsf(a2 - b2) <= kTolerance; |
| 28 | } |
| 29 | |
| 30 | static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) { |
| 31 | for (int i = 0; i < 4; ++i) { |
| 32 | if (!eq_within_half_float(a.fVec[i], b.fVec[i])) { |
| 33 | return false; |
| 34 | } |
| 35 | } |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | DEF_TEST(color_half_float, reporter) { |
| 40 | const int w = 100; |
| 41 | const int h = 100; |
| 42 | |
| 43 | SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType); |
| 44 | |
| 45 | SkAutoPixmapStorage pm; |
| 46 | pm.alloc(info); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, pm.computeByteSize() == SkToSizeT(w * h * sizeof(uint64_t))); |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 48 | |
brianosman | e074d1f | 2016-06-24 06:31:47 -0700 | [diff] [blame] | 49 | SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f }; |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 50 | pm.erase(c4); |
| 51 | |
| 52 | SkPM4f origpm4 = c4.premul(); |
| 53 | for (int y = 0; y < pm.height(); ++y) { |
| 54 | for (int x = 0; x < pm.width(); ++x) { |
| 55 | SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y)); |
| 56 | REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4)); |
| 57 | } |
| 58 | } |
| 59 | } |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 60 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 61 | static bool is_denorm(uint16_t h) { |
| 62 | return (h & 0x7fff) < 0x0400; |
mtklein | ddb64c8 | 2016-02-11 12:48:23 -0800 | [diff] [blame] | 63 | } |
| 64 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 65 | static bool is_finite(uint16_t h) { |
| 66 | return (h & 0x7c00) != 0x7c00; |
| 67 | } |
| 68 | |
| 69 | DEF_TEST(SkHalfToFloat_finite_ftz, r) { |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 70 | for (uint32_t h = 0; h <= 0xffff; h++) { |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 71 | if (!is_finite(h)) { |
| 72 | // _finite_ftz() only works for values that can be represented as a finite half float. |
| 73 | continue; |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 74 | } |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 75 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 76 | // _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f. |
| 77 | float expected = SkHalfToFloat(h), |
| 78 | alternate = is_denorm(h) ? 0.0f : expected; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 79 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 80 | float actual = SkHalfToFloat_finite_ftz(h)[0]; |
| 81 | |
| 82 | REPORTER_ASSERT(r, actual == expected || actual == alternate); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 86 | DEF_TEST(SkFloatToHalf_finite_ftz, r) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 87 | #if 0 |
mtklein | 58e389b | 2016-07-15 07:00:11 -0700 | [diff] [blame] | 88 | for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 89 | #else |
| 90 | SkRandom rand; |
| 91 | for (int i = 0; i < 1000000; i++) { |
| 92 | uint32_t bits = rand.nextU(); |
| 93 | #endif |
| 94 | float f; |
| 95 | memcpy(&f, &bits, 4); |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 96 | |
| 97 | uint16_t expected = SkFloatToHalf(f); |
| 98 | if (!is_finite(expected)) { |
| 99 | // _finite_ftz() only works for values that can be represented as a finite half float. |
| 100 | continue; |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 101 | } |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 102 | |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 103 | uint16_t alternate = expected; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 104 | if (is_denorm(expected)) { |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 105 | // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit. |
Ben Wagner | 8a1036c | 2016-11-09 15:00:49 -0500 | [diff] [blame] | 106 | alternate = std::signbit(f) ? 0x8000 : 0x0000; |
mtklein | 8ae991e | 2016-08-22 13:20:18 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0]; |
mtklein | a2d2f38 | 2016-08-23 08:58:12 -0700 | [diff] [blame] | 110 | // _finite_ftz() may truncate instead of rounding, so it may be one too small. |
| 111 | REPORTER_ASSERT(r, actual == expected || actual == expected - 1 || |
| 112 | actual == alternate || actual == alternate - 1); |
mtklein | fff055c | 2016-02-11 06:30:03 -0800 | [diff] [blame] | 113 | } |
| 114 | } |