winex11: Make sure that all glX functions are called under the X11 lock.
[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 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
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 /***********************************************************************
40  *           X11DRV_BITMAP_Init
41  */
42 void X11DRV_BITMAP_Init(void)
43 {
44     Pixmap tmpPixmap;
45
46       /* Create the necessary GCs */
47
48     wine_tsx11_lock();
49     bitmap_context = XUniqueContext();
50     BITMAP_stock_phys_bitmap.pixmap_depth = 1;
51     BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
52     BITMAP_monoGC = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
53     XSetGraphicsExposures( gdi_display, BITMAP_monoGC, False );
54     XSetSubwindowMode( gdi_display, BITMAP_monoGC, IncludeInferiors );
55
56     if (screen_depth != 1)
57     {
58         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, screen_depth )))
59         {
60             BITMAP_colorGC = XCreateGC( gdi_display, tmpPixmap, 0, NULL );
61             XSetGraphicsExposures( gdi_display, BITMAP_colorGC, False );
62             XSetSubwindowMode( gdi_display, BITMAP_colorGC, IncludeInferiors );
63             XFreePixmap( gdi_display, tmpPixmap );
64         }
65     }
66     wine_tsx11_unlock();
67 }
68
69 /***********************************************************************
70  *           SelectBitmap   (X11DRV.@)
71  */
72 HBITMAP X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
73 {
74     X_PHYSBITMAP *physBitmap;
75     BITMAP bitmap;
76
77     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
78
79     if(physDev->xrender)
80         X11DRV_XRender_UpdateDrawable( physDev );
81
82     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
83     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
84
85     physDev->bitmap = physBitmap;
86     physDev->drawable = physBitmap->pixmap;
87     SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
88     physDev->dc_rect = physDev->drawable_rect;
89
90       /* Change GC depth if needed */
91
92     if (physDev->depth != physBitmap->pixmap_depth)
93     {
94         physDev->depth = physBitmap->pixmap_depth;
95         wine_tsx11_lock();
96         XFreeGC( gdi_display, physDev->gc );
97         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
98         XSetGraphicsExposures( gdi_display, physDev->gc, False );
99         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
100         XFlush( gdi_display );
101         wine_tsx11_unlock();
102     }
103     return hbitmap;
104 }
105
106
107 /****************************************************************************
108  *        CreateBitmap   (X11DRV.@)
109  *
110  * Create a device dependent X11 bitmap
111  *
112  * Returns TRUE on success else FALSE
113  */
114 BOOL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
115 {
116     X_PHYSBITMAP *physBitmap;
117     BITMAP bitmap;
118
119     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
120
121       /* Check parameters */
122     if (bitmap.bmPlanes != 1) return FALSE;
123
124     /* check if bpp is compatible with screen depth */
125     if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
126     {
127         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
128             bitmap.bmPlanes, bitmap.bmBitsPixel);
129         return FALSE;
130     }
131     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
132     {
133         ERR( "called for stock bitmap, please report\n" );
134         return FALSE;
135     }
136
137     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
138
139     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
140
141       /* Create the pixmap */
142     wine_tsx11_lock();
143     physBitmap->pixmap_depth = (bitmap.bmBitsPixel == 1) ? 1 : screen_depth;
144     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
145                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
146     wine_tsx11_unlock();
147     if (!physBitmap->pixmap)
148     {
149         WARN("Can't create Pixmap\n");
150         HeapFree( GetProcessHeap(), 0, physBitmap );
151         return FALSE;
152     }
153
154     if (bmBits) /* Set bitmap bits */
155     {
156         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
157     }
158     else  /* else clear the bitmap */
159     {
160         wine_tsx11_lock();
161         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXclear );
162         XFillRectangle( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap), 0, 0,
163                         bitmap.bmWidth, bitmap.bmHeight );
164         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXcopy );
165         wine_tsx11_unlock();
166     }
167     return TRUE;
168 }
169
170
171 /***********************************************************************
172  *           GetBitmapBits   (X11DRV.@)
173  *
174  * RETURNS
175  *    Success: Number of bytes copied
176  *    Failure: 0
177  */
178 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
179 {
180     BITMAP bitmap;
181     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
182     LONG height;
183     XImage *image;
184     LPBYTE tbuf, startline;
185     int h, w;
186
187     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
188
189     TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
190
191     wine_tsx11_lock();
192     height = count / bitmap.bmWidthBytes;
193     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
194                        bitmap.bmWidth, height, AllPlanes, ZPixmap );
195
196     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
197
198     startline = buffer;
199     switch (bitmap.bmBitsPixel)
200     {
201     case 1:
202         for (h=0;h<height;h++)
203         {
204             tbuf = startline;
205             *tbuf = 0;
206             for (w=0;w<bitmap.bmWidth;w++)
207             {
208                 if ((w%8) == 0)
209                     *tbuf = 0;
210                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
211                 if ((w&7) == 7) ++tbuf;
212             }
213             startline += bitmap.bmWidthBytes;
214         }
215         break;
216     case 4:
217         for (h=0;h<height;h++)
218         {
219             tbuf = startline;
220             for (w=0;w<bitmap.bmWidth;w++)
221             {
222                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
223                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
224             }
225             startline += bitmap.bmWidthBytes;
226         }
227         break;
228     case 8:
229         for (h=0;h<height;h++)
230         {
231             tbuf = startline;
232             for (w=0;w<bitmap.bmWidth;w++)
233                 *tbuf++ = XGetPixel(image,w,h);
234             startline += bitmap.bmWidthBytes;
235         }
236         break;
237     case 15:
238     case 16:
239         for (h=0;h<height;h++)
240         {
241             tbuf = startline;
242             for (w=0;w<bitmap.bmWidth;w++)
243             {
244                 long pixel = XGetPixel(image,w,h);
245
246                 *tbuf++ = pixel & 0xff;
247                 *tbuf++ = (pixel>>8) & 0xff;
248             }
249             startline += bitmap.bmWidthBytes;
250         }
251         break;
252     case 24:
253         for (h=0;h<height;h++)
254         {
255             tbuf = startline;
256             for (w=0;w<bitmap.bmWidth;w++)
257             {
258                 long pixel = XGetPixel(image,w,h);
259
260                 *tbuf++ = pixel & 0xff;
261                 *tbuf++ = (pixel>> 8) & 0xff;
262                 *tbuf++ = (pixel>>16) & 0xff;
263             }
264             startline += bitmap.bmWidthBytes;
265         }
266         break;
267
268     case 32:
269         for (h=0;h<height;h++)
270         {
271             tbuf = startline;
272             for (w=0;w<bitmap.bmWidth;w++)
273             {
274                 long pixel = XGetPixel(image,w,h);
275
276                 *tbuf++ = pixel & 0xff;
277                 *tbuf++ = (pixel>> 8) & 0xff;
278                 *tbuf++ = (pixel>>16) & 0xff;
279                 *tbuf++ = (pixel>>24) & 0xff;
280             }
281             startline += bitmap.bmWidthBytes;
282         }
283         break;
284     default:
285         FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
286     }
287     XDestroyImage( image );
288     wine_tsx11_unlock();
289     return count;
290 }
291
292
293
294 /******************************************************************************
295  *             SetBitmapBits   (X11DRV.@)
296  *
297  * RETURNS
298  *    Success: Number of bytes used in setting the bitmap bits
299  *    Failure: 0
300  */
301 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
302 {
303     BITMAP bitmap;
304     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
305     LONG height;
306     XImage *image;
307     const BYTE *sbuf, *startline;
308     int w, h;
309
310     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
311
312     TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
313
314     height = count / bitmap.bmWidthBytes;
315
316     wine_tsx11_lock();
317     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
318                           bitmap.bmWidth, height, 32, 0 );
319     if (!(image->data = malloc(image->bytes_per_line * height)))
320     {
321         WARN("No memory to create image data.\n");
322         XDestroyImage( image );
323         wine_tsx11_unlock();
324         return 0;
325     }
326
327     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
328
329     startline = bits;
330
331     switch (bitmap.bmBitsPixel)
332     {
333     case 1:
334         for (h=0;h<height;h++)
335         {
336             sbuf = startline;
337             for (w=0;w<bitmap.bmWidth;w++)
338             {
339                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
340                 if ((w&7) == 7)
341                     sbuf++;
342             }
343             startline += bitmap.bmWidthBytes;
344         }
345         break;
346     case 4:
347         for (h=0;h<height;h++)
348         {
349             sbuf = startline;
350             for (w=0;w<bitmap.bmWidth;w++)
351             {
352                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
353                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
354             }
355             startline += bitmap.bmWidthBytes;
356         }
357         break;
358     case 8:
359         for (h=0;h<height;h++)
360         {
361             sbuf = startline;
362             for (w=0;w<bitmap.bmWidth;w++)
363                 XPutPixel(image,w,h,*sbuf++);
364             startline += bitmap.bmWidthBytes;
365         }
366         break;
367     case 15:
368     case 16:
369         for (h=0;h<height;h++)
370         {
371             sbuf = startline;
372             for (w=0;w<bitmap.bmWidth;w++)
373             {
374                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
375                 sbuf+=2;
376             }
377             startline += bitmap.bmWidthBytes;
378         }
379         break;
380     case 24:
381         for (h=0;h<height;h++)
382         {
383             sbuf = startline;
384             for (w=0;w<bitmap.bmWidth;w++)
385             {
386                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
387                 sbuf += 3;
388             }
389             startline += bitmap.bmWidthBytes;
390         }
391         break;
392     case 32:
393         for (h=0;h<height;h++)
394         {
395             sbuf = startline;
396             for (w=0;w<bitmap.bmWidth;w++)
397             {
398                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
399                 sbuf += 4;
400             }
401             startline += bitmap.bmWidthBytes;
402         }
403         break;
404     default:
405       FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
406
407     }
408     XPutImage( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap),
409                image, 0, 0, 0, 0, bitmap.bmWidth, height );
410     XDestroyImage( image ); /* frees image->data too */
411     wine_tsx11_unlock();
412     return count;
413 }
414
415 /***********************************************************************
416  *           DeleteBitmap   (X11DRV.@)
417  */
418 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
419 {
420     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
421
422     if (physBitmap)
423     {
424         DIBSECTION dib;
425
426         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
427             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
428
429         if (physBitmap->glxpixmap)
430             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
431         wine_tsx11_lock();
432         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
433         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
434         wine_tsx11_unlock();
435         HeapFree( GetProcessHeap(), 0, physBitmap );
436     }
437     return TRUE;
438 }
439
440
441 /***********************************************************************
442  *           X11DRV_get_phys_bitmap
443  *
444  * Retrieve the X physical bitmap info.
445  */
446 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
447 {
448     X_PHYSBITMAP *ret;
449
450     wine_tsx11_lock();
451     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
452     wine_tsx11_unlock();
453     return ret;
454 }
455
456
457 /***********************************************************************
458  *           X11DRV_init_phys_bitmap
459  *
460  * Initialize the X physical bitmap info.
461  */
462 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
463 {
464     X_PHYSBITMAP *ret;
465
466     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
467     {
468         ret->hbitmap = hbitmap;
469         wine_tsx11_lock();
470         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
471         wine_tsx11_unlock();
472     }
473     return ret;
474 }
475
476
477 /***********************************************************************
478  *           X11DRV_get_pixmap
479  *
480  * Retrieve the pixmap associated to a bitmap.
481  */
482 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
483 {
484     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
485
486     if (!physBitmap) return 0;
487     return physBitmap->pixmap;
488 }