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 "wgl/stw_wgl_arbmultisample.h"
41 #include "stw_context.h"
42 //#include "stw_wgl.h"
44 static struct wgl_context
*ctx_head
= NULL
;
46 static HDC current_hdc
= NULL
;
47 static HGLRC current_hrc
= NULL
;
63 stw_wgl_create_context(
68 const struct pixelformat_info
*pf
;
69 struct wgl_context
*ctx
;
71 struct pipe_context
*pipe
;
76 pfi
= wglGetPixelFormat( hdc
);
80 pf
= pixelformat_get_info( pfi
- 1 );
82 ctx
= CALLOC_STRUCT( wgl_context
);
87 ctx
->color_bits
= GetDeviceCaps( ctx
->hdc
, BITSPIXEL
);
89 /* Create visual based on flags
91 visual
= _mesa_create_visual(
93 (pf
->flags
& PF_FLAG_DOUBLEBUFFER
) ? GL_TRUE
: GL_FALSE
,
101 pf
->depth
.stencilbits
,
106 (pf
->flags
& PF_FLAG_MULTISAMPLED
) ? wgl_query_samples() : 0 );
107 if (visual
== NULL
) {
112 pipe
= stw_dev
->stw_winsys
->create_context( stw_dev
->screen
);
114 _mesa_destroy_visual( visual
);
122 ctx
->st
= st_create_context( pipe
, visual
, NULL
);
123 if (ctx
->st
== NULL
) {
124 pipe
->destroy( pipe
);
125 _mesa_destroy_visual( visual
);
129 ctx
->st
->ctx
->DriverCtx
= ctx
;
131 ctx
->next
= ctx_head
;
139 stw_wgl_delete_context(
142 struct wgl_context
**link
= &ctx_head
;
143 struct wgl_context
*ctx
= ctx_head
;
145 while (ctx
!= NULL
) {
146 if (ctx
== (struct wgl_context
*) hglrc
) {
147 GLcontext
*glctx
= ctx
->st
->ctx
;
148 GET_CURRENT_CONTEXT( glcurctx
);
149 struct stw_framebuffer
*fb
;
151 /* Unbind current if deleting current context.
153 if (glcurctx
== glctx
)
154 st_make_current( NULL
, NULL
, NULL
);
156 fb
= framebuffer_from_hdc( ctx
->hdc
);
158 framebuffer_destroy( fb
);
160 if (WindowFromDC( ctx
->hdc
) != NULL
)
161 ReleaseDC( WindowFromDC( ctx
->hdc
), ctx
->hdc
);
163 st_destroy_context( ctx
->st
);
177 /* Find the width and height of the window named by hdc.
180 get_window_size( HDC hdc
, GLuint
*width
, GLuint
*height
)
182 if (WindowFromDC( hdc
)) {
185 GetClientRect( WindowFromDC( hdc
), &rect
);
186 *width
= rect
.right
- rect
.left
;
187 *height
= rect
.bottom
- rect
.top
;
190 *width
= GetDeviceCaps( hdc
, HORZRES
);
191 *height
= GetDeviceCaps( hdc
, VERTRES
);
196 stw_wgl_get_current_context( void )
202 stw_wgl_get_current_dc( void )
208 stw_wgl_make_current(
212 struct wgl_context
*ctx
= ctx_head
;
213 GET_CURRENT_CONTEXT( glcurctx
);
214 struct stw_framebuffer
*fb
;
221 if (hdc
== NULL
|| hglrc
== NULL
) {
222 st_make_current( NULL
, NULL
, NULL
);
226 while (ctx
!= NULL
) {
227 if (ctx
== (struct wgl_context
*) hglrc
)
234 /* Return if already current.
236 if (glcurctx
!= NULL
) {
237 struct wgl_context
*curctx
= (struct wgl_context
*) glcurctx
->DriverCtx
;
239 if (curctx
!= NULL
&& curctx
== ctx
&& ctx
->hdc
== hdc
)
243 fb
= framebuffer_from_hdc( hdc
);
246 get_window_size( hdc
, &width
, &height
);
248 /* Lazy creation of framebuffers.
250 if (fb
== NULL
&& ctx
!= NULL
&& hdc
!= NULL
) {
251 GLvisual
*visual
= &ctx
->st
->ctx
->Visual
;
253 fb
= framebuffer_create( hdc
, visual
, width
, height
);
257 fb
->dib_hDC
= CreateCompatibleDC( hdc
);
263 st_make_current( ctx
->st
, fb
->stfb
, fb
->stfb
);
264 framebuffer_resize( fb
, width
, height
);
266 ctx
->st
->pipe
->priv
= hdc
;
270 st_make_current( NULL
, NULL
, NULL
);
277 wgl_context_from_hdc(
280 struct wgl_context
*ctx
= ctx_head
;
282 while (ctx
!= NULL
) {