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_screen.h"
31 #include "util/u_memory.h"
32 #include "hud/hud_context.h"
33 #include "state_tracker/st_api.h"
36 #include "stw_framebuffer.h"
37 #include "stw_device.h"
38 #include "stw_winsys.h"
40 #include "stw_context.h"
45 * Search the framebuffer with the matching HWND while holding the
46 * stw_dev::fb_mutex global lock.
47 * If a stw_framebuffer is found, lock it and return the pointer.
50 static inline struct stw_framebuffer
*
51 stw_framebuffer_from_hwnd_locked(HWND hwnd
)
53 struct stw_framebuffer
*fb
;
55 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
56 if (fb
->hWnd
== hwnd
) {
57 stw_framebuffer_lock(fb
);
58 assert(fb
->mutex
.RecursionCount
== 1);
67 * Decrement the reference count on the given stw_framebuffer object.
68 * If the reference count hits zero, destroy the object.
70 * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already be
71 * locked. After this function completes, the fb's mutex will be unlocked.
74 stw_framebuffer_release_locked(struct stw_framebuffer
*fb
)
76 struct stw_framebuffer
**link
;
79 assert(stw_own_mutex(&fb
->mutex
));
80 assert(stw_own_mutex(&stw_dev
->fb_mutex
));
82 /* check the reference count */
85 stw_framebuffer_unlock(fb
);
89 /* remove this stw_framebuffer from the device's linked list */
90 link
= &stw_dev
->fb_head
;
92 link
= &(*link
)->next
;
97 if (fb
->shared_surface
)
98 stw_dev
->stw_winsys
->shared_surface_close(stw_dev
->screen
,
101 stw_st_destroy_framebuffer_locked(fb
->stfb
);
103 stw_framebuffer_unlock(fb
);
105 DeleteCriticalSection(&fb
->mutex
);
112 * Query the size of the given framebuffer's on-screen window and update
113 * the stw_framebuffer's width/height.
116 stw_framebuffer_get_size(struct stw_framebuffer
*fb
)
127 assert(fb
->width
&& fb
->height
);
128 assert(fb
->client_rect
.right
== fb
->client_rect
.left
+ fb
->width
);
129 assert(fb
->client_rect
.bottom
== fb
->client_rect
.top
+ fb
->height
);
132 * Get the client area size.
134 if (!GetClientRect(fb
->hWnd
, &client_rect
)) {
138 assert(client_rect
.left
== 0);
139 assert(client_rect
.top
== 0);
140 width
= client_rect
.right
- client_rect
.left
;
141 height
= client_rect
.bottom
- client_rect
.top
;
143 fb
->minimized
= width
== 0 || height
== 0;
145 if (width
<= 0 || height
<= 0) {
147 * When the window is minimized GetClientRect will return zeros. Simply
148 * preserve the current window size, until the window is restored or
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: hwnd = %p\n", __FUNCTION__
, fb
->hWnd
);
174 debug_printf("%s: client_position = (%li, %li)\n",
175 __FUNCTION__
, client_pos
.x
, client_pos
.y
);
176 debug_printf("%s: window_rect = (%li, %li) - (%li, %li)\n",
178 window_rect
.left
, window_rect
.top
,
179 window_rect
.right
, window_rect
.bottom
);
180 debug_printf("%s: client_rect = (%li, %li) - (%li, %li)\n",
182 fb
->client_rect
.left
, fb
->client_rect
.top
,
183 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
189 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
190 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
193 stw_call_window_proc(int nCode
, WPARAM wParam
, LPARAM lParam
)
195 struct stw_tls_data
*tls_data
;
196 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
197 struct stw_framebuffer
*fb
;
199 tls_data
= stw_tls_get_data();
203 if (nCode
< 0 || !stw_dev
)
204 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
206 /* We check that the stw_dev object is initialized before we try to do
207 * anything with it. Otherwise, in multi-threaded programs there's a
208 * chance of executing this code before the stw_dev object is fully
211 if (stw_dev
&& stw_dev
->initialized
) {
212 if (pParams
->message
== WM_WINDOWPOSCHANGED
) {
213 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according
214 * to http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
215 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
216 * can be masked out by the application.
218 LPWINDOWPOS lpWindowPos
= (LPWINDOWPOS
)pParams
->lParam
;
219 if ((lpWindowPos
->flags
& SWP_SHOWWINDOW
) ||
220 !(lpWindowPos
->flags
& SWP_NOMOVE
) ||
221 !(lpWindowPos
->flags
& SWP_NOSIZE
)) {
222 fb
= stw_framebuffer_from_hwnd( pParams
->hwnd
);
224 /* Size in WINDOWPOS includes the window frame, so get the size
225 * of the client area via GetClientRect.
227 stw_framebuffer_get_size(fb
);
228 stw_framebuffer_unlock(fb
);
232 else if (pParams
->message
== WM_DESTROY
) {
233 stw_lock_framebuffers(stw_dev
);
234 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
236 stw_framebuffer_release_locked(fb
);
237 stw_unlock_framebuffers(stw_dev
);
241 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
246 * Create a new stw_framebuffer object which corresponds to the given
247 * HDC/window. If successful, we return the new stw_framebuffer object
248 * with its mutex locked.
250 struct stw_framebuffer
*
251 stw_framebuffer_create(HDC hdc
, int iPixelFormat
)
254 struct stw_framebuffer
*fb
;
255 const struct stw_pixelformat_info
*pfi
;
257 /* We only support drawing to a window. */
258 hWnd
= WindowFromDC( hdc
);
262 fb
= CALLOC_STRUCT( stw_framebuffer
);
267 fb
->iPixelFormat
= iPixelFormat
;
270 * We often need a displayable pixel format to make GDI happy. Set it
271 * here (always 1, i.e., out first pixel format) where appropriate.
273 fb
->iDisplayablePixelFormat
= iPixelFormat
<= stw_dev
->pixelformat_count
276 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
);
277 fb
->stfb
= stw_st_create_framebuffer( fb
);
286 * Windows can be sometimes have zero width and or height, but we ensure
287 * a non-zero framebuffer size at all times.
290 fb
->must_resize
= TRUE
;
293 fb
->client_rect
.left
= 0;
294 fb
->client_rect
.top
= 0;
295 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
296 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
298 stw_framebuffer_get_size(fb
);
300 InitializeCriticalSection(&fb
->mutex
);
302 /* This is the only case where we lock the stw_framebuffer::mutex before
303 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
304 * and we must prevent any other thread from destroying it before we return.
306 stw_framebuffer_lock(fb
);
308 stw_lock_framebuffers(stw_dev
);
309 fb
->next
= stw_dev
->fb_head
;
310 stw_dev
->fb_head
= fb
;
311 stw_unlock_framebuffers(stw_dev
);
318 * Update the framebuffer's size if necessary.
321 stw_framebuffer_update(struct stw_framebuffer
*fb
)
327 /* XXX: It would be nice to avoid checking the size again -- in theory
328 * stw_call_window_proc would have cought the resize and stored the right
329 * size already, but unfortunately threads created before the DllMain is
330 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
331 * to know of their existing without using the not very portable PSAPI.
333 stw_framebuffer_get_size(fb
);
338 * Try to free all stw_framebuffer objects associated with the device.
341 stw_framebuffer_cleanup(void)
343 struct stw_framebuffer
*fb
;
344 struct stw_framebuffer
*next
;
349 stw_lock_framebuffers(stw_dev
);
351 fb
= stw_dev
->fb_head
;
355 stw_framebuffer_lock(fb
);
356 stw_framebuffer_release_locked(fb
);
360 stw_dev
->fb_head
= NULL
;
362 stw_unlock_framebuffers(stw_dev
);
367 * Given an hdc, return the corresponding stw_framebuffer.
368 * The returned stw_framebuffer will have its mutex locked.
370 static inline struct stw_framebuffer
*
371 stw_framebuffer_from_hdc_locked(HDC hdc
)
375 hwnd
= WindowFromDC(hdc
);
380 return stw_framebuffer_from_hwnd_locked(hwnd
);
385 * Given an HDC, return the corresponding stw_framebuffer.
386 * The returned stw_framebuffer will have its mutex locked.
388 struct stw_framebuffer
*
389 stw_framebuffer_from_hdc(HDC hdc
)
391 struct stw_framebuffer
*fb
;
396 stw_lock_framebuffers(stw_dev
);
397 fb
= stw_framebuffer_from_hdc_locked(hdc
);
398 stw_unlock_framebuffers(stw_dev
);
405 * Given an HWND, return the corresponding stw_framebuffer.
406 * The returned stw_framebuffer will have its mutex locked.
408 struct stw_framebuffer
*
409 stw_framebuffer_from_hwnd(HWND hwnd
)
411 struct stw_framebuffer
*fb
;
413 stw_lock_framebuffers(stw_dev
);
414 fb
= stw_framebuffer_from_hwnd_locked(hwnd
);
415 stw_unlock_framebuffers(stw_dev
);
422 DrvSetPixelFormat(HDC hdc
, LONG iPixelFormat
)
426 struct stw_framebuffer
*fb
;
431 index
= (uint
) iPixelFormat
- 1;
432 count
= stw_pixelformat_get_count();
436 fb
= stw_framebuffer_from_hdc_locked(hdc
);
439 * SetPixelFormat must be called only once. However ignore
440 * pbuffers, for which the framebuffer object is created first.
442 boolean bPbuffer
= fb
->bPbuffer
;
444 stw_framebuffer_unlock( fb
);
449 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
454 stw_framebuffer_unlock( fb
);
456 /* Some applications mistakenly use the undocumented wglSetPixelFormat
457 * function instead of SetPixelFormat, so we call SetPixelFormat here to
458 * avoid opengl32.dll's wglCreateContext to fail */
459 if (GetPixelFormat(hdc
) == 0) {
460 BOOL bRet
= SetPixelFormat(hdc
, iPixelFormat
, NULL
);
462 debug_printf("SetPixelFormat failed\n");
471 stw_pixelformat_get(HDC hdc
)
473 int iPixelFormat
= 0;
474 struct stw_framebuffer
*fb
;
476 fb
= stw_framebuffer_from_hdc(hdc
);
478 iPixelFormat
= fb
->iPixelFormat
;
479 stw_framebuffer_unlock(fb
);
487 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
489 struct stw_framebuffer
*fb
;
490 struct pipe_screen
*screen
;
491 struct pipe_resource
*res
;
496 fb
= stw_framebuffer_from_hdc( hdc
);
500 screen
= stw_dev
->screen
;
502 res
= (struct pipe_resource
*)data
->pPrivateData
;
504 if (data
->hSharedSurface
!= fb
->hSharedSurface
) {
505 if (fb
->shared_surface
) {
506 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
507 fb
->shared_surface
= NULL
;
510 fb
->hSharedSurface
= data
->hSharedSurface
;
512 if (data
->hSharedSurface
&&
513 stw_dev
->stw_winsys
->shared_surface_open
) {
515 stw_dev
->stw_winsys
->shared_surface_open(screen
,
520 if (!fb
->minimized
) {
521 if (fb
->shared_surface
) {
522 stw_dev
->stw_winsys
->compose(screen
,
526 data
->PresentHistoryToken
);
529 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
533 stw_framebuffer_update(fb
);
534 stw_notify_current_locked(fb
);
536 stw_framebuffer_unlock(fb
);
543 * Queue a composition.
545 * The stw_framebuffer object must have its mutex locked. The mutex will
546 * be unlocked here before returning.
549 stw_framebuffer_present_locked(HDC hdc
,
550 struct stw_framebuffer
*fb
,
551 struct pipe_resource
*res
)
553 if (stw_dev
->callbacks
.wglCbPresentBuffers
&&
554 stw_dev
->stw_winsys
->compose
) {
555 GLCBPRESENTBUFFERSDATA data
;
557 memset(&data
, 0, sizeof data
);
560 data
.AdapterLuid
= stw_dev
->AdapterLuid
;
561 data
.rect
= fb
->client_rect
;
562 data
.pPrivateData
= (void *)res
;
564 stw_notify_current_locked(fb
);
565 stw_framebuffer_unlock(fb
);
567 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
570 struct pipe_screen
*screen
= stw_dev
->screen
;
572 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
574 stw_framebuffer_update(fb
);
575 stw_notify_current_locked(fb
);
576 stw_framebuffer_unlock(fb
);
584 DrvSwapBuffers(HDC hdc
)
586 struct stw_context
*ctx
;
587 struct stw_framebuffer
*fb
;
592 fb
= stw_framebuffer_from_hdc( hdc
);
596 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
597 stw_framebuffer_unlock(fb
);
601 ctx
= stw_current_context();
604 /* Display the HUD */
605 struct pipe_resource
*back
=
606 stw_get_framebuffer_resource(fb
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
608 hud_draw(ctx
->hud
, back
);
612 if (ctx
->current_framebuffer
== fb
) {
613 /* flush current context */
614 ctx
->st
->flush(ctx
->st
, ST_FLUSH_END_OF_FRAME
, NULL
);
618 return stw_st_swap_framebuffer_locked(hdc
, fb
->stfb
);
623 DrvSwapLayerBuffers(HDC hdc
, UINT fuPlanes
)
625 if (fuPlanes
& WGL_SWAP_MAIN_PLANE
)
626 return DrvSwapBuffers(hdc
);