1 /**************************************************************************
3 * Copyright 2008-2009 Vmware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
37 #include "stw_framebuffer.h"
38 #include "stw_device.h"
39 #include "stw_winsys.h"
41 #include "stw_context.h"
46 * Search the framebuffer with the matching HWND while holding the
47 * stw_dev::fb_mutex global lock.
49 static INLINE
struct stw_framebuffer
*
50 stw_framebuffer_from_hwnd_locked(
53 struct stw_framebuffer
*fb
;
55 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
56 if (fb
->hWnd
== hwnd
) {
57 pipe_mutex_lock(fb
->mutex
);
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.
71 stw_framebuffer_destroy_locked(
72 struct stw_framebuffer
*fb
)
74 struct stw_framebuffer
**link
;
76 /* check the reference count */
79 pipe_mutex_unlock( fb
->mutex
);
83 link
= &stw_dev
->fb_head
;
85 link
= &(*link
)->next
;
90 if(fb
->shared_surface
)
91 stw_dev
->stw_winsys
->shared_surface_close(stw_dev
->screen
, fb
->shared_surface
);
93 stw_st_destroy_framebuffer_locked(fb
->stfb
);
95 pipe_mutex_unlock( fb
->mutex
);
97 pipe_mutex_destroy( fb
->mutex
);
104 stw_framebuffer_release(
105 struct stw_framebuffer
*fb
)
108 pipe_mutex_unlock( fb
->mutex
);
113 stw_framebuffer_get_size( struct stw_framebuffer
*fb
)
125 assert(fb
->width
&& fb
->height
);
126 assert(fb
->client_rect
.right
== fb
->client_rect
.left
+ fb
->width
);
127 assert(fb
->client_rect
.bottom
== fb
->client_rect
.top
+ fb
->height
);
130 * Get the client area size.
133 if (!GetClientRect(fb
->hWnd
, &client_rect
)) {
137 assert(client_rect
.left
== 0);
138 assert(client_rect
.top
== 0);
139 width
= client_rect
.right
- client_rect
.left
;
140 height
= client_rect
.bottom
- client_rect
.top
;
142 if (width
<= 0 || height
<= 0) {
144 * When the window is minimized GetClientRect will return zeros. Simply
145 * preserve the current window size, until the window is restored or
149 assert(width
== 0 && height
== 0);
154 if (width
!= fb
->width
|| height
!= fb
->height
) {
155 fb
->must_resize
= TRUE
;
162 if (ClientToScreen(fb
->hWnd
, &client_pos
) &&
163 GetWindowRect(fb
->hWnd
, &window_rect
)) {
164 fb
->client_rect
.left
= client_pos
.x
- window_rect
.left
;
165 fb
->client_rect
.top
= client_pos
.y
- window_rect
.top
;
168 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
169 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
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",
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",
181 fb
->client_rect
.left
, fb
->client_rect
.top
,
182 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
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
192 stw_call_window_proc(
197 struct stw_tls_data
*tls_data
;
198 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
199 struct stw_framebuffer
*fb
;
201 tls_data
= stw_tls_get_data();
205 if (nCode
< 0 || !stw_dev
)
206 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
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
);
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
);
226 else if (pParams
->message
== WM_DESTROY
) {
227 pipe_mutex_lock( stw_dev
->fb_mutex
);
228 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
230 stw_framebuffer_destroy_locked(fb
);
231 pipe_mutex_unlock( stw_dev
->fb_mutex
);
234 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
238 struct stw_framebuffer
*
239 stw_framebuffer_create(
244 struct stw_framebuffer
*fb
;
245 const struct stw_pixelformat_info
*pfi
;
247 /* We only support drawing to a window. */
248 hWnd
= WindowFromDC( hdc
);
252 fb
= CALLOC_STRUCT( stw_framebuffer
);
258 fb
->iPixelFormat
= iPixelFormat
;
260 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
- 1 );
261 fb
->stfb
= stw_st_create_framebuffer( fb
);
270 * Windows can be sometimes have zero width and or height, but we ensure
271 * a non-zero framebuffer size at all times.
274 fb
->must_resize
= TRUE
;
277 fb
->client_rect
.left
= 0;
278 fb
->client_rect
.top
= 0;
279 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
280 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
282 stw_framebuffer_get_size(fb
);
284 pipe_mutex_init( fb
->mutex
);
286 /* This is the only case where we lock the stw_framebuffer::mutex before
287 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
288 * and we must prevent any other thread from destroying it before we return.
290 pipe_mutex_lock( fb
->mutex
);
292 pipe_mutex_lock( stw_dev
->fb_mutex
);
293 fb
->next
= stw_dev
->fb_head
;
294 stw_dev
->fb_head
= fb
;
295 pipe_mutex_unlock( stw_dev
->fb_mutex
);
301 * Have ptr reference fb. The referenced framebuffer should be locked.
304 stw_framebuffer_reference(
305 struct stw_framebuffer
**ptr
,
306 struct stw_framebuffer
*fb
)
308 struct stw_framebuffer
*old_fb
= *ptr
;
316 pipe_mutex_lock(stw_dev
->fb_mutex
);
318 pipe_mutex_lock(old_fb
->mutex
);
319 stw_framebuffer_destroy_locked(old_fb
);
321 pipe_mutex_unlock(stw_dev
->fb_mutex
);
329 * Update the framebuffer's size if necessary.
332 stw_framebuffer_update(
333 struct stw_framebuffer
*fb
)
339 /* XXX: It would be nice to avoid checking the size again -- in theory
340 * stw_call_window_proc would have cought the resize and stored the right
341 * size already, but unfortunately threads created before the DllMain is
342 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
343 * to know of their existing without using the not very portable PSAPI.
345 stw_framebuffer_get_size(fb
);
350 stw_framebuffer_cleanup( void )
352 struct stw_framebuffer
*fb
;
353 struct stw_framebuffer
*next
;
358 pipe_mutex_lock( stw_dev
->fb_mutex
);
360 fb
= stw_dev
->fb_head
;
364 pipe_mutex_lock(fb
->mutex
);
365 stw_framebuffer_destroy_locked(fb
);
369 stw_dev
->fb_head
= NULL
;
371 pipe_mutex_unlock( stw_dev
->fb_mutex
);
376 * Given an hdc, return the corresponding stw_framebuffer.
378 static INLINE
struct stw_framebuffer
*
379 stw_framebuffer_from_hdc_locked(
383 struct stw_framebuffer
*fb
;
386 * Some applications create and use several HDCs for the same window, so
387 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
390 hwnd
= WindowFromDC(hdc
);
392 return stw_framebuffer_from_hwnd_locked(hwnd
);
394 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
395 if (fb
->hDC
== hdc
) {
396 pipe_mutex_lock(fb
->mutex
);
405 * Given an hdc, return the corresponding stw_framebuffer.
407 struct stw_framebuffer
*
408 stw_framebuffer_from_hdc(
411 struct stw_framebuffer
*fb
;
416 pipe_mutex_lock( stw_dev
->fb_mutex
);
417 fb
= stw_framebuffer_from_hdc_locked(hdc
);
418 pipe_mutex_unlock( stw_dev
->fb_mutex
);
425 * Given an hdc, return the corresponding stw_framebuffer.
427 struct stw_framebuffer
*
428 stw_framebuffer_from_hwnd(
431 struct stw_framebuffer
*fb
;
433 pipe_mutex_lock( stw_dev
->fb_mutex
);
434 fb
= stw_framebuffer_from_hwnd_locked(hwnd
);
435 pipe_mutex_unlock( stw_dev
->fb_mutex
);
448 struct stw_framebuffer
*fb
;
453 index
= (uint
) iPixelFormat
- 1;
454 count
= stw_pixelformat_get_extended_count();
458 fb
= stw_framebuffer_from_hdc_locked(hdc
);
460 /* SetPixelFormat must be called only once */
461 stw_framebuffer_release( fb
);
465 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
470 stw_framebuffer_release( fb
);
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 SetPixelFormat(hdc
, iPixelFormat
, NULL
);
487 int iPixelFormat
= 0;
488 struct stw_framebuffer
*fb
;
490 fb
= stw_framebuffer_from_hdc(hdc
);
492 iPixelFormat
= fb
->iPixelFormat
;
493 stw_framebuffer_release(fb
);
501 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
503 struct stw_framebuffer
*fb
;
504 struct pipe_screen
*screen
;
505 struct pipe_resource
*res
;
510 fb
= stw_framebuffer_from_hdc( hdc
);
514 screen
= stw_dev
->screen
;
516 res
= (struct pipe_resource
*)data
->pPrivateData
;
518 if(data
->hSharedSurface
!= fb
->hSharedSurface
) {
519 if(fb
->shared_surface
) {
520 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
521 fb
->shared_surface
= NULL
;
524 fb
->hSharedSurface
= data
->hSharedSurface
;
526 if(data
->hSharedSurface
&&
527 stw_dev
->stw_winsys
->shared_surface_open
) {
528 fb
->shared_surface
= stw_dev
->stw_winsys
->shared_surface_open(screen
, fb
->hSharedSurface
);
532 if(fb
->shared_surface
) {
533 stw_dev
->stw_winsys
->compose(screen
,
537 data
->PresentHistoryToken
);
540 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
543 stw_framebuffer_update(fb
);
544 stw_notify_current_locked(fb
);
546 stw_framebuffer_release(fb
);
553 * Queue a composition.
555 * It will drop the lock on success.
558 stw_framebuffer_present_locked(HDC hdc
,
559 struct stw_framebuffer
*fb
,
560 struct pipe_resource
*res
)
562 if(stw_dev
->callbacks
.wglCbPresentBuffers
&&
563 stw_dev
->stw_winsys
->compose
) {
564 GLCBPRESENTBUFFERSDATA data
;
566 memset(&data
, 0, sizeof data
);
569 data
.AdapterLuid
= stw_dev
->AdapterLuid
;
570 data
.rect
= fb
->client_rect
;
571 data
.pPrivateData
= (void *)res
;
573 stw_notify_current_locked(fb
);
574 stw_framebuffer_release(fb
);
576 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
579 struct pipe_screen
*screen
= stw_dev
->screen
;
581 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
583 stw_framebuffer_update(fb
);
584 stw_notify_current_locked(fb
);
585 stw_framebuffer_release(fb
);
596 struct stw_framebuffer
*fb
;
601 fb
= stw_framebuffer_from_hdc( hdc
);
605 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
606 stw_framebuffer_release(fb
);
610 stw_flush_current_locked(fb
);
612 return stw_st_swap_framebuffer_locked(fb
->stfb
);
621 if(fuPlanes
& WGL_SWAP_MAIN_PLANE
)
622 return DrvSwapBuffers(hdc
);