force readbuffer clients to use specialized readFoo for flattenables
BUG=
R=mtklein@google.com
Review URL: https://codereview.chromium.org/26702002
git-svn-id: http://skia.googlecode.com/svn/trunk@11803 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkFlattenableBuffers.h b/include/core/SkFlattenableBuffers.h
index 8e1652a..51016c7 100644
--- a/include/core/SkFlattenableBuffers.h
+++ b/include/core/SkFlattenableBuffers.h
@@ -15,6 +15,7 @@
#include "SkPoint.h"
class SkBitmap;
+class SkDrawLooper;
class SkFlattenable;
struct SkIRect;
class SkMatrix;
@@ -28,8 +29,22 @@
class SkStream;
class SkString;
class SkTypeface;
+class SkUnitMapper;
class SkWStream;
+enum SkEffectType {
+ kColorFilter_SkEffectType,
+ kDrawLooper_SkEffectType,
+ kImageFilter_SkEffectType,
+ kMaskFilter_SkEffectType,
+ kPathEffect_SkEffectType,
+ kPixelRef_SkEffectType,
+ kRasterizer_SkEffectType,
+ kShader_SkEffectType,
+ kUnitMapper_SkEffectType,
+ kXfermode_SkEffectType,
+};
+
class SkFlattenableReadBuffer {
public:
SkFlattenableReadBuffer();
@@ -64,8 +79,40 @@
virtual void readString(SkString* string) = 0;
virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) = 0;
+ virtual SkFlattenable* readFlattenable(SkEffectType) = 0;
+
+ SkColorFilter* readColorFilter() {
+ return (SkColorFilter*)this->readFlattenable(kColorFilter_SkEffectType);
+ }
+ SkDrawLooper* readDrawLooper() {
+ return (SkDrawLooper*)this->readFlattenable(kDrawLooper_SkEffectType);
+ }
+ SkImageFilter* readImageFilter() {
+ return (SkImageFilter*)this->readFlattenable(kImageFilter_SkEffectType);
+ }
+ SkMaskFilter* readMaskFilter() {
+ return (SkMaskFilter*)this->readFlattenable(kMaskFilter_SkEffectType);
+ }
+ SkPathEffect* readPathEffect() {
+ return (SkPathEffect*)this->readFlattenable(kPathEffect_SkEffectType);
+ }
+ SkPixelRef* readPixelRef() {
+ return (SkPixelRef*)this->readFlattenable(kPixelRef_SkEffectType);
+ }
+ SkRasterizer* readRasterizer() {
+ return (SkRasterizer*)this->readFlattenable(kRasterizer_SkEffectType);
+ }
+ SkShader* readShader() {
+ return (SkShader*)this->readFlattenable(kShader_SkEffectType);
+ }
+ SkUnitMapper* readUnitMapper() {
+ return (SkUnitMapper*)this->readFlattenable(kUnitMapper_SkEffectType);
+ }
+ SkXfermode* readXfermode() {
+ return (SkXfermode*)this->readFlattenable(kXfermode_SkEffectType);
+ }
+
// common data structures
- virtual SkFlattenable* readFlattenable() = 0;
virtual void readPoint(SkPoint* point) = 0;
virtual void readMatrix(SkMatrix* matrix) = 0;
virtual void readIRect(SkIRect* rect) = 0;
@@ -106,10 +153,6 @@
return SkData::NewFromMalloc(buffer, len);
}
- template <typename T> T* readFlattenableT() {
- return static_cast<T*>(this->readFlattenable());
- }
-
private:
uint32_t fFlags;
};
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index 434c817..63a760c 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -1610,7 +1610,7 @@
switch (reftype) {
case SERIALIZE_PIXELTYPE_REF_DATA: {
size_t offset = buffer.readUInt();
- SkPixelRef* pr = buffer.readFlattenableT<SkPixelRef>();
+ SkPixelRef* pr = buffer.readPixelRef();
SkSafeUnref(this->setPixelRef(pr, offset));
break;
}
diff --git a/src/core/SkBlitter.cpp b/src/core/SkBlitter.cpp
index 162fdbc..6ba82bf 100644
--- a/src/core/SkBlitter.cpp
+++ b/src/core/SkBlitter.cpp
@@ -683,7 +683,7 @@
protected:
Sk3DShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
- fProxy = buffer.readFlattenableT<SkShader>();
+ fProxy = buffer.readShader();
fPMColor = buffer.readColor();
fMask = NULL;
}
diff --git a/src/core/SkComposeShader.cpp b/src/core/SkComposeShader.cpp
index de0ffdb..206608a 100644
--- a/src/core/SkComposeShader.cpp
+++ b/src/core/SkComposeShader.cpp
@@ -27,15 +27,15 @@
SkComposeShader::SkComposeShader(SkFlattenableReadBuffer& buffer) :
INHERITED(buffer) {
- fShaderA = buffer.readFlattenableT<SkShader>();
+ fShaderA = buffer.readShader();
if (NULL == fShaderA) {
fShaderA = SkNEW_ARGS(SkColorShader, (0));
}
- fShaderB = buffer.readFlattenableT<SkShader>();
+ fShaderB = buffer.readShader();
if (NULL == fShaderB) {
fShaderB = SkNEW_ARGS(SkColorShader, (0));
}
- fMode = buffer.readFlattenableT<SkXfermode>();
+ fMode = buffer.readXfermode();
}
SkComposeShader::~SkComposeShader() {
diff --git a/src/core/SkFilterShader.cpp b/src/core/SkFilterShader.cpp
index 4fcb936..1e590cb 100644
--- a/src/core/SkFilterShader.cpp
+++ b/src/core/SkFilterShader.cpp
@@ -22,8 +22,8 @@
SkFilterShader::SkFilterShader(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer) {
- fShader = buffer.readFlattenableT<SkShader>();
- fFilter = buffer.readFlattenableT<SkColorFilter>();
+ fShader = buffer.readShader();
+ fFilter = buffer.readColorFilter();
}
SkFilterShader::~SkFilterShader() {
diff --git a/src/core/SkFlattenableBuffers.cpp b/src/core/SkFlattenableBuffers.cpp
index 50a47d5..28cc76d 100644
--- a/src/core/SkFlattenableBuffers.cpp
+++ b/src/core/SkFlattenableBuffers.cpp
@@ -54,3 +54,4 @@
void SkFlattenableWriteBuffer::flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer) {
obj->flatten(buffer);
}
+
diff --git a/src/core/SkFlattenableSerialization.cpp b/src/core/SkFlattenableSerialization.cpp
index b74c82f..cda3182 100644
--- a/src/core/SkFlattenableSerialization.cpp
+++ b/src/core/SkFlattenableSerialization.cpp
@@ -9,6 +9,7 @@
#include "SkData.h"
#include "SkFlattenable.h"
+#include "SkImageFilter.h"
#include "SkOrderedReadBuffer.h"
#include "SkOrderedWriteBuffer.h"
@@ -22,7 +23,9 @@
return SkData::NewFromMalloc(data, size);
}
+// TODO: this guy should be renamed to ImageFilter, or take SkEffectType as
+// a parameter.
SkFlattenable* SkDeserializeFlattenable(const void* data, size_t size) {
SkOrderedReadBuffer buffer(data, size);
- return buffer.readFlattenable();
+ return buffer.readImageFilter();
}
diff --git a/src/core/SkImageFilter.cpp b/src/core/SkImageFilter.cpp
index 3b7de25..c8c0c4d 100644
--- a/src/core/SkImageFilter.cpp
+++ b/src/core/SkImageFilter.cpp
@@ -68,7 +68,7 @@
: fInputCount(buffer.readInt()), fInputs(new SkImageFilter*[fInputCount]) {
for (int i = 0; i < fInputCount; i++) {
if (buffer.readBool()) {
- fInputs[i] = static_cast<SkImageFilter*>(buffer.readFlattenable());
+ fInputs[i] = buffer.readImageFilter();
} else {
fInputs[i] = NULL;
}
diff --git a/src/core/SkOrderedReadBuffer.cpp b/src/core/SkOrderedReadBuffer.cpp
index 9d99194..aff6e20 100644
--- a/src/core/SkOrderedReadBuffer.cpp
+++ b/src/core/SkOrderedReadBuffer.cpp
@@ -268,7 +268,11 @@
}
}
-SkFlattenable* SkOrderedReadBuffer::readFlattenable() {
+SkFlattenable* SkOrderedReadBuffer::readFlattenable(SkEffectType et) {
+ //
+ // TODO: confirm that et matches the factory we decide to use
+ //
+
SkFlattenable::Factory factory = NULL;
if (fFactoryCount > 0) {
diff --git a/src/core/SkOrderedReadBuffer.h b/src/core/SkOrderedReadBuffer.h
index 556fce8..b29f310 100644
--- a/src/core/SkOrderedReadBuffer.h
+++ b/src/core/SkOrderedReadBuffer.h
@@ -52,7 +52,7 @@
virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) SK_OVERRIDE;
// common data structures
- virtual SkFlattenable* readFlattenable() SK_OVERRIDE;
+ virtual SkFlattenable* readFlattenable(SkEffectType) SK_OVERRIDE;
virtual void readPoint(SkPoint* point) SK_OVERRIDE;
virtual void readMatrix(SkMatrix* matrix) SK_OVERRIDE;
virtual void readIRect(SkIRect* rect) SK_OVERRIDE;
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index cfef8b1..44eef44 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -2178,14 +2178,14 @@
}
if (flatFlags & kHasEffects_FlatFlag) {
- SkSafeUnref(this->setPathEffect(buffer.readFlattenableT<SkPathEffect>()));
- SkSafeUnref(this->setShader(buffer.readFlattenableT<SkShader>()));
- SkSafeUnref(this->setXfermode(buffer.readFlattenableT<SkXfermode>()));
- SkSafeUnref(this->setMaskFilter(buffer.readFlattenableT<SkMaskFilter>()));
- SkSafeUnref(this->setColorFilter(buffer.readFlattenableT<SkColorFilter>()));
- SkSafeUnref(this->setRasterizer(buffer.readFlattenableT<SkRasterizer>()));
- SkSafeUnref(this->setLooper(buffer.readFlattenableT<SkDrawLooper>()));
- SkSafeUnref(this->setImageFilter(buffer.readFlattenableT<SkImageFilter>()));
+ SkSafeUnref(this->setPathEffect(buffer.readPathEffect()));
+ SkSafeUnref(this->setShader(buffer.readShader()));
+ SkSafeUnref(this->setXfermode(buffer.readXfermode()));
+ SkSafeUnref(this->setMaskFilter(buffer.readMaskFilter()));
+ SkSafeUnref(this->setColorFilter(buffer.readColorFilter()));
+ SkSafeUnref(this->setRasterizer(buffer.readRasterizer()));
+ SkSafeUnref(this->setLooper(buffer.readDrawLooper()));
+ SkSafeUnref(this->setImageFilter(buffer.readImageFilter()));
if (buffer.readBool()) {
this->setAnnotation(SkNEW_ARGS(SkAnnotation, (buffer)))->unref();
diff --git a/src/core/SkPathEffect.cpp b/src/core/SkPathEffect.cpp
index 8306d7a..e7f6822 100644
--- a/src/core/SkPathEffect.cpp
+++ b/src/core/SkPathEffect.cpp
@@ -48,8 +48,8 @@
}
SkPairPathEffect::SkPairPathEffect(SkFlattenableReadBuffer& buffer) {
- fPE0 = buffer.readFlattenableT<SkPathEffect>();
- fPE1 = buffer.readFlattenableT<SkPathEffect>();
+ fPE0 = buffer.readPathEffect();
+ fPE1 = buffer.readPathEffect();
// either of these may fail, so we have to check for nulls later on
}
diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp
index ee9d919..83cf8f8 100644
--- a/src/core/SkScalerContext.cpp
+++ b/src/core/SkScalerContext.cpp
@@ -66,14 +66,15 @@
#define DUMP_RECx
#endif
-static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag) {
+static SkFlattenable* load_flattenable(const SkDescriptor* desc, uint32_t tag,
+ SkEffectType et) {
SkFlattenable* obj = NULL;
uint32_t len;
const void* data = desc->findEntry(tag, &len);
if (data) {
SkOrderedReadBuffer buffer(data, len);
- obj = buffer.readFlattenable();
+ obj = buffer.readFlattenable(et);
SkASSERT(buffer.offset() == buffer.size());
}
return obj;
@@ -84,9 +85,9 @@
, fBaseGlyphCount(0)
, fTypeface(SkRef(typeface))
- , fPathEffect(static_cast<SkPathEffect*>(load_flattenable(desc, kPathEffect_SkDescriptorTag)))
- , fMaskFilter(static_cast<SkMaskFilter*>(load_flattenable(desc, kMaskFilter_SkDescriptorTag)))
- , fRasterizer(static_cast<SkRasterizer*>(load_flattenable(desc, kRasterizer_SkDescriptorTag)))
+ , fPathEffect(static_cast<SkPathEffect*>(load_flattenable(desc, kPathEffect_SkDescriptorTag, kPathEffect_SkEffectType)))
+ , fMaskFilter(static_cast<SkMaskFilter*>(load_flattenable(desc, kMaskFilter_SkDescriptorTag, kMaskFilter_SkEffectType)))
+ , fRasterizer(static_cast<SkRasterizer*>(load_flattenable(desc, kRasterizer_SkDescriptorTag, kRasterizer_SkEffectType)))
// Initialize based on our settings. Subclasses can also force this.
, fGenerateImageFromPath(fRec.fFrameWidth > 0 || fPathEffect != NULL || fRasterizer != NULL)
diff --git a/src/effects/SkBlurDrawLooper.cpp b/src/effects/SkBlurDrawLooper.cpp
index e8407f9..51e6ec9 100644
--- a/src/effects/SkBlurDrawLooper.cpp
+++ b/src/effects/SkBlurDrawLooper.cpp
@@ -69,8 +69,8 @@
fDx = buffer.readScalar();
fDy = buffer.readScalar();
fBlurColor = buffer.readColor();
- fBlur = buffer.readFlattenableT<SkMaskFilter>();
- fColorFilter = buffer.readFlattenableT<SkColorFilter>();
+ fBlur = buffer.readMaskFilter();
+ fColorFilter = buffer.readColorFilter();
fBlurFlags = buffer.readUInt() & kAll_BlurFlag;
}
diff --git a/src/effects/SkColorFilterImageFilter.cpp b/src/effects/SkColorFilterImageFilter.cpp
index a756a47..fdcc7ff 100755
--- a/src/effects/SkColorFilterImageFilter.cpp
+++ b/src/effects/SkColorFilterImageFilter.cpp
@@ -83,7 +83,7 @@
}
SkColorFilterImageFilter::SkColorFilterImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
- fColorFilter = buffer.readFlattenableT<SkColorFilter>();
+ fColorFilter = buffer.readColorFilter();
}
void SkColorFilterImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
diff --git a/src/effects/SkRectShaderImageFilter.cpp b/src/effects/SkRectShaderImageFilter.cpp
index a240907..e99899e 100644
--- a/src/effects/SkRectShaderImageFilter.cpp
+++ b/src/effects/SkRectShaderImageFilter.cpp
@@ -45,7 +45,7 @@
SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer) {
- fShader = buffer.readFlattenableT<SkShader>();
+ fShader = buffer.readShader();
}
void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp
index 5175fd4..4d30f00 100644
--- a/src/effects/SkXfermodeImageFilter.cpp
+++ b/src/effects/SkXfermodeImageFilter.cpp
@@ -34,7 +34,7 @@
SkXfermodeImageFilter::SkXfermodeImageFilter(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer) {
- fMode = buffer.readFlattenableT<SkXfermode>();
+ fMode = buffer.readXfermode();
}
void SkXfermodeImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index 9360ba9..fb8bbac 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -146,7 +146,7 @@
SkGradientShaderBase::SkGradientShaderBase(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
fCacheAlpha = 256;
- fMapper = buffer.readFlattenableT<SkUnitMapper>();
+ fMapper = buffer.readUnitMapper();
fCache16 = fCache16Storage = NULL;
fCache32 = NULL;
diff --git a/src/pipe/SkGPipePriv.h b/src/pipe/SkGPipePriv.h
index 2954ebf..9272496 100644
--- a/src/pipe/SkGPipePriv.h
+++ b/src/pipe/SkGPipePriv.h
@@ -19,11 +19,11 @@
enum PaintFlats {
kColorFilter_PaintFlat,
kDrawLooper_PaintFlat,
+ kImageFilter_PaintFlat,
kMaskFilter_PaintFlat,
kPathEffect_PaintFlat,
kRasterizer_PaintFlat,
kShader_PaintFlat,
- kImageFilter_PaintFlat,
kXfermode_PaintFlat,
kLast_PaintFlat = kXfermode_PaintFlat
diff --git a/src/pipe/SkGPipeRead.cpp b/src/pipe/SkGPipeRead.cpp
index ac0aadd..e76ec12 100644
--- a/src/pipe/SkGPipeRead.cpp
+++ b/src/pipe/SkGPipeRead.cpp
@@ -18,6 +18,7 @@
#include "SkAnnotation.h"
#include "SkColorFilter.h"
#include "SkDrawLooper.h"
+#include "SkImageFilter.h"
#include "SkMaskFilter.h"
#include "SkOrderedReadBuffer.h"
#include "SkPathEffect.h"
@@ -27,6 +28,22 @@
#include "SkTypeface.h"
#include "SkXfermode.h"
+static SkEffectType paintflat_to_effecttype(PaintFlats pf) {
+ static const uint8_t gEffectTypesInPaintFlatsOrder[] = {
+ kColorFilter_SkEffectType,
+ kDrawLooper_SkEffectType,
+ kImageFilter_SkEffectType,
+ kMaskFilter_SkEffectType,
+ kPathEffect_SkEffectType,
+ kRasterizer_SkEffectType,
+ kShader_SkEffectType,
+ kXfermode_SkEffectType,
+ };
+
+ SkASSERT((size_t)pf < SK_ARRAY_COUNT(gEffectTypesInPaintFlatsOrder));
+ return (SkEffectType)gEffectTypesInPaintFlatsOrder[pf];
+}
+
static void set_paintflat(SkPaint* paint, SkFlattenable* obj, unsigned paintFlat) {
SkASSERT(paintFlat < kCount_PaintFlats);
switch (paintFlat) {
@@ -105,7 +122,7 @@
void defFlattenable(PaintFlats pf, int index) {
index--;
- SkFlattenable* obj = fReader->readFlattenable();
+ SkFlattenable* obj = fReader->readFlattenable(paintflat_to_effecttype(pf));
if (fFlatArray.count() == index) {
*fFlatArray.append() = obj;
} else {
diff --git a/tests/ColorFilterTest.cpp b/tests/ColorFilterTest.cpp
index 8c96d75..544c9f0 100644
--- a/tests/ColorFilterTest.cpp
+++ b/tests/ColorFilterTest.cpp
@@ -15,7 +15,7 @@
#include "SkOrderedReadBuffer.h"
#include "SkOrderedWriteBuffer.h"
-static SkFlattenable* reincarnate_flattenable(SkFlattenable* obj) {
+static SkColorFilter* reincarnate_colorfilter(SkFlattenable* obj) {
SkOrderedWriteBuffer wb(1024);
wb.writeFlattenable(obj);
@@ -25,11 +25,7 @@
wb.writeToMemory(storage.get());
SkOrderedReadBuffer rb(storage.get(), size);
- return rb.readFlattenable();
-}
-
-template <typename T> T* reincarnate(T* obj) {
- return (T*)reincarnate_flattenable(obj);
+ return rb.readColorFilter();
}
///////////////////////////////////////////////////////////////////////////////
@@ -82,7 +78,7 @@
REPORTER_ASSERT(reporter, m == expectedMode);
{
- SkColorFilter* cf2 = reincarnate(cf);
+ SkColorFilter* cf2 = reincarnate_colorfilter(cf);
SkAutoUnref aur2(cf2);
REPORTER_ASSERT(reporter, cf2);