Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Unified Diff: src/core/SkPicture.cpp

Issue 195223003: Fixing SkPicture serialization (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fixed nits Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include/core/SkReadBuffer.h ('k') | src/core/SkPicturePlayback.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkPicture.cpp
diff --git a/src/core/SkPicture.cpp b/src/core/SkPicture.cpp
index f83a5fb9aadc33ff1aacdefa42b3244bbbaec4a7..92702365726713b1e98437dbce586ce250b3a3d4 100644
--- a/src/core/SkPicture.cpp
+++ b/src/core/SkPicture.cpp
@@ -260,26 +260,29 @@ void SkPicture::draw(SkCanvas* surface, SkDrawPictureCallback* callback) {
#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::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
- if (NULL == stream) {
+bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
+ if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
return false;
}
- // Check magic bytes.
- char magic[sizeof(kMagic)];
- if (!stream->read(magic, sizeof(kMagic)) ||
- (0 != memcmp(magic, kMagic, sizeof(kMagic)))) {
+ if (info.fVersion < MIN_PICTURE_VERSION ||
+ info.fVersion > CURRENT_PICTURE_VERSION) {
return false;
}
- SkPictInfo info;
- if (!stream->read(&info, sizeof(SkPictInfo))) {
+ return true;
+}
+
+bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
+ if (NULL == stream) {
return false;
}
- if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_VERSION) {
+ // Check magic bytes.
+ SkPictInfo info;
+ SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
+ if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) {
return false;
}
@@ -291,19 +294,9 @@ bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
bool SkPicture::InternalOnly_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;
- }
-
- if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_VERSION) {
+ SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
+ if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) {
return false;
}
@@ -361,13 +354,13 @@ SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) {
return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight));
}
-void SkPicture::createHeader(void* header) const {
+void SkPicture::createHeader(SkPictInfo* info) const {
// Copy magic bytes at the beginning of the header
SkASSERT(sizeof(kMagic) == 8);
- memcpy(header, kMagic, sizeof(kMagic));
+ SkASSERT(sizeof(kMagic) == sizeof(info->fMagic));
+ memcpy(info->fMagic, kMagic, sizeof(kMagic));
// Set picture info after magic bytes in the header
- SkPictInfo* info = (SkPictInfo*)(((char*)header) + sizeof(kMagic));
info->fVersion = CURRENT_PICTURE_VERSION;
info->fWidth = fWidth;
info->fHeight = fHeight;
@@ -387,9 +380,9 @@ void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const {
playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
}
- char header[kHeaderSize];
+ SkPictInfo header;
this->createHeader(&header);
- stream->write(header, kHeaderSize);
+ stream->write(&header, sizeof(header));
if (playback) {
stream->writeBool(true);
playback->serialize(stream, encoder);
@@ -409,9 +402,9 @@ void SkPicture::flatten(SkWriteBuffer& buffer) const {
playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
}
- char header[kHeaderSize];
+ SkPictInfo header;
this->createHeader(&header);
- buffer.writeByteArray(header, kHeaderSize);
+ buffer.writeByteArray(&header, sizeof(header));
if (playback) {
buffer.writeBool(true);
playback->flatten(buffer);
« no previous file with comments | « include/core/SkReadBuffer.h ('k') | src/core/SkPicturePlayback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698