Added new CLIENT_DebuggerRequest routine, implemented support for
[wine/wine-kai.git] / graphics / x11drv / bitmap.c
blob1820aa2f8c9e3bb986d4a5977cf8c3d9f64c8be2
1 /*
2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include "config.h"
9 #ifndef X_DISPLAY_MISSING
11 #include "ts_xlib.h"
12 #include "ts_xutil.h"
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"
28 /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
32 /***********************************************************************
33 * X11DRV_BITMAP_Init
35 BOOL X11DRV_BITMAP_Init(void)
37 Pixmap tmpPixmap;
39 /* Create the necessary GCs */
41 if ((tmpPixmap = TSXCreatePixmap(display,
42 X11DRV_GetXRootWindow(),
43 1, 1,
44 1)))
46 BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
47 TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
48 TSXFreePixmap( display, tmpPixmap );
51 if (MONITOR_GetDepth(&MONITOR_PrimaryMonitor) != 1)
53 if ((tmpPixmap = TSXCreatePixmap(display,
54 X11DRV_GetXRootWindow(),
55 1, 1,
56 MONITOR_GetDepth(&MONITOR_PrimaryMonitor))))
58 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
59 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
60 TSXFreePixmap( display, tmpPixmap );
63 return TRUE;
66 /***********************************************************************
67 * X11DRV_BITMAP_SelectObject
69 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
70 BITMAPOBJ * bmp )
72 HRGN hrgn;
73 HBITMAP prevHandle = dc->w.hBitmap;
74 X11DRV_PHYSBITMAP *pbitmap;
75 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
78 if (!(dc->w.flags & DC_MEMORY)) return 0;
80 if(!bmp->DDBitmap)
81 if(!X11DRV_CreateBitmap(hbitmap))
82 return 0;
84 if(bmp->DDBitmap->funcs != dc->funcs) {
85 WARN(x11drv, "Trying to select non-X11 DDB into an X11 dc\n");
86 return 0;
89 pbitmap = bmp->DDBitmap->physBitmap;
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;
96 if (dc->w.hVisRgn)
97 SetRectRgn( dc->w.hVisRgn, 0, 0,
98 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight );
99 else
101 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
102 if (!hrgn) return 0;
103 dc->w.hVisRgn = hrgn;
106 physDev->drawable = pbitmap->pixmap;
107 dc->w.hBitmap = hbitmap;
109 /* Change GC depth if needed */
111 if (dc->w.bitsPerPixel != bmp->bitmap.bmBitsPixel)
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 );
119 else CLIPPING_UpdateGCRegion( dc ); /* Just update GC clip region */
120 return prevHandle;
124 /***********************************************************************
125 * XPutImage_wrapper
127 * Wrapper to call XPutImage with CALL_LARGE_STACK.
130 struct XPutImage_descr
132 BITMAPOBJ *bmp;
133 XImage *image;
134 INT width;
135 INT height;
138 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
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 );
147 /***************************************************************************
149 * X11DRV_AllocBitmap
151 * Allocate DDBitmap and physBitmap
154 X11DRV_PHYSBITMAP *X11DRV_AllocBitmap( BITMAPOBJ *bmp )
156 X11DRV_PHYSBITMAP *pbitmap;
158 if(!(bmp->DDBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DDBITMAP)))) {
159 WARN(x11drv, "Can't alloc DDBITMAP\n");
160 return NULL;
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;
169 bmp->DDBitmap->physBitmap = pbitmap;
170 bmp->DDBitmap->funcs = DRIVER_FindDriver( "DISPLAY" );
171 return pbitmap;
175 /****************************************************************************
177 * X11DRV_CreateBitmap
179 * Create a device dependent X11 bitmap
181 * Returns TRUE on success else FALSE
185 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
187 X11DRV_PHYSBITMAP *pbitmap;
188 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
190 if(!bmp) {
191 WARN(x11drv, "Bad bitmap handle %08x\n", hbitmap);
192 return FALSE;
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;
205 TRACE(x11drv, "(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
206 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
208 pbitmap = X11DRV_AllocBitmap( bmp );
209 if(!pbitmap) return FALSE;
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;
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 );
227 GDI_HEAP_UNLOCK( hbitmap );
228 return TRUE;
232 /***********************************************************************
233 * X11DRV_BITMAP_GetXImage
235 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
237 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
239 return XGetImage( display,
240 ((X11DRV_PHYSBITMAP *)bmp->DDBitmap->physBitmap)->pixmap,
241 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
242 AllPlanes, ZPixmap );
246 /***********************************************************************
247 * X11DRV_GetBitmapBits
249 * RETURNS
250 * Success: Number of bytes copied
251 * Failure: 0
253 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
255 LONG old_height, height;
256 XImage *image;
257 LPBYTE tbuf, startline;
258 int h, w;
260 TRACE(x11drv, "(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
262 EnterCriticalSection( &X11DRV_CritSection );
264 /* Hack: change the bitmap height temporarily to avoid */
265 /* getting unnecessary bitmap rows. */
267 old_height = bmp->bitmap.bmHeight;
268 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
270 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
272 bmp->bitmap.bmHeight = old_height;
274 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
276 startline = buffer;
277 switch (bmp->bitmap.bmBitsPixel)
279 case 1:
280 for (h=0;h<height;h++)
282 tbuf = startline;
283 *tbuf = 0;
284 for (w=0;w<bmp->bitmap.bmWidth;w++)
286 if ((w%8) == 0)
287 *tbuf = 0;
288 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
289 if ((w&7) == 7) ++tbuf;
291 startline += bmp->bitmap.bmWidthBytes;
293 break;
294 case 4:
295 for (h=0;h<height;h++)
297 tbuf = startline;
298 for (w=0;w<bmp->bitmap.bmWidth;w++)
300 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
301 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
303 startline += bmp->bitmap.bmWidthBytes;
305 break;
306 case 8:
307 for (h=0;h<height;h++)
309 tbuf = startline;
310 for (w=0;w<bmp->bitmap.bmWidth;w++)
311 *tbuf++ = XGetPixel(image,w,h);
312 startline += bmp->bitmap.bmWidthBytes;
314 break;
315 case 15:
316 case 16:
317 for (h=0;h<height;h++)
319 tbuf = startline;
320 for (w=0;w<bmp->bitmap.bmWidth;w++)
322 long pixel = XGetPixel(image,w,h);
324 *tbuf++ = pixel & 0xff;
325 *tbuf++ = (pixel>>8) & 0xff;
327 startline += bmp->bitmap.bmWidthBytes;
329 break;
330 case 24:
331 for (h=0;h<height;h++)
333 tbuf = startline;
334 for (w=0;w<bmp->bitmap.bmWidth;w++)
336 long pixel = XGetPixel(image,w,h);
338 *tbuf++ = pixel & 0xff;
339 *tbuf++ = (pixel>> 8) & 0xff;
340 *tbuf++ = (pixel>>16) & 0xff;
342 startline += bmp->bitmap.bmWidthBytes;
344 break;
346 case 32:
347 for (h=0;h<height;h++)
349 tbuf = startline;
350 for (w=0;w<bmp->bitmap.bmWidth;w++)
352 long pixel = XGetPixel(image,w,h);
354 *tbuf++ = pixel & 0xff;
355 *tbuf++ = (pixel>> 8) & 0xff;
356 *tbuf++ = (pixel>>16) & 0xff;
357 *tbuf++ = (pixel>>24) & 0xff;
359 startline += bmp->bitmap.bmWidthBytes;
361 break;
362 default:
363 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
365 XDestroyImage( image );
366 LeaveCriticalSection( &X11DRV_CritSection );
368 return count;
373 /******************************************************************************
374 * X11DRV_SetBitmapBits
376 * RETURNS
377 * Success: Number of bytes used in setting the bitmap bits
378 * Failure: 0
380 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
382 struct XPutImage_descr descr;
383 LONG height;
384 XImage *image;
385 LPBYTE sbuf, startline;
386 int w, h;
388 TRACE(x11drv, "(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
390 height = count / bmp->bitmap.bmWidthBytes;
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);
398 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
400 startline = bits;
402 switch (bmp->bitmap.bmBitsPixel)
404 case 1:
405 for (h=0;h<height;h++)
407 sbuf = startline;
408 for (w=0;w<bmp->bitmap.bmWidth;w++)
410 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
411 if ((w&7) == 7)
412 sbuf++;
414 startline += bmp->bitmap.bmWidthBytes;
416 break;
417 case 4:
418 for (h=0;h<height;h++)
420 sbuf = startline;
421 for (w=0;w<bmp->bitmap.bmWidth;w++)
423 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
424 else XPutPixel( image, w, h, *sbuf++ & 0xf );
426 startline += bmp->bitmap.bmWidthBytes;
428 break;
429 case 8:
430 for (h=0;h<height;h++)
432 sbuf = startline;
433 for (w=0;w<bmp->bitmap.bmWidth;w++)
434 XPutPixel(image,w,h,*sbuf++);
435 startline += bmp->bitmap.bmWidthBytes;
437 break;
438 case 15:
439 case 16:
440 for (h=0;h<height;h++)
442 sbuf = startline;
443 for (w=0;w<bmp->bitmap.bmWidth;w++)
445 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
446 sbuf+=2;
448 startline += bmp->bitmap.bmWidthBytes;
450 break;
451 case 24:
452 for (h=0;h<height;h++)
454 sbuf = startline;
455 for (w=0;w<bmp->bitmap.bmWidth;w++)
457 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
458 sbuf += 3;
460 startline += bmp->bitmap.bmWidthBytes;
462 break;
463 case 32:
464 for (h=0;h<height;h++)
466 sbuf = startline;
467 for (w=0;w<bmp->bitmap.bmWidth;w++)
469 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
470 sbuf += 4;
472 startline += bmp->bitmap.bmWidthBytes;
474 break;
475 default:
476 FIXME(x11drv, "Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
480 descr.bmp = bmp;
481 descr.image = image;
482 descr.width = bmp->bitmap.bmWidth;
483 descr.height = height;
485 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
486 XDestroyImage( image ); /* frees image->data too */
487 LeaveCriticalSection( &X11DRV_CritSection );
489 return count;
492 /***********************************************************************
493 * X11DRV_BitmapBits
495 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
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;
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;
513 GDI_HEAP_UNLOCK( hbitmap );
514 return ret;
517 /***********************************************************************
518 * X11DRV_BITMAP_DeleteObject
520 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
522 X11DRV_PHYSBITMAP *pbitmap = bmp->DDBitmap->physBitmap;
524 TSXFreePixmap( display, pbitmap->pixmap );
526 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap->physBitmap );
527 HeapFree( GetProcessHeap(), 0, bmp->DDBitmap );
528 bmp->DDBitmap = NULL;
530 return TRUE;
533 /***********************************************************************
534 * X11DRV_BITMAP_Pixmap
536 * This function exists solely for x11 driver of the window system.
538 BOOL X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
540 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
541 return ((X11DRV_PHYSBITMAP *)(bmp->DDBitmap->physBitmap))->pixmap;
544 #endif /* !defined(X_DISPLAY_MISSING) */