Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
CodecExactReadTest.cpp
Go to the documentation of this file.
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
11#include "include/core/SkData.h"
16#include "tests/Test.h"
17#include "tools/Resources.h"
18
19#include <cstring>
20#include <initializer_list>
21#include <memory>
22#include <utility>
23
24namespace {
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.
28class UnowningStream : public SkStream {
29public:
30 explicit UnowningStream(SkStream* stream)
31 : fStream(stream)
32 {}
33
34 size_t read(void* buf, size_t bytes) override {
35 return fStream->read(buf, bytes);
36 }
37
38 bool rewind() override {
39 return fStream->rewind();
40 }
41
42 bool isAtEnd() const override {
43 return fStream->isAtEnd();
44 }
45private:
46 SkStream* fStream; // Unowned.
47};
48} // namespace
49
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.
53DEF_TEST(Codec_end, r) {
54 for (const char* path : { "images/plane.png",
55 "images/yellow_rose.png",
56 "images/plane_interlaced.png",
57 "images/mandrill.wbmp",
58 "images/randPixels.bmp",
59 }) {
61 if (!data) {
62 continue;
63 }
64
65 const int kNumImages = 2;
66 const size_t size = data->size();
67 sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages);
68 void* dst = multiData->writable_data();
69 for (int i = 0; i < kNumImages; i++) {
70 memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size);
71 }
72 data.reset();
73
74 SkMemoryStream stream(std::move(multiData));
75 for (int i = 0; i < kNumImages; ++i) {
76 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
77 std::make_unique<UnowningStream>(&stream)));
78 if (!codec) {
79 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
80 continue;
81 }
82
83 auto info = codec->getInfo().makeColorType(kN32_SkColorType);
84 SkBitmap bm;
85 bm.allocPixels(info);
86
87 auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
89 ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result);
90 continue;
91 }
92 }
93 }
94}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition Resources.cpp:42
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define ERRORF(r,...)
Definition Test.h:293
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
const SkImageInfo & info() const
Definition SkBitmap.h:139
static std::unique_ptr< SkCodec > MakeFromStream(std::unique_ptr< SkStream >, SkSpan< const SkCodecs::Decoder > decoders, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
Definition SkCodec.cpp:163
@ kSuccess
Definition SkCodec.h:80
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
virtual bool rewind()
Definition SkStream.h:100
virtual bool isAtEnd() const =0
virtual size_t read(void *buffer, size_t size)=0
GAsyncResult * result