diff --git a/include/core/SkFlattenableBuffers.h b/include/core/SkFlattenableBuffers.h index 8e1652a775..51016c737f 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 SkRegion; 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 @@ public: 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 @@ public: return SkData::NewFromMalloc(buffer, len); } - template T* readFlattenableT() { - return static_cast(this->readFlattenable()); - } - private: uint32_t fFlags; }; diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index 434c817e52..63a760c4cf 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -1610,7 +1610,7 @@ void SkBitmap::unflatten(SkFlattenableReadBuffer& buffer) { switch (reftype) { case SERIALIZE_PIXELTYPE_REF_DATA: { size_t offset = buffer.readUInt(); - SkPixelRef* pr = buffer.readFlattenableT(); + SkPixelRef* pr = buffer.readPixelRef(); SkSafeUnref(this->setPixelRef(pr, offset)); break; } diff --git a/src/core/SkBlitter.cpp b/src/core/SkBlitter.cpp index 162fdbcd29..6ba82bf077 100644 --- a/src/core/SkBlitter.cpp +++ b/src/core/SkBlitter.cpp @@ -683,7 +683,7 @@ public: protected: Sk3DShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fProxy = buffer.readFlattenableT(); + fProxy = buffer.readShader(); fPMColor = buffer.readColor(); fMask = NULL; } diff --git a/src/core/SkComposeShader.cpp b/src/core/SkComposeShader.cpp index de0ffdb24a..206608a4e0 100644 --- a/src/core/SkComposeShader.cpp +++ b/src/core/SkComposeShader.cpp @@ -27,15 +27,15 @@ SkComposeShader::SkComposeShader(SkShader* sA, SkShader* sB, SkXfermode* mode) { SkComposeShader::SkComposeShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fShaderA = buffer.readFlattenableT(); + fShaderA = buffer.readShader(); if (NULL == fShaderA) { fShaderA = SkNEW_ARGS(SkColorShader, (0)); } - fShaderB = buffer.readFlattenableT(); + fShaderB = buffer.readShader(); if (NULL == fShaderB) { fShaderB = SkNEW_ARGS(SkColorShader, (0)); } - fMode = buffer.readFlattenableT(); + fMode = buffer.readXfermode(); } SkComposeShader::~SkComposeShader() { diff --git a/src/core/SkFilterShader.cpp b/src/core/SkFilterShader.cpp index 4fcb9361f1..1e590cb66f 100644 --- a/src/core/SkFilterShader.cpp +++ b/src/core/SkFilterShader.cpp @@ -22,8 +22,8 @@ SkFilterShader::SkFilterShader(SkShader* shader, SkColorFilter* filter) { SkFilterShader::SkFilterShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fShader = buffer.readFlattenableT(); - fFilter = buffer.readFlattenableT(); + fShader = buffer.readShader(); + fFilter = buffer.readColorFilter(); } SkFilterShader::~SkFilterShader() { diff --git a/src/core/SkFlattenableBuffers.cpp b/src/core/SkFlattenableBuffers.cpp index 50a47d5c47..28cc76db37 100644 --- a/src/core/SkFlattenableBuffers.cpp +++ b/src/core/SkFlattenableBuffers.cpp @@ -54,3 +54,4 @@ void SkFlattenableWriteBuffer::writePaint(const SkPaint& paint) { void SkFlattenableWriteBuffer::flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer) { obj->flatten(buffer); } + diff --git a/src/core/SkFlattenableSerialization.cpp b/src/core/SkFlattenableSerialization.cpp index b74c82f051..cda3182835 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 @@ SkData* SkSerializeFlattenable(SkFlattenable* flattenable) { 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 3b7de25c93..c8c0c4dbf8 100644 --- a/src/core/SkImageFilter.cpp +++ b/src/core/SkImageFilter.cpp @@ -68,7 +68,7 @@ SkImageFilter::SkImageFilter(SkFlattenableReadBuffer& buffer) : fInputCount(buffer.readInt()), fInputs(new SkImageFilter*[fInputCount]) { for (int i = 0; i < fInputCount; i++) { if (buffer.readBool()) { - fInputs[i] = static_cast(buffer.readFlattenable()); + fInputs[i] = buffer.readImageFilter(); } else { fInputs[i] = NULL; } diff --git a/src/core/SkOrderedReadBuffer.cpp b/src/core/SkOrderedReadBuffer.cpp index 9d99194908..aff6e20e7b 100644 --- a/src/core/SkOrderedReadBuffer.cpp +++ b/src/core/SkOrderedReadBuffer.cpp @@ -268,7 +268,11 @@ SkTypeface* SkOrderedReadBuffer::readTypeface() { } } -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 556fce8b41..b29f310baa 100644 --- a/src/core/SkOrderedReadBuffer.h +++ b/src/core/SkOrderedReadBuffer.h @@ -52,7 +52,7 @@ public: 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 cfef8b18cd..44eef44880 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -2178,14 +2178,14 @@ void SkPaint::unflatten(SkFlattenableReadBuffer& buffer) { } if (flatFlags & kHasEffects_FlatFlag) { - SkSafeUnref(this->setPathEffect(buffer.readFlattenableT())); - SkSafeUnref(this->setShader(buffer.readFlattenableT())); - SkSafeUnref(this->setXfermode(buffer.readFlattenableT())); - SkSafeUnref(this->setMaskFilter(buffer.readFlattenableT())); - SkSafeUnref(this->setColorFilter(buffer.readFlattenableT())); - SkSafeUnref(this->setRasterizer(buffer.readFlattenableT())); - SkSafeUnref(this->setLooper(buffer.readFlattenableT())); - SkSafeUnref(this->setImageFilter(buffer.readFlattenableT())); + 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 8306d7aa51..e7f68225c3 100644 --- a/src/core/SkPathEffect.cpp +++ b/src/core/SkPathEffect.cpp @@ -48,8 +48,8 @@ void SkPairPathEffect::flatten(SkFlattenableWriteBuffer& buffer) const { } SkPairPathEffect::SkPairPathEffect(SkFlattenableReadBuffer& buffer) { - fPE0 = buffer.readFlattenableT(); - fPE1 = buffer.readFlattenableT(); + 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 ee9d919427..83cf8f8006 100644 --- a/src/core/SkScalerContext.cpp +++ b/src/core/SkScalerContext.cpp @@ -66,14 +66,15 @@ void SkGlyph::zeroMetrics() { #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 @@ SkScalerContext::SkScalerContext(SkTypeface* typeface, const SkDescriptor* desc) , fBaseGlyphCount(0) , fTypeface(SkRef(typeface)) - , fPathEffect(static_cast(load_flattenable(desc, kPathEffect_SkDescriptorTag))) - , fMaskFilter(static_cast(load_flattenable(desc, kMaskFilter_SkDescriptorTag))) - , fRasterizer(static_cast(load_flattenable(desc, kRasterizer_SkDescriptorTag))) + , fPathEffect(static_cast(load_flattenable(desc, kPathEffect_SkDescriptorTag, kPathEffect_SkEffectType))) + , fMaskFilter(static_cast(load_flattenable(desc, kMaskFilter_SkDescriptorTag, kMaskFilter_SkEffectType))) + , fRasterizer(static_cast(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 e8407f9f78..51e6ec98e0 100644 --- a/src/effects/SkBlurDrawLooper.cpp +++ b/src/effects/SkBlurDrawLooper.cpp @@ -69,8 +69,8 @@ SkBlurDrawLooper::SkBlurDrawLooper(SkFlattenableReadBuffer& buffer) fDx = buffer.readScalar(); fDy = buffer.readScalar(); fBlurColor = buffer.readColor(); - fBlur = buffer.readFlattenableT(); - fColorFilter = buffer.readFlattenableT(); + fBlur = buffer.readMaskFilter(); + fColorFilter = buffer.readColorFilter(); fBlurFlags = buffer.readUInt() & kAll_BlurFlag; } diff --git a/src/effects/SkColorFilterImageFilter.cpp b/src/effects/SkColorFilterImageFilter.cpp index a756a4766c..fdcc7fff6b 100755 --- a/src/effects/SkColorFilterImageFilter.cpp +++ b/src/effects/SkColorFilterImageFilter.cpp @@ -83,7 +83,7 @@ SkColorFilterImageFilter::SkColorFilterImageFilter(SkColorFilter* cf, } SkColorFilterImageFilter::SkColorFilterImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fColorFilter = buffer.readFlattenableT(); + fColorFilter = buffer.readColorFilter(); } void SkColorFilterImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { diff --git a/src/effects/SkRectShaderImageFilter.cpp b/src/effects/SkRectShaderImageFilter.cpp index a2409076c4..e99899e191 100644 --- a/src/effects/SkRectShaderImageFilter.cpp +++ b/src/effects/SkRectShaderImageFilter.cpp @@ -45,7 +45,7 @@ SkRectShaderImageFilter::SkRectShaderImageFilter(SkShader* s, const CropRect* cr SkRectShaderImageFilter::SkRectShaderImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fShader = buffer.readFlattenableT(); + fShader = buffer.readShader(); } void SkRectShaderImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { diff --git a/src/effects/SkXfermodeImageFilter.cpp b/src/effects/SkXfermodeImageFilter.cpp index 5175fd4890..4d30f0031f 100644 --- a/src/effects/SkXfermodeImageFilter.cpp +++ b/src/effects/SkXfermodeImageFilter.cpp @@ -34,7 +34,7 @@ SkXfermodeImageFilter::~SkXfermodeImageFilter() { SkXfermodeImageFilter::SkXfermodeImageFilter(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { - fMode = buffer.readFlattenableT(); + fMode = buffer.readXfermode(); } void SkXfermodeImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp index 9360ba920e..fb8bbacdc1 100644 --- a/src/effects/gradients/SkGradientShader.cpp +++ b/src/effects/gradients/SkGradientShader.cpp @@ -146,7 +146,7 @@ static uint32_t unpack_flags(uint32_t packed) { SkGradientShaderBase::SkGradientShaderBase(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { fCacheAlpha = 256; - fMapper = buffer.readFlattenableT(); + fMapper = buffer.readUnitMapper(); fCache16 = fCache16Storage = NULL; fCache32 = NULL; diff --git a/src/pipe/SkGPipePriv.h b/src/pipe/SkGPipePriv.h index 2954ebf1f2..9272496e3b 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 ac0aaddead..e76ec1240a 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 @@ public: 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 8c96d752fb..544c9f0f25 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 @@ static SkFlattenable* reincarnate_flattenable(SkFlattenable* obj) { wb.writeToMemory(storage.get()); SkOrderedReadBuffer rb(storage.get(), size); - return rb.readFlattenable(); -} - -template T* reincarnate(T* obj) { - return (T*)reincarnate_flattenable(obj); + return rb.readColorFilter(); } /////////////////////////////////////////////////////////////////////////////// @@ -82,7 +78,7 @@ static void test_asColorMode(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, m == expectedMode); { - SkColorFilter* cf2 = reincarnate(cf); + SkColorFilter* cf2 = reincarnate_colorfilter(cf); SkAutoUnref aur2(cf2); REPORTER_ASSERT(reporter, cf2);