dxgi/tests: Add test for swapchain window messages.
[wine.git] / dlls / gdi32 / dibdrv / opengl.c
blobdba5db03565e0d9c8a04d2b6df5c8d8b3fcc420c
1 /*
2 * DIB driver OpenGL support
4 * Copyright 2012 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "gdi_private.h"
25 #include "dibdrv.h"
27 #include "wine/library.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dib);
32 #ifdef SONAME_LIBOSMESA
34 #include "wine/wgl.h"
35 #include "wine/wgl_driver.h"
37 #define OSMESA_COLOR_INDEX GL_COLOR_INDEX
38 #define OSMESA_RGBA GL_RGBA
39 #define OSMESA_BGRA 0x1
40 #define OSMESA_ARGB 0x2
41 #define OSMESA_RGB GL_RGB
42 #define OSMESA_BGR 0x4
43 #define OSMESA_RGB_565 0x5
44 #define OSMESA_ROW_LENGTH 0x10
45 #define OSMESA_Y_UP 0x11
47 typedef struct osmesa_context *OSMesaContext;
49 extern BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT fmt, const PIXELFORMATDESCRIPTOR *pfd );
51 struct wgl_context
53 OSMesaContext context;
54 int format;
57 static struct opengl_funcs opengl_funcs;
59 #define USE_GL_FUNC(name) #name,
60 static const char *opengl_func_names[] = { ALL_WGL_FUNCS };
61 #undef USE_GL_FUNC
63 static OSMesaContext (*pOSMesaCreateContextExt)( GLenum format, GLint depthBits, GLint stencilBits,
64 GLint accumBits, OSMesaContext sharelist );
65 static void (*pOSMesaDestroyContext)( OSMesaContext ctx );
66 static void * (*pOSMesaGetProcAddress)( const char *funcName );
67 static GLboolean (*pOSMesaMakeCurrent)( OSMesaContext ctx, void *buffer, GLenum type,
68 GLsizei width, GLsizei height );
69 static void (*pOSMesaPixelStore)( GLint pname, GLint value );
71 static const struct
73 UINT mesa;
74 BYTE color_bits;
75 BYTE red_bits, red_shift;
76 BYTE green_bits, green_shift;
77 BYTE blue_bits, blue_shift;
78 BYTE alpha_bits, alpha_shift;
79 BYTE accum_bits;
80 BYTE depth_bits;
81 BYTE stencil_bits;
82 } pixel_formats[] =
84 { OSMESA_BGRA, 32, 8, 16, 8, 8, 8, 0, 8, 24, 16, 32, 8 },
85 { OSMESA_BGRA, 32, 8, 16, 8, 8, 8, 0, 8, 24, 16, 16, 8 },
86 { OSMESA_RGBA, 32, 8, 0, 8, 8, 8, 16, 8, 24, 16, 32, 8 },
87 { OSMESA_RGBA, 32, 8, 0, 8, 8, 8, 16, 8, 24, 16, 16, 8 },
88 { OSMESA_ARGB, 32, 8, 8, 8, 16, 8, 24, 8, 0, 16, 32, 8 },
89 { OSMESA_ARGB, 32, 8, 8, 8, 16, 8, 24, 8, 0, 16, 16, 8 },
90 { OSMESA_RGB, 24, 8, 0, 8, 8, 8, 16, 0, 0, 16, 32, 8 },
91 { OSMESA_RGB, 24, 8, 0, 8, 8, 8, 16, 0, 0, 16, 16, 8 },
92 { OSMESA_BGR, 24, 8, 16, 8, 8, 8, 0, 0, 0, 16, 32, 8 },
93 { OSMESA_BGR, 24, 8, 16, 8, 8, 8, 0, 0, 0, 16, 16, 8 },
94 { OSMESA_RGB_565, 16, 5, 0, 6, 5, 5, 11, 0, 0, 16, 32, 8 },
95 { OSMESA_RGB_565, 16, 5, 0, 6, 5, 5, 11, 0, 0, 16, 16, 8 },
97 static const int nb_formats = sizeof(pixel_formats) / sizeof(pixel_formats[0]);
99 static BOOL init_opengl(void)
101 static BOOL init_done = FALSE;
102 static void *osmesa_handle;
103 char buffer[200];
104 unsigned int i;
106 if (init_done) return (osmesa_handle != NULL);
107 init_done = TRUE;
109 osmesa_handle = wine_dlopen( SONAME_LIBOSMESA, RTLD_NOW, buffer, sizeof(buffer) );
110 if (osmesa_handle == NULL)
112 ERR( "Failed to load OSMesa: %s\n", buffer );
113 return FALSE;
116 #define LOAD_FUNCPTR(f) do if (!(p##f = wine_dlsym( osmesa_handle, #f, buffer, sizeof(buffer) ))) \
118 ERR( "%s not found in %s (%s), disabling.\n", #f, SONAME_LIBOSMESA, buffer ); \
119 goto failed; \
120 } while(0)
122 LOAD_FUNCPTR(OSMesaCreateContextExt);
123 LOAD_FUNCPTR(OSMesaDestroyContext);
124 LOAD_FUNCPTR(OSMesaGetProcAddress);
125 LOAD_FUNCPTR(OSMesaMakeCurrent);
126 LOAD_FUNCPTR(OSMesaPixelStore);
127 #undef LOAD_FUNCPTR
129 for (i = 0; i < sizeof(opengl_func_names)/sizeof(opengl_func_names[0]); i++)
131 if (!(((void **)&opengl_funcs.gl)[i] = pOSMesaGetProcAddress( opengl_func_names[i] )))
133 ERR( "%s not found in %s, disabling.\n", opengl_func_names[i], SONAME_LIBOSMESA );
134 goto failed;
138 return TRUE;
140 failed:
141 wine_dlclose( osmesa_handle, NULL, 0 );
142 osmesa_handle = NULL;
143 return FALSE;
146 /**********************************************************************
147 * dibdrv_wglDescribePixelFormat
149 static int dibdrv_wglDescribePixelFormat( HDC hdc, int fmt, UINT size, PIXELFORMATDESCRIPTOR *descr )
151 int ret = sizeof(pixel_formats) / sizeof(pixel_formats[0]);
153 if (!descr) return ret;
154 if (fmt <= 0 || fmt > ret) return 0;
155 if (size < sizeof(*descr)) return 0;
157 memset( descr, 0, sizeof(*descr) );
158 descr->nSize = sizeof(*descr);
159 descr->nVersion = 1;
160 descr->dwFlags = PFD_SUPPORT_GDI | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_BITMAP | PFD_GENERIC_FORMAT;
161 descr->iPixelType = PFD_TYPE_RGBA;
162 descr->cColorBits = pixel_formats[fmt - 1].color_bits;
163 descr->cRedBits = pixel_formats[fmt - 1].red_bits;
164 descr->cRedShift = pixel_formats[fmt - 1].red_shift;
165 descr->cGreenBits = pixel_formats[fmt - 1].green_bits;
166 descr->cGreenShift = pixel_formats[fmt - 1].green_shift;
167 descr->cBlueBits = pixel_formats[fmt - 1].blue_bits;
168 descr->cBlueShift = pixel_formats[fmt - 1].blue_shift;
169 descr->cAlphaBits = pixel_formats[fmt - 1].alpha_bits;
170 descr->cAlphaShift = pixel_formats[fmt - 1].alpha_shift;
171 descr->cAccumBits = pixel_formats[fmt - 1].accum_bits;
172 descr->cAccumRedBits = pixel_formats[fmt - 1].accum_bits / 4;
173 descr->cAccumGreenBits = pixel_formats[fmt - 1].accum_bits / 4;
174 descr->cAccumBlueBits = pixel_formats[fmt - 1].accum_bits / 4;
175 descr->cAccumAlphaBits = pixel_formats[fmt - 1].accum_bits / 4;
176 descr->cDepthBits = pixel_formats[fmt - 1].depth_bits;
177 descr->cStencilBits = pixel_formats[fmt - 1].stencil_bits;
178 descr->cAuxBuffers = 0;
179 descr->iLayerType = PFD_MAIN_PLANE;
180 return ret;
183 /***********************************************************************
184 * dibdrv_wglCopyContext
186 static BOOL dibdrv_wglCopyContext( struct wgl_context *src, struct wgl_context *dst, UINT mask )
188 FIXME( "not supported yet\n" );
189 return FALSE;
192 /***********************************************************************
193 * dibdrv_wglCreateContext
195 static struct wgl_context *dibdrv_wglCreateContext( HDC hdc )
197 struct wgl_context *context;
199 if (!(context = HeapAlloc( GetProcessHeap(), 0, sizeof( *context )))) return NULL;
200 context->format = GetPixelFormat( hdc );
201 if (!context->format || context->format > nb_formats) context->format = 1;
203 if (!(context->context = pOSMesaCreateContextExt( pixel_formats[context->format - 1].mesa,
204 pixel_formats[context->format - 1].depth_bits,
205 pixel_formats[context->format - 1].stencil_bits,
206 pixel_formats[context->format - 1].accum_bits, 0 )))
208 HeapFree( GetProcessHeap(), 0, context );
209 return NULL;
211 return context;
214 /***********************************************************************
215 * dibdrv_wglDeleteContext
217 static BOOL dibdrv_wglDeleteContext( struct wgl_context *context )
219 pOSMesaDestroyContext( context->context );
220 HeapFree( GetProcessHeap(), 0, context );
221 return TRUE;
224 /***********************************************************************
225 * dibdrv_wglGetPixelFormat
227 static int dibdrv_wglGetPixelFormat( HDC hdc )
229 DC *dc = get_dc_ptr( hdc );
230 int ret = 0;
232 if (dc)
234 ret = dc->pixel_format;
235 release_dc_ptr( dc );
237 return ret;
240 /***********************************************************************
241 * dibdrv_wglGetProcAddress
243 static PROC dibdrv_wglGetProcAddress( const char *proc )
245 if (!strncmp( proc, "wgl", 3 )) return NULL;
246 return (PROC)pOSMesaGetProcAddress( proc );
249 /***********************************************************************
250 * dibdrv_wglMakeCurrent
252 static BOOL dibdrv_wglMakeCurrent( HDC hdc, struct wgl_context *context )
254 HBITMAP bitmap;
255 BITMAPOBJ *bmp;
256 dib_info dib;
257 GLenum type;
258 BOOL ret = FALSE;
260 if (!context)
262 pOSMesaMakeCurrent( NULL, NULL, GL_UNSIGNED_BYTE, 0, 0 );
263 return TRUE;
266 if (GetPixelFormat( hdc ) != context->format)
267 FIXME( "mismatched pixel formats %u/%u not supported yet\n",
268 GetPixelFormat( hdc ), context->format );
270 bitmap = GetCurrentObject( hdc, OBJ_BITMAP );
271 bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
272 if (!bmp) return FALSE;
274 if (init_dib_info_from_bitmapobj( &dib, bmp ))
276 char *bits;
277 int width = dib.rect.right - dib.rect.left;
278 int height = dib.rect.bottom - dib.rect.top;
280 if (dib.stride < 0)
281 bits = (char *)dib.bits.ptr + (dib.rect.bottom - 1) * dib.stride;
282 else
283 bits = (char *)dib.bits.ptr + dib.rect.top * dib.stride;
284 bits += dib.rect.left * dib.bit_count / 8;
286 TRACE( "context %p bits %p size %ux%u\n", context, bits, width, height );
288 if (pixel_formats[context->format - 1].mesa == OSMESA_RGB_565)
289 type = GL_UNSIGNED_SHORT_5_6_5;
290 else
291 type = GL_UNSIGNED_BYTE;
293 ret = pOSMesaMakeCurrent( context->context, bits, type, width, height );
294 if (ret)
296 pOSMesaPixelStore( OSMESA_ROW_LENGTH, abs( dib.stride ) * 8 / dib.bit_count );
297 pOSMesaPixelStore( OSMESA_Y_UP, 1 ); /* Windows seems to assume bottom-up */
300 GDI_ReleaseObj( bitmap );
301 return ret;
304 /**********************************************************************
305 * dibdrv_wglSetPixelFormat
307 static BOOL dibdrv_wglSetPixelFormat( HDC hdc, int fmt, const PIXELFORMATDESCRIPTOR *descr )
309 if (fmt <= 0 || fmt > nb_formats) return FALSE;
310 return GdiSetPixelFormat( hdc, fmt, descr );
313 /***********************************************************************
314 * dibdrv_wglShareLists
316 static BOOL dibdrv_wglShareLists( struct wgl_context *org, struct wgl_context *dest )
318 FIXME( "not supported yet\n" );
319 return FALSE;
322 /***********************************************************************
323 * dibdrv_wglSwapBuffers
325 static BOOL dibdrv_wglSwapBuffers( HDC hdc )
327 return TRUE;
330 static struct opengl_funcs opengl_funcs =
333 dibdrv_wglCopyContext, /* p_wglCopyContext */
334 dibdrv_wglCreateContext, /* p_wglCreateContext */
335 dibdrv_wglDeleteContext, /* p_wglDeleteContext */
336 dibdrv_wglDescribePixelFormat,/* p_wglDescribePixelFormat */
337 dibdrv_wglGetPixelFormat, /* p_wglGetPixelFormat */
338 dibdrv_wglGetProcAddress, /* p_wglGetProcAddress */
339 dibdrv_wglMakeCurrent, /* p_wglMakeCurrent */
340 dibdrv_wglSetPixelFormat, /* p_wglSetPixelFormat */
341 dibdrv_wglShareLists, /* p_wglShareLists */
342 dibdrv_wglSwapBuffers, /* p_wglSwapBuffers */
346 /**********************************************************************
347 * dibdrv_wine_get_wgl_driver
349 struct opengl_funcs *dibdrv_wine_get_wgl_driver( PHYSDEV dev, UINT version )
351 if (version != WINE_WGL_DRIVER_VERSION)
353 ERR( "version mismatch, opengl32 wants %u but dibdrv has %u\n", version, WINE_WGL_DRIVER_VERSION );
354 return NULL;
357 if (!init_opengl()) return (void *)-1;
359 return &opengl_funcs;
362 #else /* SONAME_LIBOSMESA */
364 /**********************************************************************
365 * dibdrv_wine_get_wgl_driver
367 struct opengl_funcs *dibdrv_wine_get_wgl_driver( PHYSDEV dev, UINT version )
369 static int warned;
370 if (!warned++) ERR( "OSMesa not compiled in, no OpenGL bitmap support\n" );
371 return (void *)-1;
374 #endif /* SONAME_LIBOSMESA */