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