djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #ifndef SkColorTable_DEFINED |
| 11 | #define SkColorTable_DEFINED |
| 12 | |
| 13 | #include "SkColor.h" |
| 14 | #include "SkFlattenable.h" |
reed@google.com | 3443fd8 | 2013-11-13 19:09:13 +0000 | [diff] [blame] | 15 | #include "SkImageInfo.h" |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 16 | |
| 17 | /** \class SkColorTable |
| 18 | |
| 19 | SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by |
| 20 | 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable. |
| 21 | */ |
ch.dumez | 43f9d3e | 2014-08-05 12:20:19 -0700 | [diff] [blame] | 22 | class SK_API SkColorTable : public SkRefCnt { |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 23 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 24 | SK_DECLARE_INST_COUNT(SkColorTable) |
| 25 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 26 | /** Makes a deep copy of colors. |
| 27 | */ |
| 28 | SkColorTable(const SkColorTable& src); |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 29 | SkColorTable(const SkPMColor colors[], int count); |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 30 | virtual ~SkColorTable(); |
| 31 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 32 | /** Returns the number of colors in the table. |
| 33 | */ |
| 34 | int count() const { return fCount; } |
| 35 | |
| 36 | /** Returns the specified color from the table. In the debug build, this asserts that |
| 37 | the index is in range (0 <= index < count). |
| 38 | */ |
| 39 | SkPMColor operator[](int index) const { |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 40 | SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount); |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 41 | return fColors[index]; |
| 42 | } |
| 43 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Return the array of colors for reading. This must be balanced by a call |
| 46 | * to unlockColors(). |
| 47 | */ |
| 48 | const SkPMColor* lockColors() { |
djsollen@google.com | c9ab987 | 2012-08-29 18:52:07 +0000 | [diff] [blame] | 49 | SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);) |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 50 | return fColors; |
| 51 | } |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Balancing call to lockColors(). |
| 55 | */ |
| 56 | void unlockColors(); |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 57 | |
| 58 | /** Similar to lockColors(), lock16BitCache() returns the array of |
| 59 | RGB16 colors that mirror the 32bit colors. However, this function |
| 60 | will return null if kColorsAreOpaque_Flag is not set. |
| 61 | Also, unlike lockColors(), the returned array here cannot be modified. |
| 62 | */ |
| 63 | const uint16_t* lock16BitCache(); |
| 64 | /** Balancing call to lock16BitCache(). |
| 65 | */ |
| 66 | void unlock16BitCache() { |
| 67 | SkASSERT(f16BitCacheLockCount > 0); |
robertphillips | 5b69377 | 2014-11-21 06:19:36 -0800 | [diff] [blame^] | 68 | SkDEBUGCODE(sk_atomic_dec(&f16BitCacheLockCount);) |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 71 | explicit SkColorTable(SkReadBuffer&); |
| 72 | void writeToBuffer(SkWriteBuffer&) const; |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | SkPMColor* fColors; |
| 76 | uint16_t* f16BitCache; |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 77 | int fCount; |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 78 | SkDEBUGCODE(int fColorLockCount;) |
| 79 | SkDEBUGCODE(int f16BitCacheLockCount;) |
| 80 | |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 81 | void init(const SkPMColor* colors, int count); |
| 82 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 83 | void inval16BitCache(); |
| 84 | |
commit-bot@chromium.org | e0e1da3 | 2013-10-15 20:36:16 +0000 | [diff] [blame] | 85 | typedef SkRefCnt INHERITED; |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | #endif |