SkPMFloat.h revision a156a8ffbe1342a9c329e66ad1438934ac309d70
1/*
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#ifndef SkPM_DEFINED
9#define SkPM_DEFINED
10
11#include "SkTypes.h"
12#include "SkColor.h"
13#include "SkColorPriv.h"
14#include "SkNx.h"
15
16// A pre-multiplied color storing each component in the same order as SkPMColor,
17// but as a float in the range [0, 255].
18class SK_STRUCT_ALIGN(16) SkPMFloat {
19public:
20    static SkPMFloat FromPMColor(SkPMColor c) { return SkPMFloat(c); }
21    static SkPMFloat FromARGB(float a, float r, float g, float b) { return SkPMFloat(a,r,g,b); }
22
23    // May be more efficient than one at a time.  No special alignment assumed for SkPMColors.
24    static void From4PMColors(const SkPMColor[4], SkPMFloat*, SkPMFloat*, SkPMFloat*, SkPMFloat*);
25
26    // Uninitialized.
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
35
36
37    // Freely autoconvert between SkPMFloat and Sk4f.
38    /*implicit*/ SkPMFloat(const Sk4f& fs) { fColors = fs; }
39    /*implicit*/ operator Sk4f() const { return fColors; }
40
41    float a() const { return fColors.kth<SK_A32_SHIFT / 8>(); }
42    float r() const { return fColors.kth<SK_R32_SHIFT / 8>(); }
43    float g() const { return fColors.kth<SK_G32_SHIFT / 8>(); }
44    float b() const { return fColors.kth<SK_B32_SHIFT / 8>(); }
45
46    // N.B. All methods returning an SkPMColor call SkPMColorAssert on that result before returning.
47
48    // get() and clamped() round component values to the nearest integer.
49    SkPMColor     get() const;  // Assumes all values in [0, 255].  Some implementations may clamp.
50    SkPMColor clamped() const;  // Will clamp all values to [0, 255].
51
52    // Like get(), but truncates instead of rounding.
53    // The domain of this function is (-1.0f, 256.0f).  Values in (-1.0f, 0.0f] trunc to a zero.
54    SkPMColor trunc() const;
55
56    // 4-at-a-time versions of get() and clamped().  Like From4PMColors(), no alignment assumed.
57    static void To4PMColors(
58            const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, SkPMColor[4]);
59    static void ClampTo4PMColors(
60            const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, const SkPMFloat&, SkPMColor[4]);
61
62    bool isValid() const {
63        return this->a() >= 0 && this->a() <= 255
64            && this->r() >= 0 && this->r() <= this->a()
65            && this->g() >= 0 && this->g() <= this->a()
66            && this->b() >= 0 && this->b() <= this->a();
67    }
68
69private:
70    Sk4f fColors;
71};
72
73#ifdef SKNX_NO_SIMD
74    // Platform implementations of SkPMFloat assume Sk4f uses SSE or NEON.  _none is generic.
75    #include "../opts/SkPMFloat_none.h"
76#else
77    #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
78        #include "../opts/SkPMFloat_SSSE3.h"
79    #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
80        #include "../opts/SkPMFloat_SSE2.h"
81    #elif defined(SK_ARM_HAS_NEON)
82        #include "../opts/SkPMFloat_neon.h"
83    #else
84        #include "../opts/SkPMFloat_none.h"
85    #endif
86#endif
87
88#endif//SkPM_DEFINED
89