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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 SkColorTable::SkColorTable(const SkPMColor colors[], int count) { | 25 SkColorTable::SkColorTable(const SkPMColor colors[], int count) { |
26 SkASSERT(0 == count || colors); | 26 SkASSERT(0 == count || colors); |
27 if (count < 0) { | 27 if (count < 0) { |
28 count = 0; | 28 count = 0; |
29 } else if (count > 256) { | 29 } else if (count > 256) { |
30 count = 256; | 30 count = 256; |
31 } | 31 } |
32 this->init(colors, count); | 32 this->init(colors, count); |
33 } | 33 } |
34 | 34 |
| 35 SkColorTable::SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc) |
| 36 : fColors(colors) |
| 37 , fCount(count) |
| 38 { |
| 39 SkASSERT(count > 0 && count <= 255); |
| 40 SkASSERT(colors); |
| 41 } |
| 42 |
35 SkColorTable::~SkColorTable() { | 43 SkColorTable::~SkColorTable() { |
36 sk_free(fColors); | 44 sk_free(fColors); |
37 // f16BitCache frees itself | 45 // f16BitCache frees itself |
38 } | 46 } |
39 | 47 |
40 #include "SkColorPriv.h" | 48 #include "SkColorPriv.h" |
41 | 49 |
42 namespace { | 50 namespace { |
43 struct Build16BitCache { | 51 struct Build16BitCache { |
44 const SkPMColor* fColors; | 52 const SkPMColor* fColors; |
(...skipping 11 matching lines...) Expand all Loading... |
56 | 64 |
57 void SkColorTable::Free16BitCache(uint16_t* cache) { sk_free(cache); } | 65 void SkColorTable::Free16BitCache(uint16_t* cache) { sk_free(cache); } |
58 | 66 |
59 const uint16_t* SkColorTable::read16BitCache() const { | 67 const uint16_t* SkColorTable::read16BitCache() const { |
60 const Build16BitCache create = { fColors, fCount }; | 68 const Build16BitCache create = { fColors, fCount }; |
61 return f16BitCache.get(create); | 69 return f16BitCache.get(create); |
62 } | 70 } |
63 | 71 |
64 /////////////////////////////////////////////////////////////////////////////// | 72 /////////////////////////////////////////////////////////////////////////////// |
65 | 73 |
| 74 #if 0 |
66 SkColorTable::SkColorTable(SkReadBuffer& buffer) { | 75 SkColorTable::SkColorTable(SkReadBuffer& buffer) { |
67 if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { | 76 if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { |
68 /*fAlphaType = */buffer.readUInt(); | 77 /*fAlphaType = */buffer.readUInt(); |
69 } | 78 } |
70 | 79 |
71 fCount = buffer.getArrayCount(); | 80 fCount = buffer.getArrayCount(); |
72 size_t allocSize = fCount * sizeof(SkPMColor); | 81 size_t allocSize = fCount * sizeof(SkPMColor); |
73 SkDEBUGCODE(bool success = false;) | 82 SkDEBUGCODE(bool success = false;) |
74 if (buffer.validateAvailable(allocSize)) { | 83 if (buffer.validateAvailable(allocSize)) { |
75 fColors = (SkPMColor*)sk_malloc_throw(allocSize); | 84 fColors = (SkPMColor*)sk_malloc_throw(allocSize); |
76 SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); | 85 SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); |
77 } else { | 86 } else { |
78 fCount = 0; | 87 fCount = 0; |
79 fColors = nullptr; | 88 fColors = nullptr; |
80 } | 89 } |
81 #ifdef SK_DEBUG | 90 #ifdef SK_DEBUG |
82 SkASSERT((unsigned)fCount <= 256); | 91 SkASSERT((unsigned)fCount <= 256); |
83 SkASSERT(success); | 92 SkASSERT(success); |
84 #endif | 93 #endif |
85 } | 94 } |
| 95 #endif |
86 | 96 |
87 void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { | 97 void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { |
88 buffer.writeColorArray(fColors, fCount); | 98 buffer.writeColorArray(fColors, fCount); |
89 } | 99 } |
| 100 |
| 101 SkColorTable* SkColorTable::Create(SkReadBuffer& buffer) { |
| 102 if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { |
| 103 /*fAlphaType = */buffer.readUInt(); |
| 104 } |
| 105 |
| 106 const int count = buffer.getArrayCount(); |
| 107 if (0 == count) { |
| 108 return new SkColorTable(nullptr, 0); |
| 109 } |
| 110 |
| 111 if (count < 0 || count > 255) { |
| 112 buffer.validate(false); |
| 113 return nullptr; |
| 114 } |
| 115 |
| 116 const size_t allocSize = count * sizeof(SkPMColor); |
| 117 SkAutoTDelete<SkPMColor> colors((SkPMColor*)sk_malloc_throw(allocSize)); |
| 118 if (!buffer.readColorArray(colors, count)) { |
| 119 return nullptr; |
| 120 } |
| 121 |
| 122 return new SkColorTable(colors.detach(), count, kAllocatedWithMalloc); |
| 123 } |
| 124 |
OLD | NEW |