Make SkPMFloats store floats in [0,255] instead of [0,1].
This pushes the cost of the *255 and *1/255 conversions onto only those code paths that need it. We're not doing it any more efficiently than can be done with Sk4f. In microbenchmark isolation, this is about a 15% speedup. BUG=skia: NOPRESUBMIT=true Review URL: https://codereview.chromium.org/973603002
This commit is contained in:
parent
cff10b21a9
commit
60d2a32b2d
@ -5,7 +5,7 @@
|
||||
#include "SkColor.h"
|
||||
#include "Sk4x.h"
|
||||
|
||||
// A pre-multiplied color storing each component as a float.
|
||||
// A pre-multiplied color storing each component as a float in the range [0, 255].
|
||||
class SK_STRUCT_ALIGN(16) SkPMFloat {
|
||||
public:
|
||||
// Normal POD copies and do-nothing initialization.
|
||||
@ -30,10 +30,10 @@ public:
|
||||
void set(SkPMColor);
|
||||
|
||||
SkPMColor get() const; // May SkASSERT(this->isValid()). Some implementations may clamp.
|
||||
SkPMColor clamped() const; // Will clamp all values to [0,1], then SkASSERT(this->isValid()).
|
||||
SkPMColor clamped() const; // Will clamp all values to [0, 255]. Then may assert isValid().
|
||||
|
||||
bool isValid() const {
|
||||
return this->a() >= 0 && this->a() <= 1
|
||||
return this->a() >= 0 && this->a() <= 255
|
||||
&& this->r() >= 0 && this->r() <= this->a()
|
||||
&& this->g() >= 0 && this->g() <= this->a()
|
||||
&& this->b() >= 0 && this->b() <= this->a();
|
||||
|
@ -2,11 +2,10 @@
|
||||
#include <emmintrin.h>
|
||||
|
||||
// For set(), we widen our 8 bit components (fix8) to 8-bit components in 16 bits (fix8_16),
|
||||
// then widen those to 8-bit-in-32-bits (fix8_32), convert those to floats (scaled),
|
||||
// then finally scale those down from [0.0f, 255.0f] to [0.0f, 1.0f] into fColor.
|
||||
// then widen those to 8-bit-in-32-bits (fix8_32), and finally convert those to floats.
|
||||
|
||||
// get() and clamped() do the opposite, working from [0.0f, 1.0f] floats to [0.0f, 255.0f],
|
||||
// to 8-bit-in-32-bit, to 8-bit-in-16-bit, back down to 8-bit components.
|
||||
// get() and clamped() do the opposite, working from floats to 8-bit-in-32-bit,
|
||||
// to 8-bit-in-16-bit, back down to 8-bit components.
|
||||
// _mm_packus_epi16() gives us clamping for free while narrowing.
|
||||
|
||||
inline void SkPMFloat::set(SkPMColor c) {
|
||||
@ -14,8 +13,7 @@ inline void SkPMFloat::set(SkPMColor c) {
|
||||
__m128i fix8 = _mm_set_epi32(0,0,0,c),
|
||||
fix8_16 = _mm_unpacklo_epi8 (fix8, _mm_setzero_si128()),
|
||||
fix8_32 = _mm_unpacklo_epi16(fix8_16, _mm_setzero_si128());
|
||||
__m128 scaled = _mm_cvtepi32_ps(fix8_32);
|
||||
_mm_store_ps(fColor, _mm_mul_ps(scaled, _mm_set1_ps(1.0f/255.0f)));
|
||||
_mm_store_ps(fColor, _mm_cvtepi32_ps(fix8_32));
|
||||
SkASSERT(this->isValid());
|
||||
}
|
||||
|
||||
@ -25,8 +23,7 @@ inline SkPMColor SkPMFloat::get() const {
|
||||
}
|
||||
|
||||
inline SkPMColor SkPMFloat::clamped() const {
|
||||
__m128 scaled = _mm_mul_ps(_mm_load_ps(fColor), _mm_set1_ps(255.0f));
|
||||
__m128i fix8_32 = _mm_cvtps_epi32(scaled),
|
||||
__m128i fix8_32 = _mm_cvtps_epi32(_mm_load_ps(fColor)),
|
||||
fix8_16 = _mm_packus_epi16(fix8_32, fix8_32),
|
||||
fix8 = _mm_packus_epi16(fix8_16, fix8_16);
|
||||
SkPMColor c = _mm_cvtsi128_si32(fix8);
|
||||
|
@ -2,11 +2,10 @@
|
||||
#include <arm_neon.h>
|
||||
|
||||
// For set(), we widen our 8 bit components (fix8) to 8-bit components in 16 bits (fix8_16),
|
||||
// then widen those to 8-bit-in-32-bits (fix8_32), convert those to floats (scaled),
|
||||
// then finally scale those down from [0.0f, 255.0f] to [0.0f, 1.0f] into fColor.
|
||||
// then widen those to 8-bit-in-32-bits (fix8_32), and finally convert those to floats.
|
||||
|
||||
// get() and clamped() do the opposite, working from [0.0f, 1.0f] floats to [0.0f, 255.0f],
|
||||
// to 8-bit-in-32-bit, to 8-bit-in-16-bit, back down to 8-bit components.
|
||||
// get() and clamped() do the opposite, working from floats to 8-bit-in-32-bit,
|
||||
// to 8-bit-in-16-bit, back down to 8-bit components.
|
||||
// clamped() uses vqmovn to clamp while narrowing instead of just narrowing with vmovn.
|
||||
|
||||
inline void SkPMFloat::set(SkPMColor c) {
|
||||
@ -14,15 +13,13 @@ inline void SkPMFloat::set(SkPMColor c) {
|
||||
uint8x8_t fix8 = (uint8x8_t)vdup_n_u32(c);
|
||||
uint16x8_t fix8_16 = vmovl_u8(fix8);
|
||||
uint32x4_t fix8_32 = vmovl_u16(vget_low_u16(fix8_16));
|
||||
float32x4_t scaled = vcvtq_f32_u32(fix8_32);
|
||||
vst1q_f32(fColor, vmulq_f32(scaled, vdupq_n_f32(1.0f/255.0f)));
|
||||
vst1q_f32(fColor, vcvtq_f32_u32(fix8_32));
|
||||
SkASSERT(this->isValid());
|
||||
}
|
||||
|
||||
inline SkPMColor SkPMFloat::get() const {
|
||||
SkASSERT(this->isValid());
|
||||
float32x4_t scaled = vmulq_f32(vld1q_f32(fColor), vdupq_n_f32(255.0f));
|
||||
uint32x4_t fix8_32 = vcvtq_u32_f32(scaled);
|
||||
uint32x4_t fix8_32 = vcvtq_u32_f32(vld1q_f32(fColor));
|
||||
uint16x4_t fix8_16 = vmovn_u32(fix8_32);
|
||||
uint8x8_t fix8 = vmovn_u16(vcombine_u16(fix8_16, vdup_n_u16(0)));
|
||||
SkPMColor c = vget_lane_u32((uint32x2_t)fix8, 0);
|
||||
@ -31,8 +28,7 @@ inline SkPMColor SkPMFloat::get() const {
|
||||
}
|
||||
|
||||
inline SkPMColor SkPMFloat::clamped() const {
|
||||
float32x4_t scaled = vmulq_f32(vld1q_f32(fColor), vdupq_n_f32(255.0f));
|
||||
uint32x4_t fix8_32 = vcvtq_u32_f32(scaled);
|
||||
uint32x4_t fix8_32 = vcvtq_u32_f32(vld1q_f32(fColor));
|
||||
uint16x4_t fix8_16 = vqmovn_u32(fix8_32);
|
||||
uint8x8_t fix8 = vqmovn_u16(vcombine_u16(fix8_16, vdup_n_u16(0)));
|
||||
SkPMColor c = vget_lane_u32((uint32x2_t)fix8, 0);
|
||||
|
@ -1,17 +1,16 @@
|
||||
#include "SkColorPriv.h"
|
||||
|
||||
inline void SkPMFloat::set(SkPMColor c) {
|
||||
float scale = 1.0f / 255.0f;
|
||||
this->setA(SkGetPackedA32(c) * scale);
|
||||
this->setR(SkGetPackedR32(c) * scale);
|
||||
this->setG(SkGetPackedG32(c) * scale);
|
||||
this->setB(SkGetPackedB32(c) * scale);
|
||||
this->setA(SkGetPackedA32(c));
|
||||
this->setR(SkGetPackedR32(c));
|
||||
this->setG(SkGetPackedG32(c));
|
||||
this->setB(SkGetPackedB32(c));
|
||||
SkASSERT(this->isValid());
|
||||
}
|
||||
|
||||
inline SkPMColor SkPMFloat::get() const {
|
||||
SkASSERT(this->isValid());
|
||||
return SkPackARGB32(this->a() * 255, this->r() * 255, this->g() * 255, this->b() * 255);
|
||||
return SkPackARGB32(this->a(), this->r(), this->g(), this->b());
|
||||
}
|
||||
|
||||
inline SkPMColor SkPMFloat::clamped() const {
|
||||
@ -19,9 +18,9 @@ inline SkPMColor SkPMFloat::clamped() const {
|
||||
r = this->r(),
|
||||
g = this->g(),
|
||||
b = this->b();
|
||||
a = a < 0 ? 0 : (a > 1 ? 1 : a);
|
||||
r = r < 0 ? 0 : (r > 1 ? 1 : r);
|
||||
g = g < 0 ? 0 : (g > 1 ? 1 : g);
|
||||
b = b < 0 ? 0 : (b > 1 ? 1 : b);
|
||||
return SkPackARGB32(a * 255, r * 255, g * 255, b * 255);
|
||||
a = a < 0 ? 0 : (a > 255 ? 255 : a);
|
||||
r = r < 0 ? 0 : (r > 255 ? 255 : r);
|
||||
g = g < 0 ? 0 : (g > 255 ? 255 : g);
|
||||
b = b < 0 ? 0 : (b > 255 ? 255 : b);
|
||||
return SkPackARGB32(a, r, g, b);
|
||||
}
|
||||
|
@ -7,34 +7,34 @@ DEF_TEST(SkPMFloat, r) {
|
||||
|
||||
SkPMFloat pmf;
|
||||
pmf.set(c);
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(1.0f, pmf.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.8f, pmf.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.6f, pmf.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.2f, pmf.b()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, pmf.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(204.0f, pmf.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, pmf.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual( 51.0f, pmf.b()));
|
||||
|
||||
REPORTER_ASSERT(r, c == pmf.get());
|
||||
|
||||
// Test clamping.
|
||||
SkPMFloat unclamped;
|
||||
unclamped.setA(+2.0f);
|
||||
unclamped.setR(+0.2f);
|
||||
unclamped.setG(-0.2f);
|
||||
unclamped.setB(-5.0f);
|
||||
unclamped.setA(+510.0f);
|
||||
unclamped.setR(+153.0f);
|
||||
unclamped.setG( +1.0f);
|
||||
unclamped.setB( -0.2f);
|
||||
|
||||
SkPMFloat clamped;
|
||||
clamped.set(unclamped.clamped());
|
||||
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(1.0f, clamped.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.2f, clamped.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.0f, clamped.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.0f, clamped.b()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(255.0f, clamped.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(153.0f, clamped.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual( 1.0f, clamped.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.0f, clamped.b()));
|
||||
|
||||
// Test SkPMFloat <-> Sk4f conversion.
|
||||
Sk4f fs = clamped;
|
||||
SkPMFloat scaled = fs.multiply(Sk4f(4,4,4,4));
|
||||
SkPMFloat scaled = fs.multiply(0.25f);
|
||||
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(4.0f, scaled.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.8f, scaled.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.0f, scaled.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(0.0f, scaled.b()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(63.75f, scaled.a()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual(38.25f, scaled.r()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.25f, scaled.g()));
|
||||
REPORTER_ASSERT(r, SkScalarNearlyEqual( 0.00f, scaled.b()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user