Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <rfb/rfb.h>
00011 #include "minilzo.h"
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #define MAX_WRKMEM ((LZO1X_1_MEM_COMPRESS) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t)
00028
00029
00030 void rfbFreeUltraData(rfbClientPtr cl) {
00031 if (cl->compStreamInitedLZO) {
00032 free(cl->lzoWrkMem);
00033 cl->compStreamInitedLZO=FALSE;
00034 }
00035 }
00036
00037
00038 static rfbBool
00039 rfbSendOneRectEncodingUltra(rfbClientPtr cl,
00040 int x,
00041 int y,
00042 int w,
00043 int h)
00044 {
00045 rfbFramebufferUpdateRectHeader rect;
00046 rfbZlibHeader hdr;
00047 int deflateResult;
00048 int i;
00049 char *fbptr = (cl->scaledScreen->frameBuffer + (cl->scaledScreen->paddedWidthInBytes * y)
00050 + (x * (cl->scaledScreen->bitsPerPixel / 8)));
00051
00052 int maxRawSize;
00053 lzo_uint maxCompSize;
00054
00055 maxRawSize = (w * h * (cl->format.bitsPerPixel / 8));
00056
00057 if (cl->beforeEncBufSize < maxRawSize) {
00058 cl->beforeEncBufSize = maxRawSize;
00059 if (cl->beforeEncBuf == NULL)
00060 cl->beforeEncBuf = (char *)malloc(cl->beforeEncBufSize);
00061 else
00062 cl->beforeEncBuf = (char *)realloc(cl->beforeEncBuf, cl->beforeEncBufSize);
00063 }
00064
00065
00066
00067
00068
00069 maxCompSize = (maxRawSize + maxRawSize / 16 + 64 + 3);
00070
00071 if (cl->afterEncBufSize < (int)maxCompSize) {
00072 cl->afterEncBufSize = maxCompSize;
00073 if (cl->afterEncBuf == NULL)
00074 cl->afterEncBuf = (char *)malloc(cl->afterEncBufSize);
00075 else
00076 cl->afterEncBuf = (char *)realloc(cl->afterEncBuf, cl->afterEncBufSize);
00077 }
00078
00079
00080
00081
00082 (*cl->translateFn)(cl->translateLookupTable, &cl->screen->serverFormat,
00083 &cl->format, fbptr, cl->beforeEncBuf,
00084 cl->scaledScreen->paddedWidthInBytes, w, h);
00085
00086 if ( cl->compStreamInitedLZO == FALSE ) {
00087 cl->compStreamInitedLZO = TRUE;
00088
00089
00090
00091 cl->lzoWrkMem = malloc(sizeof(lzo_align_t) * (((LZO1X_1_MEM_COMPRESS) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t)));
00092 }
00093
00094
00095 deflateResult = lzo1x_1_compress((unsigned char *)cl->beforeEncBuf, (lzo_uint)(w * h * (cl->format.bitsPerPixel / 8)), (unsigned char *)cl->afterEncBuf, &maxCompSize, cl->lzoWrkMem);
00096
00097
00098
00099 cl->afterEncBufLen = maxCompSize;
00100
00101 if ( deflateResult != LZO_E_OK ) {
00102 rfbErr("lzo deflation error: %d\n", deflateResult);
00103 return FALSE;
00104 }
00105
00106
00107 rfbStatRecordEncodingSent(cl, rfbEncodingUltra, sz_rfbFramebufferUpdateRectHeader + sz_rfbZlibHeader + cl->afterEncBufLen, maxRawSize);
00108
00109 if (cl->ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbZlibHeader
00110 > UPDATE_BUF_SIZE)
00111 {
00112 if (!rfbSendUpdateBuf(cl))
00113 return FALSE;
00114 }
00115
00116 rect.r.x = Swap16IfLE(x);
00117 rect.r.y = Swap16IfLE(y);
00118 rect.r.w = Swap16IfLE(w);
00119 rect.r.h = Swap16IfLE(h);
00120 rect.encoding = Swap32IfLE(rfbEncodingUltra);
00121
00122 memcpy(&cl->updateBuf[cl->ublen], (char *)&rect,
00123 sz_rfbFramebufferUpdateRectHeader);
00124 cl->ublen += sz_rfbFramebufferUpdateRectHeader;
00125
00126 hdr.nBytes = Swap32IfLE(cl->afterEncBufLen);
00127
00128 memcpy(&cl->updateBuf[cl->ublen], (char *)&hdr, sz_rfbZlibHeader);
00129 cl->ublen += sz_rfbZlibHeader;
00130
00131
00132 for (i = 0; i < cl->afterEncBufLen;) {
00133
00134 int bytesToCopy = UPDATE_BUF_SIZE - cl->ublen;
00135
00136 if (i + bytesToCopy > cl->afterEncBufLen) {
00137 bytesToCopy = cl->afterEncBufLen - i;
00138 }
00139
00140 memcpy(&cl->updateBuf[cl->ublen], &cl->afterEncBuf[i], bytesToCopy);
00141
00142 cl->ublen += bytesToCopy;
00143 i += bytesToCopy;
00144
00145 if (cl->ublen == UPDATE_BUF_SIZE) {
00146 if (!rfbSendUpdateBuf(cl))
00147 return FALSE;
00148 }
00149 }
00150
00151 return TRUE;
00152
00153 }
00154
00155
00156
00157
00158
00159
00160 rfbBool
00161 rfbSendRectEncodingUltra(rfbClientPtr cl,
00162 int x,
00163 int y,
00164 int w,
00165 int h)
00166 {
00167 int maxLines;
00168 int linesRemaining;
00169 rfbRectangle partialRect;
00170
00171 partialRect.x = x;
00172 partialRect.y = y;
00173 partialRect.w = w;
00174 partialRect.h = h;
00175
00176
00177 maxLines = ( ULTRA_MAX_SIZE(w) / w );
00178
00179
00180 linesRemaining = h;
00181
00182
00183 while ( linesRemaining > 0 ) {
00184
00185 int linesToComp;
00186
00187 if ( maxLines < linesRemaining )
00188 linesToComp = maxLines;
00189 else
00190 linesToComp = linesRemaining;
00191
00192 partialRect.h = linesToComp;
00193
00194
00195 if ( ! rfbSendOneRectEncodingUltra( cl,
00196 partialRect.x,
00197 partialRect.y,
00198 partialRect.w,
00199 partialRect.h )) {
00200
00201 return FALSE;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 if (( cl->ublen > 0 ) &&
00216 ( linesToComp == maxLines )) {
00217 if (!rfbSendUpdateBuf(cl)) {
00218
00219 return FALSE;
00220 }
00221 }
00222
00223
00224 linesRemaining -= linesToComp;
00225 partialRect.y += linesToComp;
00226
00227 }
00228
00229 return TRUE;
00230
00231 }