jscript: Added Object function invocation implementation.
[wine] / dlls / winex11.drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32
33   /* GCs used for B&W and color bitmap operations */
34 static GC bitmap_gc[32];
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 };  /* phys bitmap for the default stock bitmap */
36
37 static XContext bitmap_context;  /* X context to associate a phys bitmap to a handle */
38
39 GC get_bitmap_gc(int depth)
40 {
41     if(depth < 1 || depth > 32)
42         return 0;
43
44     return bitmap_gc[depth-1];
45 }
46
47 /***********************************************************************
48  *           X11DRV_BITMAP_Init
49  */
50 void X11DRV_BITMAP_Init(void)
51 {
52     int depth_count, index, i;
53     int *depth_list;
54     Pixmap tmpPixmap;
55
56     wine_tsx11_lock();
57     bitmap_context = XUniqueContext();
58     BITMAP_stock_phys_bitmap.pixmap_depth = 1;
59     BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
60     bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
61     XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
62     XSetSubwindowMode( gdi_display, bitmap_gc[0], IncludeInferiors );
63
64     /* Create a GC for all available depths. GCs at depths other than 1-bit/screen_depth are for use
65      * in combination with XRender which allows us to create dibsections at more depths.
66      */
67     depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68     for (i = 0; i < depth_count; i++)
69     {
70         index = depth_list[i] - 1;
71         if (bitmap_gc[index]) continue;
72         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, depth_list[i])))
73         {
74             if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
75             {
76                 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77                 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
78             }
79             XFreePixmap( gdi_display, tmpPixmap );
80         }
81     }
82     XFree( depth_list );
83
84     wine_tsx11_unlock();
85 }
86
87 /***********************************************************************
88  *           SelectBitmap   (X11DRV.@)
89  */
90 HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
91 {
92     X_PHYSBITMAP *physBitmap;
93     BITMAP bitmap;
94
95     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
96
97     if(physDev->xrender)
98         X11DRV_XRender_UpdateDrawable( physDev );
99
100     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
101     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
102
103     physDev->bitmap = physBitmap;
104     physDev->drawable = physBitmap->pixmap;
105     SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
106     physDev->dc_rect = physDev->drawable_rect;
107
108       /* Change GC depth if needed */
109
110     if (physDev->depth != physBitmap->pixmap_depth)
111     {
112         physDev->depth = physBitmap->pixmap_depth;
113         wine_tsx11_lock();
114         XFreeGC( gdi_display, physDev->gc );
115         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
116         XSetGraphicsExposures( gdi_display, physDev->gc, False );
117         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
118         XFlush( gdi_display );
119         wine_tsx11_unlock();
120     }
121
122     if(physDev->depth == 1)
123         physDev->color_shifts = NULL;
124     else
125         physDev->color_shifts = &physBitmap->pixmap_color_shifts;
126
127     return hbitmap;
128 }
129
130
131 /****************************************************************************
132  *        CreateBitmap   (X11DRV.@)
133  *
134  * Create a device dependent X11 bitmap
135  *
136  * Returns TRUE on success else FALSE
137  */
138 BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
139 {
140     X_PHYSBITMAP *physBitmap;
141     BITMAP bitmap;
142
143     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
144
145       /* Check parameters */
146     if (bitmap.bmPlanes != 1) return FALSE;
147
148     /* check if bpp is compatible with screen depth */
149     if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
150     {
151         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
152             bitmap.bmPlanes, bitmap.bmBitsPixel);
153         return FALSE;
154     }
155     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
156     {
157         ERR( "called for stock bitmap, please report\n" );
158         return FALSE;
159     }
160
161     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
162
163     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
164
165       /* Create the pixmap */
166     wine_tsx11_lock();
167     if(bitmap.bmBitsPixel == 1)
168     {
169         physBitmap->pixmap_depth = 1;
170     }
171     else
172     {
173         physBitmap->pixmap_depth = screen_depth;
174         physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
175     }
176     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
177                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
178     wine_tsx11_unlock();
179     if (!physBitmap->pixmap)
180     {
181         WARN("Can't create Pixmap\n");
182         HeapFree( GetProcessHeap(), 0, physBitmap );
183         return FALSE;
184     }
185
186     if (bmBits) /* Set bitmap bits */
187     {
188         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
189     }
190     else  /* else clear the bitmap */
191     {
192         GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
193         wine_tsx11_lock();
194         XSetFunction( gdi_display, gc, GXclear );
195         XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
196                         bitmap.bmWidth, bitmap.bmHeight );
197         XSetFunction( gdi_display, gc, GXcopy );
198         wine_tsx11_unlock();
199     }
200     return TRUE;
201 }
202
203
204 /***********************************************************************
205  *           GetBitmapBits   (X11DRV.@)
206  *
207  * RETURNS
208  *    Success: Number of bytes copied
209  *    Failure: 0
210  */
211 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
212 {
213     BITMAP bitmap;
214     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
215     LONG height;
216     XImage *image;
217     LPBYTE tbuf, startline;
218     int h, w;
219
220     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
221
222     TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
223
224     wine_tsx11_lock();
225     height = count / bitmap.bmWidthBytes;
226     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
227                        bitmap.bmWidth, height, AllPlanes, ZPixmap );
228
229     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
230
231     startline = buffer;
232     switch (bitmap.bmBitsPixel)
233     {
234     case 1:
235         for (h=0;h<height;h++)
236         {
237             tbuf = startline;
238             *tbuf = 0;
239             for (w=0;w<bitmap.bmWidth;w++)
240             {
241                 if ((w%8) == 0)
242                     *tbuf = 0;
243                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
244                 if ((w&7) == 7) ++tbuf;
245             }
246             startline += bitmap.bmWidthBytes;
247         }
248         break;
249     case 4:
250         for (h=0;h<height;h++)
251         {
252             tbuf = startline;
253             for (w=0;w<bitmap.bmWidth;w++)
254             {
255                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
256                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
257             }
258             startline += bitmap.bmWidthBytes;
259         }
260         break;
261     case 8:
262         for (h=0;h<height;h++)
263         {
264             tbuf = startline;
265             for (w=0;w<bitmap.bmWidth;w++)
266                 *tbuf++ = XGetPixel(image,w,h);
267             startline += bitmap.bmWidthBytes;
268         }
269         break;
270     case 15:
271     case 16:
272         for (h=0;h<height;h++)
273         {
274             tbuf = startline;
275             for (w=0;w<bitmap.bmWidth;w++)
276             {
277                 long pixel = XGetPixel(image,w,h);
278
279                 *tbuf++ = pixel & 0xff;
280                 *tbuf++ = (pixel>>8) & 0xff;
281             }
282             startline += bitmap.bmWidthBytes;
283         }
284         break;
285     case 24:
286         for (h=0;h<height;h++)
287         {
288             tbuf = startline;
289             for (w=0;w<bitmap.bmWidth;w++)
290             {
291                 long pixel = XGetPixel(image,w,h);
292
293                 *tbuf++ = pixel & 0xff;
294                 *tbuf++ = (pixel>> 8) & 0xff;
295                 *tbuf++ = (pixel>>16) & 0xff;
296             }
297             startline += bitmap.bmWidthBytes;
298         }
299         break;
300
301     case 32:
302         for (h=0;h<height;h++)
303         {
304             tbuf = startline;
305             for (w=0;w<bitmap.bmWidth;w++)
306             {
307                 long pixel = XGetPixel(image,w,h);
308
309                 *tbuf++ = pixel & 0xff;
310                 *tbuf++ = (pixel>> 8) & 0xff;
311                 *tbuf++ = (pixel>>16) & 0xff;
312                 *tbuf++ = (pixel>>24) & 0xff;
313             }
314             startline += bitmap.bmWidthBytes;
315         }
316         break;
317     default:
318         FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
319     }
320     XDestroyImage( image );
321     wine_tsx11_unlock();
322     return count;
323 }
324
325
326
327 /******************************************************************************
328  *             SetBitmapBits   (X11DRV.@)
329  *
330  * RETURNS
331  *    Success: Number of bytes used in setting the bitmap bits
332  *    Failure: 0
333  */
334 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
335 {
336     BITMAP bitmap;
337     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
338     LONG height;
339     XImage *image;
340     const BYTE *sbuf, *startline;
341     int w, h;
342
343     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
344
345     TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
346
347     height = count / bitmap.bmWidthBytes;
348
349     wine_tsx11_lock();
350     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
351                           bitmap.bmWidth, height, 32, 0 );
352     if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
353     {
354         WARN("No memory to create image data.\n");
355         XDestroyImage( image );
356         wine_tsx11_unlock();
357         return 0;
358     }
359
360     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
361
362     startline = bits;
363
364     switch (bitmap.bmBitsPixel)
365     {
366     case 1:
367         for (h=0;h<height;h++)
368         {
369             sbuf = startline;
370             for (w=0;w<bitmap.bmWidth;w++)
371             {
372                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
373                 if ((w&7) == 7)
374                     sbuf++;
375             }
376             startline += bitmap.bmWidthBytes;
377         }
378         break;
379     case 4:
380         for (h=0;h<height;h++)
381         {
382             sbuf = startline;
383             for (w=0;w<bitmap.bmWidth;w++)
384             {
385                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
386                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
387             }
388             startline += bitmap.bmWidthBytes;
389         }
390         break;
391     case 8:
392         for (h=0;h<height;h++)
393         {
394             sbuf = startline;
395             for (w=0;w<bitmap.bmWidth;w++)
396                 XPutPixel(image,w,h,*sbuf++);
397             startline += bitmap.bmWidthBytes;
398         }
399         break;
400     case 15:
401     case 16:
402         for (h=0;h<height;h++)
403         {
404             sbuf = startline;
405             for (w=0;w<bitmap.bmWidth;w++)
406             {
407                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
408                 sbuf+=2;
409             }
410             startline += bitmap.bmWidthBytes;
411         }
412         break;
413     case 24:
414         for (h=0;h<height;h++)
415         {
416             sbuf = startline;
417             for (w=0;w<bitmap.bmWidth;w++)
418             {
419                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
420                 sbuf += 3;
421             }
422             startline += bitmap.bmWidthBytes;
423         }
424         break;
425     case 32:
426         for (h=0;h<height;h++)
427         {
428             sbuf = startline;
429             for (w=0;w<bitmap.bmWidth;w++)
430             {
431                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
432                 sbuf += 4;
433             }
434             startline += bitmap.bmWidthBytes;
435         }
436         break;
437     default:
438       FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
439
440     }
441     XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
442                image, 0, 0, 0, 0, bitmap.bmWidth, height );
443     HeapFree( GetProcessHeap(), 0, image->data );
444     image->data = NULL;
445     XDestroyImage( image );
446     wine_tsx11_unlock();
447     return count;
448 }
449
450 /***********************************************************************
451  *           DeleteBitmap   (X11DRV.@)
452  */
453 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
454 {
455     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
456
457     if (physBitmap)
458     {
459         DIBSECTION dib;
460
461         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
462             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
463
464         if (physBitmap->glxpixmap)
465             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
466         wine_tsx11_lock();
467         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
468         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
469         wine_tsx11_unlock();
470         HeapFree( GetProcessHeap(), 0, physBitmap );
471     }
472     return TRUE;
473 }
474
475
476 /***********************************************************************
477  *           X11DRV_get_phys_bitmap
478  *
479  * Retrieve the X physical bitmap info.
480  */
481 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
482 {
483     X_PHYSBITMAP *ret;
484
485     wine_tsx11_lock();
486     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
487     wine_tsx11_unlock();
488     return ret;
489 }
490
491
492 /***********************************************************************
493  *           X11DRV_init_phys_bitmap
494  *
495  * Initialize the X physical bitmap info.
496  */
497 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
498 {
499     X_PHYSBITMAP *ret;
500
501     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
502     {
503         ret->hbitmap = hbitmap;
504         wine_tsx11_lock();
505         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
506         wine_tsx11_unlock();
507     }
508     return ret;
509 }
510
511
512 /***********************************************************************
513  *           X11DRV_get_pixmap
514  *
515  * Retrieve the pixmap associated to a bitmap.
516  */
517 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
518 {
519     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
520
521     if (!physBitmap) return 0;
522     return physBitmap->pixmap;
523 }