Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
SkDeflateWStream Class Referencefinal

#include <SkDeflate.h>

Inheritance diagram for SkDeflateWStream:
SkWStream

Classes

struct  Impl
 

Public Member Functions

 SkDeflateWStream (SkWStream *, int compressionLevel, bool gzip=false)
 
 ~SkDeflateWStream () override
 
void finalize ()
 
bool write (const void *, size_t) override
 
size_t bytesWritten () const override
 
- Public Member Functions inherited from SkWStream
virtual ~SkWStream ()
 
 SkWStream ()
 
virtual void flush ()
 
bool write8 (U8CPU value)
 
bool write16 (U16CPU value)
 
bool write32 (uint32_t v)
 
bool writeText (const char text[])
 
bool newline ()
 
bool writeDecAsText (int32_t)
 
bool writeBigDecAsText (int64_t, int minDigits=0)
 
bool writeHexAsText (uint32_t, int minDigits=0)
 
bool writeScalarAsText (SkScalar)
 
bool writeBool (bool v)
 
bool writeScalar (SkScalar)
 
bool writePackedUInt (size_t)
 
bool writeStream (SkStream *input, size_t length)
 

Additional Inherited Members

- Static Public Member Functions inherited from SkWStream
static int SizeOfPackedUInt (size_t value)
 

Detailed Description

Wrap a stream in this class to compress the information written to this stream using the Deflate algorithm.

See http://en.wikipedia.org/wiki/DEFLATE

Definition at line 20 of file SkDeflate.h.

Constructor & Destructor Documentation

◆ SkDeflateWStream()

SkDeflateWStream::SkDeflateWStream ( SkWStream out,
int  compressionLevel,
bool  gzip = false 
)

Does not take ownership of the stream.

Parameters
compressionLevel1 is best speed; 9 is best compression. The default, -1, is to use zlib's Z_DEFAULT_COMPRESSION level. 0 would be no compression, but due to broken zlibs, users should handle that themselves.
gzipiff true, output a gzip file. "The gzip format is a wrapper, documented in RFC 1952, around a deflate stream." gzip adds a header with a magic number to the beginning of the stream, allowing a client to identify a gzip file.

Definition at line 67 of file SkDeflate.cpp.

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}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23

◆ ~SkDeflateWStream()

SkDeflateWStream::~SkDeflateWStream ( )
override

The destructor calls finalize().

Definition at line 94 of file SkDeflate.cpp.

94{ this->finalize(); }

Member Function Documentation

◆ bytesWritten()

size_t SkDeflateWStream::bytesWritten ( ) const
overridevirtual

Implements SkWStream.

Definition at line 132 of file SkDeflate.cpp.

132 {
133 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
134}

◆ finalize()

void SkDeflateWStream::finalize ( )

Write the end of the compressed stream. All subsequent calls to write() will fail. Subsequent calls to finalize() do nothing.

Definition at line 96 of file SkDeflate.cpp.

96 {
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}
static void do_deflate(int flush, z_stream *zStream, SkWStream *out, unsigned char *inBuffer, size_t inBufferSize)
Definition SkDeflate.cpp:37
#define TRACE_FUNC
#define TRACE_EVENT0(category_group, name)

◆ write()

bool SkDeflateWStream::write ( const void *  buffer,
size_t  size 
)
overridevirtual

Called to write bytes to a SkWStream. Returns true on success

Parameters
bufferthe address of at least size bytes to be written to the stream
sizeThe number of bytes in buffer to write to the stream
Returns
true on success

Implements SkWStream.

Definition at line 107 of file SkDeflate.cpp.

107 {
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}
static const uint8_t buffer[]

The documentation for this class was generated from the following files: