From 64a0ec36555352ec31aa7c5a7630a5d042b010ba Mon Sep 17 00:00:00 2001 From: "djsollen@google.com" Date: Tue, 12 Jun 2012 15:17:27 +0000 Subject: [PATCH] Move SkColorTable into its own header and reduce includes in SkFlattenable.h Review URL: https://codereview.appspot.com/6299072 git-svn-id: http://skia.googlecode.com/svn/trunk@4236 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkBitmap.h | 90 +------------------ include/core/SkColorTable.h | 110 ++++++++++++++++++++++++ include/core/SkFlattenable.h | 6 +- include/utils/SkSfntUtils.h | 1 + src/core/SkBitmap.cpp | 4 +- src/core/SkColorTable.cpp | 7 +- src/core/SkMallocPixelRef.cpp | 4 +- src/effects/SkBlurImageFilter.cpp | 1 + src/effects/SkMorphologyImageFilter.cpp | 1 + 9 files changed, 124 insertions(+), 100 deletions(-) create mode 100644 include/core/SkColorTable.h diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h index 8ce3f67604..514357f6cc 100644 --- a/include/core/SkBitmap.h +++ b/include/core/SkBitmap.h @@ -12,6 +12,7 @@ #include "Sk64.h" #include "SkColor.h" +#include "SkColorTable.h" #include "SkPoint.h" #include "SkRefCnt.h" @@ -645,95 +646,6 @@ private: static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy); }; -/** \class SkColorTable - - SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by - 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable. -*/ -class SkColorTable : public SkRefCnt { -public: - /** Makes a deep copy of colors. - */ - SkColorTable(const SkColorTable& src); - /** Preallocates the colortable to have 'count' colors, which - * are initially set to 0. - */ - explicit SkColorTable(int count); - explicit SkColorTable(SkFlattenableReadBuffer&); - SkColorTable(const SkPMColor colors[], int count); - virtual ~SkColorTable(); - - enum Flags { - kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the table are opaque (alpha==0xFF) - }; - /** Returns the flag bits for the color table. These can be changed with setFlags(). - */ - unsigned getFlags() const { return fFlags; } - /** Set the flags for the color table. See the Flags enum for possible values. - */ - void setFlags(unsigned flags); - - bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; } - void setIsOpaque(bool isOpaque); - - /** Returns the number of colors in the table. - */ - int count() const { return fCount; } - - /** Returns the specified color from the table. In the debug build, this asserts that - the index is in range (0 <= index < count). - */ - SkPMColor operator[](int index) const { - SkASSERT(fColors != NULL && (unsigned)index < fCount); - return fColors[index]; - } - - /** Specify the number of colors in the color table. This does not initialize the colors - to any value, just allocates memory for them. To initialize the values, either call - setColors(array, count), or follow setCount(count) with a call to - lockColors()/{set the values}/unlockColors(true). - */ -// void setColors(int count) { this->setColors(NULL, count); } -// void setColors(const SkPMColor[], int count); - - /** Return the array of colors for reading and/or writing. This must be - balanced by a call to unlockColors(changed?), telling the colortable if - the colors were changed during the lock. - */ - SkPMColor* lockColors() { - SkDEBUGCODE(fColorLockCount += 1;) - return fColors; - } - /** Balancing call to lockColors(). If the colors have been changed, pass true. - */ - void unlockColors(bool changed); - - /** Similar to lockColors(), lock16BitCache() returns the array of - RGB16 colors that mirror the 32bit colors. However, this function - will return null if kColorsAreOpaque_Flag is not set. - Also, unlike lockColors(), the returned array here cannot be modified. - */ - const uint16_t* lock16BitCache(); - /** Balancing call to lock16BitCache(). - */ - void unlock16BitCache() { - SkASSERT(f16BitCacheLockCount > 0); - SkDEBUGCODE(f16BitCacheLockCount -= 1); - } - - void flatten(SkFlattenableWriteBuffer&) const; - -private: - SkPMColor* fColors; - uint16_t* f16BitCache; - uint16_t fCount; - uint8_t fFlags; - SkDEBUGCODE(int fColorLockCount;) - SkDEBUGCODE(int f16BitCacheLockCount;) - - void inval16BitCache(); -}; - class SkAutoLockPixels : public SkNoncopyable { public: SkAutoLockPixels(const SkBitmap& bm, bool doLock = true) : fBitmap(bm) { diff --git a/include/core/SkColorTable.h b/include/core/SkColorTable.h new file mode 100644 index 0000000000..de6cb832f4 --- /dev/null +++ b/include/core/SkColorTable.h @@ -0,0 +1,110 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#ifndef SkColorTable_DEFINED +#define SkColorTable_DEFINED + +#include "SkColor.h" +#include "SkFlattenable.h" + +/** \class SkColorTable + + SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by + 8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable. +*/ +class SkColorTable : public SkFlattenable { +public: + /** Makes a deep copy of colors. + */ + SkColorTable(const SkColorTable& src); + /** Preallocates the colortable to have 'count' colors, which + * are initially set to 0. + */ + explicit SkColorTable(int count); + SkColorTable(const SkPMColor colors[], int count); + virtual ~SkColorTable(); + + enum Flags { + kColorsAreOpaque_Flag = 0x01 //!< if set, all of the colors in the table are opaque (alpha==0xFF) + }; + /** Returns the flag bits for the color table. These can be changed with setFlags(). + */ + unsigned getFlags() const { return fFlags; } + /** Set the flags for the color table. See the Flags enum for possible values. + */ + void setFlags(unsigned flags); + + bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; } + void setIsOpaque(bool isOpaque); + + /** Returns the number of colors in the table. + */ + int count() const { return fCount; } + + /** Returns the specified color from the table. In the debug build, this asserts that + the index is in range (0 <= index < count). + */ + SkPMColor operator[](int index) const { + SkASSERT(fColors != NULL && (unsigned)index < fCount); + return fColors[index]; + } + + /** Specify the number of colors in the color table. This does not initialize the colors + to any value, just allocates memory for them. To initialize the values, either call + setColors(array, count), or follow setCount(count) with a call to + lockColors()/{set the values}/unlockColors(true). + */ +// void setColors(int count) { this->setColors(NULL, count); } +// void setColors(const SkPMColor[], int count); + + /** Return the array of colors for reading and/or writing. This must be + balanced by a call to unlockColors(changed?), telling the colortable if + the colors were changed during the lock. + */ + SkPMColor* lockColors() { + SkDEBUGCODE(fColorLockCount += 1;) + return fColors; + } + /** Balancing call to lockColors(). If the colors have been changed, pass true. + */ + void unlockColors(bool changed); + + /** Similar to lockColors(), lock16BitCache() returns the array of + RGB16 colors that mirror the 32bit colors. However, this function + will return null if kColorsAreOpaque_Flag is not set. + Also, unlike lockColors(), the returned array here cannot be modified. + */ + const uint16_t* lock16BitCache(); + /** Balancing call to lock16BitCache(). + */ + void unlock16BitCache() { + SkASSERT(f16BitCacheLockCount > 0); + SkDEBUGCODE(f16BitCacheLockCount -= 1); + } + + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorTable) + +protected: + explicit SkColorTable(SkFlattenableReadBuffer&); + void flatten(SkFlattenableWriteBuffer&) const; + +private: + SkPMColor* fColors; + uint16_t* f16BitCache; + uint16_t fCount; + uint8_t fFlags; + SkDEBUGCODE(int fColorLockCount;) + SkDEBUGCODE(int f16BitCacheLockCount;) + + void inval16BitCache(); + + typedef SkFlattenable INHERITED; +}; + +#endif diff --git a/include/core/SkFlattenable.h b/include/core/SkFlattenable.h index 253f5710e0..c33ff67ddd 100644 --- a/include/core/SkFlattenable.h +++ b/include/core/SkFlattenable.h @@ -11,15 +11,15 @@ #define SkFlattenable_DEFINED #include "SkRefCnt.h" -#include "SkBitmap.h" -#include "SkPath.h" -#include "SkPoint.h" #include "SkReader32.h" #include "SkTDArray.h" #include "SkWriter32.h" +class SkBitmap; class SkFlattenableReadBuffer; class SkFlattenableWriteBuffer; +class SkPath; +class SkPoint; class SkString; #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS diff --git a/include/utils/SkSfntUtils.h b/include/utils/SkSfntUtils.h index 69c9c03980..d87a4ed665 100644 --- a/include/utils/SkSfntUtils.h +++ b/include/utils/SkSfntUtils.h @@ -9,6 +9,7 @@ #ifndef SkSfntUtils_DEFINED #define SkSfntUtils_DEFINED +#include "Sk64.h" #include "SkFontHost.h" struct SkSfntTable_head { diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index aff52fd84a..6d5942f9a9 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -1429,7 +1429,7 @@ void SkBitmap::flatten(SkFlattenableWriteBuffer& buffer) const { } else if (fPixels) { if (fColorTable) { buffer.write8(SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE); - fColorTable->flatten(buffer); + buffer.writeFlattenable(fColorTable); } else { buffer.write8(SERIALIZE_PIXELTYPE_RAW_NO_CTABLE); } @@ -1475,7 +1475,7 @@ void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { case SERIALIZE_PIXELTYPE_RAW_NO_CTABLE: { SkColorTable* ctable = NULL; if (SERIALIZE_PIXELTYPE_RAW_WITH_CTABLE == reftype) { - ctable = SkNEW_ARGS(SkColorTable, (buffer)); + ctable = static_cast(buffer.readFlattenable()); } size_t size = this->getSize(); if (this->allocPixels(ctable)) { diff --git a/src/core/SkColorTable.cpp b/src/core/SkColorTable.cpp index 4a9480dfde..0aa13704f7 100644 --- a/src/core/SkColorTable.cpp +++ b/src/core/SkColorTable.cpp @@ -7,8 +7,7 @@ */ -#include "SkBitmap.h" -#include "SkFlattenable.h" +#include "SkColorTable.h" #include "SkStream.h" #include "SkTemplates.h" @@ -28,8 +27,7 @@ SkColorTable::SkColorTable(int count) SkDEBUGCODE(f16BitCacheLockCount = 0;) } -// call SkRefCnt's constructor explicitly, to avoid warning -SkColorTable::SkColorTable(const SkColorTable& src) : SkRefCnt() { +SkColorTable::SkColorTable(const SkColorTable& src) : INHERITED() { f16BitCache = NULL; fFlags = src.fFlags; int count = src.count(); @@ -156,3 +154,4 @@ void SkColorTable::flatten(SkFlattenableWriteBuffer& buffer) const { buffer.writeMul4(fColors, count * sizeof(SkPMColor)); } +SK_DEFINE_FLATTENABLE_REGISTRAR(SkColorTable) diff --git a/src/core/SkMallocPixelRef.cpp b/src/core/SkMallocPixelRef.cpp index 14a064654f..4bcf1bd16d 100644 --- a/src/core/SkMallocPixelRef.cpp +++ b/src/core/SkMallocPixelRef.cpp @@ -43,7 +43,7 @@ void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { buffer.writePad(fStorage, fSize); if (fCTable) { buffer.writeBool(true); - fCTable->flatten(buffer); + buffer.writeFlattenable(fCTable); } else { buffer.writeBool(false); } @@ -55,7 +55,7 @@ SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) fStorage = sk_malloc_throw(fSize); buffer.read(fStorage, fSize); if (buffer.readBool()) { - fCTable = SkNEW_ARGS(SkColorTable, (buffer)); + fCTable = static_cast(buffer.readFlattenable()); } else { fCTable = NULL; } diff --git a/src/effects/SkBlurImageFilter.cpp b/src/effects/SkBlurImageFilter.cpp index 522f4b05ea..e3ac346653 100644 --- a/src/effects/SkBlurImageFilter.cpp +++ b/src/effects/SkBlurImageFilter.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "SkBitmap.h" #include "SkBlurImageFilter.h" #include "SkColorPriv.h" diff --git a/src/effects/SkMorphologyImageFilter.cpp b/src/effects/SkMorphologyImageFilter.cpp index 2de4b3c5cd..869ba6d97e 100644 --- a/src/effects/SkMorphologyImageFilter.cpp +++ b/src/effects/SkMorphologyImageFilter.cpp @@ -6,6 +6,7 @@ */ #include "SkMorphologyImageFilter.h" +#include "SkBitmap.h" #include "SkColorPriv.h" SkMorphologyImageFilter::SkMorphologyImageFilter(SkFlattenableReadBuffer& buffer)