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