blob: d0bf695b642688a2cbf9497e7ee2f203439789e6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "tests/Test.h"
16#include "tools/Resources.h"
Ben Wagnerb607a8f2018-03-12 13:46:21 -040017
18#include <cstring>
Ben Wagner9707a7e2019-05-06 17:17:19 -040019#include <initializer_list>
Ben Wagnerb607a8f2018-03-12 13:46:21 -040020#include <memory>
21#include <utility>
Leon Scroggins III83239652017-04-21 13:47:12 -040022
23namespace {
Leon Scroggins III600effb2017-04-24 15:44:45 -040024// 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.
27class UnowningStream : public SkStream {
Leon Scroggins III83239652017-04-21 13:47:12 -040028public:
Leon Scroggins III600effb2017-04-24 15:44:45 -040029 explicit UnowningStream(SkStream* stream)
30 : fStream(stream)
31 {}
Leon Scroggins III83239652017-04-21 13:47:12 -040032
33 size_t read(void* buf, size_t bytes) override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040034 return fStream->read(buf, bytes);
Leon Scroggins III83239652017-04-21 13:47:12 -040035 }
36
37 bool rewind() override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040038 return fStream->rewind();
Leon Scroggins III83239652017-04-21 13:47:12 -040039 }
40
41 bool isAtEnd() const override {
Leon Scroggins III600effb2017-04-24 15:44:45 -040042 return fStream->isAtEnd();
Leon Scroggins III83239652017-04-21 13:47:12 -040043 }
44private:
Leon Scroggins III600effb2017-04-24 15:44:45 -040045 SkStream* fStream; // Unowned.
Leon Scroggins III83239652017-04-21 13:47:12 -040046};
47} // namespace
48
Leon Scroggins III600effb2017-04-24 15:44:45 -040049// 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 III83239652017-04-21 13:47:12 -040052DEF_TEST(Codec_end, r) {
Hal Canaryc465d132017-12-08 10:21:31 -050053 for (const char* path : { "images/plane.png",
54 "images/yellow_rose.png",
55 "images/plane_interlaced.png",
Hal Canaryc465d132017-12-08 10:21:31 -050056 "images/mandrill.wbmp",
57 "images/randPixels.bmp",
Leon Scroggins III600effb2017-04-24 15:44:45 -040058 }) {
59 sk_sp<SkData> data = GetResourceAsData(path);
60 if (!data) {
Leon Scroggins III83239652017-04-21 13:47:12 -040061 continue;
62 }
63
Leon Scroggins III600effb2017-04-24 15:44:45 -040064 const int kNumImages = 2;
65 const size_t size = data->size();
66 sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages);
67 void* dst = multiData->writable_data();
68 for (int i = 0; i < kNumImages; i++) {
69 memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size);
Leon Scroggins III83239652017-04-21 13:47:12 -040070 }
Leon Scroggins III600effb2017-04-24 15:44:45 -040071 data.reset();
Leon Scroggins III83239652017-04-21 13:47:12 -040072
Leon Scroggins III600effb2017-04-24 15:44:45 -040073 SkMemoryStream stream(std::move(multiData));
74 for (int i = 0; i < kNumImages; ++i) {
Mike Reedede7bac2017-07-23 15:30:02 -040075 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -050076 std::make_unique<UnowningStream>(&stream)));
Leon Scroggins III600effb2017-04-24 15:44:45 -040077 if (!codec) {
78 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
79 continue;
80 }
Leon Scroggins III83239652017-04-21 13:47:12 -040081
Leon Scroggins III600effb2017-04-24 15:44:45 -040082 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
83 SkBitmap bm;
84 bm.allocPixels(info);
Leon Scroggins III83239652017-04-21 13:47:12 -040085
Leon Scroggins III600effb2017-04-24 15:44:45 -040086 auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
87 if (result != SkCodec::kSuccess) {
88 ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result);
89 continue;
90 }
Leon Scroggins III83239652017-04-21 13:47:12 -040091 }
92 }
93}