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

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

Issue 1308273011: Handle zero-length encoded images gracefully during deserialization (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: extra tests Created 5 years, 3 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/SkPicture.h ('k') | src/core/SkWriteBuffer.cpp » ('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 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 #include <new> 7 #include <new>
8 #include "SkPictureData.h" 8 #include "SkPictureData.h"
9 #include "SkPictureRecord.h" 9 #include "SkPictureRecord.h"
10 #include "SkReadBuffer.h" 10 #include "SkReadBuffer.h"
11 #include "SkSurface.h"
11 #include "SkTextBlob.h" 12 #include "SkTextBlob.h"
12 #include "SkTypeface.h" 13 #include "SkTypeface.h"
13 #include "SkWriteBuffer.h" 14 #include "SkWriteBuffer.h"
14 15
15 #if SK_SUPPORT_GPU 16 #if SK_SUPPORT_GPU
16 #include "GrContext.h" 17 #include "GrContext.h"
17 #endif 18 #endif
18 19
19 template <typename T> int SafeCount(const T* obj) { 20 template <typename T> int SafeCount(const T* obj) {
20 return obj ? obj->count() : 0; 21 return obj ? obj->count() : 0;
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 } 427 }
427 if (!buffer.isValid()) { 428 if (!buffer.isValid()) {
428 return false; 429 return false;
429 } 430 }
430 SkDEBUGCODE(haveBuffer = true;) 431 SkDEBUGCODE(haveBuffer = true;)
431 } break; 432 } break;
432 } 433 }
433 return true; // success 434 return true; // success
434 } 435 }
435 436
437 static const SkImage* make_checkerboard_image(int width, int height) {
438 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(width, height) );
439 if (!surface) {
440 return nullptr;
441 }
442
443 SkCanvas* canvas = surface->getCanvas();
444 canvas->clear(SK_ColorWHITE);
445
446 SkPaint paint;
447 paint.setColor(SK_ColorGRAY);
448
449 static const int kSize = 12;
450 for (int y = 0; y < height; y += kSize * 2) {
451 for (int x = 0; x < width; x += kSize * 2) {
452 canvas->drawRect(SkRect::MakeXYWH(x, y, kSize, kSize), paint);
453 canvas->drawRect(SkRect::MakeXYWH(x + kSize, y + kSize, kSize, kSize ), paint);
454 }
455 }
456
457 return surface->newImageSnapshot();
458 }
459
436 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) { 460 static const SkImage* create_image_from_buffer(SkReadBuffer& buffer) {
437 int width = buffer.read32(); 461 int width = buffer.read32();
438 int height = buffer.read32(); 462 int height = buffer.read32();
439 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension 463 if (width <= 0 || height <= 0) { // SkImage never has a zero dimension
440 buffer.validate(false); 464 buffer.validate(false);
441 return nullptr; 465 return nullptr;
442 } 466 }
443 467
444 SkAutoTUnref<SkData> encoded(buffer.readByteArrayAsData()); 468 SkAutoTUnref<SkData> encoded(buffer.readByteArrayAsData());
469 if (encoded->size() == 0) {
470 // The image could not be encoded at serialization time.
471 // Use a checkerboard instead.
472 return make_checkerboard_image(width, height);
reed1 2015/09/04 17:26:24 This seems odd to me, partly because its arbitrary
f(malita) 2015/09/04 17:50:41 Agreed.
473 }
474
445 int originX = buffer.read32(); 475 int originX = buffer.read32();
446 int originY = buffer.read32(); 476 int originY = buffer.read32();
447 if (0 == encoded->size() || originX < 0 || originY < 0) { 477 if (originX < 0 || originY < 0) {
448 buffer.validate(false); 478 buffer.validate(false);
449 return nullptr; 479 return nullptr;
450 } 480 }
451 481
452 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height); 482 const SkIRect subset = SkIRect::MakeXYWH(originX, originY, width, height);
453 return SkImage::NewFromEncoded(encoded, &subset); 483 return SkImage::NewFromEncoded(encoded, &subset);
454 } 484 }
455 485
456 // Need a shallow wrapper to return const SkPicture* to match the other factorie s, 486 // Need a shallow wrapper to return const SkPicture* to match the other factorie s,
457 // as SkPicture::CreateFromBuffer() returns SkPicture* 487 // as SkPicture::CreateFromBuffer() returns SkPicture*
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 } 664 }
635 } 665 }
636 666
637 bool SkPictureData::suitableForLayerOptimization() const { 667 bool SkPictureData::suitableForLayerOptimization() const {
638 return fContentInfo.numLayers() > 0; 668 return fContentInfo.numLayers() > 0;
639 } 669 }
640 #endif 670 #endif
641 /////////////////////////////////////////////////////////////////////////////// 671 ///////////////////////////////////////////////////////////////////////////////
642 672
643 673
OLDNEW
« no previous file with comments | « include/core/SkPicture.h ('k') | src/core/SkWriteBuffer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698