aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkStream.cpp
diff options
context:
space:
mode:
authorGravatar halcanary <halcanary@google.com>2014-07-14 09:12:12 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2014-07-14 09:12:12 -0700
commite322482f4d82bc704e40b8c89843f2ea5b6ddc04 (patch)
tree0b75bcc79277c2e6e8f35bce9f1d7ded6eecce87 /src/core/SkStream.cpp
parent9c6878be376f11c823b4f6a126b2ebdb29bf65cb (diff)
Move SkPDFStream back to SkStream to save memory.
SkPDFStream stores data as a SkStreamRewindable to minimize deep duplication and memory overhead. SkStreamToStreamRewindable function to deal with fact that SkTypeface returns a SkStream. BUG=skia:2743 R=djsollen@google.com, mtklein@google.com, bungeman@google.com, reed@google.com Author: halcanary@google.com Review URL: https://codereview.chromium.org/387863005
Diffstat (limited to 'src/core/SkStream.cpp')
-rw-r--r--src/core/SkStream.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/core/SkStream.cpp b/src/core/SkStream.cpp
index 1022d183d9..5069bb0a7e 100644
--- a/src/core/SkStream.cpp
+++ b/src/core/SkStream.cpp
@@ -907,3 +907,35 @@ SkData* SkCopyStreamToData(SkStream* stream) {
} while (!stream->isAtEnd());
return tempStream.copyToData();
}
+
+SkStreamRewindable* SkStreamRewindableFromSkStream(SkStream* stream) {
+ if (!stream) {
+ return NULL;
+ }
+ SkAutoTUnref<SkStreamRewindable> dupStream(stream->duplicate());
+ if (dupStream) {
+ return dupStream.detach();
+ }
+ stream->rewind();
+ if (stream->hasLength()) {
+ size_t length = stream->getLength();
+ if (stream->hasPosition()) { // If stream has length, but can't rewind.
+ length -= stream->getPosition();
+ }
+ SkAutoMalloc allocMemory(length);
+ SkDEBUGCODE(size_t read =) stream->read(allocMemory.get(), length);
+ SkASSERT(length == read);
+ SkAutoTUnref<SkData> data(
+ SkData::NewFromMalloc(allocMemory.detach(), length));
+ return SkNEW_ARGS(SkMemoryStream, (data.get()));
+ }
+ SkDynamicMemoryWStream tempStream;
+ const size_t bufferSize = 4096;
+ char buffer[bufferSize];
+ do {
+ size_t bytesRead = stream->read(buffer, bufferSize);
+ tempStream.write(buffer, bytesRead);
+ } while (!stream->isAtEnd());
+ return tempStream.detachAsStream(); // returns a SkBlockMemoryStream,
+ // cheaper than copying to SkData
+}