Index: dm/DMSrcSink.cpp |
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp |
index 6f6664f27ce2266bd12156f1ae973cada399ec25..f678f41b61a0c10b17d666915edec61184073d17 100644 |
--- a/dm/DMSrcSink.cpp |
+++ b/dm/DMSrcSink.cpp |
@@ -2,8 +2,10 @@ |
#include "SamplePipeControllers.h" |
#include "SkCommonFlags.h" |
#include "SkCodec.h" |
+#include "SkData.h" |
#include "SkDocument.h" |
#include "SkError.h" |
+#include "SkImageGenerator.h" |
#include "SkMultiPictureDraw.h" |
#include "SkNullCanvas.h" |
#include "SkOSFile.h" |
@@ -15,6 +17,17 @@ |
#include "SkXMLWriter.h" |
DEFINE_bool(codec, false, "Use SkCodec instead of SkImageDecoder"); |
+DEFINE_bool(deferDecode, false, "if true, defer decoding of bitmaps until they are locked."); |
+ |
+static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) { |
+ SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size)); |
+ return encoded && SkInstallDiscardablePixelRef(encoded, dst); |
+} |
+ |
+static SkPicture* create_picture_from_stream(SkStream* stream) { |
+ return FLAGS_deferDecode ? SkPicture::CreateFromStream(stream, &lazy_decode_bitmap) |
+ : SkPicture::CreateFromStream(stream); |
+} |
namespace DM { |
@@ -83,6 +96,11 @@ Error ImageSrc::draw(SkCanvas* canvas) const { |
// Everything else is considered a failure. |
return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str()); |
} |
+ } else if (FLAGS_deferDecode) { |
+ if (!SkInstallDiscardablePixelRef(encoded, &bitmap)) { |
+ return SkStringPrintf("SkInstallDiscardablePixelRef failed for %s.", |
+ fPath.c_str()); |
+ } |
} else { |
if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap, |
dstColorType, SkImageDecoder::kDecodePixels_Mode)) { |
@@ -151,6 +169,10 @@ SkISize ImageSrc::size() const { |
return SkISize::Make(0,0); |
} |
return info.dimensions(); |
+ } else if (FLAGS_deferDecode) { |
+ SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromData(encoded)); |
+ SkImageInfo info; |
+ return gen && gen->getInfo(&info) ? info.dimensions() : SkISize::Make(0,0); |
} else { |
SkBitmap bitmap; |
if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(), |
@@ -179,7 +201,7 @@ Error SKPSrc::draw(SkCanvas* canvas) const { |
if (!stream) { |
return SkStringPrintf("Couldn't read %s.", fPath.c_str()); |
} |
- SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream)); |
+ SkAutoTUnref<SkPicture> pic(create_picture_from_stream(stream)); |
if (!pic) { |
return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str()); |
} |
@@ -479,7 +501,7 @@ Error ViaSerialization::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream |
SkDynamicMemoryWStream wStream; |
pic->serialize(&wStream); |
SkAutoTDelete<SkStream> rStream(wStream.detachAsStream()); |
- SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream)); |
+ SkAutoTUnref<SkPicture> deserialized(create_picture_from_stream(rStream)); |
// Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream. |
struct ProxySrc : public Src { |