Allocate DC objects on the process heap, and removed WIN_DC_INFO
[wine/wine64.git] / graphics / x11drv / bitmap.c
blob181a268505abc9854d0bdbe54a45c27c0cde0f7e
1 /*
2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1999 Noel Borthwick
6 */
8 #include "config.h"
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "gdi.h"
16 #include "callback.h"
17 #include "bitmap.h"
18 #include "heap.h"
19 #include "debugtools.h"
20 #include "local.h"
21 #include "x11drv.h"
22 #include "wingdi.h"
23 #include "windef.h"
24 #include "wine/winuser16.h"
26 DEFAULT_DEBUG_CHANNEL(x11drv);
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 (X11DRV_GetDepth() != 1)
53 if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
54 1, 1, X11DRV_GetDepth())))
56 BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
57 TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
58 TSXFreePixmap( display, tmpPixmap );
61 return TRUE;
64 /***********************************************************************
65 * X11DRV_BITMAP_SelectObject
67 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
68 BITMAPOBJ * bmp )
70 HRGN hrgn;
71 HBITMAP prevHandle = dc->hBitmap;
72 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
75 if (!(dc->flags & DC_MEMORY)) return 0;
77 if(!bmp->physBitmap)
78 if(!X11DRV_CreateBitmap(hbitmap))
79 return 0;
81 if(bmp->funcs != dc->funcs) {
82 WARN("Trying to select non-X11 DDB into an X11 dc\n");
83 return 0;
86 hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
87 if (!hrgn) return 0;
89 dc->totalExtent.left = 0;
90 dc->totalExtent.top = 0;
91 dc->totalExtent.right = bmp->bitmap.bmWidth;
92 dc->totalExtent.bottom = bmp->bitmap.bmHeight;
94 physDev->drawable = (Pixmap)bmp->physBitmap;
95 dc->hBitmap = hbitmap;
97 SelectVisRgn16( dc->hSelf, hrgn );
98 DeleteObject( hrgn );
100 /* Change GC depth if needed */
102 if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
104 TSXFreeGC( display, physDev->gc );
105 physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
106 TSXSetGraphicsExposures( display, physDev->gc, False );
107 dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
108 DC_InitDC( dc );
110 return prevHandle;
114 /***********************************************************************
115 * XPutImage_wrapper
117 * Wrapper to call XPutImage with CALL_LARGE_STACK.
120 struct XPutImage_descr
122 BITMAPOBJ *bmp;
123 XImage *image;
124 INT width;
125 INT height;
128 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
130 return XPutImage( display, (Pixmap)descr->bmp->physBitmap, BITMAP_GC(descr->bmp),
131 descr->image, 0, 0, 0, 0, descr->width, descr->height );
135 /****************************************************************************
137 * X11DRV_CreateBitmap
139 * Create a device dependent X11 bitmap
141 * Returns TRUE on success else FALSE
145 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
147 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
149 if(!bmp) {
150 WARN("Bad bitmap handle %08x\n", hbitmap);
151 return FALSE;
154 /* Check parameters */
155 if (bmp->bitmap.bmPlanes != 1)
157 GDI_ReleaseObj( hbitmap );
158 return 0;
160 if ((bmp->bitmap.bmBitsPixel != 1) &&
161 (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
163 ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
164 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
165 GDI_ReleaseObj( hbitmap );
166 return FALSE;
169 TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
170 bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
172 /* Create the pixmap */
173 if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
174 bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
175 bmp->bitmap.bmBitsPixel)))
177 WARN("Can't create Pixmap\n");
178 GDI_ReleaseObj( hbitmap );
179 return FALSE;
181 bmp->funcs = &X11DRV_DC_Funcs;
183 if (bmp->bitmap.bmBits) /* Set bitmap bits */
184 X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
185 bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
186 DDB_SET );
188 GDI_ReleaseObj( hbitmap );
189 return TRUE;
193 /***********************************************************************
194 * X11DRV_BITMAP_GetXImage
196 * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
198 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
200 return XGetImage( display, (Pixmap)bmp->physBitmap,
201 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
202 AllPlanes, ZPixmap );
206 /***********************************************************************
207 * X11DRV_GetBitmapBits
209 * RETURNS
210 * Success: Number of bytes copied
211 * Failure: 0
213 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
215 LONG old_height, height;
216 XImage *image;
217 LPBYTE tbuf, startline;
218 int h, w;
220 TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
222 EnterCriticalSection( &X11DRV_CritSection );
224 /* Hack: change the bitmap height temporarily to avoid */
225 /* getting unnecessary bitmap rows. */
227 old_height = bmp->bitmap.bmHeight;
228 height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
230 image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
232 bmp->bitmap.bmHeight = old_height;
234 /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
236 startline = buffer;
237 switch (bmp->bitmap.bmBitsPixel)
239 case 1:
240 for (h=0;h<height;h++)
242 tbuf = startline;
243 *tbuf = 0;
244 for (w=0;w<bmp->bitmap.bmWidth;w++)
246 if ((w%8) == 0)
247 *tbuf = 0;
248 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
249 if ((w&7) == 7) ++tbuf;
251 startline += bmp->bitmap.bmWidthBytes;
253 break;
254 case 4:
255 for (h=0;h<height;h++)
257 tbuf = startline;
258 for (w=0;w<bmp->bitmap.bmWidth;w++)
260 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
261 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
263 startline += bmp->bitmap.bmWidthBytes;
265 break;
266 case 8:
267 for (h=0;h<height;h++)
269 tbuf = startline;
270 for (w=0;w<bmp->bitmap.bmWidth;w++)
271 *tbuf++ = XGetPixel(image,w,h);
272 startline += bmp->bitmap.bmWidthBytes;
274 break;
275 case 15:
276 case 16:
277 for (h=0;h<height;h++)
279 tbuf = startline;
280 for (w=0;w<bmp->bitmap.bmWidth;w++)
282 long pixel = XGetPixel(image,w,h);
284 *tbuf++ = pixel & 0xff;
285 *tbuf++ = (pixel>>8) & 0xff;
287 startline += bmp->bitmap.bmWidthBytes;
289 break;
290 case 24:
291 for (h=0;h<height;h++)
293 tbuf = startline;
294 for (w=0;w<bmp->bitmap.bmWidth;w++)
296 long pixel = XGetPixel(image,w,h);
298 *tbuf++ = pixel & 0xff;
299 *tbuf++ = (pixel>> 8) & 0xff;
300 *tbuf++ = (pixel>>16) & 0xff;
302 startline += bmp->bitmap.bmWidthBytes;
304 break;
306 case 32:
307 for (h=0;h<height;h++)
309 tbuf = startline;
310 for (w=0;w<bmp->bitmap.bmWidth;w++)
312 long pixel = XGetPixel(image,w,h);
314 *tbuf++ = pixel & 0xff;
315 *tbuf++ = (pixel>> 8) & 0xff;
316 *tbuf++ = (pixel>>16) & 0xff;
317 *tbuf++ = (pixel>>24) & 0xff;
319 startline += bmp->bitmap.bmWidthBytes;
321 break;
322 default:
323 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
325 XDestroyImage( image );
326 LeaveCriticalSection( &X11DRV_CritSection );
328 return count;
333 /******************************************************************************
334 * X11DRV_SetBitmapBits
336 * RETURNS
337 * Success: Number of bytes used in setting the bitmap bits
338 * Failure: 0
340 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
342 struct XPutImage_descr descr;
343 LONG height;
344 XImage *image;
345 LPBYTE sbuf, startline;
346 int w, h;
348 TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
350 height = count / bmp->bitmap.bmWidthBytes;
352 EnterCriticalSection( &X11DRV_CritSection );
353 image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
354 bmp->bitmap.bmWidth, height, 32, 0 );
355 if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
357 WARN("No memory to create image data.\n");
358 XDestroyImage( image );
359 LeaveCriticalSection( &X11DRV_CritSection );
360 return 0;
363 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
365 startline = bits;
367 switch (bmp->bitmap.bmBitsPixel)
369 case 1:
370 for (h=0;h<height;h++)
372 sbuf = startline;
373 for (w=0;w<bmp->bitmap.bmWidth;w++)
375 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
376 if ((w&7) == 7)
377 sbuf++;
379 startline += bmp->bitmap.bmWidthBytes;
381 break;
382 case 4:
383 for (h=0;h<height;h++)
385 sbuf = startline;
386 for (w=0;w<bmp->bitmap.bmWidth;w++)
388 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
389 else XPutPixel( image, w, h, *sbuf++ & 0xf );
391 startline += bmp->bitmap.bmWidthBytes;
393 break;
394 case 8:
395 for (h=0;h<height;h++)
397 sbuf = startline;
398 for (w=0;w<bmp->bitmap.bmWidth;w++)
399 XPutPixel(image,w,h,*sbuf++);
400 startline += bmp->bitmap.bmWidthBytes;
402 break;
403 case 15:
404 case 16:
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[1]*256+sbuf[0]);
411 sbuf+=2;
413 startline += bmp->bitmap.bmWidthBytes;
415 break;
416 case 24:
417 for (h=0;h<height;h++)
419 sbuf = startline;
420 for (w=0;w<bmp->bitmap.bmWidth;w++)
422 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
423 sbuf += 3;
425 startline += bmp->bitmap.bmWidthBytes;
427 break;
428 case 32:
429 for (h=0;h<height;h++)
431 sbuf = startline;
432 for (w=0;w<bmp->bitmap.bmWidth;w++)
434 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
435 sbuf += 4;
437 startline += bmp->bitmap.bmWidthBytes;
439 break;
440 default:
441 FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
445 descr.bmp = bmp;
446 descr.image = image;
447 descr.width = bmp->bitmap.bmWidth;
448 descr.height = height;
450 CALL_LARGE_STACK( XPutImage_wrapper, &descr );
451 XDestroyImage( image ); /* frees image->data too */
452 LeaveCriticalSection( &X11DRV_CritSection );
454 return count;
457 /***********************************************************************
458 * X11DRV_BitmapBits
460 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
462 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
463 LONG ret;
464 if(!bmp) {
465 WARN("Bad bitmap handle %08x\n", hbitmap);
466 return FALSE;
469 if(flags == DDB_GET)
470 ret = X11DRV_GetBitmapBits(bmp, bits, count);
471 else if(flags == DDB_SET)
472 ret = X11DRV_SetBitmapBits(bmp, bits, count);
473 else {
474 ERR("Unknown flags value %d\n", flags);
475 ret = 0;
477 GDI_ReleaseObj( hbitmap );
478 return ret;
481 /***********************************************************************
482 * X11DRV_BITMAP_DeleteObject
484 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
486 TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
487 bmp->physBitmap = NULL;
488 bmp->funcs = NULL;
489 return TRUE;
492 /**************************************************************************
493 * X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
495 * Allocates an HBITMAP which references the Pixmap passed in.
496 * Note: This function makes the bitmap an owner of the Pixmap so subsequently
497 * calling DeleteObject on this will free the Pixmap as well.
499 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
501 HBITMAP hBmp = 0;
502 BITMAPOBJ *pBmp = NULL;
503 Window root;
504 int x,y; /* Unused */
505 unsigned border_width; /* Unused */
506 unsigned int depth, width, height;
508 /* Get the Pixmap dimensions and bit depth */
509 if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
510 &border_width, &depth) )
511 goto END;
513 TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
514 width, height, depth);
517 * Create an HBITMAP with the same dimensions and BPP as the pixmap,
518 * and make it a container for the pixmap passed.
520 hBmp = CreateBitmap( width, height, 1, depth, NULL );
522 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
524 pBmp->funcs = &X11DRV_DC_Funcs;
525 pBmp->physBitmap = (void *)pixmap;
526 GDI_ReleaseObj( hBmp );
528 END:
529 TRACE("\tReturning HBITMAP %x\n", hBmp);
530 return hBmp;
534 /**************************************************************************
535 * X11DRV_BITMAP_CreateBitmapFromPixmap
537 * Allocates an HBITMAP and copies the Pixmap data into it.
538 * If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
540 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
542 HBITMAP hBmp = 0, hBmpCopy = 0;
543 BITMAPOBJ *pBmp = NULL;
544 unsigned int width, height;
546 /* Allocate an HBITMAP which references the Pixmap passed to us */
547 hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
548 if (!hBmp)
550 TRACE("\tCould not create bitmap header for Pixmap\n");
551 goto END;
554 /* Get the bitmap dimensions */
555 width = pBmp->bitmap.bmWidth;
556 height = pBmp->bitmap.bmHeight;
558 hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
560 /* We can now get rid of the HBITMAP wrapper we created earlier.
561 * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
563 if (!bDeletePixmap)
565 /* Manually clear the bitmap internals to prevent the Pixmap
566 * from being deleted by DeleteObject.
568 pBmp->physBitmap = NULL;
569 pBmp->funcs = NULL;
571 DeleteObject(hBmp);
573 END:
574 TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
575 return hBmpCopy;
579 /**************************************************************************
580 * X11DRV_BITMAP_CreatePixmapFromBitmap
582 * Creates a Pixmap from a bitmap
584 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
586 HGLOBAL hPackedDIB = 0;
587 Pixmap pixmap = 0;
590 * Create a packed DIB from the bitmap passed to us.
591 * A packed DIB contains a BITMAPINFO structure followed immediately by
592 * an optional color palette and the pixel data.
594 hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
596 /* Create a Pixmap from the packed DIB */
597 pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
599 /* Free the temporary packed DIB */
600 GlobalFree(hPackedDIB);
602 return pixmap;
606 /***********************************************************************
607 * X11DRV_BITMAP_Pixmap
609 * This function exists solely for x11 driver of the window system.
611 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
613 Pixmap pixmap;
614 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
615 pixmap = (Pixmap)bmp->physBitmap;
616 GDI_ReleaseObj( hbitmap );
617 return pixmap;