Index: src/image/SkImage.cpp |
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp |
index c55d28a177f16c5e075b3aa77113a959ee15cd56..5a67c84caf663cc16cc41f62ccff98cce68201fb 100644 |
--- a/src/image/SkImage.cpp |
+++ b/src/image/SkImage.cpp |
@@ -13,6 +13,7 @@ |
#include "SkImage_Base.h" |
#include "SkNextID.h" |
#include "SkPixelRef.h" |
+#include "SkPixelSerializer.h" |
#include "SkReadPixelsRec.h" |
#include "SkString.h" |
#include "SkSurface.h" |
@@ -71,6 +72,40 @@ SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
return nullptr; |
} |
+namespace { |
+ |
+class DefaultSerializer : public SkPixelSerializer { |
+protected: |
+ bool onUseEncodedData(const void *data, size_t len) override { |
+ return true; |
+ } |
+ |
+ SkData* onEncodePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes) override { |
+ return SkImageEncoder::EncodeData(info, pixels, rowBytes, SkImageEncoder::kPNG_Type, 100); |
+ } |
+}; |
+ |
+} // anonymous namespace |
+ |
+SkData* SkImage::encode(SkPixelSerializer* serializer) const { |
+ DefaultSerializer defaultSerializer; |
+ SkPixelSerializer* effectiveSerializer = serializer ? serializer : &defaultSerializer; |
+ |
+ SkAutoTUnref<SkData> encoded(this->refEncoded()); |
+ if (encoded && effectiveSerializer->useEncodedData(encoded->data(), encoded->size())) { |
+ return encoded.detach(); |
+ } |
+ |
+ SkBitmap bm; |
+ SkAutoPixmapUnlock apu; |
+ if (as_IB(this)->getROPixels(&bm) && bm.requestLock(&apu)) { |
+ const SkPixmap& pmap = apu.pixmap(); |
+ return effectiveSerializer->encodePixels(pmap.info(), pmap.addr(), pmap.rowBytes()); |
+ } |
+ |
+ return nullptr; |
+} |
+ |
SkData* SkImage::refEncoded() const { |
return as_IB(this)->onRefEncoded(); |
} |