Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: src/core/SkPMFloat.h

Issue 1308903003: Templatize SkPMFloat to support both 1 and 255 biases. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: pump the loops for Android Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkNx.h" 14 #include "SkNx.h"
15 15
16 // This file may be included multiple times by .cpp files with different flags, leading 16 // This file may be included multiple times by .cpp files with different flags, leading
17 // to different definitions. Usually that doesn't matter because it's all inlin ed, but 17 // to different definitions. Usually that doesn't matter because it's all inlin ed, but
18 // in Debug modes the compilers may not inline everything. So wrap everything i n an 18 // in Debug modes the compilers may not inline everything. So wrap everything i n an
19 // anonymous namespace to give each includer their own silo of this code (or the linker 19 // anonymous namespace to give each includer their own silo of this code (or the linker
20 // will probably pick one randomly for us, which is rarely correct). 20 // will probably pick one randomly for us, which is rarely correct).
21 namespace { 21 namespace {
22 22
23 // A pre-multiplied color storing each component in the same order as SkPMColor, 23 // A pre-multiplied color storing each component in the same order as SkPMColor,
24 // but as a float in the range [0, 1]. 24 // but as a float in the range [0, kBias],
25 // where kBias is either 1 (easier to work with) or 255 (sometimes a little fast er).
26 template <int kBias>
25 class SkPMFloat : public Sk4f { 27 class SkPMFloat : public Sk4f {
28 static_assert(kBias == 1 || kBias == 255, "");
26 public: 29 public:
27 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } 30 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); }
28 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF loat(a,r,g,b); } 31 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF loat(a,r,g,b); }
29 static SkPMFloat FromOpaqueColor(SkColor c); // Requires c's alpha == 0xFF. 32 static SkPMFloat FromOpaqueColor(SkColor c); // Requires c's alpha == 0xFF.
30 33
31 Sk4f alphas() const; // argb -> aaaa, generally faster than the equivalent Sk4f(this->a()). 34 Sk4f alphas() const; // argb -> aaaa, generally faster than the equivalent Sk4f(this->a()).
32 35
33 // Uninitialized. 36 // Uninitialized.
34 SkPMFloat() {} 37 SkPMFloat() {}
35 explicit SkPMFloat(SkPMColor); 38 explicit SkPMFloat(SkPMColor);
36 SkPMFloat(float a, float r, float g, float b) 39 SkPMFloat(float a, float r, float g, float b)
37 #ifdef SK_PMCOLOR_IS_RGBA 40 #ifdef SK_PMCOLOR_IS_RGBA
38 : INHERITED(r,g,b,a) {} 41 : INHERITED(r,g,b,a) {}
39 #else 42 #else
40 : INHERITED(b,g,r,a) {} 43 : INHERITED(b,g,r,a) {}
41 #endif 44 #endif
42 45
43 SkPMFloat(const Sk4f& fs) : INHERITED(fs) {} 46 SkPMFloat(const Sk4f& fs) : INHERITED(fs) {}
44 47
45 float a() const { return this->kth<SK_A32_SHIFT / 8>(); } 48 float a() const { return this->kth<SK_A32_SHIFT / 8>(); }
46 float r() const { return this->kth<SK_R32_SHIFT / 8>(); } 49 float r() const { return this->kth<SK_R32_SHIFT / 8>(); }
47 float g() const { return this->kth<SK_G32_SHIFT / 8>(); } 50 float g() const { return this->kth<SK_G32_SHIFT / 8>(); }
48 float b() const { return this->kth<SK_B32_SHIFT / 8>(); } 51 float b() const { return this->kth<SK_B32_SHIFT / 8>(); }
49 52
50 SkPMColor round() const; // Rounds from [0.0f, 1.0f] to [0, 255], clamping if out of range. 53 SkPMColor round() const; // Rounds from [0, kBias] to [0, 255], clamping if out of range.
51 54
52 bool isValid() const { 55 bool isValid() const {
53 return this->a() >= 0 && this->a() <= 1 56 return this->a() >= 0 && this->a() <= kBias
54 && this->r() >= 0 && this->r() <= this->a() 57 && this->r() >= 0 && this->r() <= this->a()
55 && this->g() >= 0 && this->g() <= this->a() 58 && this->g() >= 0 && this->g() <= this->a()
56 && this->b() >= 0 && this->b() <= this->a(); 59 && this->b() >= 0 && this->b() <= this->a();
57 } 60 }
58 61
59 private: 62 private:
60 typedef Sk4f INHERITED; 63 typedef Sk4f INHERITED;
61 }; 64 };
62 65
63 } // namespace 66 } // namespace
64 67
65 #ifdef SKNX_NO_SIMD 68 #ifdef SKNX_NO_SIMD
66 // Platform implementations of SkPMFloat assume Sk4f uses SSE or NEON. _non e is generic. 69 // Platform implementations of SkPMFloat assume Sk4f uses SSE or NEON. _non e is generic.
67 #include "../opts/SkPMFloat_none.h" 70 #include "../opts/SkPMFloat_none.h"
68 #else 71 #else
69 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 72 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
70 #include "../opts/SkPMFloat_sse.h" 73 #include "../opts/SkPMFloat_sse.h"
71 #elif defined(SK_ARM_HAS_NEON) 74 #elif defined(SK_ARM_HAS_NEON)
72 #include "../opts/SkPMFloat_neon.h" 75 #include "../opts/SkPMFloat_neon.h"
73 #else 76 #else
74 #include "../opts/SkPMFloat_none.h" 77 #include "../opts/SkPMFloat_none.h"
75 #endif 78 #endif
76 #endif 79 #endif
77 80
78 #endif//SkPM_DEFINED 81 #endif//SkPM_DEFINED
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698