1 /*
2  * scale.c - deal with server-side scaling.
3  */
4 
5 /*
6  *  Copyright (C) 2005 Rohit Kumar, Johannes E. Schindelin
7  *  Copyright (C) 2002 RealVNC Ltd.
8  *  OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
9  *  Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.
10  *  All Rights Reserved.
11  *
12  *  This is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This software is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this software; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
25  *  USA.
26  */
27 
28 #ifdef __STRICT_ANSI__
29 #define _BSD_SOURCE
30 #endif
31 #include <string.h>
32 #include <rfb/rfb.h>
33 #include <rfb/rfbregion.h>
34 #include "private.h"
35 
36 #ifdef LIBVNCSERVER_HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 
40 #ifdef WIN32
41 #define write(sock,buf,len) send(sock,buf,len,0)
42 #else
43 #ifdef LIBVNCSERVER_HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #include <pwd.h>
47 #ifdef LIBVNCSERVER_HAVE_SYS_SOCKET_H
48 #include <sys/socket.h>
49 #endif
50 #ifdef LIBVNCSERVER_HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #include <netinet/tcp.h>
53 #include <arpa/inet.h>
54 #endif
55 #endif
56 
57 #ifdef DEBUGPROTO
58 #undef DEBUGPROTO
59 #define DEBUGPROTO(x) x
60 #else
61 #define DEBUGPROTO(x)
62 #endif
63 
64 /****************************/
65 #define CEIL(x)  ( (double) ((int) (x)) == (x) ? \
66         (double) ((int) (x)) : (double) ((int) (x) + 1) )
67 #define FLOOR(x) ( (double) ((int) (x)) )
68 
pad4(int value)69 static inline int pad4(int value)
70 {
71     int remainder = value & 3;
72     if (!remainder) return value;
73     return value + 4 - remainder;
74 }
75 
ScaleX(rfbScreenInfoPtr from,rfbScreenInfoPtr to,int x)76 int ScaleX(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int x)
77 {
78     if ((from==to) || (from==NULL) || (to==NULL)) return x;
79     return ((int)(((double) x / (double)from->width) * (double)to->width ));
80 }
81 
ScaleY(rfbScreenInfoPtr from,rfbScreenInfoPtr to,int y)82 int ScaleY(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int y)
83 {
84     if ((from==to) || (from==NULL) || (to==NULL)) return y;
85     return ((int)(((double) y / (double)from->height) * (double)to->height ));
86 }
87 
88 /* So, all of the encodings point to the ->screen->frameBuffer,
89  * We need to change this!
90  */
rfbScaledCorrection(rfbScreenInfoPtr from,rfbScreenInfoPtr to,int * x,int * y,int * w,int * h,const char * function)91 void rfbScaledCorrection(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int *x, int *y, int *w, int *h, const char *function)
92 {
93     double x1,y1,w1,h1, x2, y2, w2, h2;
94     double scaleW = ((double) to->width) / ((double) from->width);
95     double scaleH = ((double) to->height) / ((double) from->height);
96 
97 
98     /*
99      * rfbLog("rfbScaledCorrection(%p -> %p, %dx%d->%dx%d (%dXx%dY-%dWx%dH)\n",
100      * from, to, from->width, from->height, to->width, to->height, *x, *y, *w, *h);
101      */
102 
103     /* If it's the original framebuffer... */
104     if (from==to) return;
105 
106     x1 = ((double) *x) * scaleW;
107     y1 = ((double) *y) * scaleH;
108     w1 = ((double) *w) * scaleW;
109     h1 = ((double) *h) * scaleH;
110 
111 
112     /*cast from double to int is same as "*x = floor(x1);" */
113     x2 = FLOOR(x1);
114     y2 = FLOOR(y1);
115 
116     /* include into W and H the jitter of scaling X and Y */
117     w2 = CEIL(w1 + ( x1 - x2 ));
118     h2 = CEIL(h1 + ( y1 - y2 ));
119 
120     /*
121      * rfbLog("%s (%dXx%dY-%dWx%dH  ->  %fXx%fY-%fWx%fH) {%dWx%dH -> %dWx%dH}\n",
122      *    function, *x, *y, *w, *h, x2, y2, w2, h2,
123      *    from->width, from->height, to->width, to->height);
124      */
125 
126     /* simulate ceil() without math library */
127     *x = (int)x2;
128     *y = (int)y2;
129     *w = (int)w2;
130     *h = (int)h2;
131 
132     /* Small changes for a thumbnail may be scaled to zero */
133     if (*w==0) (*w)++;
134     if (*h==0) (*h)++;
135     /* scaling from small to big may overstep the size a bit */
136     if (*x+*w > to->width)  *w=to->width - *x;
137     if (*y+*h > to->height) *h=to->height - *y;
138 }
139 
rfbScaledScreenUpdateRect(rfbScreenInfoPtr screen,rfbScreenInfoPtr ptr,int x0,int y0,int w0,int h0)140 void rfbScaledScreenUpdateRect(rfbScreenInfoPtr screen, rfbScreenInfoPtr ptr, int x0, int y0, int w0, int h0)
141 {
142     int x,y,w,v,z;
143     int x1, y1, w1, h1;
144     int bitsPerPixel, bytesPerPixel, bytesPerLine, areaX, areaY, area2;
145     unsigned char *srcptr, *dstptr;
146 
147     /* Nothing to do!!! */
148     if (screen==ptr) return;
149 
150     x1 = x0;
151     y1 = y0;
152     w1 = w0;
153     h1 = h0;
154 
155     rfbScaledCorrection(screen, ptr, &x1, &y1, &w1, &h1, "rfbScaledScreenUpdateRect");
156     x0 = ScaleX(ptr, screen, x1);
157     y0 = ScaleY(ptr, screen, y1);
158     w0 = ScaleX(ptr, screen, w1);
159     h0 = ScaleY(ptr, screen, h1);
160 
161     bitsPerPixel = screen->bitsPerPixel;
162     bytesPerPixel = bitsPerPixel / 8;
163     bytesPerLine = w1 * bytesPerPixel;
164     srcptr = (unsigned char *)(screen->frameBuffer +
165      (y0 * screen->paddedWidthInBytes + x0 * bytesPerPixel));
166     dstptr = (unsigned char *)(ptr->frameBuffer +
167      ( y1 * ptr->paddedWidthInBytes + x1 * bytesPerPixel));
168     /* The area of the source framebuffer for each destination pixel */
169     areaX = ScaleX(ptr,screen,1);
170     areaY = ScaleY(ptr,screen,1);
171     area2 = areaX*areaY;
172 
173 
174     /* Ensure that we do not go out of bounds */
175     if ((x1+w1) > (ptr->width))
176     {
177       if (x1==0) w1=ptr->width; else x1 = ptr->width - w1;
178     }
179     if ((y1+h1) > (ptr->height))
180     {
181       if (y1==0) h1=ptr->height; else y1 = ptr->height - h1;
182     }
183     /*
184      * rfbLog("rfbScaledScreenUpdateRect(%dXx%dY-%dWx%dH  ->  %dXx%dY-%dWx%dH <%dx%d>) {%dWx%dH -> %dWx%dH} 0x%p\n",
185      *    x0, y0, w0, h0, x1, y1, w1, h1, areaX, areaY,
186      *    screen->width, screen->height, ptr->width, ptr->height, ptr->frameBuffer);
187      */
188 
189     if (screen->serverFormat.trueColour) { /* Blend neighbouring pixels together */
190       unsigned char *srcptr2;
191       unsigned long pixel_value, red, green, blue;
192       unsigned int redShift = screen->serverFormat.redShift;
193       unsigned int greenShift = screen->serverFormat.greenShift;
194       unsigned int blueShift = screen->serverFormat.blueShift;
195       unsigned long redMax = screen->serverFormat.redMax;
196       unsigned long greenMax = screen->serverFormat.greenMax;
197       unsigned long blueMax = screen->serverFormat.blueMax;
198 
199      /* for each *destination* pixel... */
200      for (y = 0; y < h1; y++) {
201        for (x = 0; x < w1; x++) {
202          red = green = blue = 0;
203          /* Get the totals for rgb from the source grid... */
204          for (w = 0; w < areaX; w++) {
205            for (v = 0; v < areaY; v++) {
206              srcptr2 = &srcptr[(((x * areaX) + w) * bytesPerPixel) +
207                                (v * screen->paddedWidthInBytes)];
208              pixel_value = 0;
209 
210 
211              switch (bytesPerPixel) {
212              case 4: pixel_value = *((unsigned int *)srcptr2);   break;
213              case 2: pixel_value = *((unsigned short *)srcptr2); break;
214              case 1: pixel_value = *((unsigned char *)srcptr2);  break;
215              default:
216                /* fixme: endianess problem? */
217                for (z = 0; z < bytesPerPixel; z++)
218                  pixel_value += (srcptr2[z] << (8 * z));
219                 break;
220               }
221               /*
222               srcptr2 += bytesPerPixel;
223               */
224 
225             red += ((pixel_value >> redShift) & redMax);
226             green += ((pixel_value >> greenShift) & greenMax);
227             blue += ((pixel_value >> blueShift) & blueMax);
228 
229            }
230          }
231          /* We now have a total for all of the colors, find the average! */
232          red /= area2;
233          green /= area2;
234          blue /= area2;
235           /* Stuff the new value back into memory */
236          pixel_value = ((red & redMax) << redShift) | ((green & greenMax) << greenShift) | ((blue & blueMax) << blueShift);
237 
238          switch (bytesPerPixel) {
239          case 4: *((unsigned int *)dstptr)   = (unsigned int)   pixel_value; break;
240          case 2: *((unsigned short *)dstptr) = (unsigned short) pixel_value; break;
241          case 1: *((unsigned char *)dstptr)  = (unsigned char)  pixel_value; break;
242          default:
243            /* fixme: endianess problem? */
244            for (z = 0; z < bytesPerPixel; z++)
245              dstptr[z]=(pixel_value >> (8 * z)) & 0xff;
246             break;
247           }
248           dstptr += bytesPerPixel;
249        }
250        srcptr += (screen->paddedWidthInBytes * areaY);
251        dstptr += (ptr->paddedWidthInBytes - bytesPerLine);
252      }
253    } else
254    { /* Not truecolour, so we can't blend. Just use the top-left pixel instead */
255      for (y = y1; y < (y1+h1); y++) {
256        for (x = x1; x < (x1+w1); x++)
257          memcpy (&ptr->frameBuffer[(y *ptr->paddedWidthInBytes) + (x * bytesPerPixel)],
258                  &screen->frameBuffer[(y * areaY * screen->paddedWidthInBytes) + (x *areaX * bytesPerPixel)], bytesPerPixel);
259      }
260   }
261 }
262 
rfbScaledScreenUpdate(rfbScreenInfoPtr screen,int x1,int y1,int x2,int y2)263 void rfbScaledScreenUpdate(rfbScreenInfoPtr screen, int x1, int y1, int x2, int y2)
264 {
265     /* ok, now the task is to update each and every scaled version of the framebuffer
266      * and we only have to do this for this specific changed rectangle!
267      */
268     rfbScreenInfoPtr ptr;
269     int count=0;
270 
271     /* We don't point to cl->screen as it is the original */
272     for (ptr=screen->scaledScreenNext;ptr!=NULL;ptr=ptr->scaledScreenNext)
273     {
274         /* Only update if it has active clients... */
275         if (ptr->scaledScreenRefCount>0)
276         {
277           rfbScaledScreenUpdateRect(screen, ptr, x1, y1, x2-x1, y2-y1);
278           count++;
279         }
280     }
281 }
282 
283 /* Create a new scaled version of the framebuffer */
rfbScaledScreenAllocate(rfbClientPtr cl,int width,int height)284 rfbScreenInfoPtr rfbScaledScreenAllocate(rfbClientPtr cl, int width, int height)
285 {
286     rfbScreenInfoPtr ptr;
287     ptr = malloc(sizeof(rfbScreenInfo));
288     if (ptr!=NULL)
289     {
290         int allocSize;
291 
292         /* copy *everything* (we don't use most of it, but just in case) */
293         memcpy(ptr, cl->screen, sizeof(rfbScreenInfo));
294 
295         /* SECURITY: make sure that no integer overflow will occur afterwards.
296          * Note: this is defensive coding, as the check should have already been
297          * performed during initial, non-scaled screen setup.
298          */
299         allocSize = pad4(width * (ptr->bitsPerPixel/8)); /* per protocol, width<2**16 and bpp<256 */
300         if (height == 0 || allocSize >= SIZE_MAX / height)
301         {
302           free(ptr);
303           return NULL; /* malloc() will allocate an incorrect buffer size - early abort */
304         }
305 
306         /* Resume copy everything */
307         ptr->width = width;
308         ptr->height = height;
309         ptr->paddedWidthInBytes = (ptr->bitsPerPixel/8)*ptr->width;
310 
311         /* Need to by multiples of 4 for Sparc systems */
312         ptr->paddedWidthInBytes = pad4(ptr->paddedWidthInBytes);
313 
314         /* Reset the reference count to 0! */
315         ptr->scaledScreenRefCount = 0;
316 
317         ptr->sizeInBytes = ptr->paddedWidthInBytes * ptr->height;
318         ptr->serverFormat = cl->screen->serverFormat;
319 
320         ptr->frameBuffer = malloc(ptr->sizeInBytes);
321         if (ptr->frameBuffer!=NULL)
322         {
323             /* Reset to a known condition: scale the entire framebuffer */
324             rfbScaledScreenUpdateRect(cl->screen, ptr, 0, 0, cl->screen->width, cl->screen->height);
325             /* Now, insert into the chain */
326             LOCK(cl->updateMutex);
327             ptr->scaledScreenNext = cl->screen->scaledScreenNext;
328             cl->screen->scaledScreenNext = ptr;
329             UNLOCK(cl->updateMutex);
330         }
331         else
332         {
333             /* Failed to malloc the new frameBuffer, cleanup */
334             free(ptr);
335             ptr=NULL;
336         }
337     }
338     return ptr;
339 }
340 
341 /* Find an active scaled version of the framebuffer
342  * TODO: implement a refcount per scaled screen to prevent
343  * unreferenced scaled screens from hanging around
344  */
rfbScalingFind(rfbClientPtr cl,int width,int height)345 rfbScreenInfoPtr rfbScalingFind(rfbClientPtr cl, int width, int height)
346 {
347     rfbScreenInfoPtr ptr;
348     /* include the original in the search (ie: fine 1:1 scaled version of the frameBuffer) */
349     for (ptr=cl->screen; ptr!=NULL; ptr=ptr->scaledScreenNext)
350     {
351         if ((ptr->width==width) && (ptr->height==height))
352             return ptr;
353     }
354     return NULL;
355 }
356 
357 /* Future needs "scale to 320x240, as that's the client's screen size */
rfbScalingSetup(rfbClientPtr cl,int width,int height)358 void rfbScalingSetup(rfbClientPtr cl, int width, int height)
359 {
360     rfbScreenInfoPtr ptr;
361 
362     ptr = rfbScalingFind(cl,width,height);
363     if (ptr==NULL)
364         ptr = rfbScaledScreenAllocate(cl,width,height);
365     /* Now, there is a new screen available (if ptr is not NULL) */
366     if (ptr!=NULL)
367     {
368         /* Update it! */
369         if (ptr->scaledScreenRefCount<1)
370             rfbScaledScreenUpdateRect(cl->screen, ptr, 0, 0, cl->screen->width, cl->screen->height);
371         /*
372          * rfbLog("Taking one from %dx%d-%d and adding it to %dx%d-%d\n",
373          *    cl->scaledScreen->width, cl->scaledScreen->height,
374          *    cl->scaledScreen->scaledScreenRefCount,
375          *    ptr->width, ptr->height, ptr->scaledScreenRefCount);
376          */
377 
378         LOCK(cl->updateMutex);
379         cl->scaledScreen->scaledScreenRefCount--;
380         ptr->scaledScreenRefCount++;
381         cl->scaledScreen=ptr;
382         cl->newFBSizePending = TRUE;
383         UNLOCK(cl->updateMutex);
384 
385         rfbLog("Scaling to %dx%d (refcount=%d)\n",width,height,ptr->scaledScreenRefCount);
386     }
387     else
388         rfbLog("Scaling to %dx%d failed, leaving things alone\n",width,height);
389 }
390 
rfbSendNewScaleSize(rfbClientPtr cl)391 int rfbSendNewScaleSize(rfbClientPtr cl)
392 {
393     /* if the client supports newFBsize Encoding, use it */
394     if (cl->useNewFBSize && cl->newFBSizePending)
395 	return FALSE;
396 
397     LOCK(cl->updateMutex);
398     cl->newFBSizePending = FALSE;
399     UNLOCK(cl->updateMutex);
400 
401     if (cl->PalmVNC==TRUE)
402     {
403         rfbPalmVNCReSizeFrameBufferMsg pmsg;
404         pmsg.type = rfbPalmVNCReSizeFrameBuffer;
405         pmsg.pad1 = 0;
406         pmsg.desktop_w = Swap16IfLE(cl->screen->width);
407         pmsg.desktop_h = Swap16IfLE(cl->screen->height);
408         pmsg.buffer_w  = Swap16IfLE(cl->scaledScreen->width);
409         pmsg.buffer_h  = Swap16IfLE(cl->scaledScreen->height);
410         pmsg.pad2 = 0;
411 
412         rfbLog("Sending a response to a PalmVNC style frameuffer resize event (%dx%d)\n", cl->scaledScreen->width, cl->scaledScreen->height);
413         if (rfbWriteExact(cl, (char *)&pmsg, sz_rfbPalmVNCReSizeFrameBufferMsg) < 0) {
414             rfbLogPerror("rfbNewClient: write");
415             rfbCloseClient(cl);
416             return FALSE;
417         }
418     }
419     else
420     {
421         rfbResizeFrameBufferMsg        rmsg;
422         rmsg.type = rfbResizeFrameBuffer;
423         rmsg.pad1=0;
424         rmsg.framebufferWidth  = Swap16IfLE(cl->scaledScreen->width);
425         rmsg.framebufferHeigth = Swap16IfLE(cl->scaledScreen->height);
426         rfbLog("Sending a response to a UltraVNC style frameuffer resize event (%dx%d)\n", cl->scaledScreen->width, cl->scaledScreen->height);
427         if (rfbWriteExact(cl, (char *)&rmsg, sz_rfbResizeFrameBufferMsg) < 0) {
428             rfbLogPerror("rfbNewClient: write");
429             rfbCloseClient(cl);
430             return FALSE;
431         }
432     }
433     return TRUE;
434 }
435 /****************************/
436