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
);
68 struct stw_context
*src
;
69 struct stw_context
*dst
;
75 pipe_mutex_lock( stw_dev
->ctx_mutex
);
77 src
= stw_lookup_context_locked( dhrcSource
);
78 dst
= stw_lookup_context_locked( dhrcDest
);
88 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
98 struct stw_context
*ctx1
;
99 struct stw_context
*ctx2
;
105 pipe_mutex_lock( stw_dev
->ctx_mutex
);
107 ctx1
= stw_lookup_context_locked( dhglrc1
);
108 ctx2
= stw_lookup_context_locked( dhglrc2
);
110 if (ctx1
&& ctx2
&& ctx2
->st
->share
)
111 ret
= ctx2
->st
->share(ctx2
->st
, ctx1
->st
);
113 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
122 return DrvCreateLayerContext( hdc
, 0 );
126 DrvCreateLayerContext(
130 return stw_create_context_attribs(hdc
, iLayerPlane
, 0, 1, 0, 0,
131 WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
,
137 * Called via DrvCreateContext(), DrvCreateLayerContext() and
138 * wglCreateContextAttribsARB() to actually create a rendering context.
139 * \param handle the desired DHGLRC handle to use for the context, or zero
140 * if a new handle should be allocated.
141 * \return the handle for the new context or zero if there was a problem.
144 stw_create_context_attribs(HDC hdc
, INT iLayerPlane
, DHGLRC hShareContext
,
145 int majorVersion
, int minorVersion
,
146 int contextFlags
, int profileMask
,
150 struct stw_framebuffer
*fb
;
151 const struct stw_pixelformat_info
*pfi
;
152 struct st_context_attribs attribs
;
153 struct stw_context
*ctx
= NULL
;
154 struct stw_context
*shareCtx
= NULL
;
155 enum st_context_error ctx_err
= 0;
160 if (iLayerPlane
!= 0)
164 * GDI only knows about displayable pixel formats, so determine the pixel
165 * format from the framebuffer.
167 * This also allows to use a OpenGL DLL / ICD without installing.
169 fb
= stw_framebuffer_from_hdc( hdc
);
171 iPixelFormat
= fb
->iPixelFormat
;
172 stw_framebuffer_release(fb
);
177 pfi
= stw_pixelformat_get_info( iPixelFormat
);
179 if (hShareContext
!= 0) {
180 pipe_mutex_lock( stw_dev
->ctx_mutex
);
181 shareCtx
= stw_lookup_context_locked( hShareContext
);
182 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
185 ctx
= CALLOC_STRUCT( stw_context
);
190 ctx
->iPixelFormat
= iPixelFormat
;
192 memset(&attribs
, 0, sizeof(attribs
));
193 attribs
.visual
= pfi
->stvis
;
194 attribs
.major
= majorVersion
;
195 attribs
.minor
= minorVersion
;
196 if (contextFlags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
197 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
198 if (contextFlags
& WGL_CONTEXT_DEBUG_BIT_ARB
)
199 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
201 switch (profileMask
) {
202 case WGL_CONTEXT_CORE_PROFILE_BIT_ARB
:
203 /* There are no profiles before OpenGL 3.2. The
204 * WGL_ARB_create_context_profile spec says:
206 * "If the requested OpenGL version is less than 3.2,
207 * WGL_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
208 * of the context is determined solely by the requested version."
210 if (majorVersion
> 3 || (majorVersion
== 3 && minorVersion
>= 2)) {
211 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
215 case WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
:
217 * The spec also says:
219 * "If version 3.1 is requested, the context returned may implement
220 * any of the following versions:
222 * * Version 3.1. The GL_ARB_compatibility extension may or may not
223 * be implemented, as determined by the implementation.
224 * * The core profile of version 3.2 or greater."
226 * But Mesa doesn't support GL_ARB_compatibility, while most prevalent
227 * Windows OpenGL implementations do, and unfortunately many Windows
228 * applications don't check whether they receive or not a context with
229 * GL_ARB_compatibility, so returning a core profile here does more harm
232 attribs
.profile
= ST_PROFILE_DEFAULT
;
234 case WGL_CONTEXT_ES_PROFILE_BIT_EXT
:
235 if (majorVersion
>= 2) {
236 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
238 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
246 ctx
->st
= stw_dev
->stapi
->create_context(stw_dev
->stapi
,
247 stw_dev
->smapi
, &attribs
, &ctx_err
, shareCtx
? shareCtx
->st
: NULL
);
251 ctx
->st
->st_manager_private
= (void *) ctx
;
253 if (ctx
->st
->cso_context
) {
254 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
257 pipe_mutex_lock( stw_dev
->ctx_mutex
);
259 /* We're replacing the context data for this handle. See the
260 * wglCreateContextAttribsARB() function.
262 struct stw_context
*old_ctx
=
263 stw_lookup_context_locked((unsigned) handle
);
265 /* free the old context data associated with this handle */
267 hud_destroy(old_ctx
->hud
);
269 ctx
->st
->destroy(old_ctx
->st
);
273 /* replace table entry */
274 handle_table_set(stw_dev
->ctx_table
, (unsigned) handle
, ctx
);
277 /* create new table entry */
278 handle
= (DHGLRC
) handle_table_add(stw_dev
->ctx_table
, ctx
);
281 ctx
->dhglrc
= handle
;
283 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
291 hud_destroy(ctx
->hud
);
293 ctx
->st
->destroy(ctx
->st
);
304 struct stw_context
*ctx
;
310 pipe_mutex_lock( stw_dev
->ctx_mutex
);
311 ctx
= stw_lookup_context_locked(dhglrc
);
312 handle_table_remove(stw_dev
->ctx_table
, dhglrc
);
313 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
316 struct stw_context
*curctx
= stw_current_context();
318 /* Unbind current if deleting current context. */
320 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
323 hud_destroy(ctx
->hud
);
326 ctx
->st
->destroy(ctx
->st
);
339 struct stw_context
*ctx
;
344 pipe_mutex_lock( stw_dev
->ctx_mutex
);
345 ctx
= stw_lookup_context_locked( dhglrc
);
346 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
351 /* The expectation is that ctx is the same context which is
352 * current for this thread. We should check that and return False
355 if (ctx
!= stw_current_context())
358 if (stw_make_current( NULL
, 0 ) == FALSE
)
366 stw_get_current_context( void )
368 struct stw_context
*ctx
;
370 ctx
= stw_current_context();
378 stw_get_current_dc( void )
380 struct stw_context
*ctx
;
382 ctx
= stw_current_context();
394 struct stw_context
*curctx
= NULL
;
395 struct stw_context
*ctx
= NULL
;
396 struct stw_framebuffer
*fb
= NULL
;
402 curctx
= stw_current_context();
403 if (curctx
!= NULL
) {
404 if (curctx
->dhglrc
== dhglrc
) {
405 if (curctx
->hdc
== hdc
) {
406 /* Return if already current. */
410 curctx
->st
->flush(curctx
->st
, ST_FLUSH_FRONT
, NULL
);
415 pipe_mutex_lock( stw_dev
->ctx_mutex
);
416 ctx
= stw_lookup_context_locked( dhglrc
);
417 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
422 fb
= stw_framebuffer_from_hdc( hdc
);
424 stw_framebuffer_update(fb
);
427 /* Applications should call SetPixelFormat before creating a context,
428 * but not all do, and the opengl32 runtime seems to use a default pixel
429 * format in some cases, so we must create a framebuffer for those here
431 int iPixelFormat
= GetPixelFormat(hdc
);
433 fb
= stw_framebuffer_create( hdc
, iPixelFormat
);
438 if (fb
->iPixelFormat
!= ctx
->iPixelFormat
) {
439 SetLastError(ERROR_INVALID_PIXEL_FORMAT
);
443 /* Bind the new framebuffer */
446 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, ctx
->st
,
448 stw_framebuffer_reference(&ctx
->current_framebuffer
, fb
);
450 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
456 stw_framebuffer_release(fb
);
459 /* On failure, make the thread's current rendering context not current
460 * before returning */
462 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
466 /* Unreference the previous framebuffer if any. It must be done after
467 * make_current, as it can be referenced inside.
469 if (curctx
&& curctx
!= ctx
) {
470 stw_framebuffer_reference(&curctx
->current_framebuffer
, NULL
);
477 * Flush the current context if it is bound to the framebuffer.
480 stw_flush_current_locked( struct stw_framebuffer
*fb
)
482 struct stw_context
*ctx
= stw_current_context();
484 if (ctx
&& ctx
->current_framebuffer
== fb
) {
485 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
490 * Notify the current context that the framebuffer has become invalid.
493 stw_notify_current_locked( struct stw_framebuffer
*fb
)
495 p_atomic_inc(&fb
->stfb
->stamp
);
499 * Although WGL allows different dispatch entrypoints per context
501 static const GLCLTPROCTABLE cpt
=
503 OPENGL_VERSION_110_ENTRIES
,
779 &glGetPolygonStipple
,
787 &glGetTexParameterfv
,
788 &glGetTexParameteriv
,
789 &glGetTexLevelParameterfv
,
790 &glGetTexLevelParameteriv
,
814 &glDisableClientState
,
818 &glEnableClientState
,
822 &glInterleavedArrays
,
827 &glAreTexturesResident
,
830 &glCopyTexSubImage1D
,
831 &glCopyTexSubImage2D
,
836 &glPrioritizeTextures
,
844 PGLCLTPROCTABLE APIENTRY
848 PFN_SETPROCTABLE pfnSetProcTable
)
850 PGLCLTPROCTABLE r
= (PGLCLTPROCTABLE
)&cpt
;
852 if (!stw_make_current( hdc
, dhglrc
))