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@google.com | 2999f78 | 2012-06-19 15:27:46 +0000 | [diff] [blame] | 16 | // As copy constructor is hidden in the class hierarchy, we need to call |
| 17 | // default constructor explicitly to suppress a compiler warning. |
| 18 | SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 19 | f16BitCache = NULL; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 20 | fAlphaType = src.fAlphaType; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 21 | int count = src.count(); |
| 22 | fCount = SkToU16(count); |
reed@android.com | a6260ff | 2009-05-04 18:52:54 +0000 | [diff] [blame] | 23 | fColors = reinterpret_cast<SkPMColor*>( |
| 24 | sk_malloc_throw(count * sizeof(SkPMColor))); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 25 | memcpy(fColors, src.fColors, count * sizeof(SkPMColor)); |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | SkDEBUGCODE(fColorLockCount = 0;) |
| 28 | SkDEBUGCODE(f16BitCacheLockCount = 0;) |
| 29 | } |
| 30 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 31 | SkColorTable::SkColorTable(const SkPMColor colors[], int count, SkAlphaType at) |
| 32 | : f16BitCache(NULL), fAlphaType(SkToU8(at)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 34 | SkASSERT(0 == count || NULL != colors); |
| 35 | |
| 36 | if (count < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | count = 0; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 38 | } else if (count > 256) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | count = 256; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 40 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | fCount = SkToU16(count); |
reed@android.com | a6260ff | 2009-05-04 18:52:54 +0000 | [diff] [blame] | 43 | fColors = reinterpret_cast<SkPMColor*>( |
| 44 | sk_malloc_throw(count * sizeof(SkPMColor))); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 45 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 46 | memcpy(fColors, colors, count * sizeof(SkPMColor)); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | SkDEBUGCODE(fColorLockCount = 0;) |
| 49 | SkDEBUGCODE(f16BitCacheLockCount = 0;) |
| 50 | } |
| 51 | |
| 52 | SkColorTable::~SkColorTable() |
| 53 | { |
| 54 | SkASSERT(fColorLockCount == 0); |
| 55 | SkASSERT(f16BitCacheLockCount == 0); |
| 56 | |
| 57 | sk_free(fColors); |
| 58 | sk_free(f16BitCache); |
| 59 | } |
| 60 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 61 | void SkColorTable::unlockColors() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | SkASSERT(fColorLockCount != 0); |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 63 | SkDEBUGCODE(sk_atomic_dec(&fColorLockCount);) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #include "SkColorPriv.h" |
| 67 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 68 | static inline void build_16bitcache(uint16_t dst[], const SkPMColor src[], |
| 69 | int count) { |
| 70 | while (--count >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | *dst++ = SkPixel32ToPixel16_ToU16(*src++); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 72 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 75 | const uint16_t* SkColorTable::lock16BitCache() { |
| 76 | if (this->isOpaque() && NULL == f16BitCache) { |
| 77 | f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); |
| 78 | build_16bitcache(f16BitCache, fColors, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | SkDEBUGCODE(f16BitCacheLockCount += 1); |
| 82 | return f16BitCache; |
| 83 | } |
| 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 87 | SkColorTable::SkColorTable(SkReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | f16BitCache = NULL; |
| 89 | SkDEBUGCODE(fColorLockCount = 0;) |
| 90 | SkDEBUGCODE(f16BitCacheLockCount = 0;) |
| 91 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 92 | fAlphaType = SkToU8(buffer.readUInt()); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 93 | fCount = buffer.getArrayCount(); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 94 | size_t allocSize = fCount * sizeof(SkPMColor); |
| 95 | SkDEBUGCODE(bool success = false;) |
| 96 | if (buffer.validateAvailable(allocSize)) { |
| 97 | fColors = (SkPMColor*)sk_malloc_throw(allocSize); |
| 98 | SkDEBUGCODE(success =) buffer.readColorArray(fColors, fCount); |
| 99 | } else { |
| 100 | fCount = 0; |
| 101 | fColors = NULL; |
| 102 | } |
bsalomon@google.com | 57313f6 | 2013-01-07 17:35:10 +0000 | [diff] [blame] | 103 | #ifdef SK_DEBUG |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 104 | SkASSERT((unsigned)fCount <= 256); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 105 | SkASSERT(success); |
bsalomon@google.com | 57313f6 | 2013-01-07 17:35:10 +0000 | [diff] [blame] | 106 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 109 | void SkColorTable::writeToBuffer(SkWriteBuffer& buffer) const { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 110 | buffer.writeUInt(fAlphaType); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 111 | buffer.writeColorArray(fColors, fCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |