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