aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--bench/PMFloatBench.cpp25
-rw-r--r--gyp/bench.gypi1
-rw-r--r--gyp/tests.gypi1
-rw-r--r--src/core/SkPMFloat.h50
-rw-r--r--src/opts/SkPMFloat_SSE2.h36
-rw-r--r--src/opts/SkPMFloat_neon.h42
-rw-r--r--src/opts/SkPMFloat_none.h28
-rw-r--r--tests/PMFloatTest.cpp29
8 files changed, 212 insertions, 0 deletions
diff --git a/bench/PMFloatBench.cpp b/bench/PMFloatBench.cpp
new file mode 100644
index 0000000000..8250c9e17c
--- /dev/null
+++ b/bench/PMFloatBench.cpp
@@ -0,0 +1,25 @@
+#include "Benchmark.h"
+#include "SkPMFloat.h"
+#include "SkRandom.h"
+
+struct PMFloatBench : public Benchmark {
+ explicit PMFloatBench(bool clamp) : fClamp(clamp) {}
+
+ const char* onGetName() SK_OVERRIDE { return fClamp ? "SkPMFloat_clamp" : "SkPMFloat_get"; }
+ bool isSuitableFor(Backend backend) SK_OVERRIDE { return backend == kNonRendering_Backend; }
+
+ void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+ SkRandom rand;
+ for (int i = 0; i < loops; i++) {
+ SkPMColor c = SkPreMultiplyColor(rand.nextU());
+ SkPMFloat pmf;
+ pmf.set(c);
+ SkPMColor back = fClamp ? pmf.clamped() : pmf.get();
+ if (c != back) { SkFAIL("no joy"); } // This conditional makes this not compile away.
+ }
+ }
+
+ bool fClamp;
+};
+DEF_BENCH(return new PMFloatBench( true);)
+DEF_BENCH(return new PMFloatBench(false);)
diff --git a/gyp/bench.gypi b/gyp/bench.gypi
index 87c6622caa..3b77fc5383 100644
--- a/gyp/bench.gypi
+++ b/gyp/bench.gypi
@@ -74,6 +74,7 @@
'../bench/MipMapBench.cpp',
'../bench/MorphologyBench.cpp',
'../bench/MutexBench.cpp',
+ '../bench/PMFloatBench.cpp',
'../bench/PatchBench.cpp',
'../bench/PatchGridBench.cpp',
'../bench/PathBench.cpp',
diff --git a/gyp/tests.gypi b/gyp/tests.gypi
index fdd892ab90..53f3555a58 100644
--- a/gyp/tests.gypi
+++ b/gyp/tests.gypi
@@ -152,6 +152,7 @@
'../tests/PDFInvalidBitmapTest.cpp',
'../tests/PDFJpegEmbedTest.cpp',
'../tests/PDFPrimitivesTest.cpp',
+ '../tests/PMFloatTest.cpp',
'../tests/PackBitsTest.cpp',
'../tests/PaintTest.cpp',
'../tests/ParsePathTest.cpp',
diff --git a/src/core/SkPMFloat.h b/src/core/SkPMFloat.h
new file mode 100644
index 0000000000..26bc9cce07
--- /dev/null
+++ b/src/core/SkPMFloat.h
@@ -0,0 +1,50 @@
+#ifndef SkPM_DEFINED
+#define SkPM_DEFINED
+
+#include "SkTypes.h"
+#include "SkColor.h"
+
+// As usual, there are two ways to increase alignment... the MSVC way and the everyone-else way.
+#ifdef _MSC_VER
+ #define ALIGN(N) __declspec(align(N))
+#else
+ #define ALIGN(N) __attribute__((aligned(N)))
+#endif
+
+// A pre-multiplied color in the same order as SkPMColor storing each component as a float.
+struct ALIGN(16) SkPMFloat {
+ float fColor[4];
+
+ float a() const { return fColor[SK_A32_SHIFT / 8]; }
+ float r() const { return fColor[SK_R32_SHIFT / 8]; }
+ float g() const { return fColor[SK_G32_SHIFT / 8]; }
+ float b() const { return fColor[SK_B32_SHIFT / 8]; }
+
+ void setA(float val) { fColor[SK_A32_SHIFT / 8] = val; }
+ void setR(float val) { fColor[SK_R32_SHIFT / 8] = val; }
+ void setG(float val) { fColor[SK_G32_SHIFT / 8] = val; }
+ void setB(float val) { fColor[SK_B32_SHIFT / 8] = val; }
+
+ void set(SkPMColor);
+
+ SkPMColor get() const; // May SkASSERT(this->isValid()).
+ SkPMColor clamped() const; // Will clamp all values to [0,1], then SkASSERT(this->isValid()).
+
+ bool isValid() const {
+ return this->a() >= 0 && this->a() <= 1
+ && this->r() >= 0 && this->r() <= this->a()
+ && this->g() >= 0 && this->g() <= this->a()
+ && this->b() >= 0 && this->b() <= this->a();
+ }
+};
+#undef ALIGN
+
+#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
+ #include "../opts/SkPMFloat_SSE2.h"
+#elif defined(__ARM_NEON__)
+ #include "../opts/SkPMFloat_neon.h"
+#else
+ #include "../opts/SkPMFloat_none.h"
+#endif
+
+#endif//SkPM_DEFINED
diff --git a/src/opts/SkPMFloat_SSE2.h b/src/opts/SkPMFloat_SSE2.h
new file mode 100644
index 0000000000..7bacf56af7
--- /dev/null
+++ b/src/opts/SkPMFloat_SSE2.h
@@ -0,0 +1,36 @@
+#include "SkColorPriv.h"
+#include "SkPMFloat.h"
+#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.
+
+// 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.
+// _mm_packus_epi16() gives us clamping for free while narrowing.
+
+inline void SkPMFloat::set(SkPMColor c) {
+ SkPMColorAssert(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)));
+ SkASSERT(this->isValid());
+}
+
+inline SkPMColor SkPMFloat::get() const {
+ SkASSERT(this->isValid());
+ return this->clamped(); // At the moment, we don't know anything faster.
+}
+
+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),
+ fix8_16 = _mm_packus_epi16(fix8_32, fix8_32),
+ fix8 = _mm_packus_epi16(fix8_16, fix8_16);
+ SkPMColor c = _mm_cvtsi128_si32(fix8);
+ SkPMColorAssert(c);
+ return c;
+}
diff --git a/src/opts/SkPMFloat_neon.h b/src/opts/SkPMFloat_neon.h
new file mode 100644
index 0000000000..12e857a50f
--- /dev/null
+++ b/src/opts/SkPMFloat_neon.h
@@ -0,0 +1,42 @@
+#include "SkColorPriv.h"
+#include "SkPMFloat.h"
+#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.
+
+// 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.
+// clamped() uses vqmovn to clamp while narrowing instead of just narrowing with vmovn.
+
+inline void SkPMFloat::set(SkPMColor c) {
+ SkPMColorAssert(c);
+ uint8x8_t fix8 = 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)));
+ 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);
+ 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(fix8, 0);
+ SkPMColorAssert(c);
+ return c;
+}
+
+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);
+ 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(fix8, 0);
+ SkPMColorAssert(c);
+ return c;
+}
diff --git a/src/opts/SkPMFloat_none.h b/src/opts/SkPMFloat_none.h
new file mode 100644
index 0000000000..dee7ce6862
--- /dev/null
+++ b/src/opts/SkPMFloat_none.h
@@ -0,0 +1,28 @@
+#include "SkColorPriv.h"
+#include "SkPMFloat.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);
+ 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);
+}
+
+inline SkPMColor SkPMFloat::clamped() const {
+ float a = this->a(),
+ 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);
+}
diff --git a/tests/PMFloatTest.cpp b/tests/PMFloatTest.cpp
new file mode 100644
index 0000000000..94cd663a53
--- /dev/null
+++ b/tests/PMFloatTest.cpp
@@ -0,0 +1,29 @@
+#include "SkPMFloat.h"
+#include "Test.h"
+
+DEF_TEST(SkPMFloat, r) {
+ SkPMColor c = SkPreMultiplyColor(0xFFCC9933);
+
+ 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, c == pmf.get());
+
+ SkPMFloat unclamped;
+ unclamped.setA(+2.0f);
+ unclamped.setR(+0.2f);
+ unclamped.setG(-0.2f);
+ unclamped.setB(-5.0f);
+
+ 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()));
+}