Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / state_trackers / wgl / stw_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2008-2009 Vmware, Inc.
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 VMWARE 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/context.h"
31 #include "pipe/p_format.h"
32 #include "pipe/p_screen.h"
33 #include "util/u_format.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_texture.h"
40 #endif
41
42 #include "stw_icd.h"
43 #include "stw_framebuffer.h"
44 #include "stw_device.h"
45 #include "stw_winsys.h"
46 #include "stw_tls.h"
47
48
49 /**
50 * Search the framebuffer with the matching HWND while holding the
51 * stw_dev::fb_mutex global lock.
52 */
53 static INLINE struct stw_framebuffer *
54 stw_framebuffer_from_hwnd_locked(
55 HWND hwnd )
56 {
57 struct stw_framebuffer *fb;
58
59 for (fb = stw_dev->fb_head; fb != NULL; fb = fb->next)
60 if (fb->hWnd == hwnd) {
61 pipe_mutex_lock(fb->mutex);
62 break;
63 }
64
65 return fb;
66 }
67
68
69 /**
70 * Destroy this framebuffer. Both stw_dev::fb_mutex and stw_framebuffer::mutex
71 * must be held, by this order. Obviously no further access to fb can be done
72 * after this.
73 */
74 static INLINE void
75 stw_framebuffer_destroy_locked(
76 struct stw_framebuffer *fb )
77 {
78 struct stw_framebuffer **link;
79
80 link = &stw_dev->fb_head;
81 while (*link != fb)
82 link = &(*link)->next;
83 assert(*link);
84 *link = fb->next;
85 fb->next = NULL;
86
87 if(fb->shared_surface)
88 stw_dev->stw_winsys->shared_surface_close(stw_dev->screen, fb->shared_surface);
89
90 st_unreference_framebuffer(fb->stfb);
91
92 pipe_mutex_unlock( fb->mutex );
93
94 pipe_mutex_destroy( fb->mutex );
95
96 FREE( fb );
97 }
98
99
100 void
101 stw_framebuffer_release(
102 struct stw_framebuffer *fb)
103 {
104 assert(fb);
105 pipe_mutex_unlock( fb->mutex );
106 }
107
108
109 static INLINE void
110 stw_framebuffer_get_size( struct stw_framebuffer *fb )
111 {
112 unsigned width, height;
113 RECT client_rect;
114 RECT window_rect;
115 POINT client_pos;
116
117 assert(fb->hWnd);
118
119 /* Get the client area size. */
120 GetClientRect( fb->hWnd, &client_rect );
121 assert(client_rect.left == 0);
122 assert(client_rect.top == 0);
123 width = client_rect.right - client_rect.left;
124 height = client_rect.bottom - client_rect.top;
125
126 if(width < 1)
127 width = 1;
128 if(height < 1)
129 height = 1;
130
131 if(width != fb->width || height != fb->height) {
132 fb->must_resize = TRUE;
133 fb->width = width;
134 fb->height = height;
135 }
136
137 client_pos.x = 0;
138 client_pos.y = 0;
139 ClientToScreen(fb->hWnd, &client_pos);
140
141 GetWindowRect(fb->hWnd, &window_rect);
142
143 fb->client_rect.left = client_pos.x - window_rect.left;
144 fb->client_rect.top = client_pos.y - window_rect.top;
145 fb->client_rect.right = fb->client_rect.left + fb->width;
146 fb->client_rect.bottom = fb->client_rect.top + fb->height;
147
148 #if 0
149 debug_printf("\n");
150 debug_printf("%s: client_position = (%i, %i)\n",
151 __FUNCTION__, client_pos.x, client_pos.y);
152 debug_printf("%s: window_rect = (%i, %i) - (%i, %i)\n",
153 __FUNCTION__,
154 window_rect.left, window_rect.top,
155 window_rect.right, window_rect.bottom);
156 debug_printf("%s: client_rect = (%i, %i) - (%i, %i)\n",
157 __FUNCTION__,
158 fb->client_rect.left, fb->client_rect.top,
159 fb->client_rect.right, fb->client_rect.bottom);
160 #endif
161 }
162
163
164 /**
165 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
166 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
167 */
168 LRESULT CALLBACK
169 stw_call_window_proc(
170 int nCode,
171 WPARAM wParam,
172 LPARAM lParam )
173 {
174 struct stw_tls_data *tls_data;
175 PCWPSTRUCT pParams = (PCWPSTRUCT)lParam;
176 struct stw_framebuffer *fb;
177
178 tls_data = stw_tls_get_data();
179 if(!tls_data)
180 return 0;
181
182 if (nCode < 0)
183 return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
184
185 if (pParams->message == WM_WINDOWPOSCHANGED) {
186 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according to
187 * http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
188 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
189 * can be masked out by the application. */
190 LPWINDOWPOS lpWindowPos = (LPWINDOWPOS)pParams->lParam;
191 if((lpWindowPos->flags & SWP_SHOWWINDOW) ||
192 !(lpWindowPos->flags & SWP_NOMOVE) ||
193 !(lpWindowPos->flags & SWP_NOSIZE)) {
194 fb = stw_framebuffer_from_hwnd( pParams->hwnd );
195 if(fb) {
196 /* Size in WINDOWPOS includes the window frame, so get the size
197 * of the client area via GetClientRect. */
198 stw_framebuffer_get_size(fb);
199 stw_framebuffer_release(fb);
200 }
201 }
202 }
203 else if (pParams->message == WM_DESTROY) {
204 pipe_mutex_lock( stw_dev->fb_mutex );
205 fb = stw_framebuffer_from_hwnd_locked( pParams->hwnd );
206 if(fb)
207 stw_framebuffer_destroy_locked(fb);
208 pipe_mutex_unlock( stw_dev->fb_mutex );
209 }
210
211 return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
212 }
213
214
215 struct stw_framebuffer *
216 stw_framebuffer_create(
217 HDC hdc,
218 int iPixelFormat )
219 {
220 HWND hWnd;
221 struct stw_framebuffer *fb;
222 const struct stw_pixelformat_info *pfi;
223
224 /* We only support drawing to a window. */
225 hWnd = WindowFromDC( hdc );
226 if(!hWnd)
227 return NULL;
228
229 fb = CALLOC_STRUCT( stw_framebuffer );
230 if (fb == NULL)
231 return NULL;
232
233 fb->hDC = hdc;
234 fb->hWnd = hWnd;
235 fb->iPixelFormat = iPixelFormat;
236
237 fb->pfi = pfi = stw_pixelformat_get_info( iPixelFormat - 1 );
238
239 stw_pixelformat_visual(&fb->visual, pfi);
240
241 stw_framebuffer_get_size(fb);
242
243 pipe_mutex_init( fb->mutex );
244
245 /* This is the only case where we lock the stw_framebuffer::mutex before
246 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
247 * and we must prevent any other thread from destroying it before we return.
248 */
249 pipe_mutex_lock( fb->mutex );
250
251 pipe_mutex_lock( stw_dev->fb_mutex );
252 fb->next = stw_dev->fb_head;
253 stw_dev->fb_head = fb;
254 pipe_mutex_unlock( stw_dev->fb_mutex );
255
256 return fb;
257 }
258
259
260 BOOL
261 stw_framebuffer_allocate(
262 struct stw_framebuffer *fb)
263 {
264 assert(fb);
265
266 if(!fb->stfb) {
267 const struct stw_pixelformat_info *pfi = fb->pfi;
268 enum pipe_format colorFormat, depthFormat, stencilFormat;
269
270 colorFormat = pfi->color_format;
271
272 if(util_format_get_component_bits(pfi->depth_stencil_format, UTIL_FORMAT_COLORSPACE_ZS, 0))
273 depthFormat = pfi->depth_stencil_format;
274 else
275 depthFormat = PIPE_FORMAT_NONE;
276
277 if(util_format_get_component_bits(pfi->depth_stencil_format, UTIL_FORMAT_COLORSPACE_ZS, 1))
278 stencilFormat = pfi->depth_stencil_format;
279 else
280 stencilFormat = PIPE_FORMAT_NONE;
281
282 assert(fb->must_resize);
283 assert(fb->width);
284 assert(fb->height);
285
286 fb->stfb = st_create_framebuffer(
287 &fb->visual,
288 colorFormat,
289 depthFormat,
290 stencilFormat,
291 fb->width,
292 fb->height,
293 (void *) fb );
294
295 // to notify the context
296 fb->must_resize = TRUE;
297 }
298
299 return fb->stfb ? TRUE : FALSE;
300 }
301
302
303 /**
304 * Update the framebuffer's size if necessary.
305 */
306 void
307 stw_framebuffer_update(
308 struct stw_framebuffer *fb)
309 {
310 assert(fb->stfb);
311 assert(fb->height);
312 assert(fb->width);
313
314 /* XXX: It would be nice to avoid checking the size again -- in theory
315 * stw_call_window_proc would have cought the resize and stored the right
316 * size already, but unfortunately threads created before the DllMain is
317 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
318 * to know of their existing without using the not very portable PSAPI.
319 */
320 stw_framebuffer_get_size(fb);
321
322 if(fb->must_resize) {
323 st_resize_framebuffer(fb->stfb, fb->width, fb->height);
324 fb->must_resize = FALSE;
325 }
326 }
327
328
329 void
330 stw_framebuffer_cleanup( void )
331 {
332 struct stw_framebuffer *fb;
333 struct stw_framebuffer *next;
334
335 pipe_mutex_lock( stw_dev->fb_mutex );
336
337 fb = stw_dev->fb_head;
338 while (fb) {
339 next = fb->next;
340
341 pipe_mutex_lock(fb->mutex);
342 stw_framebuffer_destroy_locked(fb);
343
344 fb = next;
345 }
346 stw_dev->fb_head = NULL;
347
348 pipe_mutex_unlock( stw_dev->fb_mutex );
349 }
350
351
352 /**
353 * Given an hdc, return the corresponding stw_framebuffer.
354 */
355 static INLINE struct stw_framebuffer *
356 stw_framebuffer_from_hdc_locked(
357 HDC hdc )
358 {
359 HWND hwnd;
360 struct stw_framebuffer *fb;
361
362 /*
363 * Some applications create and use several HDCs for the same window, so
364 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
365 * possible.
366 */
367 hwnd = WindowFromDC(hdc);
368 if(hwnd)
369 return stw_framebuffer_from_hwnd_locked(hwnd);
370
371 for (fb = stw_dev->fb_head; fb != NULL; fb = fb->next)
372 if (fb->hDC == hdc) {
373 pipe_mutex_lock(fb->mutex);
374 break;
375 }
376
377 return fb;
378 }
379
380
381 /**
382 * Given an hdc, return the corresponding stw_framebuffer.
383 */
384 struct stw_framebuffer *
385 stw_framebuffer_from_hdc(
386 HDC hdc )
387 {
388 struct stw_framebuffer *fb;
389
390 pipe_mutex_lock( stw_dev->fb_mutex );
391 fb = stw_framebuffer_from_hdc_locked(hdc);
392 pipe_mutex_unlock( stw_dev->fb_mutex );
393
394 return fb;
395 }
396
397
398 /**
399 * Given an hdc, return the corresponding stw_framebuffer.
400 */
401 struct stw_framebuffer *
402 stw_framebuffer_from_hwnd(
403 HWND hwnd )
404 {
405 struct stw_framebuffer *fb;
406
407 pipe_mutex_lock( stw_dev->fb_mutex );
408 fb = stw_framebuffer_from_hwnd_locked(hwnd);
409 pipe_mutex_unlock( stw_dev->fb_mutex );
410
411 return fb;
412 }
413
414
415 BOOL APIENTRY
416 DrvSetPixelFormat(
417 HDC hdc,
418 LONG iPixelFormat )
419 {
420 uint count;
421 uint index;
422 struct stw_framebuffer *fb;
423
424 index = (uint) iPixelFormat - 1;
425 count = stw_pixelformat_get_extended_count();
426 if (index >= count)
427 return FALSE;
428
429 fb = stw_framebuffer_from_hdc_locked(hdc);
430 if(fb) {
431 /* SetPixelFormat must be called only once */
432 stw_framebuffer_release( fb );
433 return FALSE;
434 }
435
436 fb = stw_framebuffer_create(hdc, iPixelFormat);
437 if(!fb) {
438 return FALSE;
439 }
440
441 stw_framebuffer_release( fb );
442
443 /* Some applications mistakenly use the undocumented wglSetPixelFormat
444 * function instead of SetPixelFormat, so we call SetPixelFormat here to
445 * avoid opengl32.dll's wglCreateContext to fail */
446 if (GetPixelFormat(hdc) == 0) {
447 SetPixelFormat(hdc, iPixelFormat, NULL);
448 }
449
450 return TRUE;
451 }
452
453
454 int
455 stw_pixelformat_get(
456 HDC hdc )
457 {
458 int iPixelFormat = 0;
459 struct stw_framebuffer *fb;
460
461 fb = stw_framebuffer_from_hdc(hdc);
462 if(fb) {
463 iPixelFormat = fb->iPixelFormat;
464 stw_framebuffer_release(fb);
465 }
466
467 return iPixelFormat;
468 }
469
470
471 BOOL APIENTRY
472 DrvPresentBuffers(HDC hdc, PGLPRESENTBUFFERSDATA data)
473 {
474 struct stw_framebuffer *fb;
475 struct pipe_screen *screen;
476 struct pipe_surface *surface;
477
478 fb = stw_framebuffer_from_hdc( hdc );
479 if (fb == NULL)
480 return FALSE;
481
482 screen = stw_dev->screen;
483
484 surface = (struct pipe_surface *)data->pPrivateData;
485
486 #ifdef DEBUG
487 if(stw_dev->trace_running) {
488 screen = trace_screen(screen)->screen;
489 surface = trace_surface(surface)->surface;
490 }
491 #endif
492
493 if(data->hSharedSurface != fb->hSharedSurface) {
494 if(fb->shared_surface) {
495 stw_dev->stw_winsys->shared_surface_close(screen, fb->shared_surface);
496 fb->shared_surface = NULL;
497 }
498
499 fb->hSharedSurface = data->hSharedSurface;
500
501 if(data->hSharedSurface &&
502 stw_dev->stw_winsys->shared_surface_open) {
503 fb->shared_surface = stw_dev->stw_winsys->shared_surface_open(screen, fb->hSharedSurface);
504 }
505 }
506
507 if(fb->shared_surface) {
508 stw_dev->stw_winsys->compose(screen,
509 surface,
510 fb->shared_surface,
511 &fb->client_rect,
512 data->PresentHistoryToken);
513 }
514 else {
515 stw_dev->stw_winsys->present( screen, surface, hdc );
516 }
517
518 stw_framebuffer_update(fb);
519
520 stw_framebuffer_release(fb);
521
522 return TRUE;
523 }
524
525
526 /**
527 * Queue a composition.
528 *
529 * It will drop the lock on success.
530 */
531 BOOL
532 stw_framebuffer_present_locked(HDC hdc,
533 struct stw_framebuffer *fb,
534 struct pipe_surface *surface)
535 {
536 if(stw_dev->callbacks.wglCbPresentBuffers &&
537 stw_dev->stw_winsys->compose) {
538 GLCBPRESENTBUFFERSDATA data;
539
540 memset(&data, 0, sizeof data);
541 data.magic1 = 2;
542 data.magic2 = 0;
543 data.AdapterLuid = stw_dev->AdapterLuid;
544 data.rect = fb->client_rect;
545 data.pPrivateData = (void *)surface;
546
547 stw_framebuffer_release(fb);
548
549 return stw_dev->callbacks.wglCbPresentBuffers(hdc, &data);
550 }
551 else {
552 struct pipe_screen *screen = stw_dev->screen;
553
554 #ifdef DEBUG
555 if(stw_dev->trace_running) {
556 screen = trace_screen(screen)->screen;
557 surface = trace_surface(surface)->surface;
558 }
559 #endif
560
561 stw_dev->stw_winsys->present( screen, surface, hdc );
562
563 stw_framebuffer_update(fb);
564
565 stw_framebuffer_release(fb);
566
567 return TRUE;
568 }
569 }
570
571
572 BOOL APIENTRY
573 DrvSwapBuffers(
574 HDC hdc )
575 {
576 struct stw_framebuffer *fb;
577 struct pipe_surface *surface = NULL;
578
579 fb = stw_framebuffer_from_hdc( hdc );
580 if (fb == NULL)
581 return FALSE;
582
583 if (!(fb->pfi->pfd.dwFlags & PFD_DOUBLEBUFFER)) {
584 stw_framebuffer_release(fb);
585 return TRUE;
586 }
587
588 st_swapbuffers(fb->stfb, &surface, NULL);
589
590 return stw_framebuffer_present_locked(hdc, fb, surface);
591 }
592
593
594 BOOL APIENTRY
595 DrvSwapLayerBuffers(
596 HDC hdc,
597 UINT fuPlanes )
598 {
599 if(fuPlanes & WGL_SWAP_MAIN_PLANE)
600 return DrvSwapBuffers(hdc);
601
602 return FALSE;
603 }