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 "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
13 #include "SkStream.h" | 13 #include "SkStream.h" |
14 #include "SkTemplates.h" | 14 #include "SkTemplates.h" |
15 | 15 |
16 void SkColorTable::init(const SkPMColor colors[], int count) { | 16 void SkColorTable::init(const SkPMColor colors[], int count) { |
17 SkASSERT((unsigned)count <= 256); | 17 SkASSERT((unsigned)count <= 256); |
18 | 18 |
19 f16BitCache = NULL; | |
20 fCount = count; | 19 fCount = count; |
21 fColors = reinterpret_cast<SkPMColor*>(sk_malloc_throw(count * sizeof(SkPMCo
lor))); | 20 fColors = reinterpret_cast<SkPMColor*>(sk_malloc_throw(count * sizeof(SkPMCo
lor))); |
22 | 21 |
23 memcpy(fColors, colors, count * sizeof(SkPMColor)); | 22 memcpy(fColors, colors, count * sizeof(SkPMColor)); |
24 } | 23 } |
25 | 24 |
26 // As copy constructor is hidden in the class hierarchy, we need to call | |
27 // default constructor explicitly to suppress a compiler warning. | |
28 SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { | |
29 this->init(src.fColors, src.fCount); | |
30 } | |
31 | |
32 SkColorTable::SkColorTable(const SkPMColor colors[], int count) { | 25 SkColorTable::SkColorTable(const SkPMColor colors[], int count) { |
33 SkASSERT(0 == count || colors); | 26 SkASSERT(0 == count || colors); |
34 if (count < 0) { | 27 if (count < 0) { |
35 count = 0; | 28 count = 0; |
36 } else if (count > 256) { | 29 } else if (count > 256) { |
37 count = 256; | 30 count = 256; |
38 } | 31 } |
39 this->init(colors, count); | 32 this->init(colors, count); |
40 } | 33 } |
41 | 34 |
42 SkColorTable::~SkColorTable() { | 35 SkColorTable::~SkColorTable() { |
43 sk_free(fColors); | 36 sk_free(fColors); |
44 sk_free(f16BitCache); | 37 // f16BitCache frees itself |
45 } | 38 } |
46 | 39 |
47 #include "SkColorPriv.h" | 40 #include "SkColorPriv.h" |
48 | 41 |
49 static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], | 42 namespace { |
50 int count) { | 43 struct Build16BitCache { |
51 while (--count >= 0) { | 44 const SkPMColor* fColors; |
52 *dst++ = SkPixel32ToPixel16_ToU16(*src++); | 45 int fCount; |
| 46 |
| 47 uint16_t* operator()() const { |
| 48 uint16_t* cache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); |
| 49 for (int i = 0; i < fCount; i++) { |
| 50 cache[i] = SkPixel32ToPixel16_ToU16(fColors[i]); |
| 51 } |
| 52 return cache; |
53 } | 53 } |
54 } | 54 }; |
| 55 }//namespace |
55 | 56 |
56 const uint16_t* SkColorTable::read16BitCache() { | 57 const uint16_t* SkColorTable::read16BitCache() const { |
57 if (NULL == f16BitCache) { | 58 const Build16BitCache create = { fColors, fCount }; |
58 f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); | 59 return f16BitCache.get(create); |
59 build_16bitcache(f16BitCache, fColors, fCount); | |
60 } | |
61 return f16BitCache; | |
62 } | 60 } |
63 | 61 |
64 /////////////////////////////////////////////////////////////////////////////// | 62 /////////////////////////////////////////////////////////////////////////////// |
65 | 63 |
66 SkColorTable::SkColorTable(SkReadBuffer& buffer) { | 64 SkColorTable::SkColorTable(SkReadBuffer& buffer) { |
67 f16BitCache = NULL; | |
68 if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { | 65 if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { |
69 /*fAlphaType = */buffer.readUInt(); | 66 /*fAlphaType = */buffer.readUInt(); |
70 } | 67 } |
71 | 68 |
72 fCount = buffer.getArrayCount(); | 69 fCount = buffer.getArrayCount(); |
73 size_t allocSize = fCount * sizeof(SkPMColor); | 70 size_t allocSize = fCount * sizeof(SkPMColor); |
74 SkDEBUGCODE(bool success = false;) | 71 SkDEBUGCODE(bool success = false;) |
75 if (buffer.validateAvailable(allocSize)) { | 72 if (buffer.validateAvailable(allocSize)) { |
76 fColors = (SkPMColor*)sk_malloc_throw(allocSize); | 73 fColors = (SkPMColor*)sk_malloc_throw(allocSize); |
77 SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); | 74 SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); |
78 } else { | 75 } else { |
79 fCount = 0; | 76 fCount = 0; |
80 fColors = NULL; | 77 fColors = NULL; |
81 } | 78 } |
82 #ifdef SK_DEBUG | 79 #ifdef SK_DEBUG |
83 SkASSERT((unsigned)fCount <= 256); | 80 SkASSERT((unsigned)fCount <= 256); |
84 SkASSERT(success); | 81 SkASSERT(success); |
85 #endif | 82 #endif |
86 } | 83 } |
87 | 84 |
88 void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { | 85 void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { |
89 buffer.writeColorArray(fColors, fCount); | 86 buffer.writeColorArray(fColors, fCount); |
90 } | 87 } |
OLD | NEW |