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
)
147 /* No slot available, fail:
149 if (i
== STW_CONTEXT_MAX
)
152 stw_dev
->ctx_array
[i
].ctx
= ctx
;
159 pipe_mutex_unlock( stw_dev
->mutex
);
165 _mesa_destroy_visual( visual
);
168 pipe
->destroy( pipe
);
178 struct stw_context
*ctx
;
184 pipe_mutex_lock( stw_dev
->mutex
);
186 ctx
= stw_lookup_context(hglrc
);
188 GLcontext
*glctx
= ctx
->st
->ctx
;
189 GET_CURRENT_CONTEXT( glcurctx
);
190 struct stw_framebuffer
*fb
;
192 /* Unbind current if deleting current context.
194 if (glcurctx
== glctx
)
195 st_make_current( NULL
, NULL
, NULL
);
197 fb
= framebuffer_from_hdc( ctx
->hdc
);
199 framebuffer_destroy( fb
);
201 if (WindowFromDC( ctx
->hdc
) != NULL
)
202 ReleaseDC( WindowFromDC( ctx
->hdc
), ctx
->hdc
);
204 st_destroy_context( ctx
->st
);
208 stw_dev
->ctx_array
[hglrc
- 1].ctx
= NULL
;
213 pipe_mutex_unlock( stw_dev
->mutex
);
227 pipe_mutex_lock( stw_dev
->mutex
);
229 struct stw_context
*ctx
;
231 /* XXX: The expectation is that ctx is the same context which is
232 * current for this thread. We should check that and return False
235 ctx
= stw_lookup_context( hglrc
);
239 if (stw_make_current( NULL
, 0 ) == FALSE
)
245 pipe_mutex_unlock( stw_dev
->mutex
);
250 /* Find the width and height of the window named by hdc.
253 get_window_size( HDC hdc
, GLuint
*width
, GLuint
*height
)
255 if (WindowFromDC( hdc
)) {
258 GetClientRect( WindowFromDC( hdc
), &rect
);
259 *width
= rect
.right
- rect
.left
;
260 *height
= rect
.bottom
- rect
.top
;
263 *width
= GetDeviceCaps( hdc
, HORZRES
);
264 *height
= GetDeviceCaps( hdc
, VERTRES
);
269 stw_get_current_context( void )
271 return current_hglrc
;
275 stw_get_current_dc( void )
285 struct stw_context
*ctx
;
286 GET_CURRENT_CONTEXT( glcurctx
);
287 struct stw_framebuffer
*fb
;
294 pipe_mutex_lock( stw_dev
->mutex
);
295 ctx
= stw_lookup_context( hglrc
);
296 pipe_mutex_unlock( stw_dev
->mutex
);
302 current_hglrc
= hglrc
;
304 if (hdc
== NULL
|| hglrc
== 0) {
305 st_make_current( NULL
, NULL
, NULL
);
309 /* Return if already current.
311 if (glcurctx
!= NULL
) {
312 struct stw_context
*curctx
= (struct stw_context
*) glcurctx
->DriverCtx
;
314 if (curctx
!= NULL
&& curctx
== ctx
&& ctx
->hdc
== hdc
)
318 fb
= framebuffer_from_hdc( hdc
);
321 get_window_size( hdc
, &width
, &height
);
323 /* Lazy creation of framebuffers.
325 if (fb
== NULL
&& ctx
!= NULL
&& hdc
!= NULL
) {
326 GLvisual
*visual
= &ctx
->st
->ctx
->Visual
;
328 fb
= framebuffer_create( hdc
, visual
, width
, height
);
332 fb
->dib_hDC
= CreateCompatibleDC( hdc
);
338 st_make_current( ctx
->st
, fb
->stfb
, fb
->stfb
);
339 framebuffer_resize( fb
, width
, height
);
341 ctx
->st
->pipe
->priv
= hdc
;
345 st_make_current( NULL
, NULL
, NULL
);