2 * X11DRV bitmap objects
4 * Copyright 1993 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
33 /* GCs used for B&W and color bitmap operations */
34 static GC bitmap_gc[32];
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 }; /* phys bitmap for the default stock bitmap */
37 static XContext bitmap_context; /* X context to associate a phys bitmap to a handle */
39 GC get_bitmap_gc(int depth)
41 if(depth < 1 || depth > 32)
44 return bitmap_gc[depth-1];
47 /***********************************************************************
50 void X11DRV_BITMAP_Init(void)
52 int depth_count, index, i;
57 bitmap_context = XUniqueContext();
58 BITMAP_stock_phys_bitmap.pixmap_depth = 1;
59 BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
60 bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
61 XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
62 XSetSubwindowMode( gdi_display, bitmap_gc[0], IncludeInferiors );
64 /* Create a GC for all available depths. GCs at depths other than 1-bit/screen_depth are for use
65 * in combination with XRender which allows us to create dibsections at more depths.
67 depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68 for (i = 0; i < depth_count; i++)
70 index = depth_list[i] - 1;
71 if (bitmap_gc[index]) continue;
72 if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, depth_list[i])))
74 if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
76 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
79 XFreePixmap( gdi_display, tmpPixmap );
87 /***********************************************************************
88 * SelectBitmap (X11DRV.@)
90 HBITMAP X11DRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
92 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
93 X_PHYSBITMAP *physBitmap;
96 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
99 X11DRV_XRender_UpdateDrawable( physDev );
101 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
102 else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
104 physDev->bitmap = physBitmap;
105 physDev->drawable = physBitmap->pixmap;
106 physDev->color_shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
107 SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
108 physDev->dc_rect = physDev->drawable_rect;
110 /* Change GC depth if needed */
112 if (physDev->depth != physBitmap->pixmap_depth)
114 physDev->depth = physBitmap->pixmap_depth;
116 XFreeGC( gdi_display, physDev->gc );
117 physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
118 XSetGraphicsExposures( gdi_display, physDev->gc, False );
119 XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
120 XFlush( gdi_display );
128 /****************************************************************************
129 * CreateBitmap (X11DRV.@)
131 * Create a device dependent X11 bitmap
133 * Returns TRUE on success else FALSE
135 BOOL X11DRV_CreateBitmap( PHYSDEV dev, HBITMAP hbitmap, LPVOID bmBits )
137 X_PHYSBITMAP *physBitmap;
140 if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
142 /* Check parameters */
143 if (bitmap.bmPlanes != 1) return FALSE;
145 /* check if bpp is compatible with screen depth */
146 if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
148 WARN("Trying to make bitmap with planes=%d, bpp=%d\n",
149 bitmap.bmPlanes, bitmap.bmBitsPixel);
152 if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
154 ERR( "called for stock bitmap, please report\n" );
158 TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
160 if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
162 if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, bitmap.bmBitsPixel, NULL ))
164 if(bitmap.bmBitsPixel == 1)
166 physBitmap->pixmap_depth = 1;
167 physBitmap->trueColor = FALSE;
171 physBitmap->pixmap_depth = screen_depth;
172 physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
173 physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
178 /* Create the pixmap */
179 physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
180 bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
182 if (!physBitmap->pixmap)
184 WARN("Can't create Pixmap\n");
185 HeapFree( GetProcessHeap(), 0, physBitmap );
189 if (bmBits) /* Set bitmap bits */
191 X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
193 else /* else clear the bitmap */
195 GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
197 XSetFunction( gdi_display, gc, GXclear );
198 XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
199 bitmap.bmWidth, bitmap.bmHeight );
200 XSetFunction( gdi_display, gc, GXcopy );
207 /******************************************************************************
208 * SetBitmapBits (X11DRV.@)
211 * Success: Number of bytes used in setting the bitmap bits
214 LONG X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
217 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
220 const BYTE *sbuf, *startline;
223 if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
225 TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
227 height = count / bitmap.bmWidthBytes;
230 image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
231 bitmap.bmWidth, height, 32, 0 );
232 if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
234 WARN("No memory to create image data.\n");
235 XDestroyImage( image );
240 /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
244 switch (bitmap.bmBitsPixel)
247 for (h=0;h<height;h++)
250 for (w=0;w<bitmap.bmWidth;w++)
252 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
256 startline += bitmap.bmWidthBytes;
260 for (h=0;h<height;h++)
263 for (w=0;w<bitmap.bmWidth;w++)
265 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
266 else XPutPixel( image, w, h, *sbuf++ & 0xf );
268 startline += bitmap.bmWidthBytes;
272 for (h=0;h<height;h++)
275 for (w=0;w<bitmap.bmWidth;w++)
276 XPutPixel(image,w,h,*sbuf++);
277 startline += bitmap.bmWidthBytes;
282 for (h=0;h<height;h++)
285 for (w=0;w<bitmap.bmWidth;w++)
287 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
290 startline += bitmap.bmWidthBytes;
294 for (h=0;h<height;h++)
297 for (w=0;w<bitmap.bmWidth;w++)
299 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
302 startline += bitmap.bmWidthBytes;
306 for (h=0;h<height;h++)
309 for (w=0;w<bitmap.bmWidth;w++)
311 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
314 startline += bitmap.bmWidthBytes;
318 FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
321 XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
322 image, 0, 0, 0, 0, bitmap.bmWidth, height );
323 HeapFree( GetProcessHeap(), 0, image->data );
325 XDestroyImage( image );
330 /***********************************************************************
331 * DeleteBitmap (X11DRV.@)
333 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
335 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
341 if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
342 X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
344 if (physBitmap->glxpixmap)
345 destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
347 if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
348 XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
350 HeapFree( GetProcessHeap(), 0, physBitmap );
356 /***********************************************************************
357 * X11DRV_get_phys_bitmap
359 * Retrieve the X physical bitmap info.
361 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
366 if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
372 /***********************************************************************
373 * X11DRV_init_phys_bitmap
375 * Initialize the X physical bitmap info.
377 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
381 if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
383 ret->hbitmap = hbitmap;
385 XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
392 /***********************************************************************
395 * Retrieve the pixmap associated to a bitmap.
397 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
399 X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
401 if (!physBitmap) return 0;
402 return physBitmap->pixmap;