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 "util/os_time.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.
48 * If a stw_framebuffer is found, lock it and return the pointer.
51 static struct stw_framebuffer
*
52 stw_framebuffer_from_hwnd_locked(HWND hwnd
)
54 struct stw_framebuffer
*fb
;
56 for (fb
= stw_dev
->fb_head
; fb
!= NULL
; fb
= fb
->next
)
57 if (fb
->hWnd
== hwnd
) {
58 stw_framebuffer_lock(fb
);
59 assert(fb
->mutex
.RecursionCount
== 1);
68 * Decrement the reference count on the given stw_framebuffer object.
69 * If the reference count hits zero, destroy the object.
71 * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already be
72 * locked. After this function completes, the fb's mutex will be unlocked.
75 stw_framebuffer_release_locked(struct stw_framebuffer
*fb
)
77 struct stw_framebuffer
**link
;
80 assert(stw_own_mutex(&fb
->mutex
));
81 assert(stw_own_mutex(&stw_dev
->fb_mutex
));
83 /* check the reference count */
86 stw_framebuffer_unlock(fb
);
90 /* remove this stw_framebuffer from the device's linked list */
91 link
= &stw_dev
->fb_head
;
93 link
= &(*link
)->next
;
98 if (fb
->shared_surface
)
99 stw_dev
->stw_winsys
->shared_surface_close(stw_dev
->screen
,
102 stw_st_destroy_framebuffer_locked(fb
->stfb
);
104 stw_framebuffer_unlock(fb
);
106 DeleteCriticalSection(&fb
->mutex
);
113 * Query the size of the given framebuffer's on-screen window and update
114 * the stw_framebuffer's width/height.
117 stw_framebuffer_get_size(struct stw_framebuffer
*fb
)
128 assert(fb
->width
&& fb
->height
);
129 assert(fb
->client_rect
.right
== fb
->client_rect
.left
+ fb
->width
);
130 assert(fb
->client_rect
.bottom
== fb
->client_rect
.top
+ fb
->height
);
133 * Get the client area size.
135 if (!GetClientRect(fb
->hWnd
, &client_rect
)) {
139 assert(client_rect
.left
== 0);
140 assert(client_rect
.top
== 0);
141 width
= client_rect
.right
- client_rect
.left
;
142 height
= client_rect
.bottom
- client_rect
.top
;
144 fb
->minimized
= width
== 0 || height
== 0;
146 if (width
<= 0 || height
<= 0) {
148 * When the window is minimized GetClientRect will return zeros. Simply
149 * preserve the current window size, until the window is restored or
155 if (width
!= fb
->width
|| height
!= fb
->height
) {
156 fb
->must_resize
= TRUE
;
163 if (ClientToScreen(fb
->hWnd
, &client_pos
) &&
164 GetWindowRect(fb
->hWnd
, &window_rect
)) {
165 fb
->client_rect
.left
= client_pos
.x
- window_rect
.left
;
166 fb
->client_rect
.top
= client_pos
.y
- window_rect
.top
;
169 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
170 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
174 debug_printf("%s: hwnd = %p\n", __FUNCTION__
, fb
->hWnd
);
175 debug_printf("%s: client_position = (%li, %li)\n",
176 __FUNCTION__
, client_pos
.x
, client_pos
.y
);
177 debug_printf("%s: window_rect = (%li, %li) - (%li, %li)\n",
179 window_rect
.left
, window_rect
.top
,
180 window_rect
.right
, window_rect
.bottom
);
181 debug_printf("%s: client_rect = (%li, %li) - (%li, %li)\n",
183 fb
->client_rect
.left
, fb
->client_rect
.top
,
184 fb
->client_rect
.right
, fb
->client_rect
.bottom
);
190 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
191 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
194 stw_call_window_proc(int nCode
, WPARAM wParam
, LPARAM lParam
)
196 struct stw_tls_data
*tls_data
;
197 PCWPSTRUCT pParams
= (PCWPSTRUCT
)lParam
;
198 struct stw_framebuffer
*fb
;
200 tls_data
= stw_tls_get_data();
204 if (nCode
< 0 || !stw_dev
)
205 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
207 /* We check that the stw_dev object is initialized before we try to do
208 * anything with it. Otherwise, in multi-threaded programs there's a
209 * chance of executing this code before the stw_dev object is fully
212 if (stw_dev
&& stw_dev
->initialized
) {
213 if (pParams
->message
== WM_WINDOWPOSCHANGED
) {
214 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according
215 * to http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
216 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
217 * can be masked out by the application.
219 LPWINDOWPOS lpWindowPos
= (LPWINDOWPOS
)pParams
->lParam
;
220 if ((lpWindowPos
->flags
& SWP_SHOWWINDOW
) ||
221 !(lpWindowPos
->flags
& SWP_NOMOVE
) ||
222 !(lpWindowPos
->flags
& SWP_NOSIZE
)) {
223 fb
= stw_framebuffer_from_hwnd( pParams
->hwnd
);
225 /* Size in WINDOWPOS includes the window frame, so get the size
226 * of the client area via GetClientRect.
228 stw_framebuffer_get_size(fb
);
229 stw_framebuffer_unlock(fb
);
233 else if (pParams
->message
== WM_DESTROY
) {
234 stw_lock_framebuffers(stw_dev
);
235 fb
= stw_framebuffer_from_hwnd_locked( pParams
->hwnd
);
237 stw_framebuffer_release_locked(fb
);
238 stw_unlock_framebuffers(stw_dev
);
242 return CallNextHookEx(tls_data
->hCallWndProcHook
, nCode
, wParam
, lParam
);
247 * Create a new stw_framebuffer object which corresponds to the given
248 * HDC/window. If successful, we return the new stw_framebuffer object
249 * with its mutex locked.
251 struct stw_framebuffer
*
252 stw_framebuffer_create(HDC hdc
, int iPixelFormat
)
255 struct stw_framebuffer
*fb
;
256 const struct stw_pixelformat_info
*pfi
;
258 /* We only support drawing to a window. */
259 hWnd
= WindowFromDC( hdc
);
263 fb
= CALLOC_STRUCT( stw_framebuffer
);
268 fb
->iPixelFormat
= iPixelFormat
;
271 * We often need a displayable pixel format to make GDI happy. Set it
272 * here (always 1, i.e., out first pixel format) where appropriate.
274 fb
->iDisplayablePixelFormat
= iPixelFormat
<= stw_dev
->pixelformat_count
277 fb
->pfi
= pfi
= stw_pixelformat_get_info( iPixelFormat
);
278 fb
->stfb
= stw_st_create_framebuffer( fb
);
287 * Windows can be sometimes have zero width and or height, but we ensure
288 * a non-zero framebuffer size at all times.
291 fb
->must_resize
= TRUE
;
294 fb
->client_rect
.left
= 0;
295 fb
->client_rect
.top
= 0;
296 fb
->client_rect
.right
= fb
->client_rect
.left
+ fb
->width
;
297 fb
->client_rect
.bottom
= fb
->client_rect
.top
+ fb
->height
;
299 stw_framebuffer_get_size(fb
);
301 InitializeCriticalSection(&fb
->mutex
);
303 /* This is the only case where we lock the stw_framebuffer::mutex before
304 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
305 * and we must prevent any other thread from destroying it before we return.
307 stw_framebuffer_lock(fb
);
309 stw_lock_framebuffers(stw_dev
);
310 fb
->next
= stw_dev
->fb_head
;
311 stw_dev
->fb_head
= fb
;
312 stw_unlock_framebuffers(stw_dev
);
319 * Update the framebuffer's size if necessary.
322 stw_framebuffer_update(struct stw_framebuffer
*fb
)
328 /* XXX: It would be nice to avoid checking the size again -- in theory
329 * stw_call_window_proc would have cought the resize and stored the right
330 * size already, but unfortunately threads created before the DllMain is
331 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
332 * to know of their existing without using the not very portable PSAPI.
334 stw_framebuffer_get_size(fb
);
339 * Try to free all stw_framebuffer objects associated with the device.
342 stw_framebuffer_cleanup(void)
344 struct stw_framebuffer
*fb
;
345 struct stw_framebuffer
*next
;
350 stw_lock_framebuffers(stw_dev
);
352 fb
= stw_dev
->fb_head
;
356 stw_framebuffer_lock(fb
);
357 stw_framebuffer_release_locked(fb
);
361 stw_dev
->fb_head
= NULL
;
363 stw_unlock_framebuffers(stw_dev
);
368 * Given an hdc, return the corresponding stw_framebuffer.
369 * The returned stw_framebuffer will have its mutex locked.
371 static struct stw_framebuffer
*
372 stw_framebuffer_from_hdc_locked(HDC hdc
)
376 hwnd
= WindowFromDC(hdc
);
381 return stw_framebuffer_from_hwnd_locked(hwnd
);
386 * Given an HDC, return the corresponding stw_framebuffer.
387 * The returned stw_framebuffer will have its mutex locked.
389 struct stw_framebuffer
*
390 stw_framebuffer_from_hdc(HDC hdc
)
392 struct stw_framebuffer
*fb
;
397 stw_lock_framebuffers(stw_dev
);
398 fb
= stw_framebuffer_from_hdc_locked(hdc
);
399 stw_unlock_framebuffers(stw_dev
);
406 * Given an HWND, return the corresponding stw_framebuffer.
407 * The returned stw_framebuffer will have its mutex locked.
409 struct stw_framebuffer
*
410 stw_framebuffer_from_hwnd(HWND hwnd
)
412 struct stw_framebuffer
*fb
;
414 stw_lock_framebuffers(stw_dev
);
415 fb
= stw_framebuffer_from_hwnd_locked(hwnd
);
416 stw_unlock_framebuffers(stw_dev
);
423 DrvSetPixelFormat(HDC hdc
, LONG iPixelFormat
)
427 struct stw_framebuffer
*fb
;
432 index
= (uint
) iPixelFormat
- 1;
433 count
= stw_pixelformat_get_count();
437 fb
= stw_framebuffer_from_hdc_locked(hdc
);
440 * SetPixelFormat must be called only once. However ignore
441 * pbuffers, for which the framebuffer object is created first.
443 boolean bPbuffer
= fb
->bPbuffer
;
445 stw_framebuffer_unlock( fb
);
450 fb
= stw_framebuffer_create(hdc
, iPixelFormat
);
455 stw_framebuffer_unlock( fb
);
457 /* Some applications mistakenly use the undocumented wglSetPixelFormat
458 * function instead of SetPixelFormat, so we call SetPixelFormat here to
459 * avoid opengl32.dll's wglCreateContext to fail */
460 if (GetPixelFormat(hdc
) == 0) {
461 BOOL bRet
= SetPixelFormat(hdc
, iPixelFormat
, NULL
);
463 debug_printf("SetPixelFormat failed\n");
472 stw_pixelformat_get(HDC hdc
)
474 int iPixelFormat
= 0;
475 struct stw_framebuffer
*fb
;
477 fb
= stw_framebuffer_from_hdc(hdc
);
479 iPixelFormat
= fb
->iPixelFormat
;
480 stw_framebuffer_unlock(fb
);
488 DrvPresentBuffers(HDC hdc
, PGLPRESENTBUFFERSDATA data
)
490 struct stw_framebuffer
*fb
;
491 struct pipe_screen
*screen
;
492 struct pipe_resource
*res
;
497 fb
= stw_framebuffer_from_hdc( hdc
);
501 screen
= stw_dev
->screen
;
503 res
= (struct pipe_resource
*)data
->pPrivateData
;
505 if (data
->hSharedSurface
!= fb
->hSharedSurface
) {
506 if (fb
->shared_surface
) {
507 stw_dev
->stw_winsys
->shared_surface_close(screen
, fb
->shared_surface
);
508 fb
->shared_surface
= NULL
;
511 fb
->hSharedSurface
= data
->hSharedSurface
;
513 if (data
->hSharedSurface
&&
514 stw_dev
->stw_winsys
->shared_surface_open
) {
516 stw_dev
->stw_winsys
->shared_surface_open(screen
,
521 if (!fb
->minimized
) {
522 if (fb
->shared_surface
) {
523 stw_dev
->stw_winsys
->compose(screen
,
527 data
->PresentHistoryToken
);
530 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
534 stw_framebuffer_update(fb
);
535 stw_notify_current_locked(fb
);
537 stw_framebuffer_unlock(fb
);
544 * Queue a composition.
546 * The stw_framebuffer object must have its mutex locked. The mutex will
547 * be unlocked here before returning.
550 stw_framebuffer_present_locked(HDC hdc
,
551 struct stw_framebuffer
*fb
,
552 struct pipe_resource
*res
)
554 if (stw_dev
->callbacks
.wglCbPresentBuffers
&&
555 stw_dev
->stw_winsys
->compose
) {
556 GLCBPRESENTBUFFERSDATA data
;
558 memset(&data
, 0, sizeof data
);
561 data
.AdapterLuid
= stw_dev
->AdapterLuid
;
562 data
.rect
= fb
->client_rect
;
563 data
.pPrivateData
= (void *)res
;
565 stw_notify_current_locked(fb
);
566 stw_framebuffer_unlock(fb
);
568 return stw_dev
->callbacks
.wglCbPresentBuffers(hdc
, &data
);
571 struct pipe_screen
*screen
= stw_dev
->screen
;
573 stw_dev
->stw_winsys
->present( screen
, res
, hdc
);
575 stw_framebuffer_update(fb
);
576 stw_notify_current_locked(fb
);
577 stw_framebuffer_unlock(fb
);
585 * This is called just before issuing the buffer swap/present.
586 * We query the current time and determine if we should sleep before
587 * issuing the swap/present.
588 * This is a bit of a hack and is certainly not very accurate but it
590 * This is for the WGL_ARB_swap_interval extension.
593 wait_swap_interval(struct stw_framebuffer
*fb
)
595 /* Note: all time variables here are in units of microseconds */
596 int64_t cur_time
= os_time_get_nano() / 1000;
598 if (fb
->prev_swap_time
!= 0) {
599 /* Compute time since previous swap */
600 int64_t delta
= cur_time
- fb
->prev_swap_time
;
601 int64_t min_swap_period
=
602 1.0e6
/ stw_dev
->refresh_rate
* stw_dev
->swap_interval
;
604 /* If time since last swap is less than wait period, wait.
605 * Note that it's possible for the delta to be negative because of
606 * rollover. See https://bugs.freedesktop.org/show_bug.cgi?id=102241
608 if ((delta
>= 0) && (delta
< min_swap_period
)) {
609 float fudge
= 1.75f
; /* emperical fudge factor */
610 int64_t wait
= (min_swap_period
- delta
) * fudge
;
615 fb
->prev_swap_time
= cur_time
;
620 DrvSwapBuffers(HDC hdc
)
622 struct stw_context
*ctx
;
623 struct stw_framebuffer
*fb
;
628 fb
= stw_framebuffer_from_hdc( hdc
);
632 if (!(fb
->pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
)) {
633 stw_framebuffer_unlock(fb
);
637 ctx
= stw_current_context();
640 /* Display the HUD */
641 struct pipe_resource
*back
=
642 stw_get_framebuffer_resource(fb
->stfb
, ST_ATTACHMENT_BACK_LEFT
);
644 hud_draw(ctx
->hud
, back
);
648 if (ctx
->current_framebuffer
== fb
) {
649 /* flush current context */
650 ctx
->st
->flush(ctx
->st
, ST_FLUSH_END_OF_FRAME
, NULL
);
654 if (stw_dev
->swap_interval
!= 0) {
655 wait_swap_interval(fb
);
658 return stw_st_swap_framebuffer_locked(hdc
, fb
->stfb
);
663 DrvSwapLayerBuffers(HDC hdc
, UINT fuPlanes
)
665 if (fuPlanes
& WGL_SWAP_MAIN_PLANE
)
666 return DrvSwapBuffers(hdc
);