2016-02-05 19:18:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkAutoPixmapStorage.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
#include "SkColor.h"
|
|
|
|
#include "SkHalf.h"
|
2016-02-09 16:18:10 +00:00
|
|
|
#include "SkOpts.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
#include "SkPixmap.h"
|
2016-02-18 20:39:14 +00:00
|
|
|
#include "SkPM4f.h"
|
2016-02-11 14:30:03 +00:00
|
|
|
#include "SkRandom.h"
|
2016-02-05 19:18:39 +00:00
|
|
|
|
|
|
|
static bool eq_within_half_float(float a, float b) {
|
|
|
|
const float kTolerance = 1.0f / (1 << (8 + 10));
|
|
|
|
|
|
|
|
SkHalf ha = SkFloatToHalf(a);
|
|
|
|
SkHalf hb = SkFloatToHalf(b);
|
|
|
|
float a2 = SkHalfToFloat(ha);
|
|
|
|
float b2 = SkHalfToFloat(hb);
|
|
|
|
return fabsf(a2 - b2) <= kTolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(color_half_float, reporter) {
|
|
|
|
const int w = 100;
|
|
|
|
const int h = 100;
|
|
|
|
|
|
|
|
SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
|
|
|
|
|
|
|
|
SkAutoPixmapStorage pm;
|
|
|
|
pm.alloc(info);
|
|
|
|
REPORTER_ASSERT(reporter, pm.getSafeSize() == SkToSizeT(w * h * sizeof(uint64_t)));
|
|
|
|
|
2016-06-24 13:31:47 +00:00
|
|
|
SkColor4f c4 { 1, 0.5f, 0.25f, 0.5f };
|
2016-02-05 19:18:39 +00:00
|
|
|
pm.erase(c4);
|
|
|
|
|
|
|
|
SkPM4f origpm4 = c4.premul();
|
|
|
|
for (int y = 0; y < pm.height(); ++y) {
|
|
|
|
for (int x = 0; x < pm.width(); ++x) {
|
|
|
|
SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
|
|
|
|
REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 16:18:10 +00:00
|
|
|
|
2016-02-11 20:48:23 +00:00
|
|
|
static uint32_t u(float f) {
|
|
|
|
uint32_t x;
|
|
|
|
memcpy(&x, &f, 4);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-07-15 14:00:11 +00:00
|
|
|
DEF_TEST(HalfToFloat_finite, r) {
|
|
|
|
for (uint32_t h = 0; h <= 0xffff; h++) {
|
2016-02-11 14:30:03 +00:00
|
|
|
float f = SkHalfToFloat(h);
|
2016-07-15 14:00:11 +00:00
|
|
|
if (isfinite(f)) {
|
|
|
|
float got = SkHalfToFloat_finite(h)[0];
|
2016-02-11 20:48:23 +00:00
|
|
|
if (got != f) {
|
|
|
|
SkDebugf("0x%04x -> 0x%08x (%g), want 0x%08x (%g)\n",
|
|
|
|
h,
|
|
|
|
u(got), got,
|
|
|
|
u(f), f);
|
|
|
|
}
|
2016-07-15 14:00:11 +00:00
|
|
|
REPORTER_ASSERT(r, SkHalfToFloat_finite(h)[0] == f);
|
2016-07-19 16:07:55 +00:00
|
|
|
uint64_t result;
|
|
|
|
SkFloatToHalf_finite(SkHalfToFloat_finite(h)).store(&result);
|
|
|
|
REPORTER_ASSERT(r, result == h);
|
2016-02-11 14:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 14:00:11 +00:00
|
|
|
DEF_TEST(FloatToHalf_finite, r) {
|
2016-02-11 14:30:03 +00:00
|
|
|
#if 0
|
2016-07-15 14:00:11 +00:00
|
|
|
for (uint64_t bits = 0; bits <= 0xffffffff; bits++) {
|
2016-02-11 14:30:03 +00:00
|
|
|
#else
|
|
|
|
SkRandom rand;
|
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
uint32_t bits = rand.nextU();
|
|
|
|
#endif
|
|
|
|
float f;
|
|
|
|
memcpy(&f, &bits, 4);
|
2016-07-15 14:00:11 +00:00
|
|
|
if (isfinite(f) && isfinite(SkHalfToFloat(SkFloatToHalf(f)))) {
|
2016-07-19 16:07:55 +00:00
|
|
|
uint16_t h1 = SkFloatToHalf_finite(Sk4f(f,0,0,0))[0],
|
2016-02-11 14:30:03 +00:00
|
|
|
h2 = SkFloatToHalf(f);
|
|
|
|
bool ok = (h1 == h2 || h1 == h2-1);
|
|
|
|
REPORTER_ASSERT(r, ok);
|
|
|
|
if (!ok) {
|
2016-07-15 14:00:11 +00:00
|
|
|
SkDebugf("%08x (%g) -> %04x, want %04x (%g)\n",
|
|
|
|
bits, f, h1, h2, SkHalfToFloat(h2));
|
2016-02-11 14:30:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|