image serialize/deserialize callbacks in pipe

BUG=skia:

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=3607

Change-Id: I403fec72cacfc6a821f676f0afef390c89749a8b
Reviewed-on: https://skia-review.googlesource.com/3607
Commit-Queue: Mike Reed <reed@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Reed 2016-10-18 19:34:08 -04:00 committed by Skia Commit-Bot
parent 0290620b60
commit 3ac64b427a
4 changed files with 59 additions and 12 deletions

View File

@ -15,6 +15,8 @@ class SkImage;
class SkPicture; class SkPicture;
class SkTypefaceSerializer; class SkTypefaceSerializer;
class SkTypefaceDeserializer; class SkTypefaceDeserializer;
class SkImageSerializer;
class SkImageDeserializer;
class SkWStream; class SkWStream;
class SkPipeSerializer { class SkPipeSerializer {
@ -24,6 +26,7 @@ public:
// Ownership is not transferred, so caller must ceep the serializer alive // Ownership is not transferred, so caller must ceep the serializer alive
void setTypefaceSerializer(SkTypefaceSerializer*); void setTypefaceSerializer(SkTypefaceSerializer*);
void setImageSerializer(SkImageSerializer*);
void resetCache(); void resetCache();
@ -48,6 +51,7 @@ public:
// Ownership is not transferred, so caller must ceep the deserializer alive // Ownership is not transferred, so caller must ceep the deserializer alive
void setTypefaceDeserializer(SkTypefaceDeserializer*); void setTypefaceDeserializer(SkTypefaceDeserializer*);
void setImageDeserializer(SkImageDeserializer*);
sk_sp<SkImage> readImage(const SkData* data) { sk_sp<SkImage> readImage(const SkData* data) {
if (!data) { if (!data) {
@ -82,6 +86,14 @@ public:
virtual sk_sp<SkData> serialize(SkTypeface*) = 0; virtual sk_sp<SkData> serialize(SkTypeface*) = 0;
}; };
class SkImageSerializer {
public:
virtual ~SkImageSerializer() {}
virtual sk_sp<SkData> serialize(SkImage*) = 0;
};
class SkTypefaceDeserializer { class SkTypefaceDeserializer {
public: public:
virtual ~SkTypefaceDeserializer() {} virtual ~SkTypefaceDeserializer() {}
@ -89,4 +101,11 @@ public:
virtual sk_sp<SkTypeface> deserialize(const void* data, size_t size) = 0; virtual sk_sp<SkTypeface> deserialize(const void* data, size_t size) = 0;
}; };
class SkImageDeserializer {
public:
virtual ~SkImageDeserializer() {}
virtual sk_sp<SkImage> deserialize(const void*, size_t) = 0;
};
#endif #endif

View File

@ -862,6 +862,15 @@ protected:
} }
}; };
static sk_sp<SkData> default_image_serializer(SkImage* image) {
A8Serializer serial;
sk_sp<SkData> data(image->encode(&serial));
if (!data) {
data.reset(image->encode());
}
return data;
}
static bool show_deduper_traffic = false; static bool show_deduper_traffic = false;
int SkPipeDeduper::findOrDefineImage(SkImage* image) { int SkPipeDeduper::findOrDefineImage(SkImage* image) {
@ -874,11 +883,8 @@ int SkPipeDeduper::findOrDefineImage(SkImage* image) {
return index; return index;
} }
A8Serializer serial; sk_sp<SkData> data = fIMSerializer ? fIMSerializer->serialize(image)
sk_sp<SkData> data(image->encode(&serial)); : default_image_serializer(image);
if (!data) {
data.reset(image->encode());
}
if (data) { if (data) {
index = fImages.add(image->uniqueID()); index = fImages.add(image->uniqueID());
SkASSERT(index > 0); SkASSERT(index > 0);
@ -1017,6 +1023,10 @@ void SkPipeSerializer::setTypefaceSerializer(SkTypefaceSerializer* tfs) {
fImpl->fDeduper.setTypefaceSerializer(tfs); fImpl->fDeduper.setTypefaceSerializer(tfs);
} }
void SkPipeSerializer::setImageSerializer(SkImageSerializer* ims) {
fImpl->fDeduper.setImageSerializer(ims);
}
void SkPipeSerializer::resetCache() { void SkPipeSerializer::resetCache() {
fImpl->fDeduper.resetCaches(); fImpl->fDeduper.resetCaches();
} }

View File

@ -63,6 +63,7 @@ public:
void setCanvas(SkPipeCanvas* canvas) { fPipeCanvas = canvas; } void setCanvas(SkPipeCanvas* canvas) { fPipeCanvas = canvas; }
void setStream(SkWStream* stream) { fStream = stream; } void setStream(SkWStream* stream) { fStream = stream; }
void setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; } void setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; }
void setImageSerializer(SkImageSerializer* ims) { fIMSerializer = ims; }
// returns 0 if not found // returns 0 if not found
int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); } int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); }
@ -78,6 +79,7 @@ private:
SkWStream* fStream = nullptr; SkWStream* fStream = nullptr;
SkTypefaceSerializer* fTFSerializer = nullptr; SkTypefaceSerializer* fTFSerializer = nullptr;
SkImageSerializer* fIMSerializer = nullptr;
// All our keys (at the moment) are 32bit uniqueIDs // All our keys (at the moment) are 32bit uniqueIDs
SkTIndexSet<uint32_t> fImages; SkTIndexSet<uint32_t> fImages;

View File

@ -27,12 +27,13 @@ class SkPipeInflator : public SkInflator {
public: public:
SkPipeInflator(SkRefSet<SkImage>* images, SkRefSet<SkPicture>* pictures, SkPipeInflator(SkRefSet<SkImage>* images, SkRefSet<SkPicture>* pictures,
SkRefSet<SkTypeface>* typefaces, SkTDArray<SkFlattenable::Factory>* factories, SkRefSet<SkTypeface>* typefaces, SkTDArray<SkFlattenable::Factory>* factories,
SkTypefaceDeserializer* tfd) SkTypefaceDeserializer* tfd, SkImageDeserializer* imd)
: fImages(images) : fImages(images)
, fPictures(pictures) , fPictures(pictures)
, fTypefaces(typefaces) , fTypefaces(typefaces)
, fFactories(factories) , fFactories(factories)
, fTFDeserializer(tfd) , fTFDeserializer(tfd)
, fIMDeserializer(imd)
{} {}
SkImage* getImage(int index) override { SkImage* getImage(int index) override {
@ -76,8 +77,13 @@ public:
void setTypefaceDeserializer(SkTypefaceDeserializer* tfd) { void setTypefaceDeserializer(SkTypefaceDeserializer* tfd) {
fTFDeserializer = tfd; fTFDeserializer = tfd;
} }
void setImageDeserializer(SkImageDeserializer* imd) {
fIMDeserializer = imd;
}
sk_sp<SkTypeface> makeTypeface(const void* data, size_t size); sk_sp<SkTypeface> makeTypeface(const void* data, size_t size);
sk_sp<SkImage> makeImage(const sk_sp<SkData>&);
private: private:
SkRefSet<SkImage>* fImages; SkRefSet<SkImage>* fImages;
@ -86,6 +92,7 @@ private:
SkTDArray<SkFlattenable::Factory>* fFactories; SkTDArray<SkFlattenable::Factory>* fFactories;
SkTypefaceDeserializer* fTFDeserializer; SkTypefaceDeserializer* fTFDeserializer;
SkImageDeserializer* fIMDeserializer;
}; };
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@ -677,7 +684,10 @@ static sk_sp<SkImage> make_from_skiaimageformat(const void* encoded, size_t enco
return SkImage::MakeRasterData(info, pixels, width); return SkImage::MakeRasterData(info, pixels, width);
} }
static sk_sp<SkImage> make_from_encoded(const sk_sp<SkData>& data) { sk_sp<SkImage> SkPipeInflator::makeImage(const sk_sp<SkData>& data) {
if (fIMDeserializer) {
return fIMDeserializer->deserialize(data->data(), data->size());
}
sk_sp<SkImage> image = make_from_skiaimageformat(data->data(), data->size()); sk_sp<SkImage> image = make_from_skiaimageformat(data->data(), data->size());
if (!image) { if (!image) {
image = SkImage::MakeFromEncoded(data); image = SkImage::MakeFromEncoded(data);
@ -685,6 +695,7 @@ static sk_sp<SkImage> make_from_encoded(const sk_sp<SkData>& data) {
return image; return image;
} }
static void defineImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas*) { static void defineImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCanvas*) {
SkASSERT(SkPipeVerb::kDefineImage == unpack_verb(packedVerb)); SkASSERT(SkPipeVerb::kDefineImage == unpack_verb(packedVerb));
SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator(); SkPipeInflator* inflator = (SkPipeInflator*)reader.getInflator();
@ -697,7 +708,7 @@ static void defineImage_handler(SkPipeReader& reader, uint32_t packedVerb, SkCan
} else { } else {
// we are defining a new image // we are defining a new image
sk_sp<SkData> data = reader.readByteArrayAsData(); sk_sp<SkData> data = reader.readByteArrayAsData();
sk_sp<SkImage> image = make_from_encoded(data); sk_sp<SkImage> image = inflator->makeImage(data);
if (!image) { if (!image) {
SkDebugf("-- failed to decode\n"); SkDebugf("-- failed to decode\n");
} }
@ -833,6 +844,7 @@ public:
SkTDArray<SkFlattenable::Factory> fFactories; SkTDArray<SkFlattenable::Factory> fFactories;
SkTypefaceDeserializer* fTFDeserializer = nullptr; SkTypefaceDeserializer* fTFDeserializer = nullptr;
SkImageDeserializer* fIMDeserializer = nullptr;
}; };
SkPipeDeserializer::SkPipeDeserializer() : fImpl(new Impl) {} SkPipeDeserializer::SkPipeDeserializer() : fImpl(new Impl) {}
@ -842,6 +854,10 @@ void SkPipeDeserializer::setTypefaceDeserializer(SkTypefaceDeserializer* tfd) {
fImpl->fTFDeserializer = tfd; fImpl->fTFDeserializer = tfd;
} }
void SkPipeDeserializer::setImageDeserializer(SkImageDeserializer* imd) {
fImpl->fIMDeserializer = imd;
}
sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) { sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) {
if (size < sizeof(uint32_t)) { if (size < sizeof(uint32_t)) {
SkDebugf("-------- data length too short for readImage %d\n", size); SkDebugf("-------- data length too short for readImage %d\n", size);
@ -855,7 +871,7 @@ sk_sp<SkImage> SkPipeDeserializer::readImage(const void* data, size_t size) {
if (SkPipeVerb::kDefineImage == unpack_verb(packedVerb)) { if (SkPipeVerb::kDefineImage == unpack_verb(packedVerb)) {
SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures, SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
&fImpl->fTypefaces, &fImpl->fFactories, &fImpl->fTypefaces, &fImpl->fFactories,
fImpl->fTFDeserializer); fImpl->fTFDeserializer, fImpl->fIMDeserializer);
SkPipeReader reader(this, ptr, size); SkPipeReader reader(this, ptr, size);
reader.setInflator(&inflator); reader.setInflator(&inflator);
defineImage_handler(reader, packedVerb, nullptr); defineImage_handler(reader, packedVerb, nullptr);
@ -885,7 +901,7 @@ sk_sp<SkPicture> SkPipeDeserializer::readPicture(const void* data, size_t size)
if (SkPipeVerb::kDefinePicture == unpack_verb(packedVerb)) { if (SkPipeVerb::kDefinePicture == unpack_verb(packedVerb)) {
SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures, SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
&fImpl->fTypefaces, &fImpl->fFactories, &fImpl->fTypefaces, &fImpl->fFactories,
fImpl->fTFDeserializer); fImpl->fTFDeserializer, fImpl->fIMDeserializer);
SkPipeReader reader(this, ptr, size); SkPipeReader reader(this, ptr, size);
reader.setInflator(&inflator); reader.setInflator(&inflator);
definePicture_handler(reader, packedVerb, nullptr); definePicture_handler(reader, packedVerb, nullptr);
@ -954,7 +970,7 @@ static bool do_playback(SkPipeReader& reader, SkCanvas* canvas, int* endPictureI
bool SkPipeDeserializer::playback(const void* data, size_t size, SkCanvas* canvas) { bool SkPipeDeserializer::playback(const void* data, size_t size, SkCanvas* canvas) {
SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures, SkPipeInflator inflator(&fImpl->fImages, &fImpl->fPictures,
&fImpl->fTypefaces, &fImpl->fFactories, &fImpl->fTypefaces, &fImpl->fFactories,
fImpl->fTFDeserializer); fImpl->fTFDeserializer, fImpl->fIMDeserializer);
SkPipeReader reader(this, data, size); SkPipeReader reader(this, data, size);
reader.setInflator(&inflator); reader.setInflator(&inflator);
return do_playback(reader, canvas); return do_playback(reader, canvas);