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