09dba61603f277156a290bb1d8ff911fa3a0c040
[mesa.git] / src / gallium / state_trackers / wgl / shared / stw_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
37 #ifdef DEBUG
38 #include "trace/tr_screen.h"
39 #include "trace/tr_context.h"
40 #endif
41
42 #include "shared/stw_device.h"
43 #include "shared/stw_winsys.h"
44 #include "shared/stw_framebuffer.h"
45 #include "shared/stw_pixelformat.h"
46 #include "stw_public.h"
47 #include "stw_context.h"
48 #include "stw_tls.h"
49
50 BOOL
51 stw_copy_context(
52 UINT_PTR hglrcSrc,
53 UINT_PTR hglrcDst,
54 UINT mask )
55 {
56 struct stw_context *src;
57 struct stw_context *dst;
58 BOOL ret = FALSE;
59
60 pipe_mutex_lock( stw_dev->mutex );
61
62 src = stw_lookup_context_locked( hglrcSrc );
63 dst = stw_lookup_context_locked( hglrcDst );
64
65 if (src && dst) {
66 /* FIXME */
67 assert(0);
68 (void) src;
69 (void) dst;
70 (void) mask;
71 }
72
73 pipe_mutex_unlock( stw_dev->mutex );
74
75 return ret;
76 }
77
78 BOOL
79 stw_share_lists(
80 UINT_PTR hglrc1,
81 UINT_PTR hglrc2 )
82 {
83 struct stw_context *ctx1;
84 struct stw_context *ctx2;
85 BOOL ret = FALSE;
86
87 pipe_mutex_lock( stw_dev->mutex );
88
89 ctx1 = stw_lookup_context_locked( hglrc1 );
90 ctx2 = stw_lookup_context_locked( hglrc2 );
91
92 if (ctx1 && ctx2 &&
93 ctx1->pfi == ctx2->pfi) {
94 ret = _mesa_share_state(ctx2->st->ctx, ctx1->st->ctx);
95 }
96
97 pipe_mutex_unlock( stw_dev->mutex );
98
99 return ret;
100 }
101
102 UINT_PTR
103 stw_create_layer_context(
104 HDC hdc,
105 int iLayerPlane )
106 {
107 uint pfi;
108 const struct stw_pixelformat_info *pf = NULL;
109 struct stw_context *ctx = NULL;
110 GLvisual *visual = NULL;
111 struct pipe_screen *screen = NULL;
112 struct pipe_context *pipe = NULL;
113
114 if(!stw_dev)
115 return 0;
116
117 if (iLayerPlane != 0)
118 return 0;
119
120 pfi = stw_pixelformat_get( hdc );
121 if (pfi == 0)
122 return 0;
123
124 pf = stw_pixelformat_get_info( pfi - 1 );
125
126 ctx = CALLOC_STRUCT( stw_context );
127 if (ctx == NULL)
128 goto no_ctx;
129
130 ctx->hdc = hdc;
131
132 /* Create visual based on flags
133 */
134 visual = _mesa_create_visual(
135 (pf->pfd.iPixelType == PFD_TYPE_RGBA) ? GL_TRUE : GL_FALSE,
136 (pf->pfd.dwFlags & PFD_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
137 (pf->pfd.dwFlags & PFD_STEREO) ? GL_TRUE : GL_FALSE,
138 pf->pfd.cRedBits,
139 pf->pfd.cGreenBits,
140 pf->pfd.cBlueBits,
141 pf->pfd.cAlphaBits,
142 (pf->pfd.iPixelType == PFD_TYPE_COLORINDEX) ? pf->pfd.cColorBits : 0,
143 pf->pfd.cDepthBits,
144 pf->pfd.cStencilBits,
145 pf->pfd.cAccumRedBits,
146 pf->pfd.cAccumGreenBits,
147 pf->pfd.cAccumBlueBits,
148 pf->pfd.cAccumAlphaBits,
149 pf->numSamples );
150 if (visual == NULL)
151 goto no_visual;
152
153 screen = stw_dev->screen;
154
155 #ifdef DEBUG
156 /* Unwrap screen */
157 if(stw_dev->trace_running)
158 screen = trace_screen(screen)->screen;
159 #endif
160
161 pipe = stw_dev->stw_winsys->create_context( screen );
162 if (pipe == NULL)
163 goto no_pipe;
164
165 #ifdef DEBUG
166 /* Wrap context */
167 if(stw_dev->trace_running)
168 pipe = trace_context_create(stw_dev->screen, pipe);
169 #endif
170
171 assert(!pipe->priv);
172 pipe->priv = hdc;
173
174 ctx->st = st_create_context( pipe, visual, NULL );
175 if (ctx->st == NULL)
176 goto no_st_ctx;
177
178 ctx->st->ctx->DriverCtx = ctx;
179 ctx->pfi = pf;
180
181 pipe_mutex_lock( stw_dev->mutex );
182 ctx->hglrc = handle_table_add(stw_dev->ctx_table, ctx);
183 pipe_mutex_unlock( stw_dev->mutex );
184 if (!ctx->hglrc)
185 goto no_hglrc;
186
187 return ctx->hglrc;
188
189 no_hglrc:
190 st_destroy_context(ctx->st);
191 goto no_pipe; /* st_context_destroy already destroys pipe */
192 no_st_ctx:
193 pipe->destroy( pipe );
194 no_pipe:
195 _mesa_destroy_visual( visual );
196 no_visual:
197 FREE( ctx );
198 no_ctx:
199 return 0;
200 }
201
202 BOOL
203 stw_delete_context(
204 UINT_PTR hglrc )
205 {
206 struct stw_context *ctx ;
207 BOOL ret = FALSE;
208
209 if (!stw_dev)
210 return FALSE;
211
212 pipe_mutex_lock( stw_dev->mutex );
213 ctx = stw_lookup_context_locked(hglrc);
214 handle_table_remove(stw_dev->ctx_table, hglrc);
215 pipe_mutex_unlock( stw_dev->mutex );
216
217 if (ctx) {
218 GLcontext *glctx = ctx->st->ctx;
219 GET_CURRENT_CONTEXT( glcurctx );
220 struct stw_framebuffer *fb;
221
222 /* Unbind current if deleting current context.
223 */
224 if (glcurctx == glctx)
225 st_make_current( NULL, NULL, NULL );
226
227 fb = stw_framebuffer_from_hdc( ctx->hdc );
228 if (fb)
229 stw_framebuffer_destroy( fb );
230
231 if (WindowFromDC( ctx->hdc ) != NULL)
232 ReleaseDC( WindowFromDC( ctx->hdc ), ctx->hdc );
233
234 st_destroy_context(ctx->st);
235 FREE(ctx);
236
237 ret = TRUE;
238 }
239
240 return ret;
241 }
242
243 BOOL
244 stw_release_context(
245 UINT_PTR hglrc )
246 {
247 struct stw_context *ctx;
248
249 if (!stw_dev)
250 return FALSE;
251
252 pipe_mutex_lock( stw_dev->mutex );
253 ctx = stw_lookup_context_locked( hglrc );
254 pipe_mutex_unlock( stw_dev->mutex );
255
256 if (!ctx)
257 return FALSE;
258
259 /* The expectation is that ctx is the same context which is
260 * current for this thread. We should check that and return False
261 * if not the case.
262 */
263 {
264 GLcontext *glctx = ctx->st->ctx;
265 GET_CURRENT_CONTEXT( glcurctx );
266
267 if (glcurctx != glctx)
268 return FALSE;
269 }
270
271 if (stw_make_current( NULL, 0 ) == FALSE)
272 return FALSE;
273
274 return TRUE;
275 }
276
277 /* Find the width and height of the window named by hdc.
278 */
279 static void
280 stw_get_window_size( HDC hdc, GLuint *pwidth, GLuint *pheight )
281 {
282 GLuint width, height;
283 HWND hwnd;
284
285 hwnd = WindowFromDC( hdc );
286 if (hwnd) {
287 RECT rect;
288 GetClientRect( hwnd, &rect );
289 width = rect.right - rect.left;
290 height = rect.bottom - rect.top;
291 }
292 else {
293 width = GetDeviceCaps( hdc, HORZRES );
294 height = GetDeviceCaps( hdc, VERTRES );
295 }
296
297 if(width < 1)
298 width = 1;
299 if(height < 1)
300 height = 1;
301
302 *pwidth = width;
303 *pheight = height;
304 }
305
306 UINT_PTR
307 stw_get_current_context( void )
308 {
309 GET_CURRENT_CONTEXT( glcurctx );
310 struct stw_context *ctx;
311
312 if(!glcurctx)
313 return 0;
314
315 ctx = (struct stw_context *)glcurctx->DriverCtx;
316 assert(ctx);
317 if(!ctx)
318 return 0;
319
320 return ctx->hglrc;
321 }
322
323 HDC
324 stw_get_current_dc( void )
325 {
326 GET_CURRENT_CONTEXT( glcurctx );
327 struct stw_context *ctx;
328
329 if(!glcurctx)
330 return NULL;
331
332 ctx = (struct stw_context *)glcurctx->DriverCtx;
333 assert(ctx);
334 if(!ctx)
335 return NULL;
336
337 return ctx->hdc;
338 }
339
340 BOOL
341 stw_make_current(
342 HDC hdc,
343 UINT_PTR hglrc )
344 {
345 struct stw_context *ctx;
346 GET_CURRENT_CONTEXT( glcurctx );
347 struct stw_framebuffer *fb;
348 GLuint width = 0;
349 GLuint height = 0;
350 struct stw_context *curctx = NULL;
351
352 if (!stw_dev)
353 return FALSE;
354
355 pipe_mutex_lock( stw_dev->mutex );
356 ctx = stw_lookup_context_locked( hglrc );
357 pipe_mutex_unlock( stw_dev->mutex );
358
359 if (glcurctx != NULL) {
360 curctx = (struct stw_context *) glcurctx->DriverCtx;
361
362 if (curctx != ctx)
363 st_flush(glcurctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
364 }
365
366 if (hdc == NULL || hglrc == 0) {
367 st_make_current( NULL, NULL, NULL );
368 return TRUE;
369 }
370
371 /* Return if already current.
372 */
373 if (glcurctx != NULL) {
374 if (curctx != NULL && curctx == ctx && ctx->hdc == hdc)
375 return TRUE;
376 }
377
378 fb = stw_framebuffer_from_hdc( hdc );
379
380 if (hdc != NULL)
381 stw_get_window_size( hdc, &width, &height );
382
383 /* Lazy creation of stw_framebuffers.
384 */
385 if (fb == NULL && ctx != NULL && hdc != NULL) {
386 GLvisual *visual = &ctx->st->ctx->Visual;
387
388 fb = stw_framebuffer_create( hdc, visual, ctx->pfi, width, height );
389 if (fb == NULL)
390 return FALSE;
391 }
392
393 if (ctx && fb) {
394 pipe_mutex_lock( fb->mutex );
395 st_make_current( ctx->st, fb->stfb, fb->stfb );
396 st_resize_framebuffer( fb->stfb, width, height );
397 pipe_mutex_unlock( fb->mutex );
398
399 ctx->hdc = hdc;
400 ctx->st->pipe->priv = hdc;
401 }
402 else {
403 /* Detach */
404 st_make_current( NULL, NULL, NULL );
405 }
406
407 return TRUE;
408 }