blob: d00c0bcf973fc27c5eb45047985dfc3366edf57c [file] [log] [blame]
Leon Scroggins III83239652017-04-21 13:47:12 -04001/*
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 Reedede7bac2017-07-23 15:30:02 -040014#include "SkMakeUnique.h"
Leon Scroggins III83239652017-04-21 13:47:12 -040015#include "SkStream.h"
16
17namespace {
Leon Scroggins III600effb2017-04-24 15:44:45 -040018// 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.
21class UnowningStream : public SkStream {
Leon Scroggins III83239652017-04-21 13:47:12 -040022public:
Leon Scroggins III600effb2017-04-24 15:44:45 -040023 explicit UnowningStream(SkStream* stream)
24 : fStream(stream)
25 {}
Leon Scroggins III83239652017-04-21 13:47:12 -040026
27 size_t read(void* buf, size_t bytes) override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040028 return fStream->read(buf, bytes);
Leon Scroggins III83239652017-04-21 13:47:12 -040029 }
30
31 bool rewind() override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040032 return fStream->rewind();
Leon Scroggins III83239652017-04-21 13:47:12 -040033 }
34
35 bool isAtEnd() const override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040036 return fStream->isAtEnd();
Leon Scroggins III83239652017-04-21 13:47:12 -040037 }
38private:
Leon Scroggins III600effb2017-04-24 15:44:45 -040039 SkStream* fStream; // Unowned.
Leon Scroggins III83239652017-04-21 13:47:12 -040040};
41} // namespace
42
Leon Scroggins III600effb2017-04-24 15:44:45 -040043// 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 III83239652017-04-21 13:47:12 -040046DEF_TEST(Codec_end, r) {
47 for (const char* path : { "plane.png",
48 "yellow_rose.png",
Leon Scroggins III600effb2017-04-24 15:44:45 -040049 "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 III83239652017-04-21 13:47:12 -040057 continue;
58 }
59
Leon Scroggins III600effb2017-04-24 15:44:45 -040060 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 III83239652017-04-21 13:47:12 -040066 }
Leon Scroggins III600effb2017-04-24 15:44:45 -040067 data.reset();
Leon Scroggins III83239652017-04-21 13:47:12 -040068
Leon Scroggins III600effb2017-04-24 15:44:45 -040069 SkMemoryStream stream(std::move(multiData));
70 for (int i = 0; i < kNumImages; ++i) {
Mike Reedede7bac2017-07-23 15:30:02 -040071 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
72 skstd::make_unique<UnowningStream>(&stream)));
Leon Scroggins III600effb2017-04-24 15:44:45 -040073 if (!codec) {
74 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
75 continue;
76 }
Leon Scroggins III83239652017-04-21 13:47:12 -040077
Leon Scroggins III600effb2017-04-24 15:44:45 -040078 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
79 SkBitmap bm;
80 bm.allocPixels(info);
Leon Scroggins III83239652017-04-21 13:47:12 -040081
Leon Scroggins III600effb2017-04-24 15:44:45 -040082 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 III83239652017-04-21 13:47:12 -040087 }
88 }
89}