mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "SkUtils.h" |
| 9 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 10 | namespace { // See Sk4px.h |
| 11 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 12 | static_assert(sizeof(Sk4px) == 16, "This file uses memcpy / sk_memset32, so exact size matters."); |
| 13 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 14 | inline Sk4px Sk4px::DupPMColor(SkPMColor px) { |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 15 | Sk4px px4 = Sk16b(); |
| 16 | sk_memset32((uint32_t*)&px4, px, 4); |
| 17 | return px4; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 18 | } |
| 19 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 20 | inline Sk4px Sk4px::Load4(const SkPMColor px[4]) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 21 | Sk4px px4 = Sk16b(); |
| 22 | memcpy(&px4, px, 16); |
| 23 | return px4; |
| 24 | } |
| 25 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 26 | inline Sk4px Sk4px::Load2(const SkPMColor px[2]) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 27 | Sk4px px2 = Sk16b(); |
| 28 | memcpy(&px2, px, 8); |
| 29 | return px2; |
| 30 | } |
| 31 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 32 | inline Sk4px Sk4px::Load1(const SkPMColor px[1]) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 33 | Sk4px px1 = Sk16b(); |
| 34 | memcpy(&px1, px, 4); |
| 35 | return px1; |
| 36 | } |
| 37 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 38 | inline void Sk4px::store4(SkPMColor px[4]) const { memcpy(px, this, 16); } |
| 39 | inline void Sk4px::store2(SkPMColor px[2]) const { memcpy(px, this, 8); } |
| 40 | inline void Sk4px::store1(SkPMColor px[1]) const { memcpy(px, this, 4); } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 41 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 42 | inline Sk4px::Wide Sk4px::widenLo() const { |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame^] | 43 | return Sk16h((*this)[ 0], (*this)[ 1], (*this)[ 2], (*this)[ 3], |
| 44 | (*this)[ 4], (*this)[ 5], (*this)[ 6], (*this)[ 7], |
| 45 | (*this)[ 8], (*this)[ 9], (*this)[10], (*this)[11], |
| 46 | (*this)[12], (*this)[13], (*this)[14], (*this)[15]); |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 49 | inline Sk4px::Wide Sk4px::widenHi() const { return this->widenLo() << 8; } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 50 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 51 | inline Sk4px::Wide Sk4px::widenLoHi() const { return this->widenLo() + this->widenHi(); } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 52 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 53 | inline Sk4px::Wide Sk4px::mulWiden(const Sk16b& other) const { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 54 | return this->widenLo() * Sk4px(other).widenLo(); |
| 55 | } |
| 56 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 57 | inline Sk4px Sk4px::Wide::addNarrowHi(const Sk16h& other) const { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 58 | Sk4px::Wide r = (*this + other) >> 8; |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame^] | 59 | return Sk16b(r[ 0], r[ 1], r[ 2], r[ 3], |
| 60 | r[ 4], r[ 5], r[ 6], r[ 7], |
| 61 | r[ 8], r[ 9], r[10], r[11], |
| 62 | r[12], r[13], r[14], r[15]); |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 63 | } |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 64 | |
mtklein | cbf4fba | 2015-11-17 14:19:52 -0800 | [diff] [blame] | 65 | inline Sk4px Sk4px::Wide::div255() const { |
| 66 | // Calculated as ((x+128) + ((x+128)>>8)) >> 8. |
| 67 | auto v = *this + Sk16h(128); |
| 68 | return v.addNarrowHi(v>>8); |
| 69 | } |
| 70 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 71 | inline Sk4px Sk4px::alphas() const { |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 72 | static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian."); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame^] | 73 | return Sk16b((*this)[ 3], (*this)[ 3], (*this)[ 3], (*this)[ 3], |
| 74 | (*this)[ 7], (*this)[ 7], (*this)[ 7], (*this)[ 7], |
| 75 | (*this)[11], (*this)[11], (*this)[11], (*this)[11], |
| 76 | (*this)[15], (*this)[15], (*this)[15], (*this)[15]); |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 79 | inline Sk4px Sk4px::Load4Alphas(const SkAlpha a[4]) { |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 80 | return Sk16b(a[0], a[0], a[0], a[0], |
| 81 | a[1], a[1], a[1], a[1], |
| 82 | a[2], a[2], a[2], a[2], |
| 83 | a[3], a[3], a[3], a[3]); |
| 84 | } |
| 85 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 86 | inline Sk4px Sk4px::Load2Alphas(const SkAlpha a[2]) { |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 87 | return Sk16b(a[0], a[0], a[0], a[0], |
| 88 | a[1], a[1], a[1], a[1], |
| 89 | 0,0,0,0, |
| 90 | 0,0,0,0); |
| 91 | } |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 92 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 93 | inline Sk4px Sk4px::zeroAlphas() const { |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 94 | static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian."); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame^] | 95 | return Sk16b((*this)[ 0], (*this)[ 1], (*this)[ 2], 0, |
| 96 | (*this)[ 4], (*this)[ 5], (*this)[ 6], 0, |
| 97 | (*this)[ 8], (*this)[ 9], (*this)[10], 0, |
| 98 | (*this)[12], (*this)[13], (*this)[14], 0); |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 101 | inline Sk4px Sk4px::zeroColors() const { |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 102 | static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian."); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame^] | 103 | return Sk16b(0,0,0, (*this)[ 3], |
| 104 | 0,0,0, (*this)[ 7], |
| 105 | 0,0,0, (*this)[11], |
| 106 | 0,0,0, (*this)[15]); |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 107 | } |
| 108 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 109 | } // namespace |