1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 #define WGL_WGLEXT_PROTOTYPES
33 #include <GL/wglext.h>
35 #include "pipe/p_compiler.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_state.h"
38 #include "util/u_memory.h"
39 #include "util/u_atomic.h"
40 #include "state_tracker/st_api.h"
41 #include "hud/hud_context.h"
44 #include "stw_device.h"
45 #include "stw_winsys.h"
46 #include "stw_framebuffer.h"
47 #include "stw_pixelformat.h"
48 #include "stw_context.h"
53 stw_current_context(void)
55 struct st_context_iface
*st
;
57 st
= (stw_dev
) ? stw_dev
->stapi
->get_current(stw_dev
->stapi
) : NULL
;
59 return (struct stw_context
*) ((st
) ? st
->st_manager_private
: NULL
);
64 DrvCopyContext(DHGLRC dhrcSource
, DHGLRC dhrcDest
, UINT fuMask
)
66 struct stw_context
*src
;
67 struct stw_context
*dst
;
73 stw_lock_contexts(stw_dev
);
75 src
= stw_lookup_context_locked( dhrcSource
);
76 dst
= stw_lookup_context_locked( dhrcDest
);
86 stw_unlock_contexts(stw_dev
);
93 DrvShareLists(DHGLRC dhglrc1
, DHGLRC dhglrc2
)
95 struct stw_context
*ctx1
;
96 struct stw_context
*ctx2
;
102 stw_lock_contexts(stw_dev
);
104 ctx1
= stw_lookup_context_locked( dhglrc1
);
105 ctx2
= stw_lookup_context_locked( dhglrc2
);
107 if (ctx1
&& ctx2
&& ctx2
->st
->share
)
108 ret
= ctx2
->st
->share(ctx2
->st
, ctx1
->st
);
110 stw_unlock_contexts(stw_dev
);
117 DrvCreateContext(HDC hdc
)
119 return DrvCreateLayerContext( hdc
, 0 );
124 DrvCreateLayerContext(HDC hdc
, INT iLayerPlane
)
126 return stw_create_context_attribs(hdc
, iLayerPlane
, 0, 1, 0, 0,
127 WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
,
133 * Called via DrvCreateContext(), DrvCreateLayerContext() and
134 * wglCreateContextAttribsARB() to actually create a rendering context.
135 * \param handle the desired DHGLRC handle to use for the context, or zero
136 * if a new handle should be allocated.
137 * \return the handle for the new context or zero if there was a problem.
140 stw_create_context_attribs(HDC hdc
, INT iLayerPlane
, DHGLRC hShareContext
,
141 int majorVersion
, int minorVersion
,
142 int contextFlags
, int profileMask
,
146 struct stw_framebuffer
*fb
;
147 const struct stw_pixelformat_info
*pfi
;
148 struct st_context_attribs attribs
;
149 struct stw_context
*ctx
= NULL
;
150 struct stw_context
*shareCtx
= NULL
;
151 enum st_context_error ctx_err
= 0;
156 if (iLayerPlane
!= 0)
160 * GDI only knows about displayable pixel formats, so determine the pixel
161 * format from the framebuffer.
163 * This also allows to use a OpenGL DLL / ICD without installing.
165 fb
= stw_framebuffer_from_hdc( hdc
);
167 iPixelFormat
= fb
->iPixelFormat
;
168 stw_framebuffer_unlock(fb
);
173 pfi
= stw_pixelformat_get_info( iPixelFormat
);
175 if (hShareContext
!= 0) {
176 stw_lock_contexts(stw_dev
);
177 shareCtx
= stw_lookup_context_locked( hShareContext
);
178 stw_unlock_contexts(stw_dev
);
181 ctx
= CALLOC_STRUCT( stw_context
);
186 ctx
->iPixelFormat
= iPixelFormat
;
188 memset(&attribs
, 0, sizeof(attribs
));
189 attribs
.visual
= pfi
->stvis
;
190 attribs
.major
= majorVersion
;
191 attribs
.minor
= minorVersion
;
192 if (contextFlags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
193 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
194 if (contextFlags
& WGL_CONTEXT_DEBUG_BIT_ARB
)
195 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
197 switch (profileMask
) {
198 case WGL_CONTEXT_CORE_PROFILE_BIT_ARB
:
199 /* There are no profiles before OpenGL 3.2. The
200 * WGL_ARB_create_context_profile spec says:
202 * "If the requested OpenGL version is less than 3.2,
203 * WGL_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
204 * of the context is determined solely by the requested version."
206 if (majorVersion
> 3 || (majorVersion
== 3 && minorVersion
>= 2)) {
207 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
211 case WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
213 * The spec also says:
215 * "If version 3.1 is requested, the context returned may implement
216 * any of the following versions:
218 * * Version 3.1. The GL_ARB_compatibility extension may or may not
219 * be implemented, as determined by the implementation.
220 * * The core profile of version 3.2 or greater."
222 * But Mesa doesn't support GL_ARB_compatibility, while most prevalent
223 * Windows OpenGL implementations do, and unfortunately many Windows
224 * applications don't check whether they receive or not a context with
225 * GL_ARB_compatibility, so returning a core profile here does more harm
228 attribs
.profile
= ST_PROFILE_DEFAULT
;
230 case WGL_CONTEXT_ES_PROFILE_BIT_EXT
:
231 if (majorVersion
>= 2) {
232 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
234 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
242 ctx
->st
= stw_dev
->stapi
->create_context(stw_dev
->stapi
,
243 stw_dev
->smapi
, &attribs
, &ctx_err
, shareCtx
? shareCtx
->st
: NULL
);
247 ctx
->st
->st_manager_private
= (void *) ctx
;
249 if (ctx
->st
->cso_context
) {
250 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
253 stw_lock_contexts(stw_dev
);
255 /* We're replacing the context data for this handle. See the
256 * wglCreateContextAttribsARB() function.
258 struct stw_context
*old_ctx
=
259 stw_lookup_context_locked((unsigned) handle
);
261 /* free the old context data associated with this handle */
263 hud_destroy(old_ctx
->hud
);
265 ctx
->st
->destroy(old_ctx
->st
);
269 /* replace table entry */
270 handle_table_set(stw_dev
->ctx_table
, (unsigned) handle
, ctx
);
273 /* create new table entry */
274 handle
= (DHGLRC
) handle_table_add(stw_dev
->ctx_table
, ctx
);
277 ctx
->dhglrc
= handle
;
279 stw_unlock_contexts(stw_dev
);
288 hud_destroy(ctx
->hud
);
290 ctx
->st
->destroy(ctx
->st
);
299 DrvDeleteContext(DHGLRC dhglrc
)
301 struct stw_context
*ctx
;
307 stw_lock_contexts(stw_dev
);
308 ctx
= stw_lookup_context_locked(dhglrc
);
309 handle_table_remove(stw_dev
->ctx_table
, dhglrc
);
310 stw_unlock_contexts(stw_dev
);
313 struct stw_context
*curctx
= stw_current_context();
315 /* Unbind current if deleting current context. */
317 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
320 hud_destroy(ctx
->hud
);
323 ctx
->st
->destroy(ctx
->st
);
334 DrvReleaseContext(DHGLRC dhglrc
)
336 struct stw_context
*ctx
;
341 stw_lock_contexts(stw_dev
);
342 ctx
= stw_lookup_context_locked( dhglrc
);
343 stw_unlock_contexts(stw_dev
);
348 /* The expectation is that ctx is the same context which is
349 * current for this thread. We should check that and return False
352 if (ctx
!= stw_current_context())
355 if (stw_make_current( NULL
, 0 ) == FALSE
)
363 stw_get_current_context( void )
365 struct stw_context
*ctx
;
367 ctx
= stw_current_context();
376 stw_get_current_dc( void )
378 struct stw_context
*ctx
;
380 ctx
= stw_current_context();
389 stw_make_current(HDC hdc
, DHGLRC dhglrc
)
391 struct stw_context
*curctx
= NULL
;
392 struct stw_context
*ctx
= NULL
;
393 struct stw_framebuffer
*fb
= NULL
;
399 curctx
= stw_current_context();
400 if (curctx
!= NULL
) {
401 if (curctx
->dhglrc
== dhglrc
) {
402 if (curctx
->hdc
== hdc
) {
403 /* Return if already current. */
407 curctx
->st
->flush(curctx
->st
, ST_FLUSH_FRONT
, NULL
);
412 stw_lock_contexts(stw_dev
);
413 ctx
= stw_lookup_context_locked( dhglrc
);
414 stw_unlock_contexts(stw_dev
);
419 fb
= stw_framebuffer_from_hdc( hdc
);
421 stw_framebuffer_update(fb
);
424 /* Applications should call SetPixelFormat before creating a context,
425 * but not all do, and the opengl32 runtime seems to use a default
426 * pixel format in some cases, so we must create a framebuffer for
429 int iPixelFormat
= GetPixelFormat(hdc
);
431 fb
= stw_framebuffer_create( hdc
, iPixelFormat
);
436 if (fb
->iPixelFormat
!= ctx
->iPixelFormat
) {
437 SetLastError(ERROR_INVALID_PIXEL_FORMAT
);
441 /* Bind the new framebuffer */
444 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, ctx
->st
,
446 stw_framebuffer_reference(&ctx
->current_framebuffer
, fb
);
448 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
454 stw_framebuffer_unlock(fb
);
457 /* On failure, make the thread's current rendering context not current
461 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
465 /* Unreference the previous framebuffer if any. It must be done after
466 * make_current, as it can be referenced inside.
468 if (curctx
&& curctx
!= ctx
) {
469 stw_framebuffer_reference(&curctx
->current_framebuffer
, NULL
);
477 * Notify the current context that the framebuffer has become invalid.
480 stw_notify_current_locked( struct stw_framebuffer
*fb
)
482 p_atomic_inc(&fb
->stfb
->stamp
);
487 * Although WGL allows different dispatch entrypoints per context
489 static const GLCLTPROCTABLE cpt
=
491 OPENGL_VERSION_110_ENTRIES
,
767 &glGetPolygonStipple
,
775 &glGetTexParameterfv
,
776 &glGetTexParameteriv
,
777 &glGetTexLevelParameterfv
,
778 &glGetTexLevelParameteriv
,
802 &glDisableClientState
,
806 &glEnableClientState
,
810 &glInterleavedArrays
,
815 &glAreTexturesResident
,
818 &glCopyTexSubImage1D
,
819 &glCopyTexSubImage2D
,
824 &glPrioritizeTextures
,
833 PGLCLTPROCTABLE APIENTRY
834 DrvSetContext(HDC hdc
, DHGLRC dhglrc
, PFN_SETPROCTABLE pfnSetProcTable
)
836 PGLCLTPROCTABLE r
= (PGLCLTPROCTABLE
)&cpt
;
838 if (!stw_make_current(hdc
, dhglrc
))