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 "main/mtypes.h"
31 #include "main/context.h"
32 #include "pipe/p_compiler.h"
33 #include "pipe/p_context.h"
34 #include "state_tracker/st_context.h"
35 #include "state_tracker/st_public.h"
36 #include "shared/stw_device.h"
37 #include "shared/stw_winsys.h"
38 #include "shared/stw_framebuffer.h"
39 #include "shared/stw_pixelformat.h"
40 #include "stw_public.h"
41 #include "stw_context.h"
43 static HDC current_hdc
= NULL
;
44 static UINT_PTR current_hglrc
= 0;
52 struct stw_context
*src
;
53 struct stw_context
*dst
;
56 pipe_mutex_lock( stw_dev
->mutex
);
58 src
= stw_lookup_context( hglrcSrc
);
59 dst
= stw_lookup_context( hglrcDst
);
68 pipe_mutex_unlock( stw_dev
->mutex
);
74 stw_create_layer_context(
79 const struct pixelformat_info
*pf
= NULL
;
80 struct stw_context
*ctx
= NULL
;
81 GLvisual
*visual
= NULL
;
82 struct pipe_context
*pipe
= NULL
;
91 pfi
= stw_pixelformat_get( hdc
);
95 pf
= pixelformat_get_info( pfi
- 1 );
97 ctx
= CALLOC_STRUCT( stw_context
);
102 ctx
->color_bits
= GetDeviceCaps( ctx
->hdc
, BITSPIXEL
);
104 /* Create visual based on flags
106 visual
= _mesa_create_visual(
108 (pf
->flags
& PF_FLAG_DOUBLEBUFFER
) ? GL_TRUE
: GL_FALSE
,
116 pf
->depth
.stencilbits
,
121 (pf
->flags
& PF_FLAG_MULTISAMPLED
) ? stw_query_samples() : 0 );
125 pipe
= stw_dev
->stw_winsys
->create_context( stw_dev
->screen
);
132 ctx
->st
= st_create_context( pipe
, visual
, NULL
);
136 ctx
->st
->ctx
->DriverCtx
= ctx
;
138 pipe_mutex_lock( stw_dev
->mutex
);
142 for (i
= 0; i
< STW_CONTEXT_MAX
; i
++) {
143 if (stw_dev
->ctx_array
[i
].ctx
== NULL
) {
146 stw_dev
->ctx_array
[i
].ctx
= ctx
;
152 pipe_mutex_unlock( stw_dev
->mutex
);
161 _mesa_destroy_visual( visual
);
164 pipe
->destroy( pipe
);
174 struct stw_context
*ctx
;
180 pipe_mutex_lock( stw_dev
->mutex
);
182 ctx
= stw_lookup_context(hglrc
);
184 GLcontext
*glctx
= ctx
->st
->ctx
;
185 GET_CURRENT_CONTEXT( glcurctx
);
186 struct stw_framebuffer
*fb
;
188 /* Unbind current if deleting current context.
190 if (glcurctx
== glctx
)
191 st_make_current( NULL
, NULL
, NULL
);
193 fb
= framebuffer_from_hdc( ctx
->hdc
);
195 framebuffer_destroy( fb
);
197 if (WindowFromDC( ctx
->hdc
) != NULL
)
198 ReleaseDC( WindowFromDC( ctx
->hdc
), ctx
->hdc
);
200 st_destroy_context( ctx
->st
);
204 stw_dev
->ctx_array
[hglrc
- 1].ctx
= NULL
;
209 pipe_mutex_unlock( stw_dev
->mutex
);
223 pipe_mutex_lock( stw_dev
->mutex
);
225 struct stw_context
*ctx
;
227 /* XXX: The expectation is that ctx is the same context which is
228 * current for this thread. We should check that and return False
231 ctx
= stw_lookup_context( hglrc
);
235 if (stw_make_current( NULL
, 0 ) == FALSE
)
241 pipe_mutex_unlock( stw_dev
->mutex
);
246 /* Find the width and height of the window named by hdc.
249 get_window_size( HDC hdc
, GLuint
*width
, GLuint
*height
)
251 if (WindowFromDC( hdc
)) {
254 GetClientRect( WindowFromDC( hdc
), &rect
);
255 *width
= rect
.right
- rect
.left
;
256 *height
= rect
.bottom
- rect
.top
;
259 *width
= GetDeviceCaps( hdc
, HORZRES
);
260 *height
= GetDeviceCaps( hdc
, VERTRES
);
265 stw_get_current_context( void )
267 return current_hglrc
;
271 stw_get_current_dc( void )
281 struct stw_context
*ctx
;
282 GET_CURRENT_CONTEXT( glcurctx
);
283 struct stw_framebuffer
*fb
;
290 pipe_mutex_lock( stw_dev
->mutex
);
291 ctx
= stw_lookup_context( hglrc
);
292 pipe_mutex_unlock( stw_dev
->mutex
);
298 current_hglrc
= hglrc
;
300 if (hdc
== NULL
|| hglrc
== 0) {
301 st_make_current( NULL
, NULL
, NULL
);
305 /* Return if already current.
307 if (glcurctx
!= NULL
) {
308 struct stw_context
*curctx
= (struct stw_context
*) glcurctx
->DriverCtx
;
310 if (curctx
!= NULL
&& curctx
== ctx
&& ctx
->hdc
== hdc
)
314 fb
= framebuffer_from_hdc( hdc
);
317 get_window_size( hdc
, &width
, &height
);
319 /* Lazy creation of framebuffers.
321 if (fb
== NULL
&& ctx
!= NULL
&& hdc
!= NULL
) {
322 GLvisual
*visual
= &ctx
->st
->ctx
->Visual
;
324 fb
= framebuffer_create( hdc
, visual
, width
, height
);
328 fb
->dib_hDC
= CreateCompatibleDC( hdc
);
334 st_make_current( ctx
->st
, fb
->stfb
, fb
->stfb
);
335 framebuffer_resize( fb
, width
, height
);
337 ctx
->st
->pipe
->priv
= hdc
;
341 st_make_current( NULL
, NULL
, NULL
);