Fixes for ignored WVR_[VH]REDRAW flags, made minimization in managed
[wine] / graphics / x11drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #ifndef X_DISPLAY_MISSING
10
11 #include "ts_xlib.h"
12 #include "ts_xutil.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "gdi.h"
17 #include "callback.h"
18 #include "dc.h"
19 #include "bitmap.h"
20 #include "heap.h"
21 #include "monitor.h"
22 #include "debug.h"
23 #include "xmalloc.h"
24 #include "local.h"
25 #include "x11drv.h"
26 #include "wine/winuser16.h"
27
28   /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
30
31
32 /***********************************************************************
33  *           X11DRV_BITMAP_Init
34  */
35 BOOL X11DRV_BITMAP_Init(void)
36 {
37     Pixmap tmpPixmap;
38     
39       /* Create the necessary GCs */
40     
41     if ((tmpPixmap = TSXCreatePixmap(display, 
42                                      X11DRV_GetXRootWindow(), 
43                                      1, 1, 
44                                      1)))
45     {
46         BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
47         TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
48         TSXFreePixmap( display, tmpPixmap );
49     }
50
51     if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
52     {
53         if ((tmpPixmap = TSXCreatePixmap(display, 
54                                          X11DRV_GetXRootWindow(),
55                                          1, 1,
56                                          MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
57         {
58             BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
59             TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
60             TSXFreePixmap( display, tmpPixmap );
61         }
62     }
63     return TRUE;
64 }
65
66 /***********************************************************************
67  *           X11DRV_BITMAP_SelectObject
68  */
69 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
70                                       BITMAPOBJ * bmp )
71 {
72     HRGN hrgn;
73     HBITMAP prevHandle = dc->w.hBitmap;
74     X11DRV_PHYSBITMAP *pbitmap;
75     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
76
77
78     if (!(dc->w.flags & DC_MEMORY)) return 0;
79
80     if(!bmp->DDBitmap)
81         if(!X11DRV_CreateBitmap(hbitmap))
82             return 0;
83
84     if(bmp->DDBitmap->funcs != dc->funcs) {
85         WARN(x11drv, "Trying to select non-X11 DDB into an X11 dc\n");
86         return 0;
87     }
88
89     pbitmap = bmp->DDBitmap->physBitmap;
90
91     dc->w.totalExtent.left   = 0;
92     dc->w.totalExtent.top    = 0;
93     dc->w.totalExtent.right  = bmp->bitmap.bmWidth;
94     dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
95
96     if (dc->w.hVisRgn)
97        SetRectRgn( dc->w.hVisRgn, 0, 0,
98                      bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
99     else
100     { 
101        hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
102        if (!hrgn) return 0;
103        dc->w.hVisRgn    = hrgn;
104     }
105
106     physDev->drawable = pbitmap->pixmap;
107     dc->w.hBitmap     = hbitmap;
108
109       /* Change GC depth if needed */
110
111     if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
112     {
113         TSXFreeGC( display, physDev->gc );
114         physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
115         TSXSetGraphicsExposures( display, physDev->gc, False );
116         dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
117         DC_InitDC( dc );
118     }
119     else CLIPPING_UpdateGCRegion( dc );  /* Just update GC clip region */
120     return prevHandle;
121 }
122
123
124 /***********************************************************************
125  *           XPutImage_wrapper
126  *
127  * Wrapper to call XPutImage with CALL_LARGE_STACK.
128  */
129
130 struct XPutImage_descr
131 {
132     BITMAPOBJ *bmp;
133     XImage    *image;
134     INT      width;
135     INT      height;
136 };
137
138 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
139 {
140     return XPutImage( display,
141                ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
142                BITMAP_GC(descr->bmp),
143                descr->image, 0, 0, 0, 0, descr->width, descr->height );
144 }
145
146
147 /***************************************************************************
148  *
149  *      X11DRV_AllocBitmap
150  *
151  * Allocate DDBitmap and physBitmap
152  *
153  */
154 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
155 {
156     X11DRV_PHYSBITMAP *pbitmap;
157
158     if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
159         WARN(x11drv, "Can't alloc DDBITMAP\n");
160         return NULL;
161     }
162
163     if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
164         WARN(x11drv, "Can't alloc X11DRV_PHYSBITMAP\n");
165         HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
166         return NULL;
167     }
168
169     bmp->DDBitmap->physBitmap = pbitmap;
170     bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
171     return pbitmap;
172 }
173
174
175 /****************************************************************************
176  *
177  *        X11DRV_CreateBitmap
178  *
179  * Create a device dependent X11 bitmap
180  *
181  * Returns TRUE on success else FALSE
182  *
183  */
184
185 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
186 {
187     X11DRV_PHYSBITMAP *pbitmap;
188     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
189
190     if(!bmp) {
191         WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
192         return FALSE;
193     }
194
195       /* Check parameters */
196     if (bmp->bitmap.bmPlanes != 1) return 0;
197     if ((bmp->bitmap.bmBitsPixel != 1) && 
198         (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
199         ERR(x11drv, "Trying to make bitmap with planes=%d, bpp=%d\n",
200             bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
201         GDI_HEAP_UNLOCK( hbitmap );
202         return FALSE;
203     }
204
205     TRACE(x11drv, "(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
206           bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
207
208     pbitmap = X11DRV_AllocBitmap( bmp );
209     if(!pbitmap) return FALSE;
210
211       /* Create the pixmap */
212     pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
213                               bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
214     if (!pbitmap->pixmap) {
215         WARN(x11drv, "Can't create Pixmap\n");
216         HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
217         HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
218         GDI_HEAP_UNLOCK( hbitmap );
219         return FALSE;
220     }
221
222     if (bmp->bitmap.bmBits) /* Set bitmap bits */
223         X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
224                            bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
225                            DDB_SET );
226
227     GDI_HEAP_UNLOCK( hbitmap );
228     return TRUE;
229 }
230
231
232 /***********************************************************************
233  *           X11DRV_BITMAP_GetXImage
234  *
235  * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
236  */
237 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
238 {
239     return XGetImage( display,
240                       ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
241                       0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
242                       AllPlanes, ZPixmap );
243 }
244
245
246 /***********************************************************************
247  *           X11DRV_GetBitmapBits
248  * 
249  * RETURNS
250  *    Success: Number of bytes copied
251  *    Failure: 0
252  */
253 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
254 {
255     LONG old_height, height;
256     XImage *image;
257     LPBYTE tbuf, startline;
258     int h, w;
259
260     TRACE(x11drv, "(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
261
262     EnterCriticalSection( &X11DRV_CritSection );
263
264     /* Hack: change the bitmap height temporarily to avoid */
265     /*       getting unnecessary bitmap rows. */
266
267     old_height = bmp->bitmap.bmHeight;
268     height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
269
270     image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
271
272     bmp->bitmap.bmHeight = old_height;
273
274     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
275
276     startline = buffer;
277     switch (bmp->bitmap.bmBitsPixel)
278     {
279     case 1:
280         for (h=0;h<height;h++)
281         {
282             tbuf = startline;
283             *tbuf = 0;
284             for (w=0;w<bmp->bitmap.bmWidth;w++)
285             {
286                 if ((w%8) == 0)
287                     *tbuf = 0;
288                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
289                 if ((w&7) == 7) ++tbuf;
290             }
291             startline += bmp->bitmap.bmWidthBytes;
292         }
293         break;
294     case 4:
295         for (h=0;h<height;h++)
296         {
297             tbuf = startline;
298             for (w=0;w<bmp->bitmap.bmWidth;w++)
299             {
300                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
301                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
302             }
303             startline += bmp->bitmap.bmWidthBytes;
304         }
305         break;
306     case 8:
307         for (h=0;h<height;h++)
308         {
309             tbuf = startline;
310             for (w=0;w<bmp->bitmap.bmWidth;w++)
311                 *tbuf++ = XGetPixel(image,w,h);
312             startline += bmp->bitmap.bmWidthBytes;
313         }
314         break;
315     case 15:
316     case 16:
317         for (h=0;h<height;h++)
318         {
319             tbuf = startline;
320             for (w=0;w<bmp->bitmap.bmWidth;w++)
321             {
322                 long pixel = XGetPixel(image,w,h);
323
324                 *tbuf++ = pixel & 0xff;
325                 *tbuf++ = (pixel>>8) & 0xff;
326             }
327             startline += bmp->bitmap.bmWidthBytes;
328         }
329         break;
330     case 24:
331         for (h=0;h<height;h++)
332         {
333             tbuf = startline;
334             for (w=0;w<bmp->bitmap.bmWidth;w++)
335             {
336                 long pixel = XGetPixel(image,w,h);
337
338                 *tbuf++ = pixel & 0xff;
339                 *tbuf++ = (pixel>> 8) & 0xff;
340                 *tbuf++ = (pixel>>16) & 0xff;
341             }
342             startline += bmp->bitmap.bmWidthBytes;
343         }
344         break;
345
346     case 32:
347         for (h=0;h<height;h++)
348         {
349             tbuf = startline;
350             for (w=0;w<bmp->bitmap.bmWidth;w++)
351             {
352                 long pixel = XGetPixel(image,w,h);
353
354                 *tbuf++ = pixel & 0xff;
355                 *tbuf++ = (pixel>> 8) & 0xff;
356                 *tbuf++ = (pixel>>16) & 0xff;
357                 *tbuf++ = (pixel>>24) & 0xff;
358             }
359             startline += bmp->bitmap.bmWidthBytes;
360         }
361         break;
362     default:
363         FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
364     }
365     XDestroyImage( image );
366     LeaveCriticalSection( &X11DRV_CritSection );
367
368     return count;
369 }
370
371
372
373 /******************************************************************************
374  *             X11DRV_SetBitmapBits
375  *
376  * RETURNS
377  *    Success: Number of bytes used in setting the bitmap bits
378  *    Failure: 0
379  */
380 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
381 {
382     struct XPutImage_descr descr;
383     LONG height;
384     XImage *image;
385     LPBYTE sbuf, startline;
386     int w, h;
387
388     TRACE(x11drv, "(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
389     
390     height = count / bmp->bitmap.bmWidthBytes;
391
392     EnterCriticalSection( &X11DRV_CritSection );
393     image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
394                           bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
395                           bmp->bitmap.bmWidth, height, 32, 0 );
396     image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
397     
398     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
399     
400     startline = bits;
401
402     switch (bmp->bitmap.bmBitsPixel)
403     {
404     case 1:
405         for (h=0;h<height;h++)
406         {
407             sbuf = startline;
408             for (w=0;w<bmp->bitmap.bmWidth;w++)
409             {
410                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
411                 if ((w&7) == 7)
412                     sbuf++;
413             }
414             startline += bmp->bitmap.bmWidthBytes;
415         }
416         break;
417     case 4:
418         for (h=0;h<height;h++)
419         {
420             sbuf = startline;
421             for (w=0;w<bmp->bitmap.bmWidth;w++)
422             {
423                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
424                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
425             }
426             startline += bmp->bitmap.bmWidthBytes;
427         }
428         break;
429     case 8:
430         for (h=0;h<height;h++)
431         {
432             sbuf = startline;
433             for (w=0;w<bmp->bitmap.bmWidth;w++)
434                 XPutPixel(image,w,h,*sbuf++);
435             startline += bmp->bitmap.bmWidthBytes;
436         }
437         break;
438     case 15:
439     case 16:
440         for (h=0;h<height;h++)
441         {
442             sbuf = startline;
443             for (w=0;w<bmp->bitmap.bmWidth;w++)
444             {
445                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
446                 sbuf+=2;
447             }
448             startline += bmp->bitmap.bmWidthBytes;
449         }
450         break;
451     case 24:
452         for (h=0;h<height;h++)
453         {
454             sbuf = startline;
455             for (w=0;w<bmp->bitmap.bmWidth;w++)
456             {
457                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
458                 sbuf += 3;
459             }
460             startline += bmp->bitmap.bmWidthBytes;
461         }
462         break;
463     case 32: 
464         for (h=0;h<height;h++)
465         {
466             sbuf = startline;
467             for (w=0;w<bmp->bitmap.bmWidth;w++)
468             {
469                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
470                 sbuf += 4;
471             }
472             startline += bmp->bitmap.bmWidthBytes;
473         }
474         break;
475     default:
476       FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
477
478     }
479
480     descr.bmp    = bmp;
481     descr.image  = image;
482     descr.width  = bmp->bitmap.bmWidth;
483     descr.height = height;
484
485     CALL_LARGE_STACK( XPutImage_wrapper, &descr );
486     XDestroyImage( image ); /* frees image->data too */
487     LeaveCriticalSection( &X11DRV_CritSection );
488     
489     return count;
490 }
491
492 /***********************************************************************
493  *           X11DRV_BitmapBits
494  */
495 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
496 {
497     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
498     LONG ret;
499     if(!bmp) {
500         WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
501         return FALSE;
502     }
503
504     if(flags == DDB_GET)
505         ret = X11DRV_GetBitmapBits(bmp, bits, count);
506     else if(flags == DDB_SET)
507         ret = X11DRV_SetBitmapBits(bmp, bits, count);
508     else {
509         ERR(x11drv, "Unknown flags value %d\n", flags);
510         ret = 0;
511     }
512     
513     GDI_HEAP_UNLOCK( hbitmap );
514     return ret;
515 }
516
517 /***********************************************************************
518  *           X11DRV_BITMAP_DeleteObject
519  */
520 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
521 {
522     X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
523
524     TSXFreePixmap( display, pbitmap->pixmap );
525
526     HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
527     HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
528     bmp->DDBitmap = NULL;
529
530     return TRUE;
531 }
532
533 /***********************************************************************
534  *           X11DRV_BITMAP_Pixmap
535  *
536  * This function exists solely for x11 driver of the window system.
537  */
538 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
539 {
540     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
541     return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
542 }
543
544 #endif /* !defined(X_DISPLAY_MISSING) */