2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
9 #ifndef X_DISPLAY_MISSING
26 #include "wine/winuser16.h"
28 DEFAULT_DEBUG_CHANNEL(x11drv)
30 /* GCs used for B&W and color bitmap operations */
31 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
34 /***********************************************************************
37 BOOL X11DRV_BITMAP_Init(void)
41 /* Create the necessary GCs */
43 if ((tmpPixmap = TSXCreatePixmap(display,
44 X11DRV_GetXRootWindow(),
48 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
49 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
50 TSXFreePixmap( display, tmpPixmap );
53 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
55 if ((tmpPixmap = TSXCreatePixmap(display,
56 X11DRV_GetXRootWindow(),
58 MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
60 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
61 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
62 TSXFreePixmap( display, tmpPixmap );
68 /***********************************************************************
69 * X11DRV_BITMAP_SelectObject
71 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
75 HBITMAP prevHandle = dc->w.hBitmap;
76 X11DRV_PHYSBITMAP *pbitmap;
77 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
80 if (!(dc->w.flags & DC_MEMORY)) return 0;
83 if(!X11DRV_CreateBitmap(hbitmap))
86 if(bmp->DDBitmap->funcs != dc->funcs) {
87 WARN(x11drv, "Trying to select non-X11 DDB into an X11 dc\n");
91 pbitmap = bmp->DDBitmap->physBitmap;
93 dc->w.totalExtent.left = 0;
94 dc->w.totalExtent.top = 0;
95 dc->w.totalExtent.right = bmp->bitmap.bmWidth;
96 dc->w.totalExtent.bottom = bmp->bitmap.bmHeight;
99 SetRectRgn( dc->w.hVisRgn, 0, 0,
100 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
103 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
105 dc->w.hVisRgn = hrgn;
108 physDev->drawable = pbitmap->pixmap;
109 dc->w.hBitmap = hbitmap;
111 /* Change GC depth if needed */
113 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
115 TSXFreeGC( display, physDev->gc );
116 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
117 TSXSetGraphicsExposures( display, physDev->gc, False );
118 dc->w.bitsPerPixel = bmp->bitmap.bmBitsPixel;
121 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
126 /***********************************************************************
129 * Wrapper to call XPutImage with CALL_LARGE_STACK.
132 struct XPutImage_descr
140 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
142 return XPutImage( display,
143 ((X11DRV_PHYSBITMAP *)descr->bmp->DDBitmap->physBitmap)->pixmap,
144 BITMAP_GC(descr->bmp),
145 descr->image, 0, 0, 0, 0, descr->width, descr->height );
149 /***************************************************************************
153 * Allocate DDBitmap and physBitmap
156 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
158 X11DRV_PHYSBITMAP *pbitmap;
160 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
161 WARN(x11drv, "Can't alloc DDBITMAP\n");
165 if(!(pbitmap = HeapAlloc(GetProcessHeap(), 0,sizeof(X11DRV_PHYSBITMAP)))) {
166 WARN(x11drv, "Can't alloc X11DRV_PHYSBITMAP\n");
167 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
171 bmp->DDBitmap->physBitmap = pbitmap;
172 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
177 /****************************************************************************
179 * X11DRV_CreateBitmap
181 * Create a device dependent X11 bitmap
183 * Returns TRUE on success else FALSE
187 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
189 X11DRV_PHYSBITMAP *pbitmap;
190 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
193 WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
197 /* Check parameters */
198 if (bmp->bitmap.bmPlanes != 1) return 0;
199 if ((bmp->bitmap.bmBitsPixel != 1) &&
200 (bmp->bitmap.bmBitsPixel != MONITOR_GetDepth(&MONITOR_PrimaryMonitor))) {
201 ERR(x11drv, "Trying to make bitmap with planes=%d, bpp=%d\n",
202 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
203 GDI_HEAP_UNLOCK( hbitmap );
207 TRACE(x11drv, "(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
208 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
210 pbitmap = X11DRV_AllocBitmap( bmp );
211 if(!pbitmap) return FALSE;
213 /* Create the pixmap */
214 pbitmap->pixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(), bmp->bitmap.bmWidth,
215 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
216 if (!pbitmap->pixmap) {
217 WARN(x11drv, "Can't create Pixmap\n");
218 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap->physBitmap);
219 HeapFree(GetProcessHeap(), 0, bmp->DDBitmap);
220 GDI_HEAP_UNLOCK( hbitmap );
224 if (bmp->bitmap.bmBits) /* Set bitmap bits */
225 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
226 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
229 GDI_HEAP_UNLOCK( hbitmap );
234 /***********************************************************************
235 * X11DRV_BITMAP_GetXImage
237 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
239 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
241 return XGetImage( display,
242 ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
243 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
244 AllPlanes, ZPixmap );
248 /***********************************************************************
249 * X11DRV_GetBitmapBits
252 * Success: Number of bytes copied
255 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
257 LONG old_height, height;
259 LPBYTE tbuf, startline;
262 TRACE(x11drv, "(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
264 EnterCriticalSection( &X11DRV_CritSection );
266 /* Hack: change the bitmap height temporarily to avoid */
267 /* getting unnecessary bitmap rows. */
269 old_height = bmp->bitmap.bmHeight;
270 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
272 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
274 bmp->bitmap.bmHeight = old_height;
276 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
279 switch (bmp->bitmap.bmBitsPixel)
282 for (h=0;h<height;h++)
286 for (w=0;w<bmp->bitmap.bmWidth;w++)
290 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
291 if ((w&7) == 7) ++tbuf;
293 startline += bmp->bitmap.bmWidthBytes;
297 for (h=0;h<height;h++)
300 for (w=0;w<bmp->bitmap.bmWidth;w++)
302 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
303 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
305 startline += bmp->bitmap.bmWidthBytes;
309 for (h=0;h<height;h++)
312 for (w=0;w<bmp->bitmap.bmWidth;w++)
313 *tbuf++ = XGetPixel(image,w,h);
314 startline += bmp->bitmap.bmWidthBytes;
319 for (h=0;h<height;h++)
322 for (w=0;w<bmp->bitmap.bmWidth;w++)
324 long pixel = XGetPixel(image,w,h);
326 *tbuf++ = pixel & 0xff;
327 *tbuf++ = (pixel>>8) & 0xff;
329 startline += bmp->bitmap.bmWidthBytes;
333 for (h=0;h<height;h++)
336 for (w=0;w<bmp->bitmap.bmWidth;w++)
338 long pixel = XGetPixel(image,w,h);
340 *tbuf++ = pixel & 0xff;
341 *tbuf++ = (pixel>> 8) & 0xff;
342 *tbuf++ = (pixel>>16) & 0xff;
344 startline += bmp->bitmap.bmWidthBytes;
349 for (h=0;h<height;h++)
352 for (w=0;w<bmp->bitmap.bmWidth;w++)
354 long pixel = XGetPixel(image,w,h);
356 *tbuf++ = pixel & 0xff;
357 *tbuf++ = (pixel>> 8) & 0xff;
358 *tbuf++ = (pixel>>16) & 0xff;
359 *tbuf++ = (pixel>>24) & 0xff;
361 startline += bmp->bitmap.bmWidthBytes;
365 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
367 XDestroyImage( image );
368 LeaveCriticalSection( &X11DRV_CritSection );
375 /******************************************************************************
376 * X11DRV_SetBitmapBits
379 * Success: Number of bytes used in setting the bitmap bits
382 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
384 struct XPutImage_descr descr;
387 LPBYTE sbuf, startline;
390 TRACE(x11drv, "(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
392 height = count / bmp->bitmap.bmWidthBytes;
394 EnterCriticalSection( &X11DRV_CritSection );
395 image = XCreateImage( display, DefaultVisualOfScreen(X11DRV_GetXScreen()),
396 bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
397 bmp->bitmap.bmWidth, height, 32, 0 );
398 image->data = (LPBYTE)xmalloc(image->bytes_per_line * height);
400 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
404 switch (bmp->bitmap.bmBitsPixel)
407 for (h=0;h<height;h++)
410 for (w=0;w<bmp->bitmap.bmWidth;w++)
412 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
416 startline += bmp->bitmap.bmWidthBytes;
420 for (h=0;h<height;h++)
423 for (w=0;w<bmp->bitmap.bmWidth;w++)
425 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
426 else XPutPixel( image, w, h, *sbuf++ & 0xf );
428 startline += bmp->bitmap.bmWidthBytes;
432 for (h=0;h<height;h++)
435 for (w=0;w<bmp->bitmap.bmWidth;w++)
436 XPutPixel(image,w,h,*sbuf++);
437 startline += bmp->bitmap.bmWidthBytes;
442 for (h=0;h<height;h++)
445 for (w=0;w<bmp->bitmap.bmWidth;w++)
447 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
450 startline += bmp->bitmap.bmWidthBytes;
454 for (h=0;h<height;h++)
457 for (w=0;w<bmp->bitmap.bmWidth;w++)
459 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
462 startline += bmp->bitmap.bmWidthBytes;
466 for (h=0;h<height;h++)
469 for (w=0;w<bmp->bitmap.bmWidth;w++)
471 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
474 startline += bmp->bitmap.bmWidthBytes;
478 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
484 descr.width = bmp->bitmap.bmWidth;
485 descr.height = height;
487 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
488 XDestroyImage( image ); /* frees image->data too */
489 LeaveCriticalSection( &X11DRV_CritSection );
494 /***********************************************************************
497 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
499 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
502 WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
507 ret = X11DRV_GetBitmapBits(bmp, bits, count);
508 else if(flags == DDB_SET)
509 ret = X11DRV_SetBitmapBits(bmp, bits, count);
511 ERR(x11drv, "Unknown flags value %d\n", flags);
515 GDI_HEAP_UNLOCK( hbitmap );
519 /***********************************************************************
520 * X11DRV_BITMAP_DeleteObject
522 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
524 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
526 TSXFreePixmap( display, pbitmap->pixmap );
528 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
529 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
530 bmp->DDBitmap = NULL;
535 /***********************************************************************
536 * X11DRV_BITMAP_Pixmap
538 * This function exists solely for x11 driver of the window system.
540 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
542 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
543 return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
546 #endif /* !defined(X_DISPLAY_MISSING) */