Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "Resources.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkCodec.h" |
| 13 | #include "SkData.h" |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 14 | #include "SkMakeUnique.h" |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | |
| 17 | namespace { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 18 | // This class wraps another SkStream. It does not own the underlying stream, so |
| 19 | // that the underlying stream can be reused starting from where the first |
| 20 | // client left off. This mimics Android's JavaInputStreamAdaptor. |
| 21 | class UnowningStream : public SkStream { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 22 | public: |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 23 | explicit UnowningStream(SkStream* stream) |
| 24 | : fStream(stream) |
| 25 | {} |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 26 | |
| 27 | size_t read(void* buf, size_t bytes) override { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 28 | return fStream->read(buf, bytes); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool rewind() override { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 32 | return fStream->rewind(); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | bool isAtEnd() const override { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 36 | return fStream->isAtEnd(); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 37 | } |
| 38 | private: |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 39 | SkStream* fStream; // Unowned. |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 40 | }; |
| 41 | } // namespace |
| 42 | |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 43 | // Test that some SkCodecs do not attempt to read input beyond the logical |
| 44 | // end of the data. Some other SkCodecs do, but some Android apps rely on not |
| 45 | // doing so for PNGs. Test on other formats that work. |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 46 | DEF_TEST(Codec_end, r) { |
| 47 | for (const char* path : { "plane.png", |
| 48 | "yellow_rose.png", |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 49 | "plane_interlaced.png", |
| 50 | "google_chrome.ico", |
| 51 | "color_wheel.ico", |
| 52 | "mandrill.wbmp", |
| 53 | "randPixels.bmp", |
| 54 | }) { |
| 55 | sk_sp<SkData> data = GetResourceAsData(path); |
| 56 | if (!data) { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 57 | continue; |
| 58 | } |
| 59 | |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 60 | const int kNumImages = 2; |
| 61 | const size_t size = data->size(); |
| 62 | sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages); |
| 63 | void* dst = multiData->writable_data(); |
| 64 | for (int i = 0; i < kNumImages; i++) { |
| 65 | memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 66 | } |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 67 | data.reset(); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 68 | |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 69 | SkMemoryStream stream(std::move(multiData)); |
| 70 | for (int i = 0; i < kNumImages; ++i) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 71 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream( |
| 72 | skstd::make_unique<UnowningStream>(&stream))); |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 73 | if (!codec) { |
| 74 | ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i); |
| 75 | continue; |
| 76 | } |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 77 | |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 78 | auto info = codec->getInfo().makeColorType(kN32_SkColorType); |
| 79 | SkBitmap bm; |
| 80 | bm.allocPixels(info); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 81 | |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 82 | auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes()); |
| 83 | if (result != SkCodec::kSuccess) { |
| 84 | ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result); |
| 85 | continue; |
| 86 | } |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |