gdi32: Allow a driver to implement SelectBitmap but not CreateBitmap.
[wine] / dlls / winex11.drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32
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 */
36
37 static XContext bitmap_context;  /* X context to associate a phys bitmap to a handle */
38
39 GC get_bitmap_gc(int depth)
40 {
41     if(depth < 1 || depth > 32)
42         return 0;
43
44     return bitmap_gc[depth-1];
45 }
46
47 /***********************************************************************
48  *           X11DRV_BITMAP_Init
49  */
50 void X11DRV_BITMAP_Init(void)
51 {
52     int depth_count, index, i;
53     int *depth_list;
54     Pixmap tmpPixmap;
55
56     wine_tsx11_lock();
57     bitmap_context = XUniqueContext();
58     BITMAP_stock_phys_bitmap.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 );
63
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.
66      */
67     depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68     for (i = 0; i < depth_count; i++)
69     {
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])))
73         {
74             if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
75             {
76                 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77                 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
78             }
79             XFreePixmap( gdi_display, tmpPixmap );
80         }
81     }
82     XFree( depth_list );
83
84     wine_tsx11_unlock();
85 }
86
87 /***********************************************************************
88  *           SelectBitmap   (X11DRV.@)
89  */
90 HBITMAP X11DRV_SelectBitmap( PHYSDEV dev, HBITMAP hbitmap )
91 {
92     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
93     X_PHYSBITMAP *physBitmap;
94     BITMAP bitmap;
95
96     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
97
98     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
99     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
100
101     physDev->bitmap = physBitmap;
102     physDev->drawable = physBitmap->pixmap;
103     physDev->gl_drawable = physBitmap->glxpixmap;
104     physDev->color_shifts = physBitmap->trueColor ? &physBitmap->color_shifts : NULL;
105     SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
106     physDev->dc_rect = physDev->drawable_rect;
107
108       /* Change GC depth if needed */
109
110     if (physDev->depth != physBitmap->depth)
111     {
112         physDev->depth = physBitmap->depth;
113         wine_tsx11_lock();
114         XFreeGC( gdi_display, physDev->gc );
115         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
116         XSetGraphicsExposures( gdi_display, physDev->gc, False );
117         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
118         XFlush( gdi_display );
119         wine_tsx11_unlock();
120     }
121
122     return hbitmap;
123 }
124
125
126 /***********************************************************************
127  *           X11DRV_create_phys_bitmap
128  */
129 X_PHYSBITMAP *X11DRV_create_phys_bitmap( HBITMAP hbitmap, const BITMAP *bitmap, int depth )
130 {
131     X_PHYSBITMAP *physBitmap;
132
133     if (bitmap->bmWidth > 65535 || bitmap->bmHeight > 65535 || bitmap->bmPlanes != 1 ||
134         (bitmap->bmBitsPixel != 1 && bitmap->bmBitsPixel != screen_bpp))
135     {
136         WARN( "Trying to create invalid pixmap %dx%d planes %d bpp %d\n",
137               bitmap->bmWidth, bitmap->bmHeight, bitmap->bmPlanes, bitmap->bmBitsPixel );
138         return NULL;
139     }
140
141     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return NULL;
142
143     physBitmap->depth = depth;
144
145     wine_tsx11_lock();
146     physBitmap->pixmap = XCreatePixmap( gdi_display, root_window,
147                                         bitmap->bmWidth, bitmap->bmHeight, physBitmap->depth );
148     if (physBitmap->pixmap)
149     {
150         GC gc = get_bitmap_gc( depth );
151         XSetFunction( gdi_display, gc, GXclear );
152         XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0, bitmap->bmWidth, bitmap->bmHeight );
153         XSetFunction( gdi_display, gc, GXcopy );
154     }
155     wine_tsx11_unlock();
156     if (!physBitmap->pixmap)
157     {
158         WARN("Can't create Pixmap\n");
159         HeapFree( GetProcessHeap(), 0, physBitmap );
160         return NULL;
161     }
162     TRACE( "(%p) %dx%d %d bpp -> %lx\n",
163            hbitmap, bitmap->bmWidth, bitmap->bmHeight, bitmap->bmBitsPixel, physBitmap->pixmap );
164     return physBitmap;
165 }
166
167
168 /****************************************************************************
169  *        CreateBitmap   (X11DRV.@)
170  *
171  * Create a device dependent X11 bitmap
172  *
173  * Returns TRUE on success else FALSE
174  */
175 BOOL X11DRV_CreateBitmap( PHYSDEV dev, HBITMAP hbitmap )
176 {
177     BITMAP bitmap;
178     X_PHYSBITMAP *phys_bitmap;
179
180     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
181
182     if (bitmap.bmBitsPixel == 1)
183     {
184         if (!(phys_bitmap = X11DRV_create_phys_bitmap( hbitmap, &bitmap, 1 ))) return FALSE;
185         phys_bitmap->trueColor = FALSE;
186     }
187     else
188     {
189         if (!(phys_bitmap = X11DRV_create_phys_bitmap( hbitmap, &bitmap, screen_depth ))) return FALSE;
190         phys_bitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
191         phys_bitmap->color_shifts = X11DRV_PALETTE_default_shifts;
192     }
193     return TRUE;
194 }
195
196
197 /****************************************************************************
198  *        CopyBitmap   (X11DRV.@)
199  */
200 BOOL X11DRV_CopyBitmap( HBITMAP src, HBITMAP dst )
201 {
202     X_PHYSBITMAP *phys_src, *phys_dst;
203     BITMAP bitmap;
204
205     if (!(phys_src = X11DRV_get_phys_bitmap( src ))) return FALSE;
206     if (!GetObjectW( dst, sizeof(bitmap), &bitmap )) return FALSE;
207
208     TRACE("%p->%p %dx%d %d bpp\n", src, dst, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
209
210     if (!(phys_dst = X11DRV_init_phys_bitmap( dst ))) return FALSE;
211
212     phys_dst->depth = phys_src->depth;
213     phys_dst->format = phys_src->format;
214     phys_dst->trueColor = phys_src->trueColor;
215     if (phys_dst->trueColor) phys_dst->color_shifts = phys_src->color_shifts;
216
217     wine_tsx11_lock();
218     phys_dst->pixmap = XCreatePixmap( gdi_display, root_window,
219                                       bitmap.bmWidth, bitmap.bmHeight, phys_dst->depth );
220     XCopyArea( gdi_display, phys_src->pixmap, phys_dst->pixmap,
221                get_bitmap_gc(phys_dst->depth), 0, 0, bitmap.bmWidth, bitmap.bmHeight, 0, 0 );
222     wine_tsx11_unlock();
223
224     if (!phys_dst->pixmap)
225     {
226         WARN("Can't create Pixmap\n");
227         HeapFree( GetProcessHeap(), 0, phys_dst );
228         return FALSE;
229     }
230     return TRUE;
231 }
232
233
234 /***********************************************************************
235  *           DeleteBitmap   (X11DRV.@)
236  */
237 BOOL X11DRV_DeleteBitmap( HBITMAP hbitmap )
238 {
239     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
240
241     if (physBitmap)
242     {
243         if (physBitmap->glxpixmap)
244             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
245         wine_tsx11_lock();
246         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
247         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
248         wine_tsx11_unlock();
249         HeapFree( GetProcessHeap(), 0, physBitmap );
250     }
251     return TRUE;
252 }
253
254
255 /***********************************************************************
256  *           X11DRV_get_phys_bitmap
257  *
258  * Retrieve the X physical bitmap info.
259  */
260 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
261 {
262     X_PHYSBITMAP *ret;
263
264     wine_tsx11_lock();
265     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
266     wine_tsx11_unlock();
267     return ret;
268 }
269
270
271 /***********************************************************************
272  *           X11DRV_init_phys_bitmap
273  *
274  * Initialize the X physical bitmap info.
275  */
276 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
277 {
278     X_PHYSBITMAP *ret;
279
280     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
281     {
282         ret->hbitmap = hbitmap;
283         wine_tsx11_lock();
284         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
285         wine_tsx11_unlock();
286     }
287     return ret;
288 }
289
290
291 /***********************************************************************
292  *           X11DRV_get_pixmap
293  *
294  * Retrieve the pixmap associated to a bitmap.
295  */
296 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
297 {
298     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
299
300     if (!physBitmap) return 0;
301     return physBitmap->pixmap;
302 }