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
69
70int ScaleX(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int x)
71{
72    if ((from==to) || (from==NULL) || (to==NULL)) return x;
73    return ((int)(((double) x / (double)from->width) * (double)to->width ));
74}
75
76int ScaleY(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int y)
77{
78    if ((from==to) || (from==NULL) || (to==NULL)) return y;
79    return ((int)(((double) y / (double)from->height) * (double)to->height ));
80}
81
82/* So, all of the encodings point to the ->screen->frameBuffer,
83 * We need to change this!
84 */
85void rfbScaledCorrection(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int *x, int *y, int *w, int *h, const char *function)
86{
87    double x1,y1,w1,h1, x2, y2, w2, h2;
88    double scaleW = ((double) to->width) / ((double) from->width);
89    double scaleH = ((double) to->height) / ((double) from->height);
90
91
92    /*
93     * rfbLog("rfbScaledCorrection(%p -> %p, %dx%d->%dx%d (%dXx%dY-%dWx%dH)\n",
94     * from, to, from->width, from->height, to->width, to->height, *x, *y, *w, *h);
95     */
96
97    /* If it's the original framebuffer... */
98    if (from==to) return;
99
100    x1 = ((double) *x) * scaleW;
101    y1 = ((double) *y) * scaleH;
102    w1 = ((double) *w) * scaleW;
103    h1 = ((double) *h) * scaleH;
104
105
106    /*cast from double to int is same as "*x = floor(x1);" */
107    x2 = FLOOR(x1);
108    y2 = FLOOR(y1);
109
110    /* include into W and H the jitter of scaling X and Y */
111    w2 = CEIL(w1 + ( x1 - x2 ));
112    h2 = CEIL(h1 + ( y1 - y2 ));
113
114    /*
115     * rfbLog("%s (%dXx%dY-%dWx%dH  ->  %fXx%fY-%fWx%fH) {%dWx%dH -> %dWx%dH}\n",
116     *    function, *x, *y, *w, *h, x2, y2, w2, h2,
117     *    from->width, from->height, to->width, to->height);
118     */
119
120    /* simulate ceil() without math library */
121    *x = (int)x2;
122    *y = (int)y2;
123    *w = (int)w2;
124    *h = (int)h2;
125
126    /* Small changes for a thumbnail may be scaled to zero */
127    if (*w==0) (*w)++;
128    if (*h==0) (*h)++;
129    /* scaling from small to big may overstep the size a bit */
130    if (*x+*w > to->width)  *w=to->width - *x;
131    if (*y+*h > to->height) *h=to->height - *y;
132}
133
134void rfbScaledScreenUpdateRect(rfbScreenInfoPtr screen, rfbScreenInfoPtr ptr, int x0, int y0, int w0, int h0)
135{
136    int x,y,w,v,z;
137    int x1, y1, w1, h1;
138    int bitsPerPixel, bytesPerPixel, bytesPerLine, areaX, areaY, area2;
139    unsigned char *srcptr, *dstptr;
140
141    /* Nothing to do!!! */
142    if (screen==ptr) return;
143
144    x1 = x0;
145    y1 = y0;
146    w1 = w0;
147    h1 = h0;
148
149    rfbScaledCorrection(screen, ptr, &x1, &y1, &w1, &h1, "rfbScaledScreenUpdateRect");
150    x0 = ScaleX(ptr, screen, x1);
151    y0 = ScaleY(ptr, screen, y1);
152    w0 = ScaleX(ptr, screen, w1);
153    h0 = ScaleY(ptr, screen, h1);
154
155    bitsPerPixel = screen->bitsPerPixel;
156    bytesPerPixel = bitsPerPixel / 8;
157    bytesPerLine = w1 * bytesPerPixel;
158    srcptr = (unsigned char *)(screen->frameBuffer +
159     (y0 * screen->paddedWidthInBytes + x0 * bytesPerPixel));
160    dstptr = (unsigned char *)(ptr->frameBuffer +
161     ( y1 * ptr->paddedWidthInBytes + x1 * bytesPerPixel));
162    /* The area of the source framebuffer for each destination pixel */
163    areaX = ScaleX(ptr,screen,1);
164    areaY = ScaleY(ptr,screen,1);
165    area2 = areaX*areaY;
166
167
168    /* Ensure that we do not go out of bounds */
169    if ((x1+w1) > (ptr->width))
170    {
171      if (x1==0) w1=ptr->width; else x1 = ptr->width - w1;
172    }
173    if ((y1+h1) > (ptr->height))
174    {
175      if (y1==0) h1=ptr->height; else y1 = ptr->height - h1;
176    }
177    /*
178     * rfbLog("rfbScaledScreenUpdateRect(%dXx%dY-%dWx%dH  ->  %dXx%dY-%dWx%dH <%dx%d>) {%dWx%dH -> %dWx%dH} 0x%p\n",
179     *    x0, y0, w0, h0, x1, y1, w1, h1, areaX, areaY,
180     *    screen->width, screen->height, ptr->width, ptr->height, ptr->frameBuffer);
181     */
182
183    if (screen->serverFormat.trueColour) { /* Blend neighbouring pixels together */
184      unsigned char *srcptr2;
185      unsigned long pixel_value, red, green, blue;
186      unsigned int redShift = screen->serverFormat.redShift;
187      unsigned int greenShift = screen->serverFormat.greenShift;
188      unsigned int blueShift = screen->serverFormat.blueShift;
189      unsigned long redMax = screen->serverFormat.redMax;
190      unsigned long greenMax = screen->serverFormat.greenMax;
191      unsigned long blueMax = screen->serverFormat.blueMax;
192
193     /* for each *destination* pixel... */
194     for (y = 0; y < h1; y++) {
195       for (x = 0; x < w1; x++) {
196         red = green = blue = 0;
197         /* Get the totals for rgb from the source grid... */
198         for (w = 0; w < areaX; w++) {
199           for (v = 0; v < areaY; v++) {
200             srcptr2 = &srcptr[(((x * areaX) + w) * bytesPerPixel) +
201                               (v * screen->paddedWidthInBytes)];
202             pixel_value = 0;
203
204
205             switch (bytesPerPixel) {
206             case 4: pixel_value = *((unsigned int *)srcptr2);   break;
207             case 2: pixel_value = *((unsigned short *)srcptr2); break;
208             case 1: pixel_value = *((unsigned char *)srcptr2);  break;
209             default:
210               /* fixme: endianess problem? */
211               for (z = 0; z < bytesPerPixel; z++)
212                 pixel_value += (srcptr2[z] << (8 * z));
213                break;
214              }
215              /*
216              srcptr2 += bytesPerPixel;
217              */
218
219            red += ((pixel_value >> redShift) & redMax);
220            green += ((pixel_value >> greenShift) & greenMax);
221            blue += ((pixel_value >> blueShift) & blueMax);
222
223           }
224         }
225         /* We now have a total for all of the colors, find the average! */
226         red /= area2;
227         green /= area2;
228         blue /= area2;
229          /* Stuff the new value back into memory */
230         pixel_value = ((red & redMax) << redShift) | ((green & greenMax) << greenShift) | ((blue & blueMax) << blueShift);
231
232         switch (bytesPerPixel) {
233         case 4: *((unsigned int *)dstptr)   = (unsigned int)   pixel_value; break;
234         case 2: *((unsigned short *)dstptr) = (unsigned short) pixel_value; break;
235         case 1: *((unsigned char *)dstptr)  = (unsigned char)  pixel_value; break;
236         default:
237           /* fixme: endianess problem? */
238           for (z = 0; z < bytesPerPixel; z++)
239             dstptr[z]=(pixel_value >> (8 * z)) & 0xff;
240            break;
241          }
242          dstptr += bytesPerPixel;
243       }
244       srcptr += (screen->paddedWidthInBytes * areaY);
245       dstptr += (ptr->paddedWidthInBytes - bytesPerLine);
246     }
247   } else
248   { /* Not truecolour, so we can't blend. Just use the top-left pixel instead */
249     for (y = y1; y < (y1+h1); y++) {
250       for (x = x1; x < (x1+w1); x++)
251         memcpy (&ptr->frameBuffer[(y *ptr->paddedWidthInBytes) + (x * bytesPerPixel)],
252                 &screen->frameBuffer[(y * areaY * screen->paddedWidthInBytes) + (x *areaX * bytesPerPixel)], bytesPerPixel);
253     }
254  }
255}
256
257void rfbScaledScreenUpdate(rfbScreenInfoPtr screen, int x1, int y1, int x2, int y2)
258{
259    /* ok, now the task is to update each and every scaled version of the framebuffer
260     * and we only have to do this for this specific changed rectangle!
261     */
262    rfbScreenInfoPtr ptr;
263    int count=0;
264
265    /* We don't point to cl->screen as it is the original */
266    for (ptr=screen->scaledScreenNext;ptr!=NULL;ptr=ptr->scaledScreenNext)
267    {
268        /* Only update if it has active clients... */
269        if (ptr->scaledScreenRefCount>0)
270        {
271          rfbScaledScreenUpdateRect(screen, ptr, x1, y1, x2-x1, y2-y1);
272          count++;
273        }
274    }
275}
276
277/* Create a new scaled version of the framebuffer */
278rfbScreenInfoPtr rfbScaledScreenAllocate(rfbClientPtr cl, int width, int height)
279{
280    rfbScreenInfoPtr ptr;
281    ptr = malloc(sizeof(rfbScreenInfo));
282    if (ptr!=NULL)
283    {
284        /* copy *everything* (we don't use most of it, but just in case) */
285        memcpy(ptr, cl->screen, sizeof(rfbScreenInfo));
286        ptr->width = width;
287        ptr->height = height;
288        ptr->paddedWidthInBytes = (ptr->bitsPerPixel/8)*ptr->width;
289
290        /* Need to by multiples of 4 for Sparc systems */
291        ptr->paddedWidthInBytes += (ptr->paddedWidthInBytes % 4);
292
293        /* Reset the reference count to 0! */
294        ptr->scaledScreenRefCount = 0;
295
296        ptr->sizeInBytes = ptr->paddedWidthInBytes * ptr->height;
297        ptr->serverFormat = cl->screen->serverFormat;
298
299        ptr->frameBuffer = malloc(ptr->sizeInBytes);
300        if (ptr->frameBuffer!=NULL)
301        {
302            /* Reset to a known condition: scale the entire framebuffer */
303            rfbScaledScreenUpdateRect(cl->screen, ptr, 0, 0, cl->screen->width, cl->screen->height);
304            /* Now, insert into the chain */
305            LOCK(cl->updateMutex);
306            ptr->scaledScreenNext = cl->screen->scaledScreenNext;
307            cl->screen->scaledScreenNext = ptr;
308            UNLOCK(cl->updateMutex);
309        }
310        else
311        {
312            /* Failed to malloc the new frameBuffer, cleanup */
313            free(ptr);
314            ptr=NULL;
315        }
316    }
317    return ptr;
318}
319
320/* Find an active scaled version of the framebuffer
321 * TODO: implement a refcount per scaled screen to prevent
322 * unreferenced scaled screens from hanging around
323 */
324rfbScreenInfoPtr rfbScalingFind(rfbClientPtr cl, int width, int height)
325{
326    rfbScreenInfoPtr ptr;
327    /* include the original in the search (ie: fine 1:1 scaled version of the frameBuffer) */
328    for (ptr=cl->screen; ptr!=NULL; ptr=ptr->scaledScreenNext)
329    {
330        if ((ptr->width==width) && (ptr->height==height))
331            return ptr;
332    }
333    return NULL;
334}
335
336/* Future needs "scale to 320x240, as that's the client's screen size */
337void rfbScalingSetup(rfbClientPtr cl, int width, int height)
338{
339    rfbScreenInfoPtr ptr;
340
341    ptr = rfbScalingFind(cl,width,height);
342    if (ptr==NULL)
343        ptr = rfbScaledScreenAllocate(cl,width,height);
344    /* Now, there is a new screen available (if ptr is not NULL) */
345    if (ptr!=NULL)
346    {
347        /* Update it! */
348        if (ptr->scaledScreenRefCount<1)
349            rfbScaledScreenUpdateRect(cl->screen, ptr, 0, 0, cl->screen->width, cl->screen->height);
350        /*
351         * rfbLog("Taking one from %dx%d-%d and adding it to %dx%d-%d\n",
352         *    cl->scaledScreen->width, cl->scaledScreen->height,
353         *    cl->scaledScreen->scaledScreenRefCount,
354         *    ptr->width, ptr->height, ptr->scaledScreenRefCount);
355         */
356
357        LOCK(cl->updateMutex);
358        cl->scaledScreen->scaledScreenRefCount--;
359        ptr->scaledScreenRefCount++;
360        cl->scaledScreen=ptr;
361        cl->newFBSizePending = TRUE;
362        UNLOCK(cl->updateMutex);
363
364        rfbLog("Scaling to %dx%d (refcount=%d)\n",width,height,ptr->scaledScreenRefCount);
365    }
366    else
367        rfbLog("Scaling to %dx%d failed, leaving things alone\n",width,height);
368}
369
370int rfbSendNewScaleSize(rfbClientPtr cl)
371{
372    /* if the client supports newFBsize Encoding, use it */
373    if (cl->useNewFBSize && cl->newFBSizePending)
374	return FALSE;
375
376    LOCK(cl->updateMutex);
377    cl->newFBSizePending = FALSE;
378    UNLOCK(cl->updateMutex);
379
380    if (cl->PalmVNC==TRUE)
381    {
382        rfbPalmVNCReSizeFrameBufferMsg pmsg;
383        pmsg.type = rfbPalmVNCReSizeFrameBuffer;
384        pmsg.pad1 = 0;
385        pmsg.desktop_w = Swap16IfLE(cl->screen->width);
386        pmsg.desktop_h = Swap16IfLE(cl->screen->height);
387        pmsg.buffer_w  = Swap16IfLE(cl->scaledScreen->width);
388        pmsg.buffer_h  = Swap16IfLE(cl->scaledScreen->height);
389        pmsg.pad2 = 0;
390
391        rfbLog("Sending a response to a PalmVNC style frameuffer resize event (%dx%d)\n", cl->scaledScreen->width, cl->scaledScreen->height);
392        if (rfbWriteExact(cl, (char *)&pmsg, sz_rfbPalmVNCReSizeFrameBufferMsg) < 0) {
393            rfbLogPerror("rfbNewClient: write");
394            rfbCloseClient(cl);
395            rfbClientConnectionGone(cl);
396            return FALSE;
397        }
398    }
399    else
400    {
401        rfbResizeFrameBufferMsg        rmsg;
402        rmsg.type = rfbResizeFrameBuffer;
403        rmsg.pad1=0;
404        rmsg.framebufferWidth  = Swap16IfLE(cl->scaledScreen->width);
405        rmsg.framebufferHeigth = Swap16IfLE(cl->scaledScreen->height);
406        rfbLog("Sending a response to a UltraVNC style frameuffer resize event (%dx%d)\n", cl->scaledScreen->width, cl->scaledScreen->height);
407        if (rfbWriteExact(cl, (char *)&rmsg, sz_rfbResizeFrameBufferMsg) < 0) {
408            rfbLogPerror("rfbNewClient: write");
409            rfbCloseClient(cl);
410            rfbClientConnectionGone(cl);
411            return FALSE;
412        }
413    }
414    return TRUE;
415}
416/****************************/
417