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