OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 }; | 66 }; |
67 | 67 |
68 SkDeflateWStream::SkDeflateWStream(SkWStream* out) : fImpl(new SkDeflateWStream:
:Impl) { | 68 SkDeflateWStream::SkDeflateWStream(SkWStream* out) : fImpl(new SkDeflateWStream:
:Impl) { |
69 fImpl->fOut = out; | 69 fImpl->fOut = out; |
70 fImpl->fInBufferIndex = 0; | 70 fImpl->fInBufferIndex = 0; |
71 if (!fImpl->fOut) { | 71 if (!fImpl->fOut) { |
72 return; | 72 return; |
73 } | 73 } |
74 fImpl->fZStream.zalloc = &skia_alloc_func; | 74 fImpl->fZStream.zalloc = &skia_alloc_func; |
75 fImpl->fZStream.zfree = &skia_free_func; | 75 fImpl->fZStream.zfree = &skia_free_func; |
76 fImpl->fZStream.opaque = NULL; | 76 fImpl->fZStream.opaque = nullptr; |
77 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION); | 77 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION); |
78 SkASSERT(Z_OK == r); | 78 SkASSERT(Z_OK == r); |
79 } | 79 } |
80 | 80 |
81 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); } | 81 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); } |
82 | 82 |
83 void SkDeflateWStream::finalize() { | 83 void SkDeflateWStream::finalize() { |
84 if (!fImpl->fOut) { | 84 if (!fImpl->fOut) { |
85 return; | 85 return; |
86 } | 86 } |
87 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer, | 87 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer, |
88 fImpl->fInBufferIndex); | 88 fImpl->fInBufferIndex); |
89 (void)deflateEnd(&fImpl->fZStream); | 89 (void)deflateEnd(&fImpl->fZStream); |
90 fImpl->fOut = NULL; | 90 fImpl->fOut = nullptr; |
91 } | 91 } |
92 | 92 |
93 bool SkDeflateWStream::write(const void* void_buffer, size_t len) { | 93 bool SkDeflateWStream::write(const void* void_buffer, size_t len) { |
94 if (!fImpl->fOut) { | 94 if (!fImpl->fOut) { |
95 return false; | 95 return false; |
96 } | 96 } |
97 const char* buffer = (const char*)void_buffer; | 97 const char* buffer = (const char*)void_buffer; |
98 while (len > 0) { | 98 while (len > 0) { |
99 size_t tocopy = | 99 size_t tocopy = |
100 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex); | 100 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex); |
101 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy); | 101 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy); |
102 len -= tocopy; | 102 len -= tocopy; |
103 buffer += tocopy; | 103 buffer += tocopy; |
104 fImpl->fInBufferIndex += tocopy; | 104 fImpl->fInBufferIndex += tocopy; |
105 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer)); | 105 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer)); |
106 | 106 |
107 // if the buffer isn't filled, don't call into zlib yet. | 107 // if the buffer isn't filled, don't call into zlib yet. |
108 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) { | 108 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) { |
109 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut, | 109 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut, |
110 fImpl->fInBuffer, fImpl->fInBufferIndex); | 110 fImpl->fInBuffer, fImpl->fInBufferIndex); |
111 fImpl->fInBufferIndex = 0; | 111 fImpl->fInBufferIndex = 0; |
112 } | 112 } |
113 } | 113 } |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 size_t SkDeflateWStream::bytesWritten() const { | 117 size_t SkDeflateWStream::bytesWritten() const { |
118 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; | 118 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; |
119 } | 119 } |
OLD | NEW |