CodecExactReadTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "include/codec/SkCodec.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkData.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkRefCnt.h"
  12. #include "include/core/SkStream.h"
  13. #include "include/private/SkTemplates.h"
  14. #include "src/core/SkMakeUnique.h"
  15. #include "tests/Test.h"
  16. #include "tools/Resources.h"
  17. #include <cstring>
  18. #include <initializer_list>
  19. #include <memory>
  20. #include <utility>
  21. namespace {
  22. // This class wraps another SkStream. It does not own the underlying stream, so
  23. // that the underlying stream can be reused starting from where the first
  24. // client left off. This mimics Android's JavaInputStreamAdaptor.
  25. class UnowningStream : public SkStream {
  26. public:
  27. explicit UnowningStream(SkStream* stream)
  28. : fStream(stream)
  29. {}
  30. size_t read(void* buf, size_t bytes) override {
  31. return fStream->read(buf, bytes);
  32. }
  33. bool rewind() override {
  34. return fStream->rewind();
  35. }
  36. bool isAtEnd() const override {
  37. return fStream->isAtEnd();
  38. }
  39. private:
  40. SkStream* fStream; // Unowned.
  41. };
  42. } // namespace
  43. // 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.
  46. DEF_TEST(Codec_end, r) {
  47. for (const char* path : { "images/plane.png",
  48. "images/yellow_rose.png",
  49. "images/plane_interlaced.png",
  50. "images/google_chrome.ico",
  51. "images/color_wheel.ico",
  52. "images/mandrill.wbmp",
  53. "images/randPixels.bmp",
  54. }) {
  55. sk_sp<SkData> data = GetResourceAsData(path);
  56. if (!data) {
  57. continue;
  58. }
  59. const int kNumImages = 2;
  60. const size_t size = data->size();
  61. sk_sp<SkData> multiData = SkData::MakeUninitialized(size * kNumImages);
  62. void* dst = multiData->writable_data();
  63. for (int i = 0; i < kNumImages; i++) {
  64. memcpy(SkTAddOffset<void>(dst, size * i), data->data(), size);
  65. }
  66. data.reset();
  67. SkMemoryStream stream(std::move(multiData));
  68. for (int i = 0; i < kNumImages; ++i) {
  69. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(
  70. skstd::make_unique<UnowningStream>(&stream)));
  71. if (!codec) {
  72. ERRORF(r, "Failed to create a codec from %s, iteration %i", path, i);
  73. continue;
  74. }
  75. auto info = codec->getInfo().makeColorType(kN32_SkColorType);
  76. SkBitmap bm;
  77. bm.allocPixels(info);
  78. auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
  79. if (result != SkCodec::kSuccess) {
  80. ERRORF(r, "Failed to getPixels from %s, iteration %i error %i", path, i, result);
  81. continue;
  82. }
  83. }
  84. }
  85. }