epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | a6260ff | 2009-05-04 18:52:54 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2009 The Android Open Source Project |
reed@android.com | a6260ff | 2009-05-04 18:52:54 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | a6260ff | 2009-05-04 18:52:54 +0000 | [diff] [blame] | 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 10 | #include "SkColorTable.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
| 14 | #include "SkTemplates.h" |
| 15 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 16 | void SkColorTable::init(const SkPMColor colors[], int count) { |
| 17 | SkASSERT((unsigned)count <= 256); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 18 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 19 | f16BitCache = NULL; |
| 20 | fCount = count; |
| 21 | fColors = reinterpret_cast<SkPMColor*>(sk_malloc_throw(count * sizeof(SkPMColor))); |
| 22 | |
| 23 | memcpy(fColors, colors, count * sizeof(SkPMColor)); |
| 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | SkDEBUGCODE(fColorLockCount = 0;) |
| 26 | SkDEBUGCODE(f16BitCacheLockCount = 0;) |
| 27 | } |
| 28 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 29 | // As copy constructor is hidden in the class hierarchy, we need to call |
| 30 | // default constructor explicitly to suppress a compiler warning. |
| 31 | SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { |
| 32 | this->init(src.fColors, src.fCount); |
| 33 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 34 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 35 | SkColorTable::SkColorTable(const SkPMColor colors[], int count) { |
| 36 | SkASSERT(0 == count || colors); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 37 | if (count < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | count = 0; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 39 | } else if (count > 256) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | count = 256; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 41 | } |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 42 | this->init(colors, count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 45 | SkColorTable::~SkColorTable() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | SkASSERT(fColorLockCount == 0); |
| 47 | SkASSERT(f16BitCacheLockCount == 0); |
| 48 | |
| 49 | sk_free(fColors); |
| 50 | sk_free(f16BitCache); |
| 51 | } |
| 52 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 53 | void SkColorTable::unlockColors() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkASSERT(fColorLockCount != 0); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 55 | SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | #include "SkColorPriv.h" |
| 59 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 60 | static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], |
| 61 | int count) { |
| 62 | while (--count >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | *dst++ = SkPixel32ToPixel16_ToU16(*src++); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 64 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 67 | const uint16_t* SkColorTable::lock16BitCache() { |
djsollen | 47c548a | 2014-09-29 08:57:49 -0700 | [diff] [blame] | 68 | if (NULL == f16BitCache) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 69 | f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); |
| 70 | build_16bitcache(f16BitCache, fColors, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 73 | SkDEBUGCODE(sk_atomic_inc(&f16BitCacheLockCount)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | return f16BitCache; |
| 75 | } |
| 76 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 79 | SkColorTable::SkColorTable(SkReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | f16BitCache = NULL; |
| 81 | SkDEBUGCODE(fColorLockCount = 0;) |
| 82 | SkDEBUGCODE(f16BitCacheLockCount = 0;) |
| 83 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 84 | if (buffer.isVersionLT(SkReadBuffer::kRemoveColorTableAlpha_Version)) { |
| 85 | /*fAlphaType = */buffer.readUInt(); |
| 86 | } |
| 87 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 88 | fCount = buffer.getArrayCount(); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 89 | size_t allocSize = fCount * sizeof(SkPMColor); |
| 90 | SkDEBUGCODE(bool success = false;) |
| 91 | if (buffer.validateAvailable(allocSize)) { |
| 92 | fColors = (SkPMColor*)sk_malloc_throw(allocSize); |
| 93 | SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); |
| 94 | } else { |
| 95 | fCount = 0; |
| 96 | fColors = NULL; |
| 97 | } |
bsalomon@google.com | 57313f6 | 2013-01-07 17:35:10 +0000 | [diff] [blame] | 98 | #ifdef SK_DEBUG |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 99 | SkASSERT((unsigned)fCount <= 256); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 100 | SkASSERT(success); |
bsalomon@google.com | 57313f6 | 2013-01-07 17:35:10 +0000 | [diff] [blame] | 101 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 104 | void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 105 | buffer.writeColorArray(fColors, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | } |