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

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

Issue 1201343004: Convert SkPMFloat to [0,1] range and prune its API. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tweaks Created 5 years, 6 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
« no previous file with comments | « bench/PMFloatBench.cpp ('k') | src/effects/SkColorMatrixFilter.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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, 255]. 24 // but as a float in the range [0, 1].
25 class SkPMFloat : public Sk4f { 25 class SkPMFloat : public Sk4f {
26 public: 26 public:
27 static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); } 27 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); } 28 static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMF loat(a,r,g,b); }
29 29
30 // May be more efficient than one at a time. No special alignment assumed f or SkPMColors.
31 static void From4PMColors(const SkPMColor[4], SkPMFloat*, SkPMFloat*, SkPMFl oat*, SkPMFloat*);
32
33 // Uninitialized. 30 // Uninitialized.
34 SkPMFloat() {} 31 SkPMFloat() {}
35 explicit SkPMFloat(SkPMColor); 32 explicit SkPMFloat(SkPMColor);
36 SkPMFloat(float a, float r, float g, float b) 33 SkPMFloat(float a, float r, float g, float b)
37 #ifdef SK_PMCOLOR_IS_RGBA 34 #ifdef SK_PMCOLOR_IS_RGBA
38 : INHERITED(r,g,b,a) {} 35 : INHERITED(r,g,b,a) {}
39 #else 36 #else
40 : INHERITED(b,g,r,a) {} 37 : INHERITED(b,g,r,a) {}
41 #endif 38 #endif
42 39
43 SkPMFloat(const Sk4f& fs) : INHERITED(fs) {} 40 SkPMFloat(const Sk4f& fs) : INHERITED(fs) {}
44 41
45 float a() const { return this->kth<SK_A32_SHIFT / 8>(); } 42 float a() const { return this->kth<SK_A32_SHIFT / 8>(); }
46 float r() const { return this->kth<SK_R32_SHIFT / 8>(); } 43 float r() const { return this->kth<SK_R32_SHIFT / 8>(); }
47 float g() const { return this->kth<SK_G32_SHIFT / 8>(); } 44 float g() const { return this->kth<SK_G32_SHIFT / 8>(); }
48 float b() const { return this->kth<SK_B32_SHIFT / 8>(); } 45 float b() const { return this->kth<SK_B32_SHIFT / 8>(); }
49 46
50 // N.B. All methods returning an SkPMColor call SkPMColorAssert on that resu lt before returning. 47 SkPMColor round() const; // Rounds from [0.0f, 1.0f] to [0, 255], clamping if out of range.
51
52 // round() and roundClamp() round component values to the nearest integer.
53 SkPMColor round() const; // Assumes all values in [0, 255]. Some implement ations may clamp.
54 SkPMColor roundClamp() const; // Will clamp all values to [0, 255].
55
56 // Like round(), but truncates instead of rounding.
57 // The domain of this function is (-1.0f, 256.0f). Values in (-1.0f, 0.0f] trunc to a zero.
58 SkPMColor trunc() const;
59
60 // 4-at-a-time versions of round() and roundClamp(). Like From4PMColors(), n o alignment assumed.
61 static void RoundTo4PMColors(
62 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa t&, SkPMColor[4]);
63 static void RoundClampTo4PMColors(
64 const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloa t&, SkPMColor[4]);
65 48
66 bool isValid() const { 49 bool isValid() const {
67 return this->a() >= 0 && this->a() <= 255 50 return this->a() >= 0 && this->a() <= 1
68 && this->r() >= 0 && this->r() <= this->a() 51 && this->r() >= 0 && this->r() <= this->a()
69 && this->g() >= 0 && this->g() <= this->a() 52 && this->g() >= 0 && this->g() <= this->a()
70 && this->b() >= 0 && this->b() <= this->a(); 53 && this->b() >= 0 && this->b() <= this->a();
71 } 54 }
72 55
73 private: 56 private:
74 typedef Sk4f INHERITED; 57 typedef Sk4f INHERITED;
75 }; 58 };
76 59
77 } // namespace 60 } // namespace
78 61
79 #ifdef SKNX_NO_SIMD 62 #ifdef SKNX_NO_SIMD
80 // Platform implementations of SkPMFloat assume Sk4f uses SSE or NEON. _non e is generic. 63 // Platform implementations of SkPMFloat assume Sk4f uses SSE or NEON. _non e is generic.
81 #include "../opts/SkPMFloat_none.h" 64 #include "../opts/SkPMFloat_none.h"
82 #else 65 #else
83 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3 66 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
84 #include "../opts/SkPMFloat_SSSE3.h" 67 #include "../opts/SkPMFloat_sse.h"
85 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
86 #include "../opts/SkPMFloat_SSE2.h"
87 #elif defined(SK_ARM_HAS_NEON) 68 #elif defined(SK_ARM_HAS_NEON)
88 #include "../opts/SkPMFloat_neon.h" 69 #include "../opts/SkPMFloat_neon.h"
89 #else 70 #else
90 #include "../opts/SkPMFloat_none.h" 71 #include "../opts/SkPMFloat_none.h"
91 #endif 72 #endif
92 #endif 73 #endif
93 74
94 #endif//SkPM_DEFINED 75 #endif//SkPM_DEFINED
OLDNEW
« no previous file with comments | « bench/PMFloatBench.cpp ('k') | src/effects/SkColorMatrixFilter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698