blob: 5bf4f009d974dd5717d904e126b6b5c27cc3ede4 [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",
56 "images/google_chrome.ico",
57 "images/color_wheel.ico",
58 "images/mandrill.wbmp",
59 "images/randPixels.bmp",
Leon Scroggins III600effb2017-04-24 15:44:45 -040060 }) {
61 sk_sp<SkData> data = GetResourceAsData(path);
62 if (!data) {
Leon Scroggins III83239652017-04-21 13:47:12 -040063 continue;
64 }
65
Leon Scroggins III600effb2017-04-24 15:44:45 -040066 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 III83239652017-04-21 13:47:12 -040072 }
Leon Scroggins III600effb2017-04-24 15:44:45 -040073 data.reset();
Leon Scroggins III83239652017-04-21 13:47:12 -040074
Leon Scroggins III600effb2017-04-24 15:44:45 -040075 SkMemoryStream stream(std::move(multiData));
76 for (int i = 0; i < kNumImages; ++i) {
Mike Reedede7bac2017-07-23 15:30:02 -040077 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
Mike Kleinf46d5ca2019-12-11 10:45:01 -050078 std::make_unique<UnowningStream>(&stream)));
Leon Scroggins III600effb2017-04-24 15:44:45 -040079 if (!codec) {
80 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
81 continue;
82 }
Leon Scroggins III83239652017-04-21 13:47:12 -040083
Leon Scroggins III600effb2017-04-24 15:44:45 -040084 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
85 SkBitmap bm;
86 bm.allocPixels(info);
Leon Scroggins III83239652017-04-21 13:47:12 -040087
Leon Scroggins III600effb2017-04-24 15:44:45 -040088 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 III83239652017-04-21 13:47:12 -040093 }
94 }
95}