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