diff --git a/gyp/tests.gypi b/gyp/tests.gypi index 08277b94a2..816fe5fa05 100644 --- a/gyp/tests.gypi +++ b/gyp/tests.gypi @@ -147,6 +147,7 @@ '../tests/NameAllocatorTest.cpp', '../tests/OSPathTest.cpp', '../tests/OnceTest.cpp', + '../tests/PDFInvalidBitmapTest.cpp', '../tests/PDFJpegEmbedTest.cpp', '../tests/PDFPrimitivesTest.cpp', '../tests/PackBitsTest.cpp', diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp index 122d2f2148..73d85816aa 100644 --- a/src/pdf/SkPDFImage.cpp +++ b/src/pdf/SkPDFImage.cpp @@ -222,7 +222,11 @@ static SkStream* extract_image_data(const SkBitmap& bitmap, bool transparent = extractAlpha; SkStream* stream = NULL; - bitmap.lockPixels(); + SkAutoLockPixels lock(bitmap); + if (NULL == bitmap.getPixels()) { + return NULL; + } + switch (colorType) { case kIndex_8_SkColorType: if (!extractAlpha) { @@ -253,7 +257,6 @@ static SkStream* extract_image_data(const SkBitmap& bitmap, default: SkASSERT(false); } - bitmap.unlockPixels(); if (isTransparent != NULL) { *isTransparent = transparent; diff --git a/tests/PDFInvalidBitmapTest.cpp b/tests/PDFInvalidBitmapTest.cpp new file mode 100644 index 0000000000..2f4753e4a4 --- /dev/null +++ b/tests/PDFInvalidBitmapTest.cpp @@ -0,0 +1,60 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkBitmap.h" +#include "SkCanvas.h" +#include "SkDocument.h" +#include "SkImageInfo.h" +#include "SkPixelRef.h" +#include "SkRefCnt.h" +#include "SkStream.h" + +#include "Test.h" + +namespace { + +// SkPixelRef which fails to lock, as a lazy pixel ref might if its pixels +// cannot be generated. +class InvalidPixelRef : public SkPixelRef { +public: + InvalidPixelRef(const SkImageInfo& info) : SkPixelRef(info) {} +private: + virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE { return false; } + virtual void onUnlockPixels() SK_OVERRIDE { + SkDEBUGFAIL("InvalidPixelRef can't be locked"); + } +}; + +SkBitmap make_invalid_bitmap(const SkImageInfo& imageInfo) { + SkBitmap bitmap; + bitmap.setInfo(imageInfo); + bitmap.setPixelRef(SkNEW_ARGS(InvalidPixelRef, (imageInfo)))->unref(); + return bitmap; +} + +SkBitmap make_invalid_bitmap(SkColorType colorType) { + return make_invalid_bitmap( + SkImageInfo::Make(100, 100, colorType, kPremul_SkAlphaType)); +} + +} // namespace + +DEF_TEST(PDFInvalidBitmap, reporter) { + SkDynamicMemoryWStream stream; + SkAutoTUnref document(SkDocument::CreatePDF(&stream)); + SkCanvas* canvas = document->beginPage(100, 100); + + canvas->drawBitmap(SkBitmap(), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(SkImageInfo()), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(kN32_SkColorType), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(kIndex_8_SkColorType), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(kARGB_4444_SkColorType), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(kRGB_565_SkColorType), 0, 0); + canvas->drawBitmap(make_invalid_bitmap(kAlpha_8_SkColorType), 0, 0); + + // This test passes if it does not crash. +}