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)
163 iPixelFormat
= GetPixelFormat(hdc
);
168 * GDI only knows about displayable pixel formats, so determine the pixel
169 * format from the framebuffer.
171 * TODO: Remove the GetPixelFormat() above, and stop relying on GDI.
173 fb
= stw_framebuffer_from_hdc( hdc
);
175 assert(iPixelFormat
== fb
->iDisplayablePixelFormat
);
176 iPixelFormat
= fb
->iPixelFormat
;
177 stw_framebuffer_release(fb
);
180 pfi
= stw_pixelformat_get_info( iPixelFormat
);
182 if (hShareContext
!= 0) {
183 pipe_mutex_lock( stw_dev
->ctx_mutex
);
184 shareCtx
= stw_lookup_context_locked( hShareContext
);
185 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
188 ctx
= CALLOC_STRUCT( stw_context
);
193 ctx
->iPixelFormat
= iPixelFormat
;
195 memset(&attribs
, 0, sizeof(attribs
));
196 attribs
.visual
= pfi
->stvis
;
197 attribs
.major
= majorVersion
;
198 attribs
.minor
= minorVersion
;
199 if (contextFlags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
200 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
201 if (contextFlags
& WGL_CONTEXT_DEBUG_BIT_ARB
)
202 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
204 /* There are no profiles before OpenGL 3.2. The
205 * WGL_ARB_create_context_profile spec says:
207 * "If the requested OpenGL version is less than 3.2,
208 * WGL_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality of the
209 * context is determined solely by the requested version."
211 * The spec also says:
213 * "The default value for WGL_CONTEXT_PROFILE_MASK_ARB is
214 * WGL_CONTEXT_CORE_PROFILE_BIT_ARB."
216 * The spec also says:
218 * "If version 3.1 is requested, the context returned may implement
219 * any of the following versions:
221 * * Version 3.1. The GL_ARB_compatibility extension may or may not
222 * be implemented, as determined by the implementation.
223 * * The core profile of version 3.2 or greater."
225 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
226 * honour a 3.1 context is through core profile.
228 attribs
.profile
= ST_PROFILE_DEFAULT
;
229 if (((majorVersion
> 3 || (majorVersion
== 3 && minorVersion
>= 2))
230 && ((profileMask
& WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
) == 0)) ||
231 (majorVersion
== 3 && minorVersion
== 1))
232 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
234 ctx
->st
= stw_dev
->stapi
->create_context(stw_dev
->stapi
,
235 stw_dev
->smapi
, &attribs
, &ctx_err
, shareCtx
? shareCtx
->st
: NULL
);
239 ctx
->st
->st_manager_private
= (void *) ctx
;
241 if (ctx
->st
->cso_context
) {
242 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
245 pipe_mutex_lock( stw_dev
->ctx_mutex
);
247 /* We're replacing the context data for this handle. See the
248 * wglCreateContextAttribsARB() function.
250 struct stw_context
*old_ctx
=
251 stw_lookup_context_locked((unsigned) handle
);
253 /* free the old context data associated with this handle */
255 hud_destroy(old_ctx
->hud
);
257 ctx
->st
->destroy(old_ctx
->st
);
261 /* replace table entry */
262 handle_table_set(stw_dev
->ctx_table
, (unsigned) handle
, ctx
);
265 /* create new table entry */
266 handle
= (DHGLRC
) handle_table_add(stw_dev
->ctx_table
, ctx
);
269 ctx
->dhglrc
= handle
;
271 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
279 hud_destroy(ctx
->hud
);
281 ctx
->st
->destroy(ctx
->st
);
292 struct stw_context
*ctx
;
298 pipe_mutex_lock( stw_dev
->ctx_mutex
);
299 ctx
= stw_lookup_context_locked(dhglrc
);
300 handle_table_remove(stw_dev
->ctx_table
, dhglrc
);
301 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
304 struct stw_context
*curctx
= stw_current_context();
306 /* Unbind current if deleting current context. */
308 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
311 hud_destroy(ctx
->hud
);
314 ctx
->st
->destroy(ctx
->st
);
327 struct stw_context
*ctx
;
332 pipe_mutex_lock( stw_dev
->ctx_mutex
);
333 ctx
= stw_lookup_context_locked( dhglrc
);
334 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
339 /* The expectation is that ctx is the same context which is
340 * current for this thread. We should check that and return False
343 if (ctx
!= stw_current_context())
346 if (stw_make_current( NULL
, 0 ) == FALSE
)
354 stw_get_current_context( void )
356 struct stw_context
*ctx
;
358 ctx
= stw_current_context();
366 stw_get_current_dc( void )
368 struct stw_context
*ctx
;
370 ctx
= stw_current_context();
382 struct stw_context
*curctx
= NULL
;
383 struct stw_context
*ctx
= NULL
;
384 struct stw_framebuffer
*fb
= NULL
;
390 curctx
= stw_current_context();
391 if (curctx
!= NULL
) {
392 if (curctx
->dhglrc
== dhglrc
) {
393 if (curctx
->hdc
== hdc
) {
394 /* Return if already current. */
398 curctx
->st
->flush(curctx
->st
, ST_FLUSH_FRONT
, NULL
);
403 pipe_mutex_lock( stw_dev
->ctx_mutex
);
404 ctx
= stw_lookup_context_locked( dhglrc
);
405 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
410 fb
= stw_framebuffer_from_hdc( hdc
);
412 stw_framebuffer_update(fb
);
415 /* Applications should call SetPixelFormat before creating a context,
416 * but not all do, and the opengl32 runtime seems to use a default pixel
417 * format in some cases, so we must create a framebuffer for those here
419 int iPixelFormat
= GetPixelFormat(hdc
);
421 fb
= stw_framebuffer_create( hdc
, iPixelFormat
);
426 if (fb
->iPixelFormat
!= ctx
->iPixelFormat
) {
427 SetLastError(ERROR_INVALID_PIXEL_FORMAT
);
431 /* Bind the new framebuffer */
434 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, ctx
->st
,
436 stw_framebuffer_reference(&ctx
->current_framebuffer
, fb
);
438 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
444 stw_framebuffer_release(fb
);
447 /* On failure, make the thread's current rendering context not current
448 * before returning */
450 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
454 /* Unreference the previous framebuffer if any. It must be done after
455 * make_current, as it can be referenced inside.
457 if (curctx
&& curctx
!= ctx
) {
458 stw_framebuffer_reference(&curctx
->current_framebuffer
, NULL
);
465 * Flush the current context if it is bound to the framebuffer.
468 stw_flush_current_locked( struct stw_framebuffer
*fb
)
470 struct stw_context
*ctx
= stw_current_context();
472 if (ctx
&& ctx
->current_framebuffer
== fb
) {
473 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
478 * Notify the current context that the framebuffer has become invalid.
481 stw_notify_current_locked( struct stw_framebuffer
*fb
)
483 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
,
832 PGLCLTPROCTABLE APIENTRY
836 PFN_SETPROCTABLE pfnSetProcTable
)
838 PGLCLTPROCTABLE r
= (PGLCLTPROCTABLE
)&cpt
;
840 if (!stw_make_current( hdc
, dhglrc
))