Index: src/core/SkPicture.cpp |
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp |
index ca1b6fa22a94cf0f84d7bd6ac0c61a7338f7c12e..e226c2642ac1e0d4a6ad23a8ce4a218795937c06 100644 |
--- a/src/core/SkPicture.cpp |
+++ b/src/core/SkPicture.cpp |
@@ -265,6 +265,7 @@ |
#include "SkStream.h" |
static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; |
+static const size_t kHeaderSize = sizeof(kMagic) + sizeof(SkPictInfo); |
bool SkPicture::StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { |
if (NULL == stream) { |
@@ -273,13 +274,37 @@ |
// Check magic bytes. |
char magic[sizeof(kMagic)]; |
- stream->read(magic, sizeof(kMagic)); |
- if (0 != memcmp(magic, kMagic, sizeof(kMagic))) { |
+ if (!stream->read(magic, sizeof(kMagic)) || |
+ (0 != memcmp(magic, kMagic, sizeof(kMagic)))) { |
return false; |
} |
SkPictInfo info; |
if (!stream->read(&info, sizeof(SkPictInfo))) { |
+ return false; |
+ } |
+ |
+ if (PICTURE_VERSION != info.fVersion) { |
+ return false; |
+ } |
+ |
+ if (pInfo != NULL) { |
+ *pInfo = info; |
+ } |
+ return true; |
+} |
+ |
+bool SkPicture::BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) { |
+ // Check magic bytes. |
+ char magic[sizeof(kMagic)]; |
+ |
+ if (!buffer.readByteArray(magic, sizeof(kMagic)) || |
+ (0 != memcmp(magic, kMagic, sizeof(kMagic)))) { |
+ return false; |
+ } |
+ |
+ SkPictInfo info; |
+ if (!buffer.readByteArray(&info, sizeof(SkPictInfo))) { |
return false; |
} |
@@ -320,6 +345,46 @@ |
return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); |
} |
+SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) { |
+ SkPictInfo info; |
+ |
+ if (!BufferIsSKP(buffer, &info)) { |
+ return NULL; |
+ } |
+ |
+ SkPicturePlayback* playback; |
+ // Check to see if there is a playback to recreate. |
+ if (buffer.readBool()) { |
+ playback = SkPicturePlayback::CreateFromBuffer(buffer); |
+ if (NULL == playback) { |
+ return NULL; |
+ } |
+ } else { |
+ playback = NULL; |
+ } |
+ |
+ return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); |
+} |
+ |
+void SkPicture::createHeader(void* header) const { |
+ // Copy magic bytes at the beginning of the header |
+ SkASSERT(sizeof(kMagic) == 8); |
+ memcpy(header, kMagic, sizeof(kMagic)); |
+ |
+ // Set piture info after magic bytes in the header |
+ SkPictInfo* info = (SkPictInfo*)(((char*)header) + sizeof(kMagic)); |
+ info->fVersion = PICTURE_VERSION; |
+ info->fWidth = fWidth; |
+ info->fHeight = fHeight; |
+ info->fFlags = SkPictInfo::kCrossProcess_Flag; |
+ // TODO: remove this flag, since we're always float (now) |
+ info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
+ |
+ if (8 == sizeof(void*)) { |
+ info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
+ } |
+} |
+ |
void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { |
SkPicturePlayback* playback = fPlayback; |
@@ -327,24 +392,9 @@ |
playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); |
} |
- SkPictInfo info; |
- |
- info.fVersion = PICTURE_VERSION; |
- info.fWidth = fWidth; |
- info.fHeight = fHeight; |
- info.fFlags = SkPictInfo::kCrossProcess_Flag; |
- // TODO: remove this flag, since we're always float (now) |
- info.fFlags |= SkPictInfo::kScalarIsFloat_Flag; |
- |
- if (8 == sizeof(void*)) { |
- info.fFlags |= SkPictInfo::kPtrIs64Bit_Flag; |
- } |
- |
- // Write 8 magic bytes to ID this file format. |
- SkASSERT(sizeof(kMagic) == 8); |
- stream->write(kMagic, sizeof(kMagic)); |
- |
- stream->write(&info, sizeof(info)); |
+ char header[kHeaderSize]; |
+ createHeader(&header); |
+ stream->write(header, kHeaderSize); |
if (playback) { |
stream->writeBool(true); |
playback->serialize(stream, encoder); |
@@ -357,6 +407,28 @@ |
} |
} |
+void SkPicture::flatten(SkWriteBuffer& buffer) const { |
+ SkPicturePlayback* playback = fPlayback; |
+ |
+ if (NULL == playback && fRecord) { |
+ playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); |
+ } |
+ |
+ char header[kHeaderSize]; |
+ createHeader(&header); |
+ buffer.writeByteArray(header, kHeaderSize); |
+ if (playback) { |
+ buffer.writeBool(true); |
+ playback->flatten(buffer); |
+ // delete playback if it is a local version (i.e. cons'd up just now) |
+ if (playback != fPlayback) { |
+ SkDELETE(playback); |
+ } |
+ } else { |
+ buffer.writeBool(false); |
+ } |
+} |
+ |
bool SkPicture::willPlayBackBitmaps() const { |
if (!fPlayback) return false; |
return fPlayback->containsBitmaps(); |