Flutter Engine
The Flutter Engine
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
15
16#include <algorithm>
17#include <cstdint>
18#include <cstring>
19
20#include "zlib.h" // NO_G3_REWRITE
21
22namespace {
23
24// Different zlib implementations use different T.
25// We've seen size_t and unsigned.
26template <typename T> void* skia_alloc_func(void*, T items, T size) {
27 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size));
28}
29
30void skia_free_func(void*, void* address) { sk_free(address); }
31
32} // namespace
33
34#define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
35#define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
36 // enough to always do a
37 // single loop.
38
39// called by both write() and finalize()
40static void do_deflate(int flush,
41 z_stream* zStream,
43 unsigned char* inBuffer,
44 size_t inBufferSize) {
45 zStream->next_in = inBuffer;
46 zStream->avail_in = SkToInt(inBufferSize);
47 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
48 SkDEBUGCODE(int returnValue;)
49 do {
50 zStream->next_out = outBuffer;
51 zStream->avail_out = sizeof(outBuffer);
52 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
53 SkASSERT(!zStream->msg);
54
55 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
56 } while (zStream->avail_in || !zStream->avail_out);
57 SkASSERT(flush == Z_FINISH
58 ? returnValue == Z_STREAM_END
59 : returnValue == Z_OK);
60}
61
62// Hide all zlib impl details.
67 z_stream fZStream;
68};
69
71 int compressionLevel,
72 bool gzip)
73 : fImpl(std::make_unique<SkDeflateWStream::Impl>()) {
74
75 // There has existed at some point at least one zlib implementation which thought it was being
76 // clever by randomizing the compression level. This is actually not entirely incorrect, except
77 // for the no-compression level which should always be deterministically pass-through.
78 // Users should instead consider the zero compression level broken and handle it themselves.
79 SkASSERT(compressionLevel != 0);
80
81 fImpl->fOut = out;
82 fImpl->fInBufferIndex = 0;
83 if (!fImpl->fOut) {
84 return;
85 }
86 fImpl->fZStream.next_in = nullptr;
87 fImpl->fZStream.zalloc = &skia_alloc_func;
88 fImpl->fZStream.zfree = &skia_free_func;
89 fImpl->fZStream.opaque = nullptr;
90 SkASSERT(compressionLevel <= 9 && compressionLevel >= -1);
91 SkDEBUGCODE(int r =) deflateInit2(&fImpl->fZStream, compressionLevel,
92 Z_DEFLATED, gzip ? 0x1F : 0x0F,
93 8, Z_DEFAULT_STRATEGY);
94 SkASSERT(Z_OK == r);
95}
96
98
100 TRACE_EVENT0("skia", TRACE_FUNC);
101 if (!fImpl->fOut) {
102 return;
103 }
104 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
105 fImpl->fInBufferIndex);
106 (void)deflateEnd(&fImpl->fZStream);
107 fImpl->fOut = nullptr;
108}
109
110bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
111 TRACE_EVENT0("skia", TRACE_FUNC);
112 if (!fImpl->fOut) {
113 return false;
114 }
115 const char* buffer = (const char*)void_buffer;
116 while (len > 0) {
117 size_t tocopy =
118 std::min(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
119 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
120 len -= tocopy;
121 buffer += tocopy;
122 fImpl->fInBufferIndex += tocopy;
123 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
124
125 // if the buffer isn't filled, don't call into zlib yet.
126 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
127 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut,
128 fImpl->fInBuffer, fImpl->fInBufferIndex);
129 fImpl->fInBufferIndex = 0;
130 }
131 }
132 return true;
133}
134
136 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
137}
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE
Definition: SkDeflate.cpp:34
static void do_deflate(int flush, z_stream *zStream, SkWStream *out, unsigned char *inBuffer, size_t inBufferSize)
Definition: SkDeflate.cpp:40
#define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE
Definition: SkDeflate.cpp:35
static void * sk_calloc_throw(size_t size)
Definition: SkMalloc.h:71
SK_API void sk_free(void *)
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
constexpr size_t SkToSizeT(S x)
Definition: SkTo.h:31
constexpr int SkToInt(S x)
Definition: SkTo.h:29
#define TRACE_FUNC
Definition: SkTraceEvent.h:30
bool write(const void *, size_t) override
Definition: SkDeflate.cpp:110
SkDeflateWStream(SkWStream *, int compressionLevel, bool gzip=false)
Definition: SkDeflate.cpp:70
~SkDeflateWStream() override
Definition: SkDeflate.cpp:97
size_t bytesWritten() const override
Definition: SkDeflate.cpp:135
static float min(float r, float g, float b)
Definition: hsl.cpp:48
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Definition: ref_ptr.h:256
#define T
Definition: precompiler.cc:65
unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE]
Definition: SkDeflate.cpp:65
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131