OLD | NEW |
1 #ifndef SkPM_DEFINED | 1 #ifndef SkPM_DEFINED |
2 #define SkPM_DEFINED | 2 #define SkPM_DEFINED |
3 | 3 |
4 #include "SkTypes.h" | 4 #include "SkTypes.h" |
5 #include "SkColor.h" | 5 #include "SkColor.h" |
6 #include "Sk4x.h" | 6 #include "Sk4x.h" |
7 | 7 |
8 // A pre-multiplied color storing each component as a float in the range [0, 255
]. | 8 // A pre-multiplied color storing each component in the same order as SkPMColor, |
| 9 // but as a float in the range [0, 255]. |
9 class SK_STRUCT_ALIGN(16) SkPMFloat { | 10 class SK_STRUCT_ALIGN(16) SkPMFloat { |
10 public: | 11 public: |
11 // Normal POD copies and do-nothing initialization. | 12 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } |
12 SkPMFloat() = default; | 13 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF
loat(a,r,g,b); } |
13 SkPMFloat(const SkPMFloat&) = default; | 14 |
14 SkPMFloat& operator=(const SkPMFloat&) = default; | 15 explicit SkPMFloat(SkPMColor); |
| 16 SkPMFloat(float a, float r, float g, float b) { |
| 17 // TODO: faster when specialized? |
| 18 fColor[SK_A32_SHIFT / 8] = a; |
| 19 fColor[SK_R32_SHIFT / 8] = r; |
| 20 fColor[SK_G32_SHIFT / 8] = g; |
| 21 fColor[SK_B32_SHIFT / 8] = b; |
| 22 } |
| 23 |
| 24 // Uninitialized. |
| 25 SkPMFloat() {} |
| 26 |
| 27 // Copy and assign are fastest if we remind the compiler we work best as Sk4
f. |
| 28 SkPMFloat(const SkPMFloat& that) { Sk4f(that).storeAligned(fColor); } |
| 29 SkPMFloat& operator=(const SkPMFloat& that) { |
| 30 Sk4f(that).storeAligned(fColor); |
| 31 return *this; |
| 32 } |
15 | 33 |
16 // Freely autoconvert between SkPMFloat and Sk4f. | 34 // Freely autoconvert between SkPMFloat and Sk4f. |
17 /*implicit*/ SkPMFloat(const Sk4f& fs) { fs.storeAligned(fColor); } | 35 /*implicit*/ SkPMFloat(const Sk4f& fs) { fs.storeAligned(fColor); } |
18 /*implicit*/ operator Sk4f() const { return Sk4f::LoadAligned(fColor); } | 36 /*implicit*/ operator Sk4f() const { return Sk4f::LoadAligned(fColor); } |
19 | 37 |
20 float a() const { return fColor[SK_A32_SHIFT / 8]; } | 38 float a() const { return fColor[SK_A32_SHIFT / 8]; } |
21 float r() const { return fColor[SK_R32_SHIFT / 8]; } | 39 float r() const { return fColor[SK_R32_SHIFT / 8]; } |
22 float g() const { return fColor[SK_G32_SHIFT / 8]; } | 40 float g() const { return fColor[SK_G32_SHIFT / 8]; } |
23 float b() const { return fColor[SK_B32_SHIFT / 8]; } | 41 float b() const { return fColor[SK_B32_SHIFT / 8]; } |
24 | 42 |
25 void setA(float val) { fColor[SK_A32_SHIFT / 8] = val; } | |
26 void setR(float val) { fColor[SK_R32_SHIFT / 8] = val; } | |
27 void setG(float val) { fColor[SK_G32_SHIFT / 8] = val; } | |
28 void setB(float val) { fColor[SK_B32_SHIFT / 8] = val; } | |
29 | |
30 void set(SkPMColor); | |
31 | |
32 // get() and clamped() round component values to the nearest integer. | 43 // get() and clamped() round component values to the nearest integer. |
33 SkPMColor get() const; // May SkASSERT(this->isValid()). Some implemen
tations may clamp. | 44 SkPMColor get() const; // May SkASSERT(this->isValid()). Some implemen
tations may clamp. |
34 SkPMColor clamped() const; // Will clamp all values to [0, 255]. Then may
assert isValid(). | 45 SkPMColor clamped() const; // Will clamp all values to [0, 255]. Then may
assert isValid(). |
35 | 46 |
36 bool isValid() const { | 47 bool isValid() const { |
37 return this->a() >= 0 && this->a() <= 255 | 48 return this->a() >= 0 && this->a() <= 255 |
38 && this->r() >= 0 && this->r() <= this->a() | 49 && this->r() >= 0 && this->r() <= this->a() |
39 && this->g() >= 0 && this->g() <= this->a() | 50 && this->g() >= 0 && this->g() <= this->a() |
40 && this->b() >= 0 && this->b() <= this->a(); | 51 && this->b() >= 0 && this->b() <= this->a(); |
41 } | 52 } |
42 | 53 |
43 private: | 54 private: |
44 // We mirror SkPMColor order only to make set()/get()/clamped() as fast as p
ossible. | |
45 float fColor[4]; | 55 float fColor[4]; |
46 }; | 56 }; |
47 | 57 |
48 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 | 58 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 |
49 #include "../opts/SkPMFloat_SSSE3.h" | 59 #include "../opts/SkPMFloat_SSSE3.h" |
50 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 | 60 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
51 #include "../opts/SkPMFloat_SSE2.h" | 61 #include "../opts/SkPMFloat_SSE2.h" |
52 #elif defined(__ARM_NEON__) | 62 #elif defined(__ARM_NEON__) |
53 #include "../opts/SkPMFloat_neon.h" | 63 #include "../opts/SkPMFloat_neon.h" |
54 #else | 64 #else |
55 #include "../opts/SkPMFloat_none.h" | 65 #include "../opts/SkPMFloat_none.h" |
56 #endif | 66 #endif |
57 | 67 |
58 #endif//SkPM_DEFINED | 68 #endif//SkPM_DEFINED |
OLD | NEW |