Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkDeflate.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2010 The Android Open Source Project
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
8#include "src/pdf/SkDeflate.h"
9
10#include "include/core/SkData.h"
14
15#include "zlib.h" // NO_G3_REWRITE
16
17#include <algorithm>
18
19namespace {
20
21// Different zlib implementations use different T.
22// We've seen size_t and unsigned.
23template <typename T> void* skia_alloc_func(void*, T items, T size) {
24 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size));
25}
26
27void skia_free_func(void*, void* address) { sk_free(address); }
28
29} // namespace
30
31#define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
32#define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
33 // enough to always do a
34 // single loop.
35
36// called by both write() and finalize()
37static void do_deflate(int flush,
38 z_stream* zStream,
39 SkWStream* out,
40 unsigned char* inBuffer,
41 size_t inBufferSize) {
42 zStream->next_in = inBuffer;
43 zStream->avail_in = SkToInt(inBufferSize);
44 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
45 SkDEBUGCODE(int returnValue;)
46 do {
47 zStream->next_out = outBuffer;
48 zStream->avail_out = sizeof(outBuffer);
49 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
50 SkASSERT(!zStream->msg);
51
52 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
53 } while (zStream->avail_in || !zStream->avail_out);
54 SkASSERT(flush == Z_FINISH
55 ? returnValue == Z_STREAM_END
56 : returnValue == Z_OK);
57}
58
59// Hide all zlib impl details.
66
68 int compressionLevel,
69 bool gzip)
70 : fImpl(std::make_unique<SkDeflateWStream::Impl>()) {
71
72 // There has existed at some point at least one zlib implementation which thought it was being
73 // clever by randomizing the compression level. This is actually not entirely incorrect, except
74 // for the no-compression level which should always be deterministically pass-through.
75 // Users should instead consider the zero compression level broken and handle it themselves.
76 SkASSERT(compressionLevel != 0);
77
78 fImpl->fOut = out;
79 fImpl->fInBufferIndex = 0;
80 if (!fImpl->fOut) {
81 return;
82 }
83 fImpl->fZStream.next_in = nullptr;
84 fImpl->fZStream.zalloc = &skia_alloc_func;
85 fImpl->fZStream.zfree = &skia_free_func;
86 fImpl->fZStream.opaque = nullptr;
87 SkASSERT(compressionLevel <= 9 && compressionLevel >= -1);
88 SkDEBUGCODE(int r =) deflateInit2(&fImpl->fZStream, compressionLevel,
89 Z_DEFLATED, gzip ? 0x1F : 0x0F,
90 8, Z_DEFAULT_STRATEGY);
91 SkASSERT(Z_OK == r);
92}
93
95
97 TRACE_EVENT0("skia", TRACE_FUNC);
98 if (!fImpl->fOut) {
99 return;
100 }
101 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
102 fImpl->fInBufferIndex);
103 (void)deflateEnd(&fImpl->fZStream);
104 fImpl->fOut = nullptr;
105}
106
107bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
108 TRACE_EVENT0("skia", TRACE_FUNC);
109 if (!fImpl->fOut) {
110 return false;
111 }
112 const char* buffer = (const char*)void_buffer;
113 while (len > 0) {
114 size_t tocopy =
115 std::min(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
116 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
117 len -= tocopy;
118 buffer += tocopy;
119 fImpl->fInBufferIndex += tocopy;
120 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
121
122 // if the buffer isn't filled, don't call into zlib yet.
123 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
124 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut,
125 fImpl->fInBuffer, fImpl->fInBufferIndex);
126 fImpl->fInBufferIndex = 0;
127 }
128 }
129 return true;
130}
131
133 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
134}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
#define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE
Definition SkDeflate.cpp:31
static void do_deflate(int flush, z_stream *zStream, SkWStream *out, unsigned char *inBuffer, size_t inBufferSize)
Definition SkDeflate.cpp:37
#define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE
Definition SkDeflate.cpp:32
static void * sk_calloc_throw(size_t size)
Definition SkMalloc.h:71
SK_API void sk_free(void *)
constexpr size_t SkToSizeT(S x)
Definition SkTo.h:31
constexpr int SkToInt(S x)
Definition SkTo.h:29
#define TRACE_FUNC
bool write(const void *, size_t) override
SkDeflateWStream(SkWStream *, int compressionLevel, bool gzip=false)
Definition SkDeflate.cpp:67
~SkDeflateWStream() override
Definition SkDeflate.cpp:94
size_t bytesWritten() const override
static const uint8_t buffer[]
Definition ref_ptr.h:256
#define T
unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE]
Definition SkDeflate.cpp:62
#define TRACE_EVENT0(category_group, name)