1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
);
135 stw_create_context_attribs(
138 DHGLRC hShareContext
,
139 int majorVersion
, int minorVersion
,
140 int contextFlags
, int profileMask
)
143 struct stw_framebuffer
*fb
;
144 const struct stw_pixelformat_info
*pfi
;
145 struct st_context_attribs attribs
;
146 struct stw_context
*ctx
= NULL
;
147 struct stw_context
*shareCtx
= NULL
;
148 enum st_context_error ctx_err
= 0;
153 if (iLayerPlane
!= 0)
156 iPixelFormat
= GetPixelFormat(hdc
);
161 * GDI only knows about displayable pixel formats, so determine the pixel
162 * format from the framebuffer.
164 * TODO: Remove the GetPixelFormat() above, and stop relying on GDI.
166 fb
= stw_framebuffer_from_hdc( hdc
);
168 assert(iPixelFormat
== fb
->iDisplayablePixelFormat
);
169 iPixelFormat
= fb
->iPixelFormat
;
170 stw_framebuffer_release(fb
);
173 pfi
= stw_pixelformat_get_info( iPixelFormat
);
175 if (hShareContext
!= 0) {
176 pipe_mutex_lock( stw_dev
->ctx_mutex
);
177 shareCtx
= stw_lookup_context_locked( hShareContext
);
178 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
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 /* There are no profiles before OpenGL 3.2. The
198 * WGL_ARB_create_context_profile spec says:
200 * "If the requested OpenGL version is less than 3.2,
201 * WGL_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality of the
202 * context is determined solely by the requested version."
204 * The spec also says:
206 * "The default value for WGL_CONTEXT_PROFILE_MASK_ARB is
207 * WGL_CONTEXT_CORE_PROFILE_BIT_ARB."
209 attribs
.profile
= ST_PROFILE_DEFAULT
;
210 if ((majorVersion
> 3 || (majorVersion
== 3 && minorVersion
>= 2))
211 && ((profileMask
& WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
) == 0))
212 attribs
.profile
= ST_PROFILE_OPENGL_CORE
;
214 ctx
->st
= stw_dev
->stapi
->create_context(stw_dev
->stapi
,
215 stw_dev
->smapi
, &attribs
, &ctx_err
, shareCtx
? shareCtx
->st
: NULL
);
219 ctx
->st
->st_manager_private
= (void *) ctx
;
221 if (ctx
->st
->cso_context
) {
222 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
225 pipe_mutex_lock( stw_dev
->ctx_mutex
);
226 ctx
->dhglrc
= handle_table_add(stw_dev
->ctx_table
, ctx
);
227 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
235 hud_destroy(ctx
->hud
);
237 ctx
->st
->destroy(ctx
->st
);
248 struct stw_context
*ctx
;
254 pipe_mutex_lock( stw_dev
->ctx_mutex
);
255 ctx
= stw_lookup_context_locked(dhglrc
);
256 handle_table_remove(stw_dev
->ctx_table
, dhglrc
);
257 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
260 struct stw_context
*curctx
= stw_current_context();
262 /* Unbind current if deleting current context. */
264 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
267 hud_destroy(ctx
->hud
);
270 ctx
->st
->destroy(ctx
->st
);
283 struct stw_context
*ctx
;
288 pipe_mutex_lock( stw_dev
->ctx_mutex
);
289 ctx
= stw_lookup_context_locked( dhglrc
);
290 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
295 /* The expectation is that ctx is the same context which is
296 * current for this thread. We should check that and return False
299 if (ctx
!= stw_current_context())
302 if (stw_make_current( NULL
, 0 ) == FALSE
)
310 stw_get_current_context( void )
312 struct stw_context
*ctx
;
314 ctx
= stw_current_context();
322 stw_get_current_dc( void )
324 struct stw_context
*ctx
;
326 ctx
= stw_current_context();
338 struct stw_context
*curctx
= NULL
;
339 struct stw_context
*ctx
= NULL
;
340 struct stw_framebuffer
*fb
= NULL
;
346 curctx
= stw_current_context();
347 if (curctx
!= NULL
) {
348 if (curctx
->dhglrc
== dhglrc
) {
349 if (curctx
->hdc
== hdc
) {
350 /* Return if already current. */
354 curctx
->st
->flush(curctx
->st
, ST_FLUSH_FRONT
, NULL
);
359 pipe_mutex_lock( stw_dev
->ctx_mutex
);
360 ctx
= stw_lookup_context_locked( dhglrc
);
361 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
366 fb
= stw_framebuffer_from_hdc( hdc
);
368 stw_framebuffer_update(fb
);
371 /* Applications should call SetPixelFormat before creating a context,
372 * but not all do, and the opengl32 runtime seems to use a default pixel
373 * format in some cases, so we must create a framebuffer for those here
375 int iPixelFormat
= GetPixelFormat(hdc
);
377 fb
= stw_framebuffer_create( hdc
, iPixelFormat
);
382 if (fb
->iPixelFormat
!= ctx
->iPixelFormat
) {
383 SetLastError(ERROR_INVALID_PIXEL_FORMAT
);
387 /* Bind the new framebuffer */
390 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, ctx
->st
,
392 stw_framebuffer_reference(&ctx
->current_framebuffer
, fb
);
394 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
400 stw_framebuffer_release(fb
);
403 /* On failure, make the thread's current rendering context not current
404 * before returning */
406 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
410 /* Unreference the previous framebuffer if any. It must be done after
411 * make_current, as it can be referenced inside.
413 if (curctx
&& curctx
!= ctx
) {
414 stw_framebuffer_reference(&curctx
->current_framebuffer
, NULL
);
421 * Flush the current context if it is bound to the framebuffer.
424 stw_flush_current_locked( struct stw_framebuffer
*fb
)
426 struct stw_context
*ctx
= stw_current_context();
428 if (ctx
&& ctx
->current_framebuffer
== fb
) {
429 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
434 * Notify the current context that the framebuffer has become invalid.
437 stw_notify_current_locked( struct stw_framebuffer
*fb
)
439 p_atomic_inc(&fb
->stfb
->stamp
);
443 * Although WGL allows different dispatch entrypoints per context
445 static const GLCLTPROCTABLE cpt
=
447 OPENGL_VERSION_110_ENTRIES
,
723 &glGetPolygonStipple
,
731 &glGetTexParameterfv
,
732 &glGetTexParameteriv
,
733 &glGetTexLevelParameterfv
,
734 &glGetTexLevelParameteriv
,
758 &glDisableClientState
,
762 &glEnableClientState
,
766 &glInterleavedArrays
,
771 &glAreTexturesResident
,
774 &glCopyTexSubImage1D
,
775 &glCopyTexSubImage2D
,
780 &glPrioritizeTextures
,
788 PGLCLTPROCTABLE APIENTRY
792 PFN_SETPROCTABLE pfnSetProcTable
)
794 PGLCLTPROCTABLE r
= (PGLCLTPROCTABLE
)&cpt
;
796 if (!stw_make_current( hdc
, dhglrc
))