aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar jbroman <jbroman@chromium.org>2014-12-12 11:28:16 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2014-12-12 11:28:16 -0800
commit160715668957925d99440ac6aa10d3219b9b29b4 (patch)
tree6264ba8c6966588b7fdcb98b61bd32dc9ddfea09
parent6005e9b86f3954f29b7f51ad2aff50fbefda8f9b (diff)
Handle SkBitmaps which have no pixels in SkPDFImage.
A test that ensures this no longer crashes has been added. BUG=skia:3232 Review URL: https://codereview.chromium.org/802713002
-rw-r--r--gyp/tests.gypi1
-rw-r--r--src/pdf/SkPDFImage.cpp7
-rw-r--r--tests/PDFInvalidBitmapTest.cpp60
3 files changed, 66 insertions, 2 deletions
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<SkDocument> 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.
+}