PageRenderTime 51ms CodeModel.GetById 4ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/desktop/libvncserver/scale.c

http://ftk.googlecode.com/
C | 416 lines | 276 code | 51 blank | 89 comment | 45 complexity | b7b719406c97869bca3a22e4b718a7c5 MD5 | raw file
  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 CORBA
 58#include <vncserverctrl.h>
 59#endif
 60
 61#ifdef DEBUGPROTO
 62#undef DEBUGPROTO
 63#define DEBUGPROTO(x) x
 64#else
 65#define DEBUGPROTO(x)
 66#endif
 67
 68/****************************/
 69#define CEIL(x)  ( (double) ((int) (x)) == (x) ? \
 70        (double) ((int) (x)) : (double) ((int) (x) + 1) )
 71#define FLOOR(x) ( (double) ((int) (x)) )
 72
 73
 74int ScaleX(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int x)
 75{
 76    if ((from==to) || (from==NULL) || (to==NULL)) return x;
 77    return ((int)(((double) x / (double)from->width) * (double)to->width ));
 78}
 79
 80int ScaleY(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int y)
 81{
 82    if ((from==to) || (from==NULL) || (to==NULL)) return y;
 83    return ((int)(((double) y / (double)from->height) * (double)to->height ));
 84}
 85
 86/* So, all of the encodings point to the ->screen->frameBuffer,
 87 * We need to change this!
 88 */
 89void rfbScaledCorrection(rfbScreenInfoPtr from, rfbScreenInfoPtr to, int *x, int *y, int *w, int *h, char *function)
 90{
 91    double x1,y1,w1,h1, x2, y2, w2, h2;
 92    double scaleW = ((double) to->width) / ((double) from->width);
 93    double scaleH = ((double) to->height) / ((double) from->height);
 94
 95
 96    /*
 97     * rfbLog("rfbScaledCorrection(%p -> %p, %dx%d->%dx%d (%dXx%dY-%dWx%dH)\n",
 98     * from, to, from->width, from->height, to->width, to->height, *x, *y, *w, *h);
 99     */
100
101    /* If it's the original framebuffer... */
102    if (from==to) return;
103
104    x1 = ((double) *x) * scaleW;
105    y1 = ((double) *y) * scaleH;
106    w1 = ((double) *w) * scaleW;
107    h1 = ((double) *h) * scaleH;
108
109
110    /*cast from double to int is same as "*x = floor(x1);" */
111    x2 = FLOOR(x1);
112    y2 = FLOOR(y1);
113
114    /* include into W and H the jitter of scaling X and Y */
115    w2 = CEIL(w1 + ( x1 - x2 ));
116    h2 = CEIL(h1 + ( y1 - y2 ));
117
118    /*
119     * rfbLog("%s (%dXx%dY-%dWx%dH  ->  %fXx%fY-%fWx%fH) {%dWx%dH -> %dWx%dH}\n",
120     *    function, *x, *y, *w, *h, x2, y2, w2, h2,
121     *    from->width, from->height, to->width, to->height);
122     */
123
124    /* simulate ceil() without math library */
125    *x = (int)x2;
126    *y = (int)y2;
127    *w = (int)w2;
128    *h = (int)h2;
129
130    /* Small changes for a thumbnail may be scaled to zero */
131    if (*w==0) (*w)++;
132    if (*h==0) (*h)++;
133    /* scaling from small to big may overstep the size a bit */
134    if (*x+*w > to->width)  *w=to->width - *x;
135    if (*y+*h > to->height) *h=to->height - *y;
136}
137
138void rfbScaledScreenUpdateRect(rfbScreenInfoPtr screen, rfbScreenInfoPtr ptr, int x0, int y0, int w0, int h0)
139{
140    int x,y,w,v,z;
141    int x1, y1, w1, h1;
142    int bitsPerPixel, bytesPerPixel, bytesPerLine, areaX, areaY, area2;
143    unsigned char *srcptr, *dstptr;
144
145    /* Nothing to do!!! */
146    if (screen==ptr) return;
147
148    x1 = x0;
149    y1 = y0;
150    w1 = w0;
151    h1 = h0;
152
153    rfbScaledCorrection(screen, ptr, &x1, &y1, &w1, &h1, "rfbScaledScreenUpdateRect");
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) + v) * bytesPerPixel) +
201                               (w * 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/****************************/