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" 15 16 /** \class SkColorTable 17 18 SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 19 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable. 20 */ 21 class SkColorTable : public SkFlattenable { 22 public: 23 SK_DECLARE_INST_COUNT(SkColorTable) 24 25 /** Makes a deep copy of colors. 26 */ 27 SkColorTable(const SkColorTable& src); 28 /** Preallocates the colortable to have 'count' colors, which 29 * are initially set to 0. 30 */ 31 explicit SkColorTable(int count); 32 SkColorTable(const SkPMColor colors[], int count); 33 virtual ~SkColorTable(); 34 35 enum Flags { 36 kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the table are opaque (alpha==0xFF) 37 }; 38 /** Returns the flag bits for the color table. These can be changed with setFlags(). 39 */ 40 unsigned getFlags() const { return fFlags; } 41 /** Set the flags for the color table. See the Flags enum for possible values. 42 */ 43 void setFlags(unsigned flags); 44 45 bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; } 46 void setIsOpaque(bool isOpaque); 47 48 /** Returns the number of colors in the table. 49 */ 50 int count() const { return fCount; } 51 52 /** Returns the specified color from the table. In the debug build, this asserts that 53 the index is in range (0 <= index < count). 54 */ 55 SkPMColor operator[](int index) const { 56 SkASSERT(fColors != NULL && (unsigned)index < fCount); 57 return fColors[index]; 58 } 59 60 /** Specify the number of colors in the color table. This does not initialize the colors 61 to any value, just allocates memory for them. To initialize the values, either call 62 setColors(array, count), or follow setCount(count) with a call to 63 lockColors()/{set the values}/unlockColors(true). 64 */ 65 // void setColors(int count) { this->setColors(NULL, count); } 66 // void setColors(const SkPMColor[], int count); 67 68 /** Return the array of colors for reading and/or writing. This must be 69 balanced by a call to unlockColors(changed?), telling the colortable if 70 the colors were changed during the lock. 71 */ 72 SkPMColor* lockColors() { 73 SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);) 74 return fColors; 75 } 76 /** Balancing call to lockColors(). If the colors have been changed, pass true. 77 */ 78 void unlockColors(bool changed); 79 80 /** Similar to lockColors(), lock16BitCache() returns the array of 81 RGB16 colors that mirror the 32bit colors. However, this function 82 will return null if kColorsAreOpaque_Flag is not set. 83 Also, unlike lockColors(), the returned array here cannot be modified. 84 */ 85 const uint16_t* lock16BitCache(); 86 /** Balancing call to lock16BitCache(). 87 */ 88 void unlock16BitCache() { 89 SkASSERT(f16BitCacheLockCount > 0); 90 SkDEBUGCODE(f16BitCacheLockCount -= 1); 91 } 92 93 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorTable) 94 95 protected: 96 explicit SkColorTable(SkFlattenableReadBuffer&); 97 void flatten(SkFlattenableWriteBuffer&) const; 98 99 private: 100 SkPMColor* fColors; 101 uint16_t* f16BitCache; 102 uint16_t fCount; 103 uint8_t fFlags; 104 SkDEBUGCODE(int fColorLockCount;) 105 SkDEBUGCODE(int f16BitCacheLockCount;) 106 107 void inval16BitCache(); 108 109 typedef SkFlattenable INHERITED; 110 }; 111 112 #endif 113