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 "util/u_format.h"
34 #include "state_tracker/st_context.h"
35 #include "state_tracker/st_public.h"
38 #include "stw_framebuffer.h"
39 #include "stw_device.h"
40 #include "stw_winsys.h"
45 * Search the framebuffer with the matching HWND while holding the
46 * stw_dev::fb_mutex global lock.
48 static INLINE
struct stw_framebuffer
*
49 stw_framebuffer_from_hwnd_locked(
52 struct stw_framebuffer
*fb
;
54 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
55 if (fb
->hWnd
== hwnd
) {
56 pipe_mutex_lock(fb
->mutex
);
65 * Destroy this framebuffer. Both stw_dev::fb_mutex and stw_framebuffer::mutex
66 * must be held, by this order. Obviously no further access to fb can be done
70 stw_framebuffer_destroy_locked(
71 struct stw_framebuffer
*fb
)
73 struct stw_framebuffer
**link
;
75 link
= &stw_dev
->fb_head
;
77 link
= &(*link
)->next
;
82 if(fb
->shared_surface
)
83 stw_dev
->stw_winsys
->shared_surface_close(stw_dev
->screen
, fb
->shared_surface
);
85 st_unreference_framebuffer(fb
->stfb
);
87 pipe_mutex_unlock( fb
->mutex
);
89 pipe_mutex_destroy( fb
->mutex
);
96 stw_framebuffer_release(
97 struct stw_framebuffer
*fb
)
100 pipe_mutex_unlock( fb
->mutex
);
105 stw_framebuffer_get_size( struct stw_framebuffer
*fb
)
107 unsigned width
, height
;
114 /* Get the client area size. */
115 GetClientRect( fb
->hWnd
, &client_rect
);
116 assert(client_rect
.left
== 0);
117 assert(client_rect
.top
== 0);
118 width
= client_rect
.right
- client_rect
.left
;
119 height
= client_rect
.bottom
- client_rect
.top
;
126 if(width
!= fb
->width
|| height
!= fb
->height
) {
127 fb
->must_resize
= TRUE
;
134 ClientToScreen(fb
->hWnd
, &client_pos
);
136 GetWindowRect(fb
->hWnd
, &window_rect
);
138 fb
->client_rect
.left
= client_pos
.x
- window_rect
.left
;
139 fb
->client_rect
.top
= client_pos
.y
- window_rect
.top
;
140 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
141 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
145 debug_printf("%s: client_position = (%i, %i)\n",
146 __FUNCTION__
, client_pos
.x
, client_pos
.y
);
147 debug_printf("%s: window_rect = (%i, %i) - (%i, %i)\n",
149 window_rect
.left
, window_rect
.top
,
150 window_rect
.right
, window_rect
.bottom
);
151 debug_printf("%s: client_rect = (%i, %i) - (%i, %i)\n",
153 fb
->client_rect
.left
, fb
->client_rect
.top
,
154 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
160 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
161 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
164 stw_call_window_proc(
169 struct stw_tls_data
*tls_data
;
170 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
171 struct stw_framebuffer
*fb
;
173 tls_data
= stw_tls_get_data();
177 if (nCode
< 0 || !stw_dev
)
178 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
180 if (pParams
->message
== WM_WINDOWPOSCHANGED
) {
181 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according to
182 * http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
183 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
184 * can be masked out by the application. */
185 LPWINDOWPOS lpWindowPos
= (LPWINDOWPOS
)pParams
->lParam
;
186 if((lpWindowPos
->flags
& SWP_SHOWWINDOW
) ||
187 !(lpWindowPos
->flags
& SWP_NOMOVE
) ||
188 !(lpWindowPos
->flags
& SWP_NOSIZE
)) {
189 fb
= stw_framebuffer_from_hwnd( pParams
->hwnd
);
191 /* Size in WINDOWPOS includes the window frame, so get the size
192 * of the client area via GetClientRect. */
193 stw_framebuffer_get_size(fb
);
194 stw_framebuffer_release(fb
);
198 else if (pParams
->message
== WM_DESTROY
) {
199 pipe_mutex_lock( stw_dev
->fb_mutex
);
200 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
202 stw_framebuffer_destroy_locked(fb
);
203 pipe_mutex_unlock( stw_dev
->fb_mutex
);
206 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
210 struct stw_framebuffer
*
211 stw_framebuffer_create(
216 struct stw_framebuffer
*fb
;
217 const struct stw_pixelformat_info
*pfi
;
219 /* We only support drawing to a window. */
220 hWnd
= WindowFromDC( hdc
);
224 fb
= CALLOC_STRUCT( stw_framebuffer
);
230 fb
->iPixelFormat
= iPixelFormat
;
232 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
- 1 );
234 stw_pixelformat_visual(&fb
->visual
, pfi
);
236 stw_framebuffer_get_size(fb
);
238 pipe_mutex_init( fb
->mutex
);
240 /* This is the only case where we lock the stw_framebuffer::mutex before
241 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
242 * and we must prevent any other thread from destroying it before we return.
244 pipe_mutex_lock( fb
->mutex
);
246 pipe_mutex_lock( stw_dev
->fb_mutex
);
247 fb
->next
= stw_dev
->fb_head
;
248 stw_dev
->fb_head
= fb
;
249 pipe_mutex_unlock( stw_dev
->fb_mutex
);
256 stw_framebuffer_allocate(
257 struct stw_framebuffer
*fb
)
262 const struct stw_pixelformat_info
*pfi
= fb
->pfi
;
263 enum pipe_format colorFormat
, depthFormat
, stencilFormat
;
265 colorFormat
= pfi
->color_format
;
267 if(util_format_get_component_bits(pfi
->depth_stencil_format
, UTIL_FORMAT_COLORSPACE_ZS
, 0))
268 depthFormat
= pfi
->depth_stencil_format
;
270 depthFormat
= PIPE_FORMAT_NONE
;
272 if(util_format_get_component_bits(pfi
->depth_stencil_format
, UTIL_FORMAT_COLORSPACE_ZS
, 1))
273 stencilFormat
= pfi
->depth_stencil_format
;
275 stencilFormat
= PIPE_FORMAT_NONE
;
277 assert(fb
->must_resize
);
281 fb
->stfb
= st_create_framebuffer(
290 // to notify the context
291 fb
->must_resize
= TRUE
;
294 return fb
->stfb
? TRUE
: FALSE
;
299 * Update the framebuffer's size if necessary.
302 stw_framebuffer_update(
303 struct stw_framebuffer
*fb
)
309 /* XXX: It would be nice to avoid checking the size again -- in theory
310 * stw_call_window_proc would have cought the resize and stored the right
311 * size already, but unfortunately threads created before the DllMain is
312 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
313 * to know of their existing without using the not very portable PSAPI.
315 stw_framebuffer_get_size(fb
);
317 if(fb
->must_resize
) {
318 st_resize_framebuffer(fb
->stfb
, fb
->width
, fb
->height
);
319 fb
->must_resize
= FALSE
;
325 stw_framebuffer_cleanup( void )
327 struct stw_framebuffer
*fb
;
328 struct stw_framebuffer
*next
;
333 pipe_mutex_lock( stw_dev
->fb_mutex
);
335 fb
= stw_dev
->fb_head
;
339 pipe_mutex_lock(fb
->mutex
);
340 stw_framebuffer_destroy_locked(fb
);
344 stw_dev
->fb_head
= NULL
;
346 pipe_mutex_unlock( stw_dev
->fb_mutex
);
351 * Given an hdc, return the corresponding stw_framebuffer.
353 static INLINE
struct stw_framebuffer
*
354 stw_framebuffer_from_hdc_locked(
358 struct stw_framebuffer
*fb
;
361 * Some applications create and use several HDCs for the same window, so
362 * looking up the framebuffer by the HDC is not reliable. Use HWND whenever
365 hwnd
= WindowFromDC(hdc
);
367 return stw_framebuffer_from_hwnd_locked(hwnd
);
369 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
370 if (fb
->hDC
== hdc
) {
371 pipe_mutex_lock(fb
->mutex
);
380 * Given an hdc, return the corresponding stw_framebuffer.
382 struct stw_framebuffer
*
383 stw_framebuffer_from_hdc(
386 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
;
428 index
= (uint
) iPixelFormat
- 1;
429 count
= stw_pixelformat_get_extended_count();
433 fb
= stw_framebuffer_from_hdc_locked(hdc
);
435 /* SetPixelFormat must be called only once */
436 stw_framebuffer_release( fb
);
440 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
445 stw_framebuffer_release( fb
);
447 /* Some applications mistakenly use the undocumented wglSetPixelFormat
448 * function instead of SetPixelFormat, so we call SetPixelFormat here to
449 * avoid opengl32.dll's wglCreateContext to fail */
450 if (GetPixelFormat(hdc
) == 0) {
451 SetPixelFormat(hdc
, iPixelFormat
, NULL
);
462 int iPixelFormat
= 0;
463 struct stw_framebuffer
*fb
;
465 fb
= stw_framebuffer_from_hdc(hdc
);
467 iPixelFormat
= fb
->iPixelFormat
;
468 stw_framebuffer_release(fb
);
476 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
478 struct stw_framebuffer
*fb
;
479 struct pipe_screen
*screen
;
480 struct pipe_surface
*surface
;
485 fb
= stw_framebuffer_from_hdc( hdc
);
489 screen
= stw_dev
->screen
;
491 surface
= (struct pipe_surface
*)data
->pPrivateData
;
493 if(data
->hSharedSurface
!= fb
->hSharedSurface
) {
494 if(fb
->shared_surface
) {
495 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
496 fb
->shared_surface
= NULL
;
499 fb
->hSharedSurface
= data
->hSharedSurface
;
501 if(data
->hSharedSurface
&&
502 stw_dev
->stw_winsys
->shared_surface_open
) {
503 fb
->shared_surface
= stw_dev
->stw_winsys
->shared_surface_open(screen
, fb
->hSharedSurface
);
507 if(fb
->shared_surface
) {
508 stw_dev
->stw_winsys
->compose(screen
,
512 data
->PresentHistoryToken
);
515 stw_dev
->stw_winsys
->present( screen
, surface
, hdc
);
518 stw_framebuffer_update(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_surface
*surface
)
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 *)surface
;
547 stw_framebuffer_release(fb
);
549 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
552 struct pipe_screen
*screen
= stw_dev
->screen
;
554 stw_dev
->stw_winsys
->present( screen
, surface
, hdc
);
556 stw_framebuffer_update(fb
);
558 stw_framebuffer_release(fb
);
569 struct stw_framebuffer
*fb
;
570 struct pipe_surface
*surface
= NULL
;
575 fb
= stw_framebuffer_from_hdc( hdc
);
579 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
580 stw_framebuffer_release(fb
);
584 st_swapbuffers(fb
->stfb
, &surface
, NULL
);
586 return stw_framebuffer_present_locked(hdc
, fb
, surface
);
595 if(fuPlanes
& WGL_SWAP_MAIN_PLANE
)
596 return DrvSwapBuffers(hdc
);