02de21ccb2b323be513b8b3ae7118e4f5f1c3a58
[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 || !stw_dev)
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 if (!stw_dev)
336 return;
337
338 pipe_mutex_lock( stw_dev->fb_mutex );
339
340 fb = stw_dev->fb_head;
341 while (fb) {
342 next = fb->next;
343
344 pipe_mutex_lock(fb->mutex);
345 stw_framebuffer_destroy_locked(fb);
346
347 fb = next;
348 }
349 stw_dev->fb_head = NULL;
350
351 pipe_mutex_unlock( stw_dev->fb_mutex );
352 }
353
354
355 /**
356 * Given an hdc, return the corresponding stw_framebuffer.
357 */
358 static INLINE struct stw_framebuffer *
359 stw_framebuffer_from_hdc_locked(
360 HDC hdc )
361 {
362 HWND hwnd;
363 struct stw_framebuffer *fb;
364
365 /*
366 * Some applications create and use several HDCs for the same window, so
367 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
368 * possible.
369 */
370 hwnd = WindowFromDC(hdc);
371 if(hwnd)
372 return stw_framebuffer_from_hwnd_locked(hwnd);
373
374 for (fb = stw_dev->fb_head; fb != NULL; fb = fb->next)
375 if (fb->hDC == hdc) {
376 pipe_mutex_lock(fb->mutex);
377 break;
378 }
379
380 return fb;
381 }
382
383
384 /**
385 * Given an hdc, return the corresponding stw_framebuffer.
386 */
387 struct stw_framebuffer *
388 stw_framebuffer_from_hdc(
389 HDC hdc )
390 {
391 struct stw_framebuffer *fb;
392
393 if (!stw_dev)
394 return NULL;
395
396 pipe_mutex_lock( stw_dev->fb_mutex );
397 fb = stw_framebuffer_from_hdc_locked(hdc);
398 pipe_mutex_unlock( stw_dev->fb_mutex );
399
400 return fb;
401 }
402
403
404 /**
405 * Given an hdc, return the corresponding stw_framebuffer.
406 */
407 struct stw_framebuffer *
408 stw_framebuffer_from_hwnd(
409 HWND hwnd )
410 {
411 struct stw_framebuffer *fb;
412
413 pipe_mutex_lock( stw_dev->fb_mutex );
414 fb = stw_framebuffer_from_hwnd_locked(hwnd);
415 pipe_mutex_unlock( stw_dev->fb_mutex );
416
417 return fb;
418 }
419
420
421 BOOL APIENTRY
422 DrvSetPixelFormat(
423 HDC hdc,
424 LONG iPixelFormat )
425 {
426 uint count;
427 uint index;
428 struct stw_framebuffer *fb;
429
430 if (!stw_dev)
431 return FALSE;
432
433 index = (uint) iPixelFormat - 1;
434 count = stw_pixelformat_get_extended_count();
435 if (index >= count)
436 return FALSE;
437
438 fb = stw_framebuffer_from_hdc_locked(hdc);
439 if(fb) {
440 /* SetPixelFormat must be called only once */
441 stw_framebuffer_release( fb );
442 return FALSE;
443 }
444
445 fb = stw_framebuffer_create(hdc, iPixelFormat);
446 if(!fb) {
447 return FALSE;
448 }
449
450 stw_framebuffer_release( fb );
451
452 /* Some applications mistakenly use the undocumented wglSetPixelFormat
453 * function instead of SetPixelFormat, so we call SetPixelFormat here to
454 * avoid opengl32.dll's wglCreateContext to fail */
455 if (GetPixelFormat(hdc) == 0) {
456 SetPixelFormat(hdc, iPixelFormat, NULL);
457 }
458
459 return TRUE;
460 }
461
462
463 int
464 stw_pixelformat_get(
465 HDC hdc )
466 {
467 int iPixelFormat = 0;
468 struct stw_framebuffer *fb;
469
470 fb = stw_framebuffer_from_hdc(hdc);
471 if(fb) {
472 iPixelFormat = fb->iPixelFormat;
473 stw_framebuffer_release(fb);
474 }
475
476 return iPixelFormat;
477 }
478
479
480 BOOL APIENTRY
481 DrvPresentBuffers(HDC hdc, PGLPRESENTBUFFERSDATA data)
482 {
483 struct stw_framebuffer *fb;
484 struct pipe_screen *screen;
485 struct pipe_surface *surface;
486
487 if (!stw_dev)
488 return FALSE;
489
490 fb = stw_framebuffer_from_hdc( hdc );
491 if (fb == NULL)
492 return FALSE;
493
494 screen = stw_dev->screen;
495
496 surface = (struct pipe_surface *)data->pPrivateData;
497
498 #ifdef DEBUG
499 if(stw_dev->trace_running) {
500 screen = trace_screen(screen)->screen;
501 surface = trace_surface(surface)->surface;
502 }
503 #endif
504
505 if(data->hSharedSurface != fb->hSharedSurface) {
506 if(fb->shared_surface) {
507 stw_dev->stw_winsys->shared_surface_close(screen, fb->shared_surface);
508 fb->shared_surface = NULL;
509 }
510
511 fb->hSharedSurface = data->hSharedSurface;
512
513 if(data->hSharedSurface &&
514 stw_dev->stw_winsys->shared_surface_open) {
515 fb->shared_surface = stw_dev->stw_winsys->shared_surface_open(screen, fb->hSharedSurface);
516 }
517 }
518
519 if(fb->shared_surface) {
520 stw_dev->stw_winsys->compose(screen,
521 surface,
522 fb->shared_surface,
523 &fb->client_rect,
524 data->PresentHistoryToken);
525 }
526 else {
527 stw_dev->stw_winsys->present( screen, surface, hdc );
528 }
529
530 stw_framebuffer_update(fb);
531
532 stw_framebuffer_release(fb);
533
534 return TRUE;
535 }
536
537
538 /**
539 * Queue a composition.
540 *
541 * It will drop the lock on success.
542 */
543 BOOL
544 stw_framebuffer_present_locked(HDC hdc,
545 struct stw_framebuffer *fb,
546 struct pipe_surface *surface)
547 {
548 if(stw_dev->callbacks.wglCbPresentBuffers &&
549 stw_dev->stw_winsys->compose) {
550 GLCBPRESENTBUFFERSDATA data;
551
552 memset(&data, 0, sizeof data);
553 data.magic1 = 2;
554 data.magic2 = 0;
555 data.AdapterLuid = stw_dev->AdapterLuid;
556 data.rect = fb->client_rect;
557 data.pPrivateData = (void *)surface;
558
559 stw_framebuffer_release(fb);
560
561 return stw_dev->callbacks.wglCbPresentBuffers(hdc, &data);
562 }
563 else {
564 struct pipe_screen *screen = stw_dev->screen;
565
566 #ifdef DEBUG
567 if(stw_dev->trace_running) {
568 screen = trace_screen(screen)->screen;
569 surface = trace_surface(surface)->surface;
570 }
571 #endif
572
573 stw_dev->stw_winsys->present( screen, surface, hdc );
574
575 stw_framebuffer_update(fb);
576
577 stw_framebuffer_release(fb);
578
579 return TRUE;
580 }
581 }
582
583
584 BOOL APIENTRY
585 DrvSwapBuffers(
586 HDC hdc )
587 {
588 struct stw_framebuffer *fb;
589 struct pipe_surface *surface = NULL;
590
591 if (!stw_dev)
592 return FALSE;
593
594 fb = stw_framebuffer_from_hdc( hdc );
595 if (fb == NULL)
596 return FALSE;
597
598 if (!(fb->pfi->pfd.dwFlags & PFD_DOUBLEBUFFER)) {
599 stw_framebuffer_release(fb);
600 return TRUE;
601 }
602
603 st_swapbuffers(fb->stfb, &surface, NULL);
604
605 return stw_framebuffer_present_locked(hdc, fb, surface);
606 }
607
608
609 BOOL APIENTRY
610 DrvSwapLayerBuffers(
611 HDC hdc,
612 UINT fuPlanes )
613 {
614 if(fuPlanes & WGL_SWAP_MAIN_PLANE)
615 return DrvSwapBuffers(hdc);
616
617 return FALSE;
618 }