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 #ifndef SkPM_DEFINED | 8 #ifndef SkPM_DEFINED |
9 #define SkPM_DEFINED | 9 #define SkPM_DEFINED |
10 | 10 |
11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
12 #include "SkColor.h" | 12 #include "SkColor.h" |
13 #include "SkColorPriv.h" | 13 #include "SkColorPriv.h" |
14 #include "Sk4x.h" | 14 #include "SkNx.h" |
15 | |
16 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | |
17 #include <immintrin.h> | |
18 #elif defined(SK_ARM_HAS_NEON) | |
19 #include <arm_neon.h> | |
20 #endif | |
21 | 15 |
22 // A pre-multiplied color storing each component in the same order as SkPMColor, | 16 // A pre-multiplied color storing each component in the same order as SkPMColor, |
23 // but as a float in the range [0, 255]. | 17 // but as a float in the range [0, 255]. |
24 class SK_STRUCT_ALIGN(16) SkPMFloat { | 18 class SK_STRUCT_ALIGN(16) SkPMFloat { |
25 public: | 19 public: |
26 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } | 20 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } |
27 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF
loat(a,r,g,b); } | 21 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF
loat(a,r,g,b); } |
28 | 22 |
29 // May be more efficient than one at a time. No special alignment assumed f
or SkPMColors. | 23 // May be more efficient than one at a time. No special alignment assumed f
or SkPMColors. |
30 static void From4PMColors(const SkPMColor[4], SkPMFloat*, SkPMFloat*, SkPMFl
oat*, SkPMFloat*); | 24 static void From4PMColors(const SkPMColor[4], SkPMFloat*, SkPMFloat*, SkPMFl
oat*, SkPMFloat*); |
31 | 25 |
32 explicit SkPMFloat(SkPMColor); | |
33 SkPMFloat(float a, float r, float g, float b) { | |
34 // TODO: faster when specialized? | |
35 fColor[SK_A32_SHIFT / 8] = a; | |
36 fColor[SK_R32_SHIFT / 8] = r; | |
37 fColor[SK_G32_SHIFT / 8] = g; | |
38 fColor[SK_B32_SHIFT / 8] = b; | |
39 } | |
40 | |
41 // Uninitialized. | 26 // Uninitialized. |
42 SkPMFloat() {} | 27 SkPMFloat() {} |
| 28 explicit SkPMFloat(SkPMColor); |
| 29 SkPMFloat(float a, float r, float g, float b) |
| 30 #ifdef SK_PMCOLOR_IS_RGBA |
| 31 : fColors(r,g,b,a) {} |
| 32 #else |
| 33 : fColors(b,g,r,a) {} |
| 34 #endif |
43 | 35 |
44 SkPMFloat(const SkPMFloat& that) { *this = that; } | |
45 SkPMFloat& operator=(const SkPMFloat& that); | |
46 | 36 |
47 // Freely autoconvert between SkPMFloat and Sk4f. They're always byte-for-b
yte identical. | 37 // Freely autoconvert between SkPMFloat and Sk4s. |
48 /*implicit*/ SkPMFloat(const Sk4f& fs) { fs.storeAligned(fColor); } | 38 /*implicit*/ SkPMFloat(const Sk4s& fs) { fColors = fs; } |
49 /*implicit*/ operator Sk4f() const { return Sk4f::LoadAligned(fColor); } | 39 /*implicit*/ operator Sk4s() const { return fColors; } |
50 | 40 |
51 float a() const { return fColor[SK_A32_SHIFT / 8]; } | 41 float a() const { return fColors[SK_A32_SHIFT / 8]; } |
52 float r() const { return fColor[SK_R32_SHIFT / 8]; } | 42 float r() const { return fColors[SK_R32_SHIFT / 8]; } |
53 float g() const { return fColor[SK_G32_SHIFT / 8]; } | 43 float g() const { return fColors[SK_G32_SHIFT / 8]; } |
54 float b() const { return fColor[SK_B32_SHIFT / 8]; } | 44 float b() const { return fColors[SK_B32_SHIFT / 8]; } |
55 | 45 |
56 // get() and clamped() round component values to the nearest integer. | 46 // get() and clamped() round component values to the nearest integer. |
57 SkPMColor get() const; // May SkASSERT(this->isValid()). Some implemen
tations may clamp. | 47 SkPMColor get() const; // May SkASSERT(this->isValid()). Some implemen
tations may clamp. |
58 SkPMColor clamped() const; // Will clamp all values to [0, 255]. Then may
assert isValid(). | 48 SkPMColor clamped() const; // Will clamp all values to [0, 255]. Then may
assert isValid(). |
59 | 49 |
60 // Like get(), but truncates instead of rounding. | 50 // Like get(), but truncates instead of rounding. |
61 // The domain of this function is (-1.0f, 256.0f). Values in (-1.0f, 0.0f]
trunc to a zero. | 51 // The domain of this function is (-1.0f, 256.0f). Values in (-1.0f, 0.0f]
trunc to a zero. |
62 SkPMColor trunc() const; | 52 SkPMColor trunc() const; |
63 | 53 |
64 // 4-at-a-time versions of get() and clamped(). Like From4PMColors(), no al
ignment assumed. | 54 // 4-at-a-time versions of get() and clamped(). Like From4PMColors(), no al
ignment assumed. |
65 static void To4PMColors( | 55 static void To4PMColors( |
66 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa
t&, SkPMColor[4]); | 56 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa
t&, SkPMColor[4]); |
67 static void ClampTo4PMColors( | 57 static void ClampTo4PMColors( |
68 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa
t&, SkPMColor[4]); | 58 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa
t&, SkPMColor[4]); |
69 | 59 |
70 bool isValid() const { | 60 bool isValid() const { |
71 return this->a() >= 0 && this->a() <= 255 | 61 return this->a() >= 0 && this->a() <= 255 |
72 && this->r() >= 0 && this->r() <= this->a() | 62 && this->r() >= 0 && this->r() <= this->a() |
73 && this->g() >= 0 && this->g() <= this->a() | 63 && this->g() >= 0 && this->g() <= this->a() |
74 && this->b() >= 0 && this->b() <= this->a(); | 64 && this->b() >= 0 && this->b() <= this->a(); |
75 } | 65 } |
76 | 66 |
77 private: | 67 private: |
78 union { | 68 Sk4s fColors; |
79 float fColor[4]; | |
80 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | |
81 __m128 fColors; | |
82 #elif defined(SK_ARM_HAS_NEON) | |
83 float32x4_t fColors; | |
84 #endif | |
85 }; | |
86 }; | 69 }; |
87 | 70 |
88 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 | 71 #ifdef SKNX_NO_SIMD |
89 #include "../opts/SkPMFloat_SSSE3.h" | 72 // Platform implementations of SkPMFloat assume Sk4s uses SSE or NEON. _non
e is generic. |
90 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | 73 #include "../opts/SkPMFloat_none.h" |
91 #include "../opts/SkPMFloat_SSE2.h" | |
92 #elif defined(SK_ARM_HAS_NEON) | |
93 #include "../opts/SkPMFloat_neon.h" | |
94 #else | 74 #else |
95 #include "../opts/SkPMFloat_none.h" | 75 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
| 76 #include "../opts/SkPMFloat_SSSE3.h" |
| 77 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 78 #include "../opts/SkPMFloat_SSE2.h" |
| 79 #elif defined(SK_ARM_HAS_NEON) |
| 80 #include "../opts/SkPMFloat_neon.h" |
| 81 #else |
| 82 #include "../opts/SkPMFloat_none.h" |
| 83 #endif |
96 #endif | 84 #endif |
97 | 85 |
98 #endif//SkPM_DEFINED | 86 #endif//SkPM_DEFINED |
OLD | NEW |