vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 8 | #include "SkData.h" |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 9 | #include "SkFlate.h" |
| 10 | #include "SkStream.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 12 | |
| 13 | // A memory stream that reports zero size with the standard call, like |
| 14 | // an unseekable file stream would. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 15 | class SkZeroSizeMemStream : public SkMemoryStream { |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 16 | public: |
| 17 | virtual size_t read(void* buffer, size_t size) { |
| 18 | if (buffer == NULL && size == 0) |
| 19 | return 0; |
| 20 | if (buffer == NULL && size == kGetSizeKey) |
| 21 | size = 0; |
| 22 | return SkMemoryStream::read(buffer, size); |
| 23 | } |
| 24 | |
| 25 | static const size_t kGetSizeKey = 0xDEADBEEF; |
| 26 | }; |
| 27 | |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 28 | // Returns a deterministic data of the given size that should be |
| 29 | // very compressible. |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 30 | static SkData* new_test_data(size_t dataSize) { |
| 31 | SkAutoTMalloc<uint8_t> testBuffer(dataSize); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 32 | for (size_t i = 0; i < dataSize; ++i) { |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 33 | testBuffer[i] = i % 64; |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 34 | } |
| 35 | return SkData::NewFromMalloc(testBuffer.detach(), dataSize); |
| 36 | } |
| 37 | |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 38 | static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream, |
| 39 | size_t dataSize) { |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 40 | SkASSERT(testStream != NULL); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 41 | |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 42 | SkAutoDataUnref testData(new_test_data(dataSize)); |
| 43 | SkASSERT(testData->size() == dataSize); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 44 | |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 45 | testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 46 | SkDynamicMemoryWStream compressed; |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 47 | bool deflateSuccess = SkFlate::Deflate(testStream, &compressed); |
| 48 | REPORTER_ASSERT(reporter, deflateSuccess); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 49 | |
| 50 | // Check that the input data wasn't changed. |
| 51 | size_t inputSize = testStream->getLength(); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 52 | if (inputSize == 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 53 | inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 54 | } |
| 55 | REPORTER_ASSERT(reporter, dataSize == inputSize); |
| 56 | if (dataSize == inputSize) { |
| 57 | REPORTER_ASSERT(reporter, memcmp(testData->data(), |
| 58 | testStream->getMemoryBase(), |
| 59 | dataSize) == 0); |
| 60 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 61 | |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 62 | size_t compressedSize = compressed.getOffset(); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 63 | |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 64 | SkAutoDataUnref compressedData(compressed.copyToData()); |
| 65 | testStream->setData(compressedData.get()); |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 66 | |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 67 | SkDynamicMemoryWStream uncompressed; |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 68 | bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed); |
| 69 | REPORTER_ASSERT(reporter, inflateSuccess); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 70 | |
| 71 | // Check that the input data wasn't changed. |
| 72 | inputSize = testStream->getLength(); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 73 | if (inputSize == 0) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 74 | inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 75 | } |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, compressedSize == inputSize); |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 77 | if (compressedData->size() == inputSize) { |
| 78 | REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(), |
| 79 | compressedData->data(), |
| 80 | compressedData->size()) == 0); |
| 81 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 82 | |
| 83 | // Check that the uncompressed data matches the source data. |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 84 | SkAutoDataUnref uncompressedData(uncompressed.copyToData()); |
| 85 | REPORTER_ASSERT(reporter, dataSize == uncompressedData->size()); |
| 86 | if (dataSize == uncompressedData->size()) { |
| 87 | REPORTER_ASSERT(reporter, memcmp(testData->data(), |
| 88 | uncompressedData->data(), |
| 89 | dataSize) == 0); |
| 90 | } |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 91 | |
halcanary | 2512372 | 2014-08-13 09:20:08 -0700 | [diff] [blame] | 92 | if (compressedSize < 1) { return; } |
| 93 | |
halcanary | fbd3576 | 2014-08-13 08:07:38 -0700 | [diff] [blame] | 94 | double compressionRatio = static_cast<double>(dataSize) / compressedSize; |
| 95 | // Assert that some compression took place. |
| 96 | REPORTER_ASSERT(reporter, compressionRatio > 1.2); |
| 97 | |
| 98 | if (reporter->verbose()) { |
| 99 | SkDebugf("Flate Test: \t input size: " SK_SIZE_T_SPECIFIER |
| 100 | "\tcompressed size: " SK_SIZE_T_SPECIFIER |
| 101 | "\tratio: %.4g\n", |
| 102 | dataSize, compressedSize, compressionRatio); |
| 103 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 104 | } |
| 105 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 106 | DEF_TEST(Flate, reporter) { |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 107 | #ifdef SK_HAS_ZLIB |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, SkFlate::HaveFlate()); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 109 | #endif |
halcanary | bc677c8 | 2014-08-13 07:14:36 -0700 | [diff] [blame] | 110 | if (SkFlate::HaveFlate()) { |
| 111 | SkMemoryStream memStream; |
| 112 | TestFlate(reporter, &memStream, 512); |
| 113 | TestFlate(reporter, &memStream, 10240); |
| 114 | |
| 115 | SkZeroSizeMemStream fileStream; |
| 116 | TestFlate(reporter, &fileStream, 512); |
| 117 | TestFlate(reporter, &fileStream, 10240); |
| 118 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 119 | } |