OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2009 The Android Open Source Project | 3 * Copyright 2009 The Android Open Source Project |
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 #include "SkColorTable.h" | 10 #include "SkColorTable.h" |
11 #include "SkFlattenableBuffers.h" | 11 #include "SkFlattenableBuffers.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 | 14 |
15 SK_DEFINE_INST_COUNT(SkColorTable) | 15 SK_DEFINE_INST_COUNT(SkColorTable) |
16 | 16 |
17 #if 0 | |
scroggo
2013/10/01 17:30:38
If we're going to leave this code around, can we a
| |
17 SkColorTable::SkColorTable(int count) | 18 SkColorTable::SkColorTable(int count) |
18 : f16BitCache(NULL), fFlags(0) | 19 : f16BitCache(NULL), fFlags(0) |
19 { | 20 { |
20 if (count < 0) | 21 if (count < 0) |
21 count = 0; | 22 count = 0; |
22 else if (count > 256) | 23 else if (count > 256) |
23 count = 256; | 24 count = 256; |
24 | 25 |
25 fCount = SkToU16(count); | 26 fCount = SkToU16(count); |
26 fColors = (SkPMColor*)sk_malloc_throw(count * sizeof(SkPMColor)); | 27 fColors = (SkPMColor*)sk_malloc_throw(count * sizeof(SkPMColor)); |
27 memset(fColors, 0, count * sizeof(SkPMColor)); | 28 memset(fColors, 0, count * sizeof(SkPMColor)); |
28 | 29 |
29 SkDEBUGCODE(fColorLockCount = 0;) | 30 SkDEBUGCODE(fColorLockCount = 0;) |
30 SkDEBUGCODE(f16BitCacheLockCount = 0;) | 31 SkDEBUGCODE(f16BitCacheLockCount = 0;) |
31 } | 32 } |
33 #endif | |
32 | 34 |
33 // As copy constructor is hidden in the class hierarchy, we need to call | 35 // As copy constructor is hidden in the class hierarchy, we need to call |
34 // default constructor explicitly to suppress a compiler warning. | 36 // default constructor explicitly to suppress a compiler warning. |
35 SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { | 37 SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { |
36 f16BitCache = NULL; | 38 f16BitCache = NULL; |
37 fFlags = src.fFlags; | 39 fAlphaType = src.fAlphaType; |
38 int count = src.count(); | 40 int count = src.count(); |
39 fCount = SkToU16(count); | 41 fCount = SkToU16(count); |
40 fColors = reinterpret_cast<SkPMColor*>( | 42 fColors = reinterpret_cast<SkPMColor*>( |
41 sk_malloc_throw(count * sizeof(SkPMColor))); | 43 sk_malloc_throw(count * sizeof(SkPMColor))); |
42 memcpy(fColors, src.fColors, count * sizeof(SkPMColor)); | 44 memcpy(fColors, src.fColors, count * sizeof(SkPMColor)); |
43 | 45 |
44 SkDEBUGCODE(fColorLockCount = 0;) | 46 SkDEBUGCODE(fColorLockCount = 0;) |
45 SkDEBUGCODE(f16BitCacheLockCount = 0;) | 47 SkDEBUGCODE(f16BitCacheLockCount = 0;) |
46 } | 48 } |
47 | 49 |
48 SkColorTable::SkColorTable(const SkPMColor colors[], int count) | 50 SkColorTable::SkColorTable(const SkPMColor colors[], int count, SkAlphaType at) |
49 : f16BitCache(NULL), fFlags(0) | 51 : f16BitCache(NULL), fAlphaType(SkToU8(at)) |
50 { | 52 { |
51 if (count < 0) | 53 SkASSERT(0 == count || NULL != colors); |
54 | |
55 if (count < 0) { | |
52 count = 0; | 56 count = 0; |
53 else if (count > 256) | 57 } else if (count > 256) { |
54 count = 256; | 58 count = 256; |
59 } | |
55 | 60 |
56 fCount = SkToU16(count); | 61 fCount = SkToU16(count); |
57 fColors = reinterpret_cast<SkPMColor*>( | 62 fColors = reinterpret_cast<SkPMColor*>( |
58 sk_malloc_throw(count * sizeof(SkPMColor))); | 63 sk_malloc_throw(count * sizeof(SkPMColor))); |
59 | 64 |
60 if (colors) | 65 memcpy(fColors, colors, count * sizeof(SkPMColor)); |
61 memcpy(fColors, colors, count * sizeof(SkPMColor)); | |
62 | 66 |
63 SkDEBUGCODE(fColorLockCount = 0;) | 67 SkDEBUGCODE(fColorLockCount = 0;) |
64 SkDEBUGCODE(f16BitCacheLockCount = 0;) | 68 SkDEBUGCODE(f16BitCacheLockCount = 0;) |
65 } | 69 } |
66 | 70 |
67 SkColorTable::~SkColorTable() | 71 SkColorTable::~SkColorTable() |
68 { | 72 { |
69 SkASSERT(fColorLockCount == 0); | 73 SkASSERT(fColorLockCount == 0); |
70 SkASSERT(f16BitCacheLockCount == 0); | 74 SkASSERT(f16BitCacheLockCount == 0); |
71 | 75 |
72 sk_free(fColors); | 76 sk_free(fColors); |
73 sk_free(f16BitCache); | 77 sk_free(f16BitCache); |
74 } | 78 } |
75 | 79 |
76 void SkColorTable::setFlags(unsigned flags) | 80 void SkColorTable::unlockColors() { |
77 { | |
78 fFlags = SkToU8(flags); | |
79 } | |
80 | |
81 void SkColorTable::unlockColors(bool changed) | |
82 { | |
83 SkASSERT(fColorLockCount != 0); | 81 SkASSERT(fColorLockCount != 0); |
84 SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);) | 82 SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);) |
85 if (changed) | |
86 this->inval16BitCache(); | |
87 } | |
88 | |
89 void SkColorTable::inval16BitCache() | |
90 { | |
91 SkASSERT(f16BitCacheLockCount == 0); | |
92 if (f16BitCache) | |
93 { | |
94 sk_free(f16BitCache); | |
95 f16BitCache = NULL; | |
96 } | |
97 } | 83 } |
98 | 84 |
99 #include "SkColorPriv.h" | 85 #include "SkColorPriv.h" |
100 | 86 |
101 static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], int c ount) | 87 static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], |
102 { | 88 int count) { |
103 while (--count >= 0) | 89 while (--count >= 0) { |
104 *dst++ = SkPixel32ToPixel16_ToU16(*src++); | 90 *dst++ = SkPixel32ToPixel16_ToU16(*src++); |
91 } | |
105 } | 92 } |
106 | 93 |
107 const uint16_t* SkColorTable::lock16BitCache() | 94 const uint16_t* SkColorTable::lock16BitCache() { |
108 { | 95 if (this->isOpaque() && NULL == f16BitCache) { |
109 if (fFlags & kColorsAreOpaque_Flag) | 96 f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); |
110 { | 97 build_16bitcache(f16BitCache, fColors, fCount); |
111 if (f16BitCache == NULL) // build the cache | |
112 { | |
113 f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); | |
114 build_16bitcache(f16BitCache, fColors, fCount); | |
115 } | |
116 } | |
117 else // our colors have alpha, so no cache | |
118 { | |
119 this->inval16BitCache(); | |
120 if (f16BitCache) | |
121 { | |
122 sk_free(f16BitCache); | |
123 f16BitCache = NULL; | |
124 } | |
125 } | 98 } |
126 | 99 |
127 SkDEBUGCODE(f16BitCacheLockCount += 1); | 100 SkDEBUGCODE(f16BitCacheLockCount += 1); |
128 return f16BitCache; | 101 return f16BitCache; |
129 } | 102 } |
130 | 103 |
131 void SkColorTable::setIsOpaque(bool isOpaque) { | |
132 if (isOpaque) { | |
133 fFlags |= kColorsAreOpaque_Flag; | |
134 } else { | |
135 fFlags &= ~kColorsAreOpaque_Flag; | |
136 } | |
137 } | |
138 | |
139 /////////////////////////////////////////////////////////////////////////////// | 104 /////////////////////////////////////////////////////////////////////////////// |
140 | 105 |
141 SkColorTable::SkColorTable(SkFlattenableReadBuffer& buffer) { | 106 SkColorTable::SkColorTable(SkFlattenableReadBuffer& buffer) { |
142 f16BitCache = NULL; | 107 f16BitCache = NULL; |
143 SkDEBUGCODE(fColorLockCount = 0;) | 108 SkDEBUGCODE(fColorLockCount = 0;) |
144 SkDEBUGCODE(f16BitCacheLockCount = 0;) | 109 SkDEBUGCODE(f16BitCacheLockCount = 0;) |
145 | 110 |
146 fFlags = buffer.readUInt(); | 111 fAlphaType = SkToU8(buffer.readUInt()); |
scroggo
2013/10/01 17:30:38
If we don't update the PICTURE_VERSION, this will
| |
147 fCount = buffer.getArrayCount(); | 112 fCount = buffer.getArrayCount(); |
148 fColors = (SkPMColor*)sk_malloc_throw(fCount * sizeof(SkPMColor)); | 113 fColors = (SkPMColor*)sk_malloc_throw(fCount * sizeof(SkPMColor)); |
149 SkDEBUGCODE(const uint32_t countRead =) buffer.readColorArray(fColors); | 114 SkDEBUGCODE(const uint32_t countRead =) buffer.readColorArray(fColors); |
150 #ifdef SK_DEBUG | 115 #ifdef SK_DEBUG |
151 SkASSERT((unsigned)fCount <= 256); | 116 SkASSERT((unsigned)fCount <= 256); |
152 SkASSERT(countRead == fCount); | 117 SkASSERT(countRead == fCount); |
153 #endif | 118 #endif |
154 } | 119 } |
155 | 120 |
156 void SkColorTable::flatten(SkFlattenableWriteBuffer& buffer) const { | 121 void SkColorTable::flatten(SkFlattenableWriteBuffer& buffer) const { |
157 buffer.writeUInt(fFlags); | 122 buffer.writeUInt(fAlphaType); |
158 buffer.writeColorArray(fColors, fCount); | 123 buffer.writeColorArray(fColors, fCount); |
159 } | 124 } |
OLD | NEW |