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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « include/core/SkReadBuffer.h ('k') | src/core/SkPicturePlayback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2007 The Android Open Source Project 3 * Copyright 2007 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkPictureFlat.h" 10 #include "SkPictureFlat.h"
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 if (fPlayback) { 253 if (fPlayback) {
254 fPlayback->draw(*surface, callback); 254 fPlayback->draw(*surface, callback);
255 } 255 }
256 } 256 }
257 257
258 /////////////////////////////////////////////////////////////////////////////// 258 ///////////////////////////////////////////////////////////////////////////////
259 259
260 #include "SkStream.h" 260 #include "SkStream.h"
261 261
262 static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; 262 static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
263 static const size_t kHeaderSize = sizeof(kMagic) + sizeof(SkPictInfo); 263
264 bool SkPicture::IsValidPictInfo(const SkPictInfo& info) {
265 if (0 != memcmp(info.fMagic, kMagic, sizeof(kMagic))) {
266 return false;
267 }
268
269 if (info.fVersion < MIN_PICTURE_VERSION ||
270 info.fVersion > CURRENT_PICTURE_VERSION) {
271 return false;
272 }
273
274 return true;
275 }
264 276
265 bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { 277 bool SkPicture::InternalOnly_StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
266 if (NULL == stream) { 278 if (NULL == stream) {
267 return false; 279 return false;
268 } 280 }
269 281
270 // Check magic bytes. 282 // Check magic bytes.
271 char magic[sizeof(kMagic)]; 283 SkPictInfo info;
272 if (!stream->read(magic, sizeof(kMagic)) || 284 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
273 (0 != memcmp(magic, kMagic, sizeof(kMagic)))) { 285 if (!stream->read(&info, sizeof(info)) || !IsValidPictInfo(info)) {
274 return false; 286 return false;
275 } 287 }
276 288
277 SkPictInfo info;
278 if (!stream->read(&info, sizeof(SkPictInfo))) {
279 return false;
280 }
281
282 if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_V ERSION) {
283 return false;
284 }
285
286 if (pInfo != NULL) { 289 if (pInfo != NULL) {
287 *pInfo = info; 290 *pInfo = info;
288 } 291 }
289 return true; 292 return true;
290 } 293 }
291 294
292 bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo ) { 295 bool SkPicture::InternalOnly_BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo ) {
293 // Check magic bytes. 296 // Check magic bytes.
294 char magic[sizeof(kMagic)]; 297 SkPictInfo info;
295 298 SkASSERT(sizeof(kMagic) == sizeof(info.fMagic));
296 if (!buffer.readByteArray(magic, sizeof(kMagic)) || 299 if (!buffer.readByteArray(&info, sizeof(info)) || !IsValidPictInfo(info)) {
297 (0 != memcmp(magic, kMagic, sizeof(kMagic)))) {
298 return false; 300 return false;
299 } 301 }
300 302
301 SkPictInfo info;
302 if (!buffer.readByteArray(&info, sizeof(SkPictInfo))) {
303 return false;
304 }
305
306 if (info.fVersion < MIN_PICTURE_VERSION || info.fVersion > CURRENT_PICTURE_V ERSION) {
307 return false;
308 }
309
310 if (pInfo != NULL) { 303 if (pInfo != NULL) {
311 *pInfo = info; 304 *pInfo = info;
312 } 305 }
313 return true; 306 return true;
314 } 307 }
315 308
316 SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height) 309 SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height)
317 : fPlayback(playback) 310 : fPlayback(playback)
318 , fRecord(NULL) 311 , fRecord(NULL)
319 , fWidth(width) 312 , fWidth(width)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 if (NULL == playback) { 347 if (NULL == playback) {
355 return NULL; 348 return NULL;
356 } 349 }
357 } else { 350 } else {
358 playback = NULL; 351 playback = NULL;
359 } 352 }
360 353
361 return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); 354 return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight));
362 } 355 }
363 356
364 void SkPicture::createHeader(void* header) const { 357 void SkPicture::createHeader(SkPictInfo* info) const {
365 // Copy magic bytes at the beginning of the header 358 // Copy magic bytes at the beginning of the header
366 SkASSERT(sizeof(kMagic) == 8); 359 SkASSERT(sizeof(kMagic) == 8);
367 memcpy(header, kMagic, sizeof(kMagic)); 360 SkASSERT(sizeof(kMagic) == sizeof(info->fMagic));
361 memcpy(info->fMagic, kMagic, sizeof(kMagic));
368 362
369 // Set picture info after magic bytes in the header 363 // Set picture info after magic bytes in the header
370 SkPictInfo* info = (SkPictInfo*)(((char*)header) + sizeof(kMagic));
371 info->fVersion = CURRENT_PICTURE_VERSION; 364 info->fVersion = CURRENT_PICTURE_VERSION;
372 info->fWidth = fWidth; 365 info->fWidth = fWidth;
373 info->fHeight = fHeight; 366 info->fHeight = fHeight;
374 info->fFlags = SkPictInfo::kCrossProcess_Flag; 367 info->fFlags = SkPictInfo::kCrossProcess_Flag;
375 // TODO: remove this flag, since we're always float (now) 368 // TODO: remove this flag, since we're always float (now)
376 info->fFlags |= SkPictInfo::kScalarIsFloat_Flag; 369 info->fFlags |= SkPictInfo::kScalarIsFloat_Flag;
377 370
378 if (8 == sizeof(void*)) { 371 if (8 == sizeof(void*)) {
379 info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag; 372 info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
380 } 373 }
381 } 374 }
382 375
383 void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { 376 void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const {
384 SkPicturePlayback* playback = fPlayback; 377 SkPicturePlayback* playback = fPlayback;
385 378
386 if (NULL == playback && fRecord) { 379 if (NULL == playback && fRecord) {
387 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); 380 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
388 } 381 }
389 382
390 char header[kHeaderSize]; 383 SkPictInfo header;
391 this->createHeader(&header); 384 this->createHeader(&header);
392 stream->write(header, kHeaderSize); 385 stream->write(&header, sizeof(header));
393 if (playback) { 386 if (playback) {
394 stream->writeBool(true); 387 stream->writeBool(true);
395 playback->serialize(stream, encoder); 388 playback->serialize(stream, encoder);
396 // delete playback if it is a local version (i.e. cons'd up just now) 389 // delete playback if it is a local version (i.e. cons'd up just now)
397 if (playback != fPlayback) { 390 if (playback != fPlayback) {
398 SkDELETE(playback); 391 SkDELETE(playback);
399 } 392 }
400 } else { 393 } else {
401 stream->writeBool(false); 394 stream->writeBool(false);
402 } 395 }
403 } 396 }
404 397
405 void SkPicture::flatten(SkWriteBuffer& buffer) const { 398 void SkPicture::flatten(SkWriteBuffer& buffer) const {
406 SkPicturePlayback* playback = fPlayback; 399 SkPicturePlayback* playback = fPlayback;
407 400
408 if (NULL == playback && fRecord) { 401 if (NULL == playback && fRecord) {
409 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); 402 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
410 } 403 }
411 404
412 char header[kHeaderSize]; 405 SkPictInfo header;
413 this->createHeader(&header); 406 this->createHeader(&header);
414 buffer.writeByteArray(header, kHeaderSize); 407 buffer.writeByteArray(&header, sizeof(header));
415 if (playback) { 408 if (playback) {
416 buffer.writeBool(true); 409 buffer.writeBool(true);
417 playback->flatten(buffer); 410 playback->flatten(buffer);
418 // delete playback if it is a local version (i.e. cons'd up just now) 411 // delete playback if it is a local version (i.e. cons'd up just now)
419 if (playback != fPlayback) { 412 if (playback != fPlayback) {
420 SkDELETE(playback); 413 SkDELETE(playback);
421 } 414 }
422 } else { 415 } else {
423 buffer.writeBool(false); 416 buffer.writeBool(false);
424 } 417 }
425 } 418 }
426 419
427 bool SkPicture::willPlayBackBitmaps() const { 420 bool SkPicture::willPlayBackBitmaps() const {
428 if (!fPlayback) return false; 421 if (!fPlayback) return false;
429 return fPlayback->containsBitmaps(); 422 return fPlayback->containsBitmaps();
430 } 423 }
431 424
432 #ifdef SK_BUILD_FOR_ANDROID 425 #ifdef SK_BUILD_FOR_ANDROID
433 void SkPicture::abortPlayback() { 426 void SkPicture::abortPlayback() {
434 if (NULL == fPlayback) { 427 if (NULL == fPlayback) {
435 return; 428 return;
436 } 429 }
437 fPlayback->abort(); 430 fPlayback->abort();
438 } 431 }
439 #endif 432 #endif
OLDNEW
« 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