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

Side by Side Diff: src/core/SkPicture.cpp

Issue 138063005: Serialization of SkPictureImageFilter (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Updated past revert point, will re-land tonight Created 6 years, 10 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 | « samplecode/SampleFilterFuzz.cpp ('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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 if (fPlayback) { 258 if (fPlayback) {
259 fPlayback->draw(*surface, callback); 259 fPlayback->draw(*surface, callback);
260 } 260 }
261 } 261 }
262 262
263 /////////////////////////////////////////////////////////////////////////////// 263 ///////////////////////////////////////////////////////////////////////////////
264 264
265 #include "SkStream.h" 265 #include "SkStream.h"
266 266
267 static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' }; 267 static const char kMagic[] = { 's', 'k', 'i', 'a', 'p', 'i', 'c', 't' };
268 static const size_t kHeaderSize = sizeof(kMagic) + sizeof(SkPictInfo);
268 269
269 bool SkPicture::StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) { 270 bool SkPicture::StreamIsSKP(SkStream* stream, SkPictInfo* pInfo) {
270 if (NULL == stream) { 271 if (NULL == stream) {
271 return false; 272 return false;
272 } 273 }
273 274
274 // Check magic bytes. 275 // Check magic bytes.
275 char magic[sizeof(kMagic)]; 276 char magic[sizeof(kMagic)];
276 stream->read(magic, sizeof(kMagic)); 277 if (!stream->read(magic, sizeof(kMagic)) ||
277 if (0 != memcmp(magic, kMagic, sizeof(kMagic))) { 278 (0 != memcmp(magic, kMagic, sizeof(kMagic)))) {
278 return false; 279 return false;
279 } 280 }
280 281
281 SkPictInfo info; 282 SkPictInfo info;
282 if (!stream->read(&info, sizeof(SkPictInfo))) { 283 if (!stream->read(&info, sizeof(SkPictInfo))) {
283 return false; 284 return false;
284 } 285 }
285 286
286 if (PICTURE_VERSION != info.fVersion) { 287 if (PICTURE_VERSION != info.fVersion) {
287 return false; 288 return false;
288 } 289 }
289 290
290 if (pInfo != NULL) { 291 if (pInfo != NULL) {
291 *pInfo = info; 292 *pInfo = info;
292 } 293 }
293 return true; 294 return true;
294 } 295 }
295 296
297 bool SkPicture::BufferIsSKP(SkReadBuffer& buffer, SkPictInfo* pInfo) {
298 // Check magic bytes.
299 char magic[sizeof(kMagic)];
300
301 if (!buffer.readByteArray(magic, sizeof(kMagic)) ||
302 (0 != memcmp(magic, kMagic, sizeof(kMagic)))) {
303 return false;
304 }
305
306 SkPictInfo info;
307 if (!buffer.readByteArray(&info, sizeof(SkPictInfo))) {
308 return false;
309 }
310
311 if (PICTURE_VERSION != info.fVersion) {
312 return false;
313 }
314
315 if (pInfo != NULL) {
316 *pInfo = info;
317 }
318 return true;
319 }
320
296 SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height) 321 SkPicture::SkPicture(SkPicturePlayback* playback, int width, int height)
297 : fPlayback(playback) 322 : fPlayback(playback)
298 , fRecord(NULL) 323 , fRecord(NULL)
299 , fWidth(width) 324 , fWidth(width)
300 , fHeight(height) {} 325 , fHeight(height) {}
301 326
302 SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc pro c) { 327 SkPicture* SkPicture::CreateFromStream(SkStream* stream, InstallPixelRefProc pro c) {
303 SkPictInfo info; 328 SkPictInfo info;
304 329
305 if (!StreamIsSKP(stream, &info)) { 330 if (!StreamIsSKP(stream, &info)) {
306 return NULL; 331 return NULL;
307 } 332 }
308 333
309 SkPicturePlayback* playback; 334 SkPicturePlayback* playback;
310 // Check to see if there is a playback to recreate. 335 // Check to see if there is a playback to recreate.
311 if (stream->readBool()) { 336 if (stream->readBool()) {
312 playback = SkPicturePlayback::CreateFromStream(stream, info, proc); 337 playback = SkPicturePlayback::CreateFromStream(stream, info, proc);
313 if (NULL == playback) { 338 if (NULL == playback) {
314 return NULL; 339 return NULL;
315 } 340 }
316 } else { 341 } else {
317 playback = NULL; 342 playback = NULL;
318 } 343 }
319 344
320 return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight)); 345 return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight));
321 } 346 }
322 347
348 SkPicture* SkPicture::CreateFromBuffer(SkReadBuffer& buffer) {
349 SkPictInfo info;
350
351 if (!BufferIsSKP(buffer, &info)) {
352 return NULL;
353 }
354
355 SkPicturePlayback* playback;
356 // Check to see if there is a playback to recreate.
357 if (buffer.readBool()) {
358 playback = SkPicturePlayback::CreateFromBuffer(buffer);
359 if (NULL == playback) {
360 return NULL;
361 }
362 } else {
363 playback = NULL;
364 }
365
366 return SkNEW_ARGS(SkPicture, (playback, info.fWidth, info.fHeight));
367 }
368
369 void SkPicture::createHeader(void* header) const {
370 // Copy magic bytes at the beginning of the header
371 SkASSERT(sizeof(kMagic) == 8);
372 memcpy(header, kMagic, sizeof(kMagic));
373
374 // Set piture info after magic bytes in the header
375 SkPictInfo* info = (SkPictInfo*)(((char*)header) + sizeof(kMagic));
376 info->fVersion = PICTURE_VERSION;
377 info->fWidth = fWidth;
378 info->fHeight = fHeight;
379 info->fFlags = SkPictInfo::kCrossProcess_Flag;
380 // TODO: remove this flag, since we're always float (now)
381 info->fFlags |= SkPictInfo::kScalarIsFloat_Flag;
382
383 if (8 == sizeof(void*)) {
384 info->fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
385 }
386 }
387
323 void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const { 388 void SkPicture::serialize(SkWStream* stream, EncodeBitmap encoder) const {
324 SkPicturePlayback* playback = fPlayback; 389 SkPicturePlayback* playback = fPlayback;
325 390
326 if (NULL == playback && fRecord) { 391 if (NULL == playback && fRecord) {
327 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord)); 392 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
328 } 393 }
329 394
330 SkPictInfo info; 395 char header[kHeaderSize];
331 396 createHeader(&header);
332 info.fVersion = PICTURE_VERSION; 397 stream->write(header, kHeaderSize);
333 info.fWidth = fWidth;
334 info.fHeight = fHeight;
335 info.fFlags = SkPictInfo::kCrossProcess_Flag;
336 // TODO: remove this flag, since we're always float (now)
337 info.fFlags |= SkPictInfo::kScalarIsFloat_Flag;
338
339 if (8 == sizeof(void*)) {
340 info.fFlags |= SkPictInfo::kPtrIs64Bit_Flag;
341 }
342
343 // Write 8 magic bytes to ID this file format.
344 SkASSERT(sizeof(kMagic) == 8);
345 stream->write(kMagic, sizeof(kMagic));
346
347 stream->write(&info, sizeof(info));
348 if (playback) { 398 if (playback) {
349 stream->writeBool(true); 399 stream->writeBool(true);
350 playback->serialize(stream, encoder); 400 playback->serialize(stream, encoder);
351 // delete playback if it is a local version (i.e. cons'd up just now) 401 // delete playback if it is a local version (i.e. cons'd up just now)
352 if (playback != fPlayback) { 402 if (playback != fPlayback) {
353 SkDELETE(playback); 403 SkDELETE(playback);
354 } 404 }
355 } else { 405 } else {
356 stream->writeBool(false); 406 stream->writeBool(false);
357 } 407 }
358 } 408 }
359 409
410 void SkPicture::flatten(SkWriteBuffer& buffer) const {
411 SkPicturePlayback* playback = fPlayback;
412
413 if (NULL == playback && fRecord) {
414 playback = SkNEW_ARGS(SkPicturePlayback, (*fRecord));
415 }
416
417 char header[kHeaderSize];
418 createHeader(&header);
419 buffer.writeByteArray(header, kHeaderSize);
420 if (playback) {
421 buffer.writeBool(true);
422 playback->flatten(buffer);
423 // delete playback if it is a local version (i.e. cons'd up just now)
424 if (playback != fPlayback) {
425 SkDELETE(playback);
426 }
427 } else {
428 buffer.writeBool(false);
429 }
430 }
431
360 bool SkPicture::willPlayBackBitmaps() const { 432 bool SkPicture::willPlayBackBitmaps() const {
361 if (!fPlayback) return false; 433 if (!fPlayback) return false;
362 return fPlayback->containsBitmaps(); 434 return fPlayback->containsBitmaps();
363 } 435 }
364 436
365 #ifdef SK_BUILD_FOR_ANDROID 437 #ifdef SK_BUILD_FOR_ANDROID
366 void SkPicture::abortPlayback() { 438 void SkPicture::abortPlayback() {
367 if (NULL == fPlayback) { 439 if (NULL == fPlayback) {
368 return; 440 return;
369 } 441 }
370 fPlayback->abort(); 442 fPlayback->abort();
371 } 443 }
372 #endif 444 #endif
OLDNEW
« no previous file with comments | « samplecode/SampleFilterFuzz.cpp ('k') | src/core/SkPicturePlayback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698