blob: 540edb821dd6d4f43f7fa187c067585e832febbe [file] [log] [blame]
mtkleind2ffd362015-05-12 06:11:21 -07001/*
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
mtklein082e3292015-08-12 11:56:43 -070010namespace { // See Sk4px.h
11
mtkleind2ffd362015-05-12 06:11:21 -070012static_assert(sizeof(Sk4px) == 16, "This file uses memcpy / sk_memset32, so exact size matters.");
13
mtklein082e3292015-08-12 11:56:43 -070014inline Sk4px Sk4px::DupPMColor(SkPMColor px) {
mtklein059ac002015-06-22 10:39:38 -070015 Sk4px px4 = Sk16b();
16 sk_memset32((uint32_t*)&px4, px, 4);
17 return px4;
mtkleind2ffd362015-05-12 06:11:21 -070018}
19
mtklein082e3292015-08-12 11:56:43 -070020inline Sk4px Sk4px::Load4(const SkPMColor px[4]) {
mtkleind2ffd362015-05-12 06:11:21 -070021 Sk4px px4 = Sk16b();
22 memcpy(&px4, px, 16);
23 return px4;
24}
25
mtklein082e3292015-08-12 11:56:43 -070026inline Sk4px Sk4px::Load2(const SkPMColor px[2]) {
mtkleind2ffd362015-05-12 06:11:21 -070027 Sk4px px2 = Sk16b();
28 memcpy(&px2, px, 8);
29 return px2;
30}
31
mtklein082e3292015-08-12 11:56:43 -070032inline Sk4px Sk4px::Load1(const SkPMColor px[1]) {
mtkleind2ffd362015-05-12 06:11:21 -070033 Sk4px px1 = Sk16b();
34 memcpy(&px1, px, 4);
35 return px1;
36}
37
mtklein082e3292015-08-12 11:56:43 -070038inline void Sk4px::store4(SkPMColor px[4]) const { memcpy(px, this, 16); }
39inline void Sk4px::store2(SkPMColor px[2]) const { memcpy(px, this, 8); }
40inline void Sk4px::store1(SkPMColor px[1]) const { memcpy(px, this, 4); }
mtkleind2ffd362015-05-12 06:11:21 -070041
mtklein082e3292015-08-12 11:56:43 -070042inline Sk4px::Wide Sk4px::widenLo() const {
mtkleind2ffd362015-05-12 06:11:21 -070043 return Sk16h(this->kth< 0>(), this->kth< 1>(), this->kth< 2>(), this->kth< 3>(),
44 this->kth< 4>(), this->kth< 5>(), this->kth< 6>(), this->kth< 7>(),
45 this->kth< 8>(), this->kth< 9>(), this->kth<10>(), this->kth<11>(),
46 this->kth<12>(), this->kth<13>(), this->kth<14>(), this->kth<15>());
47}
48
mtklein082e3292015-08-12 11:56:43 -070049inline Sk4px::Wide Sk4px::widenHi() const { return this->widenLo() << 8; }
mtkleind2ffd362015-05-12 06:11:21 -070050
mtklein082e3292015-08-12 11:56:43 -070051inline Sk4px::Wide Sk4px::widenLoHi() const { return this->widenLo() + this->widenHi(); }
mtklein4be181e2015-07-14 10:54:19 -070052
mtklein082e3292015-08-12 11:56:43 -070053inline Sk4px::Wide Sk4px::mulWiden(const Sk16b& other) const {
mtkleind2ffd362015-05-12 06:11:21 -070054 return this->widenLo() * Sk4px(other).widenLo();
55}
56
mtklein082e3292015-08-12 11:56:43 -070057inline Sk4px Sk4px::Wide::addNarrowHi(const Sk16h& other) const {
mtkleind2ffd362015-05-12 06:11:21 -070058 Sk4px::Wide r = (*this + other) >> 8;
59 return Sk16b(r.kth< 0>(), r.kth< 1>(), r.kth< 2>(), r.kth< 3>(),
60 r.kth< 4>(), r.kth< 5>(), r.kth< 6>(), r.kth< 7>(),
61 r.kth< 8>(), r.kth< 9>(), r.kth<10>(), r.kth<11>(),
62 r.kth<12>(), r.kth<13>(), r.kth<14>(), r.kth<15>());
63}
mtklein8a90edc2015-05-13 12:19:42 -070064
mtklein082e3292015-08-12 11:56:43 -070065inline Sk4px Sk4px::alphas() const {
mtklein8a90edc2015-05-13 12:19:42 -070066 static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
67 return Sk16b(this->kth< 3>(), this->kth< 3>(), this->kth< 3>(), this->kth< 3>(),
68 this->kth< 7>(), this->kth< 7>(), this->kth< 7>(), this->kth< 7>(),
69 this->kth<11>(), this->kth<11>(), this->kth<11>(), this->kth<11>(),
70 this->kth<15>(), this->kth<15>(), this->kth<15>(), this->kth<15>());
71}
72
mtklein082e3292015-08-12 11:56:43 -070073inline Sk4px Sk4px::Load4Alphas(const SkAlpha a[4]) {
mtklein8a90edc2015-05-13 12:19:42 -070074 return Sk16b(a[0], a[0], a[0], a[0],
75 a[1], a[1], a[1], a[1],
76 a[2], a[2], a[2], a[2],
77 a[3], a[3], a[3], a[3]);
78}
79
mtklein082e3292015-08-12 11:56:43 -070080inline Sk4px Sk4px::Load2Alphas(const SkAlpha a[2]) {
mtklein8a90edc2015-05-13 12:19:42 -070081 return Sk16b(a[0], a[0], a[0], a[0],
82 a[1], a[1], a[1], a[1],
83 0,0,0,0,
84 0,0,0,0);
85}
mtklein0135a412015-05-15 10:36:21 -070086
mtklein082e3292015-08-12 11:56:43 -070087inline Sk4px Sk4px::zeroAlphas() const {
mtklein0135a412015-05-15 10:36:21 -070088 static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
89 return Sk16b(this->kth< 0>(), this->kth< 1>(), this->kth< 2>(), 0,
90 this->kth< 4>(), this->kth< 5>(), this->kth< 6>(), 0,
91 this->kth< 8>(), this->kth< 9>(), this->kth<10>(), 0,
92 this->kth<12>(), this->kth<13>(), this->kth<14>(), 0);
93}
94
mtklein082e3292015-08-12 11:56:43 -070095inline Sk4px Sk4px::zeroColors() const {
mtklein0135a412015-05-15 10:36:21 -070096 static_assert(SK_A32_SHIFT == 24, "This method assumes little-endian.");
97 return Sk16b(0,0,0, this->kth< 3>(),
98 0,0,0, this->kth< 7>(),
99 0,0,0, this->kth<11>(),
100 0,0,0, this->kth<15>());
101}
102
mtklein082e3292015-08-12 11:56:43 -0700103inline Sk4px Sk4px::Load4(const SkPMColor16 src[4]) {
mtkleinced15852015-07-22 10:52:53 -0700104 SkPMColor src32[4];
105 for (int i = 0; i < 4; i++) { src32[i] = SkPixel16ToPixel32(src[i]); }
106 return Load4(src32);
107}
mtklein082e3292015-08-12 11:56:43 -0700108inline Sk4px Sk4px::Load2(const SkPMColor16 src[2]) {
mtkleinced15852015-07-22 10:52:53 -0700109 SkPMColor src32[2];
110 for (int i = 0; i < 2; i++) { src32[i] = SkPixel16ToPixel32(src[i]); }
111 return Load2(src32);
112}
mtklein082e3292015-08-12 11:56:43 -0700113inline Sk4px Sk4px::Load1(const SkPMColor16 src[1]) {
mtkleinced15852015-07-22 10:52:53 -0700114 SkPMColor src32 = SkPixel16ToPixel32(src[0]);
115 return Load1(&src32);
116}
117
mtklein082e3292015-08-12 11:56:43 -0700118inline void Sk4px::store4(SkPMColor16 dst[4]) const {
mtkleinced15852015-07-22 10:52:53 -0700119 SkPMColor dst32[4];
120 this->store4(dst32);
121 for (int i = 0; i < 4; i++) { dst[i] = SkPixel32ToPixel16(dst32[i]); }
122}
mtklein082e3292015-08-12 11:56:43 -0700123inline void Sk4px::store2(SkPMColor16 dst[2]) const {
mtkleinced15852015-07-22 10:52:53 -0700124 SkPMColor dst32[2];
125 this->store2(dst32);
126 for (int i = 0; i < 2; i++) { dst[i] = SkPixel32ToPixel16(dst32[i]); }
127}
mtklein082e3292015-08-12 11:56:43 -0700128inline void Sk4px::store1(SkPMColor16 dst[1]) const {
mtkleinced15852015-07-22 10:52:53 -0700129 SkPMColor dst32;
130 this->store1(&dst32);
131 dst[0] = SkPixel32ToPixel16(dst32);
132}
mtklein082e3292015-08-12 11:56:43 -0700133
134} // namespace