• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "SkBitmap.h"
10 #include "SkCodec.h"
11 #include "SkData.h"
12 #include "SkImageInfo.h"
13 #include "SkMakeUnique.h"
14 #include "SkRefCnt.h"
15 #include "SkStream.h"
16 #include "SkTemplates.h"
17 #include "Test.h"
18 
19 #include <cstring>
20 #include <memory>
21 #include <utility>
22 
23 namespace {
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 {
28 public:
UnowningStream(SkStream * stream)29     explicit UnowningStream(SkStream* stream)
30         : fStream(stream)
31     {}
32 
read(void * buf,size_t bytes)33     size_t read(void* buf, size_t bytes) override {
34         return fStream->read(buf, bytes);
35     }
36 
rewind()37     bool rewind() override {
38         return fStream->rewind();
39     }
40 
isAtEnd() const41     bool isAtEnd() const override {
42         return fStream->isAtEnd();
43     }
44 private:
45     SkStream* fStream; // Unowned.
46 };
47 } // namespace
48 
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.
DEF_TEST(Codec_end,r)52 DEF_TEST(Codec_end, r) {
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",
60                               }) {
61         sk_sp<SkData> data = GetResourceAsData(path);
62         if (!data) {
63             continue;
64         }
65 
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);
72         }
73         data.reset();
74 
75         SkMemoryStream stream(std::move(multiData));
76         for (int i = 0; i < kNumImages; ++i) {
77             std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
78                                                    skstd::make_unique<UnowningStream>(&stream)));
79             if (!codec) {
80                 ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
81                 continue;
82             }
83 
84             auto info = codec->getInfo().makeColorType(kN32_SkColorType);
85             SkBitmap bm;
86             bm.allocPixels(info);
87 
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             }
93         }
94     }
95 }
96