wgl: Ensure public symbols get the proper DLL linkage attributes.
[mesa.git] / src / mesa / state_tracker / wgl / stw_wgl_context.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <windows.h>
29
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"
42 #include "stw_wgl.h"
43
44 static struct wgl_context *ctx_head = NULL;
45
46 static HDC current_hdc = NULL;
47 static HGLRC current_hrc = NULL;
48
49 WINGDIAPI BOOL APIENTRY
50 wglCopyContext(
51 HGLRC hglrcSrc,
52 HGLRC hglrcDst,
53 UINT mask )
54 {
55 (void) hglrcSrc;
56 (void) hglrcDst;
57 (void) mask;
58
59 return FALSE;
60 }
61
62 WINGDIAPI HGLRC APIENTRY
63 wglCreateContext(
64 HDC hdc )
65 {
66 uint pfi;
67 const struct pixelformat_info *pf;
68 struct wgl_context *ctx;
69 GLvisual *visual;
70 struct pipe_context *pipe;
71
72 pfi = wglGetPixelFormat( hdc );
73 if (pfi == 0)
74 return NULL;
75
76 pf = pixelformat_get_info( pfi - 1 );
77
78 ctx = CALLOC_STRUCT( wgl_context );
79 if (ctx == NULL)
80 return NULL;
81
82 ctx->hdc = hdc;
83 ctx->color_bits = GetDeviceCaps( ctx->hdc, BITSPIXEL );
84
85 /* Create visual based on flags
86 */
87 visual = _mesa_create_visual(
88 GL_TRUE,
89 (pf->flags & PF_FLAG_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
90 GL_FALSE,
91 pf->color.redbits,
92 pf->color.greenbits,
93 pf->color.bluebits,
94 pf->alpha.alphabits,
95 0,
96 pf->depth.depthbits,
97 pf->depth.stencilbits,
98 0,
99 0,
100 0,
101 0,
102 (pf->flags & PF_FLAG_MULTISAMPLED) ? wgl_query_samples() : 0 );
103 if (visual == NULL) {
104 FREE( ctx );
105 return NULL;
106 }
107
108 pipe = stw_winsys.create_context( stw_dev->screen );
109 if (!pipe) {
110 _mesa_destroy_visual( visual );
111 FREE( ctx );
112 return NULL;
113 }
114
115 ctx->st = st_create_context( pipe, visual, NULL );
116 if (ctx->st == NULL) {
117 pipe->destroy( pipe );
118 _mesa_destroy_visual( visual );
119 FREE( ctx );
120 return NULL;
121 }
122 ctx->st->ctx->DriverCtx = ctx;
123
124 ctx->next = ctx_head;
125 ctx_head = ctx;
126
127 return (HGLRC) ctx;
128 }
129
130 WINGDIAPI HGLRC APIENTRY
131 wglCreateLayerContext(
132 HDC hdc,
133 int iLayerPlane )
134 {
135 (void) hdc;
136 (void) iLayerPlane;
137
138 return NULL;
139 }
140
141 WINGDIAPI BOOL APIENTRY
142 wglDeleteContext(
143 HGLRC hglrc )
144 {
145 struct wgl_context **link = &ctx_head;
146 struct wgl_context *ctx = ctx_head;
147
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;
153
154 /* Unbind current if deleting current context.
155 */
156 if (glcurctx == glctx)
157 st_make_current( NULL, NULL, NULL );
158
159 fb = framebuffer_from_hdc( ctx->hdc );
160 if (fb)
161 framebuffer_destroy( fb );
162
163 if (WindowFromDC( ctx->hdc ) != NULL)
164 ReleaseDC( WindowFromDC( ctx->hdc ), ctx->hdc );
165
166 st_destroy_context( ctx->st );
167
168 *link = ctx->next;
169 FREE( ctx );
170 return TRUE;
171 }
172
173 link = &ctx->next;
174 ctx = ctx->next;
175 }
176
177 return FALSE;
178 }
179
180 /* Find the width and height of the window named by hdc.
181 */
182 static void
183 get_window_size( HDC hdc, GLuint *width, GLuint *height )
184 {
185 if (WindowFromDC( hdc )) {
186 RECT rect;
187
188 GetClientRect( WindowFromDC( hdc ), &rect );
189 *width = rect.right - rect.left;
190 *height = rect.bottom - rect.top;
191 }
192 else {
193 *width = GetDeviceCaps( hdc, HORZRES );
194 *height = GetDeviceCaps( hdc, VERTRES );
195 }
196 }
197
198 WINGDIAPI HGLRC APIENTRY
199 wglGetCurrentContext( VOID )
200 {
201 return current_hrc;
202 }
203
204 WINGDIAPI HDC APIENTRY
205 wglGetCurrentDC( VOID )
206 {
207 return current_hdc;
208 }
209
210 WINGDIAPI BOOL APIENTRY
211 wglMakeCurrent(
212 HDC hdc,
213 HGLRC hglrc )
214 {
215 struct wgl_context *ctx = ctx_head;
216 GET_CURRENT_CONTEXT( glcurctx );
217 struct stw_framebuffer *fb;
218 GLuint width = 0;
219 GLuint height = 0;
220
221 current_hdc = hdc;
222 current_hrc = hglrc;
223
224 if (hdc == NULL || hglrc == NULL) {
225 st_make_current( NULL, NULL, NULL );
226 return TRUE;
227 }
228
229 while (ctx != NULL) {
230 if (ctx == (struct wgl_context *) hglrc)
231 break;
232 ctx = ctx->next;
233 }
234 if (ctx == NULL)
235 return FALSE;
236
237 /* Return if already current.
238 */
239 if (glcurctx != NULL) {
240 struct wgl_context *curctx = (struct wgl_context *) glcurctx->DriverCtx;
241
242 if (curctx != NULL && curctx == ctx && ctx->hdc == hdc)
243 return TRUE;
244 }
245
246 fb = framebuffer_from_hdc( hdc );
247
248 if (hdc != NULL)
249 get_window_size( hdc, &width, &height );
250
251 /* Lazy creation of framebuffers.
252 */
253 if (fb == NULL && ctx != NULL && hdc != NULL) {
254 GLvisual *visual = &ctx->st->ctx->Visual;
255
256 fb = framebuffer_create( hdc, visual, width, height );
257 if (fb == NULL)
258 return FALSE;
259
260 fb->dib_hDC = CreateCompatibleDC( hdc );
261 fb->hbmDIB = NULL;
262 fb->pbPixels = NULL;
263 }
264
265 if (ctx && fb) {
266 st_make_current( ctx->st, fb->stfb, fb->stfb );
267 framebuffer_resize( fb, width, height );
268 }
269 else {
270 /* Detach */
271 st_make_current( NULL, NULL, NULL );
272 }
273
274 return TRUE;
275 }
276
277 struct wgl_context *
278 wgl_context_from_hdc(
279 HDC hdc )
280 {
281 struct wgl_context *ctx = ctx_head;
282
283 while (ctx != NULL) {
284 if (ctx->hdc == hdc)
285 return ctx;
286 ctx = ctx->next;
287 }
288 return NULL;
289 }
290
291 #include "stw_wgl.c"