50 #elif defined(_MSC_VER) 51 #define TLS __declspec(thread) 56 static TLS int zlibBeforeBufSize = 0;
57 static TLS char *zlibBeforeBuf = NULL;
59 static TLS int zlibAfterBufSize = 0;
60 static TLS char *zlibAfterBuf = NULL;
61 static TLS int zlibAfterBufLen = 0;
65 if (zlibBeforeBufSize) {
69 if (zlibAfterBufSize) {
82 rfbSendOneRectEncodingZlib(rfbClientPtr cl,
93 char *fbptr = (cl->scaledScreen->frameBuffer + (cl->scaledScreen->paddedWidthInBytes *
y)
94 + (
x * (cl->scaledScreen->bitsPerPixel / 8)));
99 maxRawSize = (cl->scaledScreen->width * cl->scaledScreen->height
100 * (cl->format.bitsPerPixel / 8));
102 if (!zlibBeforeBuf || zlibBeforeBufSize < maxRawSize) {
103 if (zlibBeforeBuf == NULL)
104 zlibBeforeBuf = (
char *)malloc(maxRawSize);
106 char *reallocedBeforeEncBuf = (
char *)realloc(zlibBeforeBuf, maxRawSize);
107 if (!reallocedBeforeEncBuf)
return FALSE;
108 zlibBeforeBuf = reallocedBeforeEncBuf;
111 zlibBeforeBufSize = maxRawSize;
117 if (( w * h * (cl->scaledScreen->bitsPerPixel / 8)) <
118 VNC_ENCODE_ZLIB_MIN_COMP_SIZE ) {
128 if (( cl->format.bitsPerPixel > 8 ) &&
129 ( cl->ublen % ( cl->format.bitsPerPixel / 8 )) != 0 ) {
144 maxCompSize = maxRawSize + (( maxRawSize + 99 ) / 100 ) + 12;
146 if (!zlibAfterBuf || zlibAfterBufSize < maxCompSize) {
147 if (zlibAfterBuf == NULL)
148 zlibAfterBuf = (
char *)malloc(maxCompSize);
150 char *reallocedAfterEncBuf = (
char *)realloc(zlibAfterBuf, maxCompSize);
151 if (!reallocedAfterEncBuf)
return FALSE;
152 zlibAfterBuf = reallocedAfterEncBuf;
155 zlibAfterBufSize = maxCompSize;
158 if (!zlibBeforeBuf || !zlibAfterBuf)
160 rfbLog(
"rfbSendOneRectEncodingZlib: failed to allocate memory\n");
167 (*cl->translateFn)(cl->translateLookupTable, &cl->screen->serverFormat,
168 &cl->format, fbptr, zlibBeforeBuf,
169 cl->scaledScreen->paddedWidthInBytes, w, h);
171 cl->compStream.next_in = ( Bytef * )zlibBeforeBuf;
172 cl->compStream.avail_in = w * h * (cl->format.bitsPerPixel / 8);
173 cl->compStream.next_out = ( Bytef * )zlibAfterBuf;
174 cl->compStream.avail_out = maxCompSize;
175 cl->compStream.data_type = Z_BINARY;
178 if ( cl->compStreamInited ==
FALSE ) {
180 cl->compStream.total_in = 0;
181 cl->compStream.total_out = 0;
182 cl->compStream.zalloc = Z_NULL;
183 cl->compStream.zfree = Z_NULL;
184 cl->compStream.opaque = Z_NULL;
186 deflateInit2( &(cl->compStream),
187 cl->zlibCompressLevel,
191 Z_DEFAULT_STRATEGY );
194 cl->compStreamInited =
TRUE;
198 previousOut = cl->compStream.total_out;
201 deflateResult = deflate( &(cl->compStream), Z_SYNC_FLUSH );
204 zlibAfterBufLen = cl->compStream.total_out - previousOut;
206 if ( deflateResult != Z_OK ) {
207 rfbErr(
"zlib deflation error: %s\n", cl->compStream.msg);
220 + w * (cl->format.bitsPerPixel / 8) * h);
235 memcpy(&cl->updateBuf[cl->ublen], (
char *)&rect,
244 for (i = 0; i < zlibAfterBufLen;) {
248 if (i + bytesToCopy > zlibAfterBufLen) {
249 bytesToCopy = zlibAfterBufLen - i;
252 memcpy(&cl->updateBuf[cl->ublen], &zlibAfterBuf[i], bytesToCopy);
254 cl->ublen += bytesToCopy;
290 maxLines = ( ZLIB_MAX_SIZE(w) / w );
296 while ( linesRemaining > 0 ) {
300 if ( maxLines < linesRemaining )
301 linesToComp = maxLines;
303 linesToComp = linesRemaining;
305 partialRect.
h = linesToComp;
308 if ( ! rfbSendOneRectEncodingZlib( cl,
328 if (( cl->ublen > 0 ) &&
329 ( linesToComp == maxLines )) {
337 linesRemaining -= linesToComp;
338 partialRect.
y += linesToComp;
rfbBool rfbSendRectEncodingRaw(rfbClientPtr cl, int x, int y, int w, int h)
void rfbZlibCleanup(rfbScreenInfoPtr screen)
#define UPDATE_BUF_SIZE
UPDATE_BUF_SIZE must be big enough to send at least one whole line of the framebuffer.
rfbBool rfbSendUpdateBuf(rfbClientPtr cl)
void rfbStatRecordEncodingSent(rfbClientPtr cl, uint32_t type, int byteCount, int byteIfRaw)
#define sz_rfbFramebufferUpdateRectHeader
rfbBool rfbSendRectEncodingZlib(rfbClientPtr cl, int x, int y, int w, int h)