]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/hw/vnc/zrle.cc
Support RDP5 logon packets.
[rdpsrv] / Xserver / programs / Xserver / hw / vnc / zrle.cc
1 //
2 // Copyright (C) 2002 RealVNC Ltd.  All Rights Reserved.
3 //
4 // This is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This software is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this software; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
17 // USA.
18 //
19
20 //
21 // zrle.cc
22 //
23 // Routines to implement Zlib Run-length Encoding (ZRLE).
24 //
25
26 #include <stdio.h>
27 extern "C" {
28 #include "rfb.h"
29 }
30 #include <rdr/MemOutStream.h>
31 #include <rdr/ZlibOutStream.h>
32
33
34 #define GET_IMAGE_INTO_BUF(tx,ty,tw,th,buf)                                \
35   char *fbptr = (rfbScreen.pfbMemory + (rfbScreen.paddedWidthInBytes * ty) \
36                  + (tx * (rfbScreen.bitsPerPixel / 8)));                   \
37                                                                            \
38   (*cl->translateFn)(cl->translateLookupTable, &rfbServerFormat,           \
39                      &cl->format, fbptr, (char*)buf,                       \
40                      rfbScreen.paddedWidthInBytes, tw, th);
41
42 #define EXTRA_ARGS , rfbClientPtr cl
43
44 #define BPP 8
45 #include <rfb/zrleEncode.h>
46 #undef BPP
47 #define BPP 16
48 #include <rfb/zrleEncode.h>
49 #undef BPP
50 #define BPP 32
51 #include <rfb/zrleEncode.h>
52 #define CPIXEL 24A
53 #include <rfb/zrleEncode.h>
54 #undef CPIXEL
55 #define CPIXEL 24B
56 #include <rfb/zrleEncode.h>
57 #undef CPIXEL
58 #undef BPP
59
60
61 /*
62  * zrleBeforeBuf contains pixel data in the client's format.  It must be at
63  * least one pixel bigger than the largest tile of pixel data, since the
64  * ZRLE encoding algorithm writes to the position one past the end of the pixel
65  * data.
66  */
67
68 static char zrleBeforeBuf[rfbZRLETileWidth * rfbZRLETileHeight * 4 + 4];
69
70 static rdr::MemOutStream mos;
71
72
73 /*
74  * rfbSendRectEncodingZRLE - send a given rectangle using ZRLE encoding.
75  */
76
77
78 Bool rfbSendRectEncodingZRLE(rfbClientPtr cl, int x, int y, int w, int h)
79 {
80   if (!cl->zrleData) cl->zrleData = new rdr::ZlibOutStream;
81   rdr::ZlibOutStream* zos = (rdr::ZlibOutStream*)cl->zrleData;
82   mos.clear();
83
84   switch (cl->format.bitsPerPixel) {
85
86   case 8:
87     zrleEncode8( x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
88     break;
89
90   case 16:
91     zrleEncode16(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
92     break;
93
94   case 32:
95     bool fitsInLS3Bytes
96       = ((cl->format.redMax   << cl->format.redShift)   < (1<<24) &&
97          (cl->format.greenMax << cl->format.greenShift) < (1<<24) &&
98          (cl->format.blueMax  << cl->format.blueShift)  < (1<<24));
99
100     bool fitsInMS3Bytes = (cl->format.redShift   > 7  &&
101                            cl->format.greenShift > 7  &&
102                            cl->format.blueShift  > 7);
103
104     if ((fitsInLS3Bytes && !cl->format.bigEndian) ||
105         (fitsInMS3Bytes && cl->format.bigEndian))
106     {
107       zrleEncode24A(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
108     }
109     else if ((fitsInLS3Bytes && cl->format.bigEndian) ||
110              (fitsInMS3Bytes && !cl->format.bigEndian))
111     {
112       zrleEncode24B(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
113     }
114     else
115     {
116       zrleEncode32(x, y, w, h, &mos, zos, zrleBeforeBuf, cl);
117     }
118     break;
119   }
120
121   cl->rfbRectanglesSent[rfbEncodingZRLE]++;
122   cl->rfbBytesSent[rfbEncodingZRLE] += (sz_rfbFramebufferUpdateRectHeader
123                                         + sz_rfbZRLEHeader + mos.length());
124
125   if (ublen + sz_rfbFramebufferUpdateRectHeader + sz_rfbZRLEHeader
126       > UPDATE_BUF_SIZE)
127     {
128       if (!rfbSendUpdateBuf(cl))
129         return FALSE;
130     }
131
132   rfbFramebufferUpdateRectHeader rect;
133   rect.r.x = Swap16IfLE(x);
134   rect.r.y = Swap16IfLE(y);
135   rect.r.w = Swap16IfLE(w);
136   rect.r.h = Swap16IfLE(h);
137   rect.encoding = Swap32IfLE(rfbEncodingZRLE);
138
139   memcpy(&updateBuf[ublen], (char *)&rect,
140          sz_rfbFramebufferUpdateRectHeader);
141   ublen += sz_rfbFramebufferUpdateRectHeader;
142
143   rfbZRLEHeader hdr;
144
145   hdr.length = Swap32IfLE(mos.length());
146
147   memcpy(&updateBuf[ublen], (char *)&hdr, sz_rfbZRLEHeader);
148   ublen += sz_rfbZRLEHeader;
149
150   // copy into updateBuf and send from there.  Maybe should send directly?
151
152   for (int i = 0; i < mos.length();) {
153
154     int bytesToCopy = UPDATE_BUF_SIZE - ublen;
155
156     if (i + bytesToCopy > mos.length()) {
157       bytesToCopy = mos.length() - i;
158     }
159
160     memcpy(&updateBuf[ublen], (CARD8*)mos.data() + i, bytesToCopy);
161
162     ublen += bytesToCopy;
163     i += bytesToCopy;
164
165     if (ublen == UPDATE_BUF_SIZE) {
166       if (!rfbSendUpdateBuf(cl))
167         return FALSE;
168     }
169   }
170
171   return TRUE;
172 }
173
174
175 void FreeZrleData(rfbClientPtr cl)
176 {
177   delete (rdr::ZlibOutStream*)cl->zrleData;
178 }
179