diff options
author | 2014-03-12 17:04:28 +0000 | |
---|---|---|
committer | 2014-03-12 17:04:28 +0000 | |
commit | c30dcb9b128887c7e16afe32fdf35105cc42380b (patch) | |
tree | 97f55d320d7191e0f876ff9dc3ad2d1528064e62 /src | |
parent | 84cd099704b3896ca66081a96508572a924f850c (diff) |
Add capture snapshot as data to SkWriter32, use it to optimise record->playback.
This is a new way of implementing https://codereview.chromium.org/155863005/
It uses copy on write semantics to return a buffer without copying it, so that record -> playback does not need to copy the buffer.
BUG=skia:2125
R=tomhudson@google.com, mtklein@google.com, reed@google.com, iancottrell@chromium.org
Author: iancottrell@google.com
Review URL: https://codereview.chromium.org/167113003
git-svn-id: http://skia.googlecode.com/svn/trunk@13769 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkPicturePlayback.cpp | 10 | ||||
-rw-r--r-- | src/core/SkWriter32.cpp | 28 |
2 files changed, 30 insertions, 8 deletions
diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp index 05c2c4e56b..d61e616c4d 100644 --- a/src/core/SkPicturePlayback.cpp +++ b/src/core/SkPicturePlayback.cpp @@ -70,10 +70,12 @@ SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record, bool deepCop record.validate(record.writeStream().bytesWritten(), 0); const SkWriter32& writer = record.writeStream(); init(); + SkASSERT(!fOpData); if (writer.bytesWritten() == 0) { fOpData = SkData::NewEmpty(); return; } + fOpData = writer.snapshotAsData(); fBoundingHierarchy = record.fBoundingHierarchy; fStateTree = record.fStateTree; @@ -85,14 +87,6 @@ SkPicturePlayback::SkPicturePlayback(const SkPictureRecord& record, bool deepCop fBoundingHierarchy->flushDeferredInserts(); } - { - size_t size = writer.bytesWritten(); - void* buffer = sk_malloc_throw(size); - writer.flatten(buffer); - SkASSERT(!fOpData); - fOpData = SkData::NewFromMalloc(buffer, size); - } - // copy over the refcnt dictionary to our reader record.fFlattenableHeap.setupPlaybacks(); diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp index fe33e4a39c..c7bfd92d56 100644 --- a/src/core/SkWriter32.cpp +++ b/src/core/SkWriter32.cpp @@ -74,4 +74,32 @@ void SkWriter32::growToAtLeast(size_t size) { // we were external, so copy in the data memcpy(fData, fExternal, fUsed); } + // Invalidate the snapshot, we know it is no longer useful. + fSnapshot.reset(NULL); +} + +SkData* SkWriter32::snapshotAsData() const { + // get a non const version of this, we are only conceptually const + SkWriter32& mutable_this = *const_cast<SkWriter32*>(this); + // we use size change detection to invalidate the cached data + if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) { + mutable_this.fSnapshot.reset(NULL); + } + if (fSnapshot.get() == NULL) { + uint8_t* buffer = NULL; + if ((fExternal != NULL) && (fData == fExternal)) { + // We need to copy to an allocated buffer before returning. + buffer = (uint8_t*)sk_malloc_throw(fUsed); + memcpy(buffer, fData, fUsed); + } else { + buffer = mutable_this.fInternal.detach(); + // prepare us to do copy on write, by pretending the data buffer + // is external and size limited + mutable_this.fData = buffer; + mutable_this.fCapacity = fUsed; + mutable_this.fExternal = buffer; + } + mutable_this.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed)); + } + return SkRef(fSnapshot.get()); // Take an extra ref for the caller. } |