OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkColorTable_DEFINED | 10 #ifndef SkColorTable_DEFINED |
11 #define SkColorTable_DEFINED | 11 #define SkColorTable_DEFINED |
12 | 12 |
| 13 #include "SkAlpha.h" |
13 #include "SkColor.h" | 14 #include "SkColor.h" |
14 #include "SkFlattenable.h" | 15 #include "SkFlattenable.h" |
15 | 16 |
16 /** \class SkColorTable | 17 /** \class SkColorTable |
17 | 18 |
18 SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by | 19 SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by |
19 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the co
lortable. | 20 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the co
lortable. |
20 */ | 21 */ |
21 class SkColorTable : public SkFlattenable { | 22 class SkColorTable : public SkFlattenable { |
22 public: | 23 public: |
23 SK_DECLARE_INST_COUNT(SkColorTable) | 24 SK_DECLARE_INST_COUNT(SkColorTable) |
24 | 25 |
25 /** Makes a deep copy of colors. | 26 /** Makes a deep copy of colors. |
26 */ | 27 */ |
27 SkColorTable(const SkColorTable& src); | 28 SkColorTable(const SkColorTable& src); |
28 /** Preallocates the colortable to have 'count' colors, which | 29 SkColorTable(const SkPMColor colors[], int count, |
29 * are initially set to 0. | 30 SkAlphaType alphaType = kPremul_SkAlphaType); |
30 */ | |
31 explicit SkColorTable(int count); | |
32 SkColorTable(const SkPMColor colors[], int count); | |
33 virtual ~SkColorTable(); | 31 virtual ~SkColorTable(); |
34 | 32 |
35 enum Flags { | 33 SkAlphaType alphaType() const { return (SkAlphaType)fAlphaType; } |
36 kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the ta
ble are opaque (alpha==0xFF) | |
37 }; | |
38 /** Returns the flag bits for the color table. These can be changed with set
Flags(). | |
39 */ | |
40 unsigned getFlags() const { return fFlags; } | |
41 /** Set the flags for the color table. See the Flags enum for possible value
s. | |
42 */ | |
43 void setFlags(unsigned flags); | |
44 | 34 |
45 bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; } | 35 bool isOpaque() const { |
46 void setIsOpaque(bool isOpaque); | 36 return SkAlphaTypeIsOpaque(this->alphaType()); |
| 37 } |
47 | 38 |
48 /** Returns the number of colors in the table. | 39 /** Returns the number of colors in the table. |
49 */ | 40 */ |
50 int count() const { return fCount; } | 41 int count() const { return fCount; } |
51 | 42 |
52 /** Returns the specified color from the table. In the debug build, this ass
erts that | 43 /** Returns the specified color from the table. In the debug build, this ass
erts that |
53 the index is in range (0 <= index < count). | 44 the index is in range (0 <= index < count). |
54 */ | 45 */ |
55 SkPMColor operator[](int index) const { | 46 SkPMColor operator[](int index) const { |
56 SkASSERT(fColors != NULL && (unsigned)index < fCount); | 47 SkASSERT(fColors != NULL && (unsigned)index < fCount); |
57 return fColors[index]; | 48 return fColors[index]; |
58 } | 49 } |
59 | 50 |
60 /** Specify the number of colors in the color table. This does not initializ
e the colors | 51 /** |
61 to any value, just allocates memory for them. To initialize the values,
either call | 52 * Return the array of colors for reading. This must be balanced by a call |
62 setColors(array, count), or follow setCount(count) with a call to | 53 * to unlockColors(). |
63 lockColors()/{set the values}/unlockColors(true). | 54 */ |
64 */ | 55 const SkPMColor* lockColors() { |
65 // void setColors(int count) { this->setColors(NULL, count); } | |
66 // void setColors(const SkPMColor[], int count); | |
67 | |
68 /** Return the array of colors for reading and/or writing. This must be | |
69 balanced by a call to unlockColors(changed?), telling the colortable if | |
70 the colors were changed during the lock. | |
71 */ | |
72 SkPMColor* lockColors() { | |
73 SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);) | 56 SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);) |
74 return fColors; | 57 return fColors; |
75 } | 58 } |
76 /** Balancing call to lockColors(). If the colors have been changed, pass tr
ue. | 59 |
77 */ | 60 /** |
78 void unlockColors(bool changed); | 61 * Balancing call to lockColors(). |
| 62 */ |
| 63 void unlockColors(); |
79 | 64 |
80 /** Similar to lockColors(), lock16BitCache() returns the array of | 65 /** Similar to lockColors(), lock16BitCache() returns the array of |
81 RGB16 colors that mirror the 32bit colors. However, this function | 66 RGB16 colors that mirror the 32bit colors. However, this function |
82 will return null if kColorsAreOpaque_Flag is not set. | 67 will return null if kColorsAreOpaque_Flag is not set. |
83 Also, unlike lockColors(), the returned array here cannot be modified. | 68 Also, unlike lockColors(), the returned array here cannot be modified. |
84 */ | 69 */ |
85 const uint16_t* lock16BitCache(); | 70 const uint16_t* lock16BitCache(); |
86 /** Balancing call to lock16BitCache(). | 71 /** Balancing call to lock16BitCache(). |
87 */ | 72 */ |
88 void unlock16BitCache() { | 73 void unlock16BitCache() { |
89 SkASSERT(f16BitCacheLockCount > 0); | 74 SkASSERT(f16BitCacheLockCount > 0); |
90 SkDEBUGCODE(f16BitCacheLockCount -= 1); | 75 SkDEBUGCODE(f16BitCacheLockCount -= 1); |
91 } | 76 } |
92 | 77 |
93 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorTable) | 78 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorTable) |
94 | 79 |
95 protected: | 80 protected: |
96 explicit SkColorTable(SkFlattenableReadBuffer&); | 81 explicit SkColorTable(SkFlattenableReadBuffer&); |
97 void flatten(SkFlattenableWriteBuffer&) const; | 82 void flatten(SkFlattenableWriteBuffer&) const; |
98 | 83 |
99 private: | 84 private: |
100 SkPMColor* fColors; | 85 SkPMColor* fColors; |
101 uint16_t* f16BitCache; | 86 uint16_t* f16BitCache; |
102 uint16_t fCount; | 87 uint16_t fCount; |
103 uint8_t fFlags; | 88 uint8_t fAlphaType; |
104 SkDEBUGCODE(int fColorLockCount;) | 89 SkDEBUGCODE(int fColorLockCount;) |
105 SkDEBUGCODE(int f16BitCacheLockCount;) | 90 SkDEBUGCODE(int f16BitCacheLockCount;) |
106 | 91 |
107 void inval16BitCache(); | 92 void inval16BitCache(); |
108 | 93 |
109 typedef SkFlattenable INHERITED; | 94 typedef SkFlattenable INHERITED; |
110 }; | 95 }; |
111 | 96 |
112 #endif | 97 #endif |
OLD | NEW |