blob: 40efe1a5d85f9ff8f2e414a11292b961f6abc883 [file] [log] [blame]
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00006 */
7
reed@google.com8a85d0c2011-06-24 19:12:12 +00008#include "SkData.h"
vandebo@chromium.orgee34e352010-12-02 22:55:33 +00009#include "SkFlate.h"
10#include "SkStream.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "Test.h"
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000012
13// A memory stream that reports zero size with the standard call, like
14// an unseekable file stream would.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000015class SkZeroSizeMemStream : public SkMemoryStream {
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000016public:
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
halcanaryfbd35762014-08-13 08:07:38 -070028// Returns a deterministic data of the given size that should be
29// very compressible.
halcanarybc677c82014-08-13 07:14:36 -070030static SkData* new_test_data(size_t dataSize) {
31 SkAutoTMalloc<uint8_t> testBuffer(dataSize);
halcanarybc677c82014-08-13 07:14:36 -070032 for (size_t i = 0; i < dataSize; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080033 testBuffer[SkToInt(i)] = i % 64;
halcanarybc677c82014-08-13 07:14:36 -070034 }
35 return SkData::NewFromMalloc(testBuffer.detach(), dataSize);
36}
37
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000038static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
39 size_t dataSize) {
halcanarybc677c82014-08-13 07:14:36 -070040 SkASSERT(testStream != NULL);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000041
halcanarybc677c82014-08-13 07:14:36 -070042 SkAutoDataUnref testData(new_test_data(dataSize));
43 SkASSERT(testData->size() == dataSize);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000044
halcanarybc677c82014-08-13 07:14:36 -070045 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000046 SkDynamicMemoryWStream compressed;
halcanarybc677c82014-08-13 07:14:36 -070047 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed);
48 REPORTER_ASSERT(reporter, deflateSuccess);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000049
50 // Check that the input data wasn't changed.
51 size_t inputSize = testStream->getLength();
halcanarybc677c82014-08-13 07:14:36 -070052 if (inputSize == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000053 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
halcanarybc677c82014-08-13 07:14:36 -070054 }
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.orgee34e352010-12-02 22:55:33 +000061
halcanaryfbd35762014-08-13 08:07:38 -070062 size_t compressedSize = compressed.getOffset();
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000063
halcanarybc677c82014-08-13 07:14:36 -070064 SkAutoDataUnref compressedData(compressed.copyToData());
65 testStream->setData(compressedData.get());
reed@google.com8a85d0c2011-06-24 19:12:12 +000066
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000067 SkDynamicMemoryWStream uncompressed;
halcanarybc677c82014-08-13 07:14:36 -070068 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed);
69 REPORTER_ASSERT(reporter, inflateSuccess);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000070
71 // Check that the input data wasn't changed.
72 inputSize = testStream->getLength();
halcanarybc677c82014-08-13 07:14:36 -070073 if (inputSize == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000074 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
halcanarybc677c82014-08-13 07:14:36 -070075 }
halcanaryfbd35762014-08-13 08:07:38 -070076 REPORTER_ASSERT(reporter, compressedSize == inputSize);
halcanarybc677c82014-08-13 07:14:36 -070077 if (compressedData->size() == inputSize) {
78 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
79 compressedData->data(),
80 compressedData->size()) == 0);
81 }
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000082
83 // Check that the uncompressed data matches the source data.
halcanarybc677c82014-08-13 07:14:36 -070084 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 }
halcanaryfbd35762014-08-13 08:07:38 -070091
halcanary25123722014-08-13 09:20:08 -070092 if (compressedSize < 1) { return; }
93
halcanaryfbd35762014-08-13 08:07:38 -070094 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.orgee34e352010-12-02 22:55:33 +0000104}
105
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000106DEF_TEST(Flate, reporter) {
halcanary91d1d622015-02-17 14:43:06 -0800107 SkMemoryStream memStream;
108 TestFlate(reporter, &memStream, 512);
109 TestFlate(reporter, &memStream, 10240);
halcanarybc677c82014-08-13 07:14:36 -0700110
halcanary91d1d622015-02-17 14:43:06 -0800111 SkZeroSizeMemStream fileStream;
112 TestFlate(reporter, &fileStream, 512);
113 TestFlate(reporter, &fileStream, 10240);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +0000114}