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
)
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
;
129 if (width
<= 0 || height
<= 0) {
131 * When the window is minimized GetClientRect will return zeros. Simply
132 * preserve the current window size, until the window is restored or
136 assert(width
== 0 && height
== 0);
141 if(width
!= fb
->width
|| height
!= fb
->height
) {
142 fb
->must_resize
= TRUE
;
149 ClientToScreen(fb
->hWnd
, &client_pos
);
151 GetWindowRect(fb
->hWnd
, &window_rect
);
153 fb
->client_rect
.left
= client_pos
.x
- window_rect
.left
;
154 fb
->client_rect
.top
= client_pos
.y
- window_rect
.top
;
155 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
156 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
160 debug_printf("%s: client_position = (%li, %li)\n",
161 __FUNCTION__
, client_pos
.x
, client_pos
.y
);
162 debug_printf("%s: window_rect = (%li, %li) - (%li, %li)\n",
164 window_rect
.left
, window_rect
.top
,
165 window_rect
.right
, window_rect
.bottom
);
166 debug_printf("%s: client_rect = (%li, %li) - (%li, %li)\n",
168 fb
->client_rect
.left
, fb
->client_rect
.top
,
169 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
175 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
176 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
179 stw_call_window_proc(
184 struct stw_tls_data
*tls_data
;
185 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
186 struct stw_framebuffer
*fb
;
188 tls_data
= stw_tls_get_data();
192 if (nCode
< 0 || !stw_dev
)
193 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
195 if (pParams
->message
== WM_WINDOWPOSCHANGED
) {
196 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according to
197 * http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
198 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
199 * can be masked out by the application. */
200 LPWINDOWPOS lpWindowPos
= (LPWINDOWPOS
)pParams
->lParam
;
201 if((lpWindowPos
->flags
& SWP_SHOWWINDOW
) ||
202 !(lpWindowPos
->flags
& SWP_NOMOVE
) ||
203 !(lpWindowPos
->flags
& SWP_NOSIZE
)) {
204 fb
= stw_framebuffer_from_hwnd( pParams
->hwnd
);
206 /* Size in WINDOWPOS includes the window frame, so get the size
207 * of the client area via GetClientRect. */
208 stw_framebuffer_get_size(fb
);
209 stw_framebuffer_release(fb
);
213 else if (pParams
->message
== WM_DESTROY
) {
214 pipe_mutex_lock( stw_dev
->fb_mutex
);
215 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
217 stw_framebuffer_destroy_locked(fb
);
218 pipe_mutex_unlock( stw_dev
->fb_mutex
);
221 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
225 struct stw_framebuffer
*
226 stw_framebuffer_create(
231 struct stw_framebuffer
*fb
;
232 const struct stw_pixelformat_info
*pfi
;
234 /* We only support drawing to a window. */
235 hWnd
= WindowFromDC( hdc
);
239 fb
= CALLOC_STRUCT( stw_framebuffer
);
245 fb
->iPixelFormat
= iPixelFormat
;
247 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
- 1 );
248 fb
->stfb
= stw_st_create_framebuffer( fb
);
256 stw_framebuffer_get_size(fb
);
258 pipe_mutex_init( fb
->mutex
);
260 /* This is the only case where we lock the stw_framebuffer::mutex before
261 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
262 * and we must prevent any other thread from destroying it before we return.
264 pipe_mutex_lock( fb
->mutex
);
266 pipe_mutex_lock( stw_dev
->fb_mutex
);
267 fb
->next
= stw_dev
->fb_head
;
268 stw_dev
->fb_head
= fb
;
269 pipe_mutex_unlock( stw_dev
->fb_mutex
);
275 * Have ptr reference fb. The referenced framebuffer should be locked.
278 stw_framebuffer_reference(
279 struct stw_framebuffer
**ptr
,
280 struct stw_framebuffer
*fb
)
282 struct stw_framebuffer
*old_fb
= *ptr
;
290 pipe_mutex_lock(stw_dev
->fb_mutex
);
292 pipe_mutex_lock(old_fb
->mutex
);
293 stw_framebuffer_destroy_locked(old_fb
);
295 pipe_mutex_unlock(stw_dev
->fb_mutex
);
303 * Update the framebuffer's size if necessary.
306 stw_framebuffer_update(
307 struct stw_framebuffer
*fb
)
313 /* XXX: It would be nice to avoid checking the size again -- in theory
314 * stw_call_window_proc would have cought the resize and stored the right
315 * size already, but unfortunately threads created before the DllMain is
316 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
317 * to know of their existing without using the not very portable PSAPI.
319 stw_framebuffer_get_size(fb
);
324 stw_framebuffer_cleanup( void )
326 struct stw_framebuffer
*fb
;
327 struct stw_framebuffer
*next
;
332 pipe_mutex_lock( stw_dev
->fb_mutex
);
334 fb
= stw_dev
->fb_head
;
338 pipe_mutex_lock(fb
->mutex
);
339 stw_framebuffer_destroy_locked(fb
);
343 stw_dev
->fb_head
= NULL
;
345 pipe_mutex_unlock( stw_dev
->fb_mutex
);
350 * Given an hdc, return the corresponding stw_framebuffer.
352 static INLINE
struct stw_framebuffer
*
353 stw_framebuffer_from_hdc_locked(
357 struct stw_framebuffer
*fb
;
360 * Some applications create and use several HDCs for the same window, so
361 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
364 hwnd
= WindowFromDC(hdc
);
366 return stw_framebuffer_from_hwnd_locked(hwnd
);
368 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
369 if (fb
->hDC
== hdc
) {
370 pipe_mutex_lock(fb
->mutex
);
379 * Given an hdc, return the corresponding stw_framebuffer.
381 struct stw_framebuffer
*
382 stw_framebuffer_from_hdc(
385 struct stw_framebuffer
*fb
;
390 pipe_mutex_lock( stw_dev
->fb_mutex
);
391 fb
= stw_framebuffer_from_hdc_locked(hdc
);
392 pipe_mutex_unlock( stw_dev
->fb_mutex
);
399 * Given an hdc, return the corresponding stw_framebuffer.
401 struct stw_framebuffer
*
402 stw_framebuffer_from_hwnd(
405 struct stw_framebuffer
*fb
;
407 pipe_mutex_lock( stw_dev
->fb_mutex
);
408 fb
= stw_framebuffer_from_hwnd_locked(hwnd
);
409 pipe_mutex_unlock( stw_dev
->fb_mutex
);
422 struct stw_framebuffer
*fb
;
427 index
= (uint
) iPixelFormat
- 1;
428 count
= stw_pixelformat_get_extended_count();
432 fb
= stw_framebuffer_from_hdc_locked(hdc
);
434 /* SetPixelFormat must be called only once */
435 stw_framebuffer_release( fb
);
439 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
444 stw_framebuffer_release( fb
);
446 /* Some applications mistakenly use the undocumented wglSetPixelFormat
447 * function instead of SetPixelFormat, so we call SetPixelFormat here to
448 * avoid opengl32.dll's wglCreateContext to fail */
449 if (GetPixelFormat(hdc
) == 0) {
450 SetPixelFormat(hdc
, iPixelFormat
, NULL
);
461 int iPixelFormat
= 0;
462 struct stw_framebuffer
*fb
;
464 fb
= stw_framebuffer_from_hdc(hdc
);
466 iPixelFormat
= fb
->iPixelFormat
;
467 stw_framebuffer_release(fb
);
475 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
477 struct stw_framebuffer
*fb
;
478 struct pipe_screen
*screen
;
479 struct pipe_resource
*res
;
484 fb
= stw_framebuffer_from_hdc( hdc
);
488 screen
= stw_dev
->screen
;
490 res
= (struct pipe_resource
*)data
->pPrivateData
;
492 if(data
->hSharedSurface
!= fb
->hSharedSurface
) {
493 if(fb
->shared_surface
) {
494 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
495 fb
->shared_surface
= NULL
;
498 fb
->hSharedSurface
= data
->hSharedSurface
;
500 if(data
->hSharedSurface
&&
501 stw_dev
->stw_winsys
->shared_surface_open
) {
502 fb
->shared_surface
= stw_dev
->stw_winsys
->shared_surface_open(screen
, fb
->hSharedSurface
);
506 if(fb
->shared_surface
) {
507 stw_dev
->stw_winsys
->compose(screen
,
511 data
->PresentHistoryToken
);
514 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
517 stw_framebuffer_update(fb
);
518 stw_notify_current_locked(fb
);
520 stw_framebuffer_release(fb
);
527 * Queue a composition.
529 * It will drop the lock on success.
532 stw_framebuffer_present_locked(HDC hdc
,
533 struct stw_framebuffer
*fb
,
534 struct pipe_resource
*res
)
536 if(stw_dev
->callbacks
.wglCbPresentBuffers
&&
537 stw_dev
->stw_winsys
->compose
) {
538 GLCBPRESENTBUFFERSDATA data
;
540 memset(&data
, 0, sizeof data
);
543 data
.AdapterLuid
= stw_dev
->AdapterLuid
;
544 data
.rect
= fb
->client_rect
;
545 data
.pPrivateData
= (void *)res
;
547 stw_notify_current_locked(fb
);
548 stw_framebuffer_release(fb
);
550 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
553 struct pipe_screen
*screen
= stw_dev
->screen
;
555 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
557 stw_framebuffer_update(fb
);
558 stw_notify_current_locked(fb
);
559 stw_framebuffer_release(fb
);
570 struct stw_framebuffer
*fb
;
575 fb
= stw_framebuffer_from_hdc( hdc
);
579 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
580 stw_framebuffer_release(fb
);
584 stw_flush_current_locked(fb
);
586 return stw_st_swap_framebuffer_locked(fb
->stfb
);
595 if(fuPlanes
& WGL_SWAP_MAIN_PLANE
)
596 return DrvSwapBuffers(hdc
);