SkPM4fPriv.h revision fbc1e296b2e98dc76de533a2bb45d9ccc8c2498f
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 8#include "SkColorPriv.h" 9#include "SkNx.h" 10 11static inline float get_alpha(const Sk4f& f4) { 12 return f4.kth<SkPM4f::A>(); 13} 14 15static inline Sk4f set_alpha(const Sk4f& f4, float alpha) { 16 static_assert(3 == SkPM4f::A, ""); 17 return Sk4f(f4.kth<0>(), f4.kth<1>(), f4.kth<2>(), alpha); 18} 19 20static inline uint32_t to_4b(const Sk4f& f4) { 21 uint32_t b4; 22 SkNx_cast<uint8_t>(f4).store((uint8_t*)&b4); 23 return b4; 24} 25 26static inline Sk4f to_4f(uint32_t b4) { 27 return SkNx_cast<float>(Sk4b::Load((const uint8_t*)&b4)); 28} 29 30static inline Sk4f s2l(const Sk4f& s4) { 31 return set_alpha(s4 * s4, get_alpha(s4)); 32} 33 34static inline Sk4f l2s(const Sk4f& l4) { 35 return set_alpha(l4.rsqrt1() * l4, get_alpha(l4)); 36} 37 38/////////////////////////////////////////////////////////////////////////////////////////////////// 39 40static inline Sk4f Sk4f_fromL32(uint32_t src) { 41 return to_4f(src) * Sk4f(1.0f/255); 42} 43 44static inline Sk4f Sk4f_fromS32(uint32_t src) { 45 return s2l(to_4f(src) * Sk4f(1.0f/255)); 46} 47 48static inline uint32_t Sk4f_toL32(const Sk4f& x4) { 49 return to_4b(x4 * Sk4f(255) + Sk4f(0.5f)); 50} 51 52static inline uint32_t Sk4f_toS32(const Sk4f& x4) { 53 return to_4b(l2s(x4) * Sk4f(255) + Sk4f(0.5f)); 54} 55 56/////////////////////////////////////////////////////////////////////////////////////////////////// 57 58static Sk4f unit_to_l255_round(const SkPM4f& pm4) { 59 return Sk4f::Load(pm4.fVec) * Sk4f(255) + Sk4f(0.5f); 60} 61 62static Sk4f unit_to_s255_round(const SkPM4f& pm4) { 63 return l2s(Sk4f::Load(pm4.fVec)) * Sk4f(255) + Sk4f(0.5f); 64} 65 66static inline void SkPM4f_l32_src_mode(SkPMColor dst[], const SkPM4f src[], int count) { 67 for (int i = 0; i < (count >> 2); ++i) { 68 SkASSERT(src[0].isUnit()); 69 SkASSERT(src[1].isUnit()); 70 SkASSERT(src[2].isUnit()); 71 SkASSERT(src[3].isUnit()); 72 Sk4f_ToBytes((uint8_t*)dst, 73 unit_to_l255_round(src[0]), unit_to_l255_round(src[1]), 74 unit_to_l255_round(src[2]), unit_to_l255_round(src[3])); 75 src += 4; 76 dst += 4; 77 } 78 count &= 3; 79 for (int i = 0; i < count; ++i) { 80 SkASSERT(src[i].isUnit()); 81 SkNx_cast<uint8_t>(unit_to_l255_round(src[i])).store((uint8_t*)&dst[i]); 82 } 83} 84 85static inline void SkPM4f_l32_srcover_mode(SkPMColor dst[], const SkPM4f src[], int count) { 86 for (int i = 0; i < count; ++i) { 87 SkASSERT(src[i].isUnit()); 88 Sk4f s4 = Sk4f::Load(src[i].fVec); 89 Sk4f d4 = Sk4f_fromL32(dst[i]); 90 dst[i] = Sk4f_toL32(s4 + d4 * Sk4f(1 - get_alpha(s4))); 91 } 92} 93 94static inline void SkPM4f_s32_src_mode(SkPMColor dst[], const SkPM4f src[], int count) { 95 for (int i = 0; i < (count >> 2); ++i) { 96 SkASSERT(src[0].isUnit()); 97 SkASSERT(src[1].isUnit()); 98 SkASSERT(src[2].isUnit()); 99 SkASSERT(src[3].isUnit()); 100 Sk4f_ToBytes((uint8_t*)dst, 101 unit_to_s255_round(src[0]), unit_to_s255_round(src[1]), 102 unit_to_s255_round(src[2]), unit_to_s255_round(src[3])); 103 src += 4; 104 dst += 4; 105 } 106 count &= 3; 107 for (int i = 0; i < count; ++i) { 108 SkASSERT(src[i].isUnit()); 109 SkNx_cast<uint8_t>(unit_to_s255_round(src[i])).store((uint8_t*)&dst[i]); 110 } 111} 112 113static inline void SkPM4f_s32_srcover_mode(SkPMColor dst[], const SkPM4f src[], int count) { 114 for (int i = 0; i < count; ++i) { 115 SkASSERT(src[i].isUnit()); 116 Sk4f s4 = Sk4f::Load(src[i].fVec); 117 Sk4f d4 = Sk4f_fromS32(dst[i]); 118 dst[i] = Sk4f_toS32(s4 + d4 * Sk4f(1 - get_alpha(s4))); 119 } 120} 121 122