| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 namespace { // See SkPMFloat.h | 8 namespace { // See SkPMFloat.h |
| 9 | 9 |
| 10 // For SkPMFloat(SkPMFColor), we widen our 8 bit components (fix8) to 8-bit comp
onents in 16 bits | |
| 11 // (fix8_16), then widen those to 8-bit-in-32-bits (fix8_32), and finally conver
t those to floats. | |
| 12 | |
| 13 // round() and roundClamp() do the opposite, working from floats to 8-bit-in-32-
bit, | |
| 14 // to 8-bit-in-16-bit, back down to 8-bit components. | |
| 15 // roundClamp() uses vqmovn to clamp while narrowing instead of just narrowing w
ith vmovn. | |
| 16 | |
| 17 inline SkPMFloat::SkPMFloat(SkPMColor c) { | 10 inline SkPMFloat::SkPMFloat(SkPMColor c) { |
| 18 SkPMColorAssert(c); | 11 SkPMColorAssert(c); |
| 19 uint8x8_t fix8 = (uint8x8_t)vdup_n_u32(c); | 12 uint8x8_t fix8 = (uint8x8_t)vdup_n_u32(c); |
| 20 uint16x8_t fix8_16 = vmovl_u8(fix8); | 13 uint16x8_t fix8_16 = vmovl_u8(fix8); |
| 21 uint32x4_t fix8_32 = vmovl_u16(vget_low_u16(fix8_16)); | 14 uint32x4_t fix8_32 = vmovl_u16(vget_low_u16(fix8_16)); |
| 22 fVec = vcvtq_f32_u32(fix8_32); | 15 fVec = vcvtq_n_f32_u32(fix8_32, 8); |
| 23 SkASSERT(this->isValid()); | 16 SkASSERT(this->isValid()); |
| 24 } | 17 } |
| 25 | 18 |
| 26 inline SkPMColor SkPMFloat::trunc() const { | 19 inline SkPMColor SkPMFloat::round() const { |
| 27 uint32x4_t fix8_32 = vcvtq_u32_f32(fVec); // vcvtq_u32_f32 truncates | 20 // vcvtq_n_u32_f32 truncates, so we round manually by adding a half before c
onverting. |
| 28 uint16x4_t fix8_16 = vmovn_u32(fix8_32); | 21 float32x4_t rounded = vaddq_f32(fVec, vdupq_n_f32(0.5f/255)); |
| 29 uint8x8_t fix8 = vmovn_u16(vcombine_u16(fix8_16, vdup_n_u16(0))); | 22 uint32x4_t fix8_32 = vcvtq_n_u32_f32(rounded, 8); |
| 23 uint16x4_t fix8_16 = vqmovn_u32(fix8_32); |
| 24 uint8x8_t fix8 = vqmovn_u16(vcombine_u16(fix8_16, vdup_n_u16(0))); |
| 30 SkPMColor c = vget_lane_u32((uint32x2_t)fix8, 0); | 25 SkPMColor c = vget_lane_u32((uint32x2_t)fix8, 0); |
| 31 SkPMColorAssert(c); | 26 SkPMColorAssert(c); |
| 32 return c; | 27 return c; |
| 33 } | 28 } |
| 34 | 29 |
| 35 inline SkPMColor SkPMFloat::round() const { | |
| 36 return SkPMFloat(Sk4f(0.5f) + *this).trunc(); | |
| 37 } | |
| 38 | |
| 39 inline SkPMColor SkPMFloat::roundClamp() const { | |
| 40 float32x4_t add_half = vaddq_f32(fVec, vdupq_n_f32(0.5f)); | |
| 41 uint32x4_t fix8_32 = vcvtq_u32_f32(add_half); // vcvtq_u32_f32 truncates,
so round manually | |
| 42 uint16x4_t fix8_16 = vqmovn_u32(fix8_32); | |
| 43 uint8x8_t fix8 = vqmovn_u16(vcombine_u16(fix8_16, vdup_n_u16(0))); | |
| 44 SkPMColor c = vget_lane_u32((uint32x2_t)fix8, 0); | |
| 45 SkPMColorAssert(c); | |
| 46 return c; | |
| 47 } | |
| 48 | |
| 49 // TODO: we should be able to beat these loops on all three methods. | |
| 50 inline void SkPMFloat::From4PMColors(const SkPMColor colors[4], | |
| 51 SkPMFloat* a, SkPMFloat* b, SkPMFloat* c, S
kPMFloat* d) { | |
| 52 *a = FromPMColor(colors[0]); | |
| 53 *b = FromPMColor(colors[1]); | |
| 54 *c = FromPMColor(colors[2]); | |
| 55 *d = FromPMColor(colors[3]); | |
| 56 } | |
| 57 | |
| 58 inline void SkPMFloat::RoundTo4PMColors( | |
| 59 const SkPMFloat& a, const SkPMFloat& b, const SkPMFloat&c, const SkPMFlo
at& d, | |
| 60 SkPMColor colors[4]) { | |
| 61 colors[0] = a.round(); | |
| 62 colors[1] = b.round(); | |
| 63 colors[2] = c.round(); | |
| 64 colors[3] = d.round(); | |
| 65 } | |
| 66 | |
| 67 inline void SkPMFloat::RoundClampTo4PMColors( | |
| 68 const SkPMFloat& a, const SkPMFloat& b, const SkPMFloat&c, const SkPMFlo
at& d, | |
| 69 SkPMColor colors[4]) { | |
| 70 colors[0] = a.roundClamp(); | |
| 71 colors[1] = b.roundClamp(); | |
| 72 colors[2] = c.roundClamp(); | |
| 73 colors[3] = d.roundClamp(); | |
| 74 } | |
| 75 | |
| 76 } // namespace | 30 } // namespace |
| OLD | NEW |