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 "stw_device.h"
37 #include "stw_winsys.h"
38 #include "stw_framebuffer.h"
39 #include "stw_pixelformat.h"
40 #include "stw_wgl_arbmultisample.h"
41 #include "stw_wgl_context.h"
44 static struct wgl_context
*ctx_head
= NULL
;
46 static HDC current_hdc
= NULL
;
47 static HGLRC current_hrc
= NULL
;
49 WINGDIAPI BOOL APIENTRY
62 WINGDIAPI HGLRC APIENTRY
67 const struct pixelformat_info
*pf
;
68 struct wgl_context
*ctx
;
70 struct pipe_context
*pipe
;
72 pfi
= wglGetPixelFormat( hdc
);
76 pf
= pixelformat_get_info( pfi
- 1 );
78 ctx
= CALLOC_STRUCT( wgl_context
);
83 ctx
->color_bits
= GetDeviceCaps( ctx
->hdc
, BITSPIXEL
);
85 /* Create visual based on flags
87 visual
= _mesa_create_visual(
89 (pf
->flags
& PF_FLAG_DOUBLEBUFFER
) ? GL_TRUE
: GL_FALSE
,
97 pf
->depth
.stencilbits
,
102 (pf
->flags
& PF_FLAG_MULTISAMPLED
) ? wgl_query_samples() : 0 );
103 if (visual
== NULL
) {
108 pipe
= stw_winsys
.create_context( stw_dev
->screen
);
110 _mesa_destroy_visual( visual
);
115 ctx
->st
= st_create_context( pipe
, visual
, NULL
);
116 if (ctx
->st
== NULL
) {
117 pipe
->destroy( pipe
);
118 _mesa_destroy_visual( visual
);
122 ctx
->st
->ctx
->DriverCtx
= ctx
;
124 ctx
->next
= ctx_head
;
130 WINGDIAPI HGLRC APIENTRY
131 wglCreateLayerContext(
141 WINGDIAPI BOOL APIENTRY
145 struct wgl_context
**link
= &ctx_head
;
146 struct wgl_context
*ctx
= ctx_head
;
148 while (ctx
!= NULL
) {
149 if (ctx
== (struct wgl_context
*) hglrc
) {
150 GLcontext
*glctx
= ctx
->st
->ctx
;
151 GET_CURRENT_CONTEXT( glcurctx
);
152 struct stw_framebuffer
*fb
;
154 /* Unbind current if deleting current context.
156 if (glcurctx
== glctx
)
157 st_make_current( NULL
, NULL
, NULL
);
159 fb
= framebuffer_from_hdc( ctx
->hdc
);
161 framebuffer_destroy( fb
);
163 if (WindowFromDC( ctx
->hdc
) != NULL
)
164 ReleaseDC( WindowFromDC( ctx
->hdc
), ctx
->hdc
);
166 st_destroy_context( ctx
->st
);
180 /* Find the width and height of the window named by hdc.
183 get_window_size( HDC hdc
, GLuint
*width
, GLuint
*height
)
185 if (WindowFromDC( hdc
)) {
188 GetClientRect( WindowFromDC( hdc
), &rect
);
189 *width
= rect
.right
- rect
.left
;
190 *height
= rect
.bottom
- rect
.top
;
193 *width
= GetDeviceCaps( hdc
, HORZRES
);
194 *height
= GetDeviceCaps( hdc
, VERTRES
);
198 WINGDIAPI HGLRC APIENTRY
199 wglGetCurrentContext( VOID
)
204 WINGDIAPI HDC APIENTRY
205 wglGetCurrentDC( VOID
)
210 WINGDIAPI BOOL APIENTRY
215 struct wgl_context
*ctx
= ctx_head
;
216 GET_CURRENT_CONTEXT( glcurctx
);
217 struct stw_framebuffer
*fb
;
224 if (hdc
== NULL
|| hglrc
== NULL
) {
225 st_make_current( NULL
, NULL
, NULL
);
229 while (ctx
!= NULL
) {
230 if (ctx
== (struct wgl_context
*) hglrc
)
237 /* Return if already current.
239 if (glcurctx
!= NULL
) {
240 struct wgl_context
*curctx
= (struct wgl_context
*) glcurctx
->DriverCtx
;
242 if (curctx
!= NULL
&& curctx
== ctx
&& ctx
->hdc
== hdc
)
246 fb
= framebuffer_from_hdc( hdc
);
249 get_window_size( hdc
, &width
, &height
);
251 /* Lazy creation of framebuffers.
253 if (fb
== NULL
&& ctx
!= NULL
&& hdc
!= NULL
) {
254 GLvisual
*visual
= &ctx
->st
->ctx
->Visual
;
256 fb
= framebuffer_create( hdc
, visual
, width
, height
);
260 fb
->dib_hDC
= CreateCompatibleDC( hdc
);
266 st_make_current( ctx
->st
, fb
->stfb
, fb
->stfb
);
267 framebuffer_resize( fb
, width
, height
);
271 st_make_current( NULL
, NULL
, NULL
);
278 wgl_context_from_hdc(
281 struct wgl_context
*ctx
= ctx_head
;
283 while (ctx
!= NULL
) {