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