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 "main/context.h"
31 #include "pipe/p_format.h"
32 #include "pipe/p_screen.h"
33 #include "state_tracker/st_context.h"
34 #include "state_tracker/st_public.h"
37 #include "trace/tr_screen.h"
38 #include "trace/tr_texture.h"
42 #include "stw_framebuffer.h"
43 #include "stw_device.h"
44 #include "stw_winsys.h"
49 * Search the framebuffer with the matching HWND while holding the
50 * stw_dev::fb_mutex global lock.
52 static INLINE
struct stw_framebuffer
*
53 stw_framebuffer_from_hwnd_locked(
56 struct stw_framebuffer
*fb
;
58 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
59 if (fb
->hWnd
== hwnd
) {
60 pipe_mutex_lock(fb
->mutex
);
69 * Destroy this framebuffer. Both stw_dev::fb_mutex and stw_framebuffer::mutex
70 * must be held, by this order. Obviously no further access to fb can be done
74 stw_framebuffer_destroy_locked(
75 struct stw_framebuffer
*fb
)
77 struct stw_framebuffer
**link
;
79 link
= &stw_dev
->fb_head
;
81 link
= &(*link
)->next
;
86 if(fb
->shared_surface
)
87 stw_dev
->stw_winsys
->shared_surface_close(stw_dev
->screen
, fb
->shared_surface
);
89 st_unreference_framebuffer(fb
->stfb
);
91 pipe_mutex_unlock( fb
->mutex
);
93 pipe_mutex_destroy( fb
->mutex
);
100 stw_framebuffer_release(
101 struct stw_framebuffer
*fb
)
104 pipe_mutex_unlock( fb
->mutex
);
109 stw_framebuffer_get_size( struct stw_framebuffer
*fb
)
111 unsigned width
, height
;
118 /* Get the client area size. */
119 GetClientRect( fb
->hWnd
, &client_rect
);
120 assert(client_rect
.left
== 0);
121 assert(client_rect
.top
== 0);
122 width
= client_rect
.right
- client_rect
.left
;
123 height
= client_rect
.bottom
- client_rect
.top
;
130 if(width
!= fb
->width
|| height
!= fb
->height
) {
131 fb
->must_resize
= TRUE
;
138 ClientToScreen(fb
->hWnd
, &client_pos
);
140 GetWindowRect(fb
->hWnd
, &window_rect
);
142 fb
->client_rect
.left
= client_pos
.x
- window_rect
.left
;
143 fb
->client_rect
.top
= client_pos
.y
- window_rect
.top
;
144 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
145 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
149 debug_printf("%s: client_position = (%i, %i)\n",
150 __FUNCTION__
, client_pos
.x
, client_pos
.y
);
151 debug_printf("%s: window_rect = (%i, %i) - (%i, %i)\n",
153 window_rect
.left
, window_rect
.top
,
154 window_rect
.right
, window_rect
.bottom
);
155 debug_printf("%s: client_rect = (%i, %i) - (%i, %i)\n",
157 fb
->client_rect
.left
, fb
->client_rect
.top
,
158 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
164 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
165 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
168 stw_call_window_proc(
173 struct stw_tls_data
*tls_data
;
174 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
175 struct stw_framebuffer
*fb
;
177 tls_data
= stw_tls_get_data();
182 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
184 if (pParams
->message
== WM_WINDOWPOSCHANGED
) {
185 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according to
186 * http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
187 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
188 * can be masked out by the application. */
189 LPWINDOWPOS lpWindowPos
= (LPWINDOWPOS
)pParams
->lParam
;
190 if((lpWindowPos
->flags
& SWP_SHOWWINDOW
) ||
191 !(lpWindowPos
->flags
& SWP_NOMOVE
) ||
192 !(lpWindowPos
->flags
& SWP_NOSIZE
)) {
193 fb
= stw_framebuffer_from_hwnd( pParams
->hwnd
);
195 /* Size in WINDOWPOS includes the window frame, so get the size
196 * of the client area via GetClientRect. */
197 stw_framebuffer_get_size(fb
);
198 stw_framebuffer_release(fb
);
202 else if (pParams
->message
== WM_DESTROY
) {
203 pipe_mutex_lock( stw_dev
->fb_mutex
);
204 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
206 stw_framebuffer_destroy_locked(fb
);
207 pipe_mutex_unlock( stw_dev
->fb_mutex
);
210 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
214 struct stw_framebuffer
*
215 stw_framebuffer_create(
220 struct stw_framebuffer
*fb
;
221 const struct stw_pixelformat_info
*pfi
;
223 /* We only support drawing to a window. */
224 hWnd
= WindowFromDC( hdc
);
228 fb
= CALLOC_STRUCT( stw_framebuffer
);
234 fb
->iPixelFormat
= iPixelFormat
;
236 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
- 1 );
238 stw_pixelformat_visual(&fb
->visual
, pfi
);
240 stw_framebuffer_get_size(fb
);
242 pipe_mutex_init( fb
->mutex
);
244 /* This is the only case where we lock the stw_framebuffer::mutex before
245 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
246 * and we must prevent any other thread from destroying it before we return.
248 pipe_mutex_lock( fb
->mutex
);
250 pipe_mutex_lock( stw_dev
->fb_mutex
);
251 fb
->next
= stw_dev
->fb_head
;
252 stw_dev
->fb_head
= fb
;
253 pipe_mutex_unlock( stw_dev
->fb_mutex
);
260 stw_framebuffer_allocate(
261 struct stw_framebuffer
*fb
)
266 const struct stw_pixelformat_info
*pfi
= fb
->pfi
;
267 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
269 colorFormat
= pfi
->color_format
;
271 assert(pf_layout( pfi
->depth_stencil_format
) == PIPE_FORMAT_LAYOUT_RGBAZS
);
273 if(pf_get_component_bits( pfi
->depth_stencil_format
, PIPE_FORMAT_COMP_Z
))
274 depthFormat
= pfi
->depth_stencil_format
;
276 depthFormat
= PIPE_FORMAT_NONE
;
278 if(pf_get_component_bits( pfi
->depth_stencil_format
, PIPE_FORMAT_COMP_S
))
279 stencilFormat
= pfi
->depth_stencil_format
;
281 stencilFormat
= PIPE_FORMAT_NONE
;
283 assert(fb
->must_resize
);
287 fb
->stfb
= st_create_framebuffer(
296 // to notify the context
297 fb
->must_resize
= TRUE
;
300 return fb
->stfb
? TRUE
: FALSE
;
305 * Update the framebuffer's size if necessary.
308 stw_framebuffer_update(
309 struct stw_framebuffer
*fb
)
315 /* XXX: It would be nice to avoid checking the size again -- in theory
316 * stw_call_window_proc would have cought the resize and stored the right
317 * size already, but unfortunately threads created before the DllMain is
318 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
319 * to know of their existing without using the not very portable PSAPI.
321 stw_framebuffer_get_size(fb
);
323 if(fb
->must_resize
) {
324 st_resize_framebuffer(fb
->stfb
, fb
->width
, fb
->height
);
325 fb
->must_resize
= FALSE
;
331 stw_framebuffer_cleanup( void )
333 struct stw_framebuffer
*fb
;
334 struct stw_framebuffer
*next
;
336 pipe_mutex_lock( stw_dev
->fb_mutex
);
338 fb
= stw_dev
->fb_head
;
342 pipe_mutex_lock(fb
->mutex
);
343 stw_framebuffer_destroy_locked(fb
);
347 stw_dev
->fb_head
= NULL
;
349 pipe_mutex_unlock( stw_dev
->fb_mutex
);
354 * Given an hdc, return the corresponding stw_framebuffer.
356 static INLINE
struct stw_framebuffer
*
357 stw_framebuffer_from_hdc_locked(
361 struct stw_framebuffer
*fb
;
364 * Some applications create and use several HDCs for the same window, so
365 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
368 hwnd
= WindowFromDC(hdc
);
370 return stw_framebuffer_from_hwnd_locked(hwnd
);
372 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
373 if (fb
->hDC
== hdc
) {
374 pipe_mutex_lock(fb
->mutex
);
383 * Given an hdc, return the corresponding stw_framebuffer.
385 struct stw_framebuffer
*
386 stw_framebuffer_from_hdc(
389 struct stw_framebuffer
*fb
;
391 pipe_mutex_lock( stw_dev
->fb_mutex
);
392 fb
= stw_framebuffer_from_hdc_locked(hdc
);
393 pipe_mutex_unlock( stw_dev
->fb_mutex
);
400 * Given an hdc, return the corresponding stw_framebuffer.
402 struct stw_framebuffer
*
403 stw_framebuffer_from_hwnd(
406 struct stw_framebuffer
*fb
;
408 pipe_mutex_lock( stw_dev
->fb_mutex
);
409 fb
= stw_framebuffer_from_hwnd_locked(hwnd
);
410 pipe_mutex_unlock( stw_dev
->fb_mutex
);
423 struct stw_framebuffer
*fb
;
425 index
= (uint
) iPixelFormat
- 1;
426 count
= stw_pixelformat_get_extended_count();
430 fb
= stw_framebuffer_from_hdc_locked(hdc
);
432 /* SetPixelFormat must be called only once */
433 stw_framebuffer_release( fb
);
437 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
442 stw_framebuffer_release( fb
);
444 /* Some applications mistakenly use the undocumented wglSetPixelFormat
445 * function instead of SetPixelFormat, so we call SetPixelFormat here to
446 * avoid opengl32.dll's wglCreateContext to fail */
447 if (GetPixelFormat(hdc
) == 0) {
448 SetPixelFormat(hdc
, iPixelFormat
, NULL
);
459 int iPixelFormat
= 0;
460 struct stw_framebuffer
*fb
;
462 fb
= stw_framebuffer_from_hdc(hdc
);
464 iPixelFormat
= fb
->iPixelFormat
;
465 stw_framebuffer_release(fb
);
473 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
475 struct stw_framebuffer
*fb
;
476 struct pipe_screen
*screen
;
477 struct pipe_surface
*surface
;
478 unsigned surface_index
;
481 fb
= stw_framebuffer_from_hdc( hdc
);
485 screen
= stw_dev
->screen
;
487 surface_index
= (unsigned)(uintptr_t)data
->pPrivateData
;
488 if(!st_get_framebuffer_surface( fb
->stfb
, surface_index
, &surface
))
492 if(stw_dev
->trace_running
) {
493 screen
= trace_screen(screen
)->screen
;
494 surface
= trace_surface(surface
)->surface
;
498 if(data
->hSharedSurface
!= fb
->hSharedSurface
) {
499 if(fb
->shared_surface
) {
500 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
501 fb
->shared_surface
= NULL
;
504 fb
->hSharedSurface
= data
->hSharedSurface
;
506 if(data
->hSharedSurface
&&
507 stw_dev
->stw_winsys
->shared_surface_open
) {
508 fb
->shared_surface
= stw_dev
->stw_winsys
->shared_surface_open(screen
, fb
->hSharedSurface
);
512 if(fb
->shared_surface
) {
513 stw_dev
->stw_winsys
->compose(screen
,
517 data
->PresentHistoryToken
);
520 stw_dev
->stw_winsys
->present( screen
, surface
, hdc
);
527 stw_framebuffer_update(fb
);
529 stw_framebuffer_release(fb
);
536 * Queue a composition.
538 * It will drop the lock on success.
541 stw_framebuffer_present_locked(HDC hdc
,
542 struct stw_framebuffer
*fb
,
543 unsigned surface_index
)
545 if(stw_dev
->callbacks
.wglCbPresentBuffers
&&
546 stw_dev
->stw_winsys
->compose
) {
547 GLCBPRESENTBUFFERSDATA data
;
549 memset(&data
, 0, sizeof data
);
552 data
.AdapterLuid
= stw_dev
->AdapterLuid
;
553 data
.rect
= fb
->client_rect
;
554 data
.pPrivateData
= (void *)(uintptr_t)surface_index
;
556 stw_framebuffer_release(fb
);
558 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
561 struct pipe_screen
*screen
= stw_dev
->screen
;
562 struct pipe_surface
*surface
;
564 if(!st_get_framebuffer_surface( fb
->stfb
, surface_index
, &surface
)) {
565 /* FIXME: this shouldn't happen, but does on glean */
566 stw_framebuffer_release(fb
);
571 if(stw_dev
->trace_running
) {
572 screen
= trace_screen(screen
)->screen
;
573 surface
= trace_surface(surface
)->surface
;
577 stw_dev
->stw_winsys
->present( screen
, surface
, hdc
);
579 stw_framebuffer_update(fb
);
581 stw_framebuffer_release(fb
);
592 struct stw_framebuffer
*fb
;
594 fb
= stw_framebuffer_from_hdc( hdc
);
598 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
599 stw_framebuffer_release(fb
);
603 /* If we're swapping the buffer associated with the current context
604 * we have to flush any pending rendering commands first.
606 st_notify_swapbuffers( fb
->stfb
);
608 return stw_framebuffer_present_locked(hdc
, fb
, ST_SURFACE_BACK_LEFT
);
617 if(fuPlanes
& WGL_SWAP_MAIN_PLANE
)
618 return DrvSwapBuffers(hdc
);