jbroman | 1607156 | 2014-12-12 11:28:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkDocument.h" |
| 11 | #include "SkImageInfo.h" |
| 12 | #include "SkPixelRef.h" |
| 13 | #include "SkRefCnt.h" |
| 14 | #include "SkStream.h" |
| 15 | |
| 16 | #include "Test.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // SkPixelRef which fails to lock, as a lazy pixel ref might if its pixels |
| 21 | // cannot be generated. |
| 22 | class InvalidPixelRef : public SkPixelRef { |
| 23 | public: |
| 24 | InvalidPixelRef(const SkImageInfo& info) : SkPixelRef(info) {} |
| 25 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | bool onNewLockPixels(LockRec*) override { return false; } |
| 27 | void onUnlockPixels() override { |
jbroman | 1607156 | 2014-12-12 11:28:16 -0800 | [diff] [blame] | 28 | SkDEBUGFAIL("InvalidPixelRef can't be locked"); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | SkBitmap make_invalid_bitmap(const SkImageInfo& imageInfo) { |
| 33 | SkBitmap bitmap; |
| 34 | bitmap.setInfo(imageInfo); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 35 | bitmap.setPixelRef(sk_make_sp<InvalidPixelRef>(imageInfo), 0 ,0); |
jbroman | 1607156 | 2014-12-12 11:28:16 -0800 | [diff] [blame] | 36 | return bitmap; |
| 37 | } |
| 38 | |
| 39 | SkBitmap make_invalid_bitmap(SkColorType colorType) { |
| 40 | return make_invalid_bitmap( |
| 41 | SkImageInfo::Make(100, 100, colorType, kPremul_SkAlphaType)); |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 46 | DEF_TEST(SkPDF_InvalidBitmap, reporter) { |
jbroman | 1607156 | 2014-12-12 11:28:16 -0800 | [diff] [blame] | 47 | SkDynamicMemoryWStream stream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 48 | sk_sp<SkDocument> document(SkDocument::MakePDF(&stream)); |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 49 | if (!document) { |
| 50 | return; |
| 51 | } |
jbroman | 1607156 | 2014-12-12 11:28:16 -0800 | [diff] [blame] | 52 | SkCanvas* canvas = document->beginPage(100, 100); |
| 53 | |
| 54 | canvas->drawBitmap(SkBitmap(), 0, 0); |
| 55 | canvas->drawBitmap(make_invalid_bitmap(SkImageInfo()), 0, 0); |
| 56 | canvas->drawBitmap(make_invalid_bitmap(kN32_SkColorType), 0, 0); |
| 57 | canvas->drawBitmap(make_invalid_bitmap(kIndex_8_SkColorType), 0, 0); |
| 58 | canvas->drawBitmap(make_invalid_bitmap(kARGB_4444_SkColorType), 0, 0); |
| 59 | canvas->drawBitmap(make_invalid_bitmap(kRGB_565_SkColorType), 0, 0); |
| 60 | canvas->drawBitmap(make_invalid_bitmap(kAlpha_8_SkColorType), 0, 0); |
| 61 | |
| 62 | // This test passes if it does not crash. |
| 63 | } |