180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc. 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkColorTable_DEFINED 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkColorTable_DEFINED 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkColor.h" 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkFlattenable.h" 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/** \class SkColorTable 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable. 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru*/ 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkColorTable : public SkFlattenable { 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic: 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SK_DECLARE_INST_COUNT(SkColorTable) 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Makes a deep copy of colors. 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkColorTable(const SkColorTable& src); 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Preallocates the colortable to have 'count' colors, which 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * are initially set to 0. 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru explicit SkColorTable(int count); 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkColorTable(const SkPMColor colors[], int count); 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru virtual ~SkColorTable(); 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru enum Flags { 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the table are opaque (alpha==0xFF) 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Returns the flag bits for the color table. These can be changed with setFlags(). 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru unsigned getFlags() const { return fFlags; } 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Set the flags for the color table. See the Flags enum for possible values. 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setFlags(unsigned flags); 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; } 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void setIsOpaque(bool isOpaque); 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Returns the number of colors in the table. 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int count() const { return fCount; } 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Returns the specified color from the table. In the debug build, this asserts that 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru the index is in range (0 <= index < count). 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPMColor operator[](int index) const { 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkASSERT(fColors != NULL && (unsigned)index < fCount); 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return fColors[index]; 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Specify the number of colors in the color table. This does not initialize the colors 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru to any value, just allocates memory for them. To initialize the values, either call 6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru setColors(array, count), or follow setCount(count) with a call to 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru lockColors()/{set the values}/unlockColors(true). 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// void setColors(int count) { this->setColors(NULL, count); } 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// void setColors(const SkPMColor[], int count); 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Return the array of colors for reading and/or writing. This must be 6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru balanced by a call to unlockColors(changed?), telling the colortable if 7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru the colors were changed during the lock. 7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPMColor* lockColors() { 7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkDEBUGCODE(sk_atomic_inc(&fColorLockCount);) 7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return fColors; 7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Balancing call to lockColors(). If the colors have been changed, pass true. 7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void unlockColors(bool changed); 7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Similar to lockColors(), lock16BitCache() returns the array of 8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru RGB16 colors that mirror the 32bit colors. However, this function 8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru will return null if kColorsAreOpaque_Flag is not set. 8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru Also, unlike lockColors(), the returned array here cannot be modified. 8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const uint16_t* lock16BitCache(); 8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** Balancing call to lock16BitCache(). 8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void unlock16BitCache() { 8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkASSERT(f16BitCacheLockCount > 0); 9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkDEBUGCODE(f16BitCacheLockCount -= 1); 9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorTable) 9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected: 9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru explicit SkColorTable(SkFlattenableReadBuffer&); 9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void flatten(SkFlattenableWriteBuffer&) const; 9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate: 10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPMColor* fColors; 10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru uint16_t* f16BitCache; 10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru uint16_t fCount; 10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru uint8_t fFlags; 10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkDEBUGCODE(int fColorLockCount;) 10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkDEBUGCODE(int f16BitCacheLockCount;) 10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void inval16BitCache(); 10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru typedef SkFlattenable INHERITED; 11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}; 11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif 113