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 #include "pipe/p_compiler.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "util/u_memory.h"
34 #include "util/u_atomic.h"
35 #include "state_tracker/st_api.h"
38 #include "stw_device.h"
39 #include "stw_winsys.h"
40 #include "stw_framebuffer.h"
41 #include "stw_pixelformat.h"
42 #include "stw_context.h"
46 static INLINE
struct stw_context
*
47 stw_current_context(void)
49 struct st_context_iface
*st
;
51 st
= (stw_dev
) ? stw_dev
->stapi
->get_current(stw_dev
->stapi
) : NULL
;
53 return (struct stw_context
*) ((st
) ? st
->st_manager_private
: NULL
);
62 struct stw_context
*src
;
63 struct stw_context
*dst
;
69 pipe_mutex_lock( stw_dev
->ctx_mutex
);
71 src
= stw_lookup_context_locked( dhrcSource
);
72 dst
= stw_lookup_context_locked( dhrcDest
);
82 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
92 struct stw_context
*ctx1
;
93 struct stw_context
*ctx2
;
99 pipe_mutex_lock( stw_dev
->ctx_mutex
);
101 ctx1
= stw_lookup_context_locked( dhglrc1
);
102 ctx2
= stw_lookup_context_locked( dhglrc2
);
104 if (ctx1
&& ctx2
&& ctx2
->st
->share
)
105 ret
= ctx2
->st
->share(ctx2
->st
, ctx1
->st
);
107 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
116 return DrvCreateLayerContext( hdc
, 0 );
120 DrvCreateLayerContext(
125 const struct stw_pixelformat_info
*pfi
;
126 struct st_context_attribs attribs
;
127 struct stw_context
*ctx
= NULL
;
132 if (iLayerPlane
!= 0)
135 iPixelFormat
= GetPixelFormat(hdc
);
139 pfi
= stw_pixelformat_get_info( iPixelFormat
- 1 );
141 ctx
= CALLOC_STRUCT( stw_context
);
146 ctx
->iPixelFormat
= iPixelFormat
;
148 memset(&attribs
, 0, sizeof(attribs
));
149 attribs
.profile
= ST_PROFILE_DEFAULT
;
150 attribs
.visual
= pfi
->stvis
;
152 ctx
->st
= stw_dev
->stapi
->create_context(stw_dev
->stapi
,
153 stw_dev
->smapi
, &attribs
, NULL
);
157 ctx
->st
->st_manager_private
= (void *) ctx
;
159 pipe_mutex_lock( stw_dev
->ctx_mutex
);
160 ctx
->dhglrc
= handle_table_add(stw_dev
->ctx_table
, ctx
);
161 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
168 ctx
->st
->destroy(ctx
->st
);
179 struct stw_context
*ctx
;
185 pipe_mutex_lock( stw_dev
->ctx_mutex
);
186 ctx
= stw_lookup_context_locked(dhglrc
);
187 handle_table_remove(stw_dev
->ctx_table
, dhglrc
);
188 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
191 struct stw_context
*curctx
= stw_current_context();
193 /* Unbind current if deleting current context. */
195 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
197 ctx
->st
->destroy(ctx
->st
);
210 struct stw_context
*ctx
;
215 pipe_mutex_lock( stw_dev
->ctx_mutex
);
216 ctx
= stw_lookup_context_locked( dhglrc
);
217 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
222 /* The expectation is that ctx is the same context which is
223 * current for this thread. We should check that and return False
226 if (ctx
!= stw_current_context())
229 if (stw_make_current( NULL
, 0 ) == FALSE
)
237 stw_get_current_context( void )
239 struct stw_context
*ctx
;
241 ctx
= stw_current_context();
249 stw_get_current_dc( void )
251 struct stw_context
*ctx
;
253 ctx
= stw_current_context();
265 struct stw_context
*curctx
= NULL
;
266 struct stw_context
*ctx
= NULL
;
267 struct stw_framebuffer
*fb
= NULL
;
273 curctx
= stw_current_context();
274 if (curctx
!= NULL
) {
275 if (curctx
->dhglrc
== dhglrc
) {
276 if (curctx
->hdc
== hdc
) {
277 /* Return if already current. */
281 curctx
->st
->flush(curctx
->st
, ST_FLUSH_FRONT
, NULL
);
286 pipe_mutex_lock( stw_dev
->ctx_mutex
);
287 ctx
= stw_lookup_context_locked( dhglrc
);
288 pipe_mutex_unlock( stw_dev
->ctx_mutex
);
293 fb
= stw_framebuffer_from_hdc( hdc
);
295 stw_framebuffer_update(fb
);
298 /* Applications should call SetPixelFormat before creating a context,
299 * but not all do, and the opengl32 runtime seems to use a default pixel
300 * format in some cases, so we must create a framebuffer for those here
302 int iPixelFormat
= GetPixelFormat(hdc
);
304 fb
= stw_framebuffer_create( hdc
, iPixelFormat
);
309 if (fb
->iPixelFormat
!= ctx
->iPixelFormat
) {
310 SetLastError(ERROR_INVALID_PIXEL_FORMAT
);
314 /* Bind the new framebuffer */
317 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, ctx
->st
, fb
->stfb
, fb
->stfb
);
318 stw_framebuffer_reference(&ctx
->current_framebuffer
, fb
);
320 ret
= stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
326 stw_framebuffer_release(fb
);
329 /* On failure, make the thread's current rendering context not current
330 * before returning */
332 stw_dev
->stapi
->make_current(stw_dev
->stapi
, NULL
, NULL
, NULL
);
336 /* Unreference the previous framebuffer if any. It must be done after
337 * make_current, as it can be referenced inside.
339 if (curctx
&& curctx
!= ctx
) {
340 stw_framebuffer_reference(&curctx
->current_framebuffer
, NULL
);
347 * Flush the current context if it is bound to the framebuffer.
350 stw_flush_current_locked( struct stw_framebuffer
*fb
)
352 struct stw_context
*ctx
= stw_current_context();
354 if (ctx
&& ctx
->current_framebuffer
== fb
) {
355 ctx
->st
->flush(ctx
->st
, ST_FLUSH_FRONT
, NULL
);
360 * Notify the current context that the framebuffer has become invalid.
363 stw_notify_current_locked( struct stw_framebuffer
*fb
)
365 p_atomic_inc(&fb
->stfb
->stamp
);
369 * Although WGL allows different dispatch entrypoints per context
371 static const GLCLTPROCTABLE cpt
=
373 OPENGL_VERSION_110_ENTRIES
,
649 &glGetPolygonStipple
,
657 &glGetTexParameterfv
,
658 &glGetTexParameteriv
,
659 &glGetTexLevelParameterfv
,
660 &glGetTexLevelParameteriv
,
684 &glDisableClientState
,
688 &glEnableClientState
,
692 &glInterleavedArrays
,
697 &glAreTexturesResident
,
700 &glCopyTexSubImage1D
,
701 &glCopyTexSubImage2D
,
706 &glPrioritizeTextures
,
714 PGLCLTPROCTABLE APIENTRY
718 PFN_SETPROCTABLE pfnSetProcTable
)
720 PGLCLTPROCTABLE r
= (PGLCLTPROCTABLE
)&cpt
;
722 if (!stw_make_current( hdc
, dhglrc
))