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