blob: 17e8b2de2ab65589ea10ddefba435bd2f6ce921d [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
halcanary91d1d622015-02-17 14:43:06 -080013#ifndef SK_NO_FLATE
14
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000015// A memory stream that reports zero size with the standard call, like
16// an unseekable file stream would.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000017class SkZeroSizeMemStream : public SkMemoryStream {
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000018public:
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
halcanaryfbd35762014-08-13 08:07:38 -070030// Returns a deterministic data of the given size that should be
31// very compressible.
halcanarybc677c82014-08-13 07:14:36 -070032static SkData* new_test_data(size_t dataSize) {
33 SkAutoTMalloc<uint8_t> testBuffer(dataSize);
halcanarybc677c82014-08-13 07:14:36 -070034 for (size_t i = 0; i < dataSize; ++i) {
bsalomonfbaace02014-12-12 16:41:46 -080035 testBuffer[SkToInt(i)] = i % 64;
halcanarybc677c82014-08-13 07:14:36 -070036 }
37 return SkData::NewFromMalloc(testBuffer.detach(), dataSize);
38}
39
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000040static void TestFlate(skiatest::Reporter* reporter, SkMemoryStream* testStream,
41 size_t dataSize) {
halcanarybc677c82014-08-13 07:14:36 -070042 SkASSERT(testStream != NULL);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000043
halcanarybc677c82014-08-13 07:14:36 -070044 SkAutoDataUnref testData(new_test_data(dataSize));
45 SkASSERT(testData->size() == dataSize);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000046
halcanarybc677c82014-08-13 07:14:36 -070047 testStream->setMemory(testData->data(), dataSize, /*copyData=*/ true);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000048 SkDynamicMemoryWStream compressed;
halcanarybc677c82014-08-13 07:14:36 -070049 bool deflateSuccess = SkFlate::Deflate(testStream, &compressed);
50 REPORTER_ASSERT(reporter, deflateSuccess);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000051
52 // Check that the input data wasn't changed.
53 size_t inputSize = testStream->getLength();
halcanarybc677c82014-08-13 07:14:36 -070054 if (inputSize == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000055 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
halcanarybc677c82014-08-13 07:14:36 -070056 }
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.orgee34e352010-12-02 22:55:33 +000063
halcanaryfbd35762014-08-13 08:07:38 -070064 size_t compressedSize = compressed.getOffset();
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000065
halcanarybc677c82014-08-13 07:14:36 -070066 SkAutoDataUnref compressedData(compressed.copyToData());
67 testStream->setData(compressedData.get());
reed@google.com8a85d0c2011-06-24 19:12:12 +000068
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000069 SkDynamicMemoryWStream uncompressed;
halcanarybc677c82014-08-13 07:14:36 -070070 bool inflateSuccess = SkFlate::Inflate(testStream, &uncompressed);
71 REPORTER_ASSERT(reporter, inflateSuccess);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000072
73 // Check that the input data wasn't changed.
74 inputSize = testStream->getLength();
halcanarybc677c82014-08-13 07:14:36 -070075 if (inputSize == 0) {
vandebo@chromium.org2a22e102011-01-25 21:01:34 +000076 inputSize = testStream->read(NULL, SkZeroSizeMemStream::kGetSizeKey);
halcanarybc677c82014-08-13 07:14:36 -070077 }
halcanaryfbd35762014-08-13 08:07:38 -070078 REPORTER_ASSERT(reporter, compressedSize == inputSize);
halcanarybc677c82014-08-13 07:14:36 -070079 if (compressedData->size() == inputSize) {
80 REPORTER_ASSERT(reporter, memcmp(testStream->getMemoryBase(),
81 compressedData->data(),
82 compressedData->size()) == 0);
83 }
vandebo@chromium.orgee34e352010-12-02 22:55:33 +000084
85 // Check that the uncompressed data matches the source data.
halcanarybc677c82014-08-13 07:14:36 -070086 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 }
halcanaryfbd35762014-08-13 08:07:38 -070093
halcanary25123722014-08-13 09:20:08 -070094 if (compressedSize < 1) { return; }
95
halcanaryfbd35762014-08-13 08:07:38 -070096 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.orgee34e352010-12-02 22:55:33 +0000106}
107
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000108DEF_TEST(Flate, reporter) {
halcanary91d1d622015-02-17 14:43:06 -0800109 SkMemoryStream memStream;
110 TestFlate(reporter, &memStream, 512);
111 TestFlate(reporter, &memStream, 10240);
halcanarybc677c82014-08-13 07:14:36 -0700112
halcanary91d1d622015-02-17 14:43:06 -0800113 SkZeroSizeMemStream fileStream;
114 TestFlate(reporter, &fileStream, 512);
115 TestFlate(reporter, &fileStream, 10240);
vandebo@chromium.orgee34e352010-12-02 22:55:33 +0000116}
halcanary91d1d622015-02-17 14:43:06 -0800117#endif // SK_NO_FLATE