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 struct stw_context
*ctx_head
= NULL
;
45 static HDC current_hdc
= NULL
;
46 static struct stw_context
*current_hrc
= NULL
;
50 struct stw_context
*src
,
51 struct stw_context
*dst
,
67 const struct pixelformat_info
*pf
= NULL
;
68 struct stw_context
*ctx
= NULL
;
69 GLvisual
*visual
= NULL
;
70 struct pipe_context
*pipe
= NULL
;
75 pfi
= stw_pixelformat_get( hdc
);
79 pf
= pixelformat_get_info( pfi
- 1 );
81 ctx
= CALLOC_STRUCT( stw_context
);
86 ctx
->color_bits
= GetDeviceCaps( ctx
->hdc
, BITSPIXEL
);
88 /* Create visual based on flags
90 visual
= _mesa_create_visual(
92 (pf
->flags
& PF_FLAG_DOUBLEBUFFER
) ? GL_TRUE
: GL_FALSE
,
100 pf
->depth
.stencilbits
,
105 (pf
->flags
& PF_FLAG_MULTISAMPLED
) ? stw_query_samples() : 0 );
109 pipe
= stw_dev
->stw_winsys
->create_context( stw_dev
->screen
);
116 ctx
->st
= st_create_context( pipe
, visual
, NULL
);
120 ctx
->st
->ctx
->DriverCtx
= ctx
;
122 ctx
->next
= ctx_head
;
129 _mesa_destroy_visual( visual
);
132 pipe
->destroy( pipe
);
141 struct stw_context
*hglrc
)
143 struct stw_context
**link
= &ctx_head
;
144 struct stw_context
*ctx
= ctx_head
;
146 while (ctx
!= NULL
) {
148 GLcontext
*glctx
= ctx
->st
->ctx
;
149 GET_CURRENT_CONTEXT( glcurctx
);
150 struct stw_framebuffer
*fb
;
152 /* Unbind current if deleting current context.
154 if (glcurctx
== glctx
)
155 st_make_current( NULL
, NULL
, NULL
);
157 fb
= framebuffer_from_hdc( ctx
->hdc
);
159 framebuffer_destroy( fb
);
161 if (WindowFromDC( ctx
->hdc
) != NULL
)
162 ReleaseDC( WindowFromDC( ctx
->hdc
), ctx
->hdc
);
164 st_destroy_context( ctx
->st
);
178 /* Find the width and height of the window named by hdc.
181 get_window_size( HDC hdc
, GLuint
*width
, GLuint
*height
)
183 if (WindowFromDC( hdc
)) {
186 GetClientRect( WindowFromDC( hdc
), &rect
);
187 *width
= rect
.right
- rect
.left
;
188 *height
= rect
.bottom
- rect
.top
;
191 *width
= GetDeviceCaps( hdc
, HORZRES
);
192 *height
= GetDeviceCaps( hdc
, VERTRES
);
197 stw_get_current_context( void )
203 stw_get_current_dc( void )
211 struct stw_context
*hglrc
)
213 struct stw_context
*ctx
= ctx_head
;
214 GET_CURRENT_CONTEXT( glcurctx
);
215 struct stw_framebuffer
*fb
;
222 if (hdc
== NULL
|| hglrc
== NULL
) {
223 st_make_current( NULL
, NULL
, NULL
);
227 while (ctx
!= NULL
) {
235 /* Return if already current.
237 if (glcurctx
!= NULL
) {
238 struct stw_context
*curctx
= (struct stw_context
*) glcurctx
->DriverCtx
;
240 if (curctx
!= NULL
&& curctx
== ctx
&& ctx
->hdc
== hdc
)
244 fb
= framebuffer_from_hdc( hdc
);
247 get_window_size( hdc
, &width
, &height
);
249 /* Lazy creation of framebuffers.
251 if (fb
== NULL
&& ctx
!= NULL
&& hdc
!= NULL
) {
252 GLvisual
*visual
= &ctx
->st
->ctx
->Visual
;
254 fb
= framebuffer_create( hdc
, visual
, width
, height
);
258 fb
->dib_hDC
= CreateCompatibleDC( hdc
);
264 st_make_current( ctx
->st
, fb
->stfb
, fb
->stfb
);
265 framebuffer_resize( fb
, width
, height
);
267 ctx
->st
->pipe
->priv
= hdc
;
271 st_make_current( NULL
, NULL
, NULL
);
278 stw_context_from_hdc(
281 struct stw_context
*ctx
= ctx_head
;
283 while (ctx
!= NULL
) {