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