wined3d: Filter out some shader compilation spam.
[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) ||
126           (bitmap.bmBitsPixel == screen_bpp) ||
127           (bitmap.bmBitsPixel == 16 && screen_bpp == 15)))    /* TODO: Confirm this    */
128     {
129         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
130             bitmap.bmPlanes, bitmap.bmBitsPixel);
131         return FALSE;
132     }
133     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
134     {
135         ERR( "called for stock bitmap, please report\n" );
136         return FALSE;
137     }
138
139     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
140
141     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
142
143       /* Create the pixmap */
144     wine_tsx11_lock();
145     physBitmap->pixmap_depth = (bitmap.bmBitsPixel == 1) ? 1 : screen_depth;
146     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
147                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
148     wine_tsx11_unlock();
149     if (!physBitmap->pixmap)
150     {
151         WARN("Can't create Pixmap\n");
152         HeapFree( GetProcessHeap(), 0, physBitmap );
153         return FALSE;
154     }
155
156     if (bmBits) /* Set bitmap bits */
157     {
158         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
159     }
160     else  /* else clear the bitmap */
161     {
162         wine_tsx11_lock();
163         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXclear );
164         XFillRectangle( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap), 0, 0,
165                         bitmap.bmWidth, bitmap.bmHeight );
166         XSetFunction( gdi_display, BITMAP_GC(physBitmap), GXcopy );
167         wine_tsx11_unlock();
168     }
169     return TRUE;
170 }
171
172
173 /***********************************************************************
174  *           GetBitmapBits   (X11DRV.@)
175  *
176  * RETURNS
177  *    Success: Number of bytes copied
178  *    Failure: 0
179  */
180 LONG X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
181 {
182     BITMAP bitmap;
183     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
184     LONG height;
185     XImage *image;
186     LPBYTE tbuf, startline;
187     int h, w;
188
189     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
190
191     TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
192
193     wine_tsx11_lock();
194     height = count / bitmap.bmWidthBytes;
195     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
196                        bitmap.bmWidth, height, AllPlanes, ZPixmap );
197
198     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
199
200     startline = buffer;
201     switch (bitmap.bmBitsPixel)
202     {
203     case 1:
204         for (h=0;h<height;h++)
205         {
206             tbuf = startline;
207             *tbuf = 0;
208             for (w=0;w<bitmap.bmWidth;w++)
209             {
210                 if ((w%8) == 0)
211                     *tbuf = 0;
212                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
213                 if ((w&7) == 7) ++tbuf;
214             }
215             startline += bitmap.bmWidthBytes;
216         }
217         break;
218     case 4:
219         for (h=0;h<height;h++)
220         {
221             tbuf = startline;
222             for (w=0;w<bitmap.bmWidth;w++)
223             {
224                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
225                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
226             }
227             startline += bitmap.bmWidthBytes;
228         }
229         break;
230     case 8:
231         for (h=0;h<height;h++)
232         {
233             tbuf = startline;
234             for (w=0;w<bitmap.bmWidth;w++)
235                 *tbuf++ = XGetPixel(image,w,h);
236             startline += bitmap.bmWidthBytes;
237         }
238         break;
239     case 15:
240     case 16:
241         for (h=0;h<height;h++)
242         {
243             tbuf = startline;
244             for (w=0;w<bitmap.bmWidth;w++)
245             {
246                 long pixel = XGetPixel(image,w,h);
247
248                 *tbuf++ = pixel & 0xff;
249                 *tbuf++ = (pixel>>8) & 0xff;
250             }
251             startline += bitmap.bmWidthBytes;
252         }
253         break;
254     case 24:
255         for (h=0;h<height;h++)
256         {
257             tbuf = startline;
258             for (w=0;w<bitmap.bmWidth;w++)
259             {
260                 long pixel = XGetPixel(image,w,h);
261
262                 *tbuf++ = pixel & 0xff;
263                 *tbuf++ = (pixel>> 8) & 0xff;
264                 *tbuf++ = (pixel>>16) & 0xff;
265             }
266             startline += bitmap.bmWidthBytes;
267         }
268         break;
269
270     case 32:
271         for (h=0;h<height;h++)
272         {
273             tbuf = startline;
274             for (w=0;w<bitmap.bmWidth;w++)
275             {
276                 long pixel = XGetPixel(image,w,h);
277
278                 *tbuf++ = pixel & 0xff;
279                 *tbuf++ = (pixel>> 8) & 0xff;
280                 *tbuf++ = (pixel>>16) & 0xff;
281                 *tbuf++ = (pixel>>24) & 0xff;
282             }
283             startline += bitmap.bmWidthBytes;
284         }
285         break;
286     default:
287         FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
288     }
289     XDestroyImage( image );
290     wine_tsx11_unlock();
291     return count;
292 }
293
294
295
296 /******************************************************************************
297  *             SetBitmapBits   (X11DRV.@)
298  *
299  * RETURNS
300  *    Success: Number of bytes used in setting the bitmap bits
301  *    Failure: 0
302  */
303 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
304 {
305     BITMAP bitmap;
306     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
307     LONG height;
308     XImage *image;
309     const BYTE *sbuf, *startline;
310     int w, h;
311
312     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
313
314     TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
315
316     height = count / bitmap.bmWidthBytes;
317
318     wine_tsx11_lock();
319     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
320                           bitmap.bmWidth, height, 32, 0 );
321     if (!(image->data = malloc(image->bytes_per_line * height)))
322     {
323         WARN("No memory to create image data.\n");
324         XDestroyImage( image );
325         wine_tsx11_unlock();
326         return 0;
327     }
328
329     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
330
331     startline = bits;
332
333     switch (bitmap.bmBitsPixel)
334     {
335     case 1:
336         for (h=0;h<height;h++)
337         {
338             sbuf = startline;
339             for (w=0;w<bitmap.bmWidth;w++)
340             {
341                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
342                 if ((w&7) == 7)
343                     sbuf++;
344             }
345             startline += bitmap.bmWidthBytes;
346         }
347         break;
348     case 4:
349         for (h=0;h<height;h++)
350         {
351             sbuf = startline;
352             for (w=0;w<bitmap.bmWidth;w++)
353             {
354                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
355                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
356             }
357             startline += bitmap.bmWidthBytes;
358         }
359         break;
360     case 8:
361         for (h=0;h<height;h++)
362         {
363             sbuf = startline;
364             for (w=0;w<bitmap.bmWidth;w++)
365                 XPutPixel(image,w,h,*sbuf++);
366             startline += bitmap.bmWidthBytes;
367         }
368         break;
369     case 15:
370     case 16:
371         for (h=0;h<height;h++)
372         {
373             sbuf = startline;
374             for (w=0;w<bitmap.bmWidth;w++)
375             {
376                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
377                 sbuf+=2;
378             }
379             startline += bitmap.bmWidthBytes;
380         }
381         break;
382     case 24:
383         for (h=0;h<height;h++)
384         {
385             sbuf = startline;
386             for (w=0;w<bitmap.bmWidth;w++)
387             {
388                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
389                 sbuf += 3;
390             }
391             startline += bitmap.bmWidthBytes;
392         }
393         break;
394     case 32:
395         for (h=0;h<height;h++)
396         {
397             sbuf = startline;
398             for (w=0;w<bitmap.bmWidth;w++)
399             {
400                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
401                 sbuf += 4;
402             }
403             startline += bitmap.bmWidthBytes;
404         }
405         break;
406     default:
407       FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
408
409     }
410     XPutImage( gdi_display, physBitmap->pixmap, BITMAP_GC(physBitmap),
411                image, 0, 0, 0, 0, bitmap.bmWidth, height );
412     XDestroyImage( image ); /* frees image->data too */
413     wine_tsx11_unlock();
414     return count;
415 }
416
417 /***********************************************************************
418  *           DeleteBitmap   (X11DRV.@)
419  */
420 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
421 {
422     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
423
424     if (physBitmap)
425     {
426         DIBSECTION dib;
427
428         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
429             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
430
431         if (physBitmap->glxpixmap)
432             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
433         wine_tsx11_lock();
434         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
435         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
436         wine_tsx11_unlock();
437         HeapFree( GetProcessHeap(), 0, physBitmap );
438     }
439     return TRUE;
440 }
441
442
443 /***********************************************************************
444  *           X11DRV_get_phys_bitmap
445  *
446  * Retrieve the X physical bitmap info.
447  */
448 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
449 {
450     X_PHYSBITMAP *ret;
451
452     wine_tsx11_lock();
453     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
454     wine_tsx11_unlock();
455     return ret;
456 }
457
458
459 /***********************************************************************
460  *           X11DRV_init_phys_bitmap
461  *
462  * Initialize the X physical bitmap info.
463  */
464 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
465 {
466     X_PHYSBITMAP *ret;
467
468     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
469     {
470         ret->hbitmap = hbitmap;
471         wine_tsx11_lock();
472         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
473         wine_tsx11_unlock();
474     }
475     return ret;
476 }
477
478
479 /***********************************************************************
480  *           X11DRV_get_pixmap
481  *
482  * Retrieve the pixmap associated to a bitmap.
483  */
484 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
485 {
486     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
487
488     if (!physBitmap) return 0;
489     return physBitmap->pixmap;
490 }