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 **************************************************************************/
28 #include "state_tracker/st_context.h"
32 #include "pipe/p_screen.h"
33 #include "pipe/p_state.h"
34 #include "util/u_memory.h"
35 #include "hud/hud_context.h"
36 #include "util/os_time.h"
37 #include "frontend/api.h"
41 #include "stw_framebuffer.h"
42 #include "stw_device.h"
43 #include "stw_winsys.h"
45 #include "stw_context.h"
50 * Search the framebuffer with the matching HWND while holding the
51 * stw_dev::fb_mutex global lock.
52 * If a stw_framebuffer is found, lock it and return the pointer.
55 static struct stw_framebuffer *
56 stw_framebuffer_from_hwnd_locked(HWND hwnd)
58 struct stw_framebuffer *fb;
60 for (fb = stw_dev->fb_head; fb != NULL; fb = fb->next)
61 if (fb->hWnd == hwnd) {
62 stw_framebuffer_lock(fb);
64 /* When running with Zink, during the Vulkan surface creation
65 * it's possible that the underlying Vulkan driver will try to
66 * access the HWND/HDC we passed in (see stw_st_fill_private_loader_data()).
67 * Because we create the Vulkan surface while holding the framebuffer
68 * lock, when the driver starts to look up properties,
69 * we'd end up double locking when looking up the framebuffer.
71 assert(stw_dev->zink || fb->mutex.RecursionCount == 1);
80 * Decrement the reference count on the given stw_framebuffer object.
81 * If the reference count hits zero, destroy the object.
83 * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already be
84 * locked. After this function completes, the fb's mutex will be unlocked.
87 stw_framebuffer_release_locked(struct stw_framebuffer *fb,
88 struct st_context *st)
90 struct stw_framebuffer **link;
93 assert(stw_own_mutex(&fb->mutex));
94 assert(stw_own_mutex(&stw_dev->fb_mutex) || fb->owner == STW_FRAMEBUFFER_EGL_WINDOW);
96 /* check the reference count */
99 stw_framebuffer_unlock(fb);
103 if (fb->owner != STW_FRAMEBUFFER_EGL_WINDOW) {
104 /* remove this stw_framebuffer from the device's linked list */
105 link = &stw_dev->fb_head;
107 link = &(*link)->next;
113 if (fb->shared_surface)
114 stw_dev->stw_winsys->shared_surface_close(stw_dev->screen,
117 if (fb->winsys_framebuffer)
118 fb->winsys_framebuffer->destroy(fb->winsys_framebuffer, st ? st->pipe : NULL);
120 stw_st_destroy_framebuffer_locked(fb->drawable);
122 stw_framebuffer_unlock(fb);
124 DeleteCriticalSection(&fb->mutex);
131 * Query the size of the given framebuffer's on-screen window and update
132 * the stw_framebuffer's width/height.
135 stw_framebuffer_get_size(struct stw_framebuffer *fb)
146 assert(fb->width && fb->height);
147 assert(fb->client_rect.right == fb->client_rect.left + fb->width);
148 assert(fb->client_rect.bottom == fb->client_rect.top + fb->height);
151 * Get the client area size.
153 if (!GetClientRect(fb->hWnd, &client_rect)) {
157 assert(client_rect.left == 0);
158 assert(client_rect.top == 0);
159 width = client_rect.right - client_rect.left;
160 height = client_rect.bottom - client_rect.top;
162 fb->minimized = width == 0 || height == 0;
164 if (width <= 0 || height <= 0) {
166 * When the window is minimized GetClientRect will return zeros. Simply
167 * preserve the current window size, until the window is restored or
173 if (width != fb->width || height != fb->height) {
174 fb->must_resize = TRUE;
181 if (ClientToScreen(fb->hWnd, &client_pos) &&
182 GetWindowRect(fb->hWnd, &window_rect)) {
183 fb->client_rect.left = client_pos.x - window_rect.left;
184 fb->client_rect.top = client_pos.y - window_rect.top;
187 fb->client_rect.right = fb->client_rect.left + fb->width;
188 fb->client_rect.bottom = fb->client_rect.top + fb->height;
192 debug_printf("%s: hwnd = %p\n", __func__, fb->hWnd);
193 debug_printf("%s: client_position = (%li, %li)\n",
194 __func__, client_pos.x, client_pos.y);
195 debug_printf("%s: window_rect = (%li, %li) - (%li, %li)\n",
197 window_rect.left, window_rect.top,
198 window_rect.right, window_rect.bottom);
199 debug_printf("%s: client_rect = (%li, %li) - (%li, %li)\n",
201 fb->client_rect.left, fb->client_rect.top,
202 fb->client_rect.right, fb->client_rect.bottom);
208 * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
209 * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
212 stw_call_window_proc(int nCode, WPARAM wParam, LPARAM lParam)
214 struct stw_tls_data *tls_data;
215 PCWPSTRUCT pParams = (PCWPSTRUCT)lParam;
216 struct stw_framebuffer *fb;
218 tls_data = stw_tls_get_data();
222 if (nCode < 0 || !stw_dev)
223 return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
225 /* We check that the stw_dev object is initialized before we try to do
226 * anything with it. Otherwise, in multi-threaded programs there's a
227 * chance of executing this code before the stw_dev object is fully
230 if (stw_dev && stw_dev->initialized) {
231 if (pParams->message == WM_WINDOWPOSCHANGED) {
232 /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according
233 * to http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
234 * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
235 * can be masked out by the application.
237 LPWINDOWPOS lpWindowPos = (LPWINDOWPOS)pParams->lParam;
238 if ((lpWindowPos->flags & SWP_SHOWWINDOW) ||
239 !(lpWindowPos->flags & SWP_NOMOVE) ||
240 !(lpWindowPos->flags & SWP_NOSIZE)) {
241 fb = stw_framebuffer_from_hwnd( pParams->hwnd );
243 /* Size in WINDOWPOS includes the window frame, so get the size
244 * of the client area via GetClientRect.
246 stw_framebuffer_get_size(fb);
247 stw_framebuffer_unlock(fb);
251 else if (pParams->message == WM_DESTROY) {
252 stw_lock_framebuffers(stw_dev);
253 fb = stw_framebuffer_from_hwnd_locked( pParams->hwnd );
255 struct stw_context *current_context = stw_current_context();
256 struct st_context *st = current_context &&
257 current_context->current_framebuffer == fb ? current_context->st : NULL;
258 stw_framebuffer_release_locked(fb, st);
260 stw_unlock_framebuffers(stw_dev);
264 return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
269 * Create a new stw_framebuffer object which corresponds to the given
270 * HDC/window. If successful, we return the new stw_framebuffer object
271 * with its mutex locked.
273 struct stw_framebuffer *
274 stw_framebuffer_create(HWND hWnd, const struct stw_pixelformat_info *pfi, enum stw_framebuffer_owner owner,
275 struct pipe_frontend_screen *fscreen)
277 struct stw_framebuffer *fb;
279 fb = CALLOC_STRUCT( stw_framebuffer );
285 if (stw_dev->stw_winsys->create_framebuffer)
286 fb->winsys_framebuffer =
287 stw_dev->stw_winsys->create_framebuffer(stw_dev->screen, hWnd, pfi->iPixelFormat);
290 * We often need a displayable pixel format to make GDI happy. Set it
291 * here (always 1, i.e., out first pixel format) where appropriate.
293 fb->iDisplayablePixelFormat = pfi->iPixelFormat <= stw_dev->pixelformat_count
294 ? pfi->iPixelFormat : 1;
298 fb->drawable = stw_st_create_framebuffer( fb, fscreen );
306 /* A -1 means defer to the global stw_dev->swap_interval */
307 fb->swap_interval = -1;
310 * Windows can be sometimes have zero width and or height, but we ensure
311 * a non-zero framebuffer size at all times.
314 fb->must_resize = TRUE;
317 fb->client_rect.left = 0;
318 fb->client_rect.top = 0;
319 fb->client_rect.right = fb->client_rect.left + fb->width;
320 fb->client_rect.bottom = fb->client_rect.top + fb->height;
322 stw_framebuffer_get_size(fb);
324 InitializeCriticalSection(&fb->mutex);
326 /* This is the only case where we lock the stw_framebuffer::mutex before
327 * stw_dev::fb_mutex, since no other thread can know about this framebuffer
328 * and we must prevent any other thread from destroying it before we return.
330 stw_framebuffer_lock(fb);
332 if (owner != STW_FRAMEBUFFER_EGL_WINDOW) {
333 stw_lock_framebuffers(stw_dev);
334 fb->next = stw_dev->fb_head;
335 stw_dev->fb_head = fb;
336 stw_unlock_framebuffers(stw_dev);
343 * Increase fb reference count. The referenced framebuffer should be locked.
345 * It's not necessary to hold stw_dev::fb_mutex global lock.
348 stw_framebuffer_reference_locked(struct stw_framebuffer *fb)
351 assert(stw_own_mutex(&fb->mutex));
357 * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
358 * after calling this function, as it may have been deleted by another thread
362 stw_framebuffer_unlock(struct stw_framebuffer *fb)
365 assert(stw_own_mutex(&fb->mutex));
366 LeaveCriticalSection(&fb->mutex);
371 * Update the framebuffer's size if necessary.
374 stw_framebuffer_update(struct stw_framebuffer *fb)
376 assert(fb->drawable);
380 /* XXX: It would be nice to avoid checking the size again -- in theory
381 * stw_call_window_proc would have cought the resize and stored the right
382 * size already, but unfortunately threads created before the DllMain is
383 * called don't get a DLL_THREAD_ATTACH notification, and there is no way
384 * to know of their existing without using the not very portable PSAPI.
386 stw_framebuffer_get_size(fb);
391 * Try to free all stw_framebuffer objects associated with the device.
394 stw_framebuffer_cleanup(void)
396 struct stw_framebuffer *fb;
397 struct stw_framebuffer *next;
402 stw_lock_framebuffers(stw_dev);
404 fb = stw_dev->fb_head;
408 stw_framebuffer_lock(fb);
409 stw_framebuffer_release_locked(fb, NULL);
413 stw_dev->fb_head = NULL;
415 stw_unlock_framebuffers(stw_dev);
420 * Given an hdc, return the corresponding stw_framebuffer.
421 * The returned stw_framebuffer will have its mutex locked.
423 static struct stw_framebuffer *
424 stw_framebuffer_from_hdc_locked(HDC hdc)
428 hwnd = WindowFromDC(hdc);
433 return stw_framebuffer_from_hwnd_locked(hwnd);
438 * Given an HDC, return the corresponding stw_framebuffer.
439 * The returned stw_framebuffer will have its mutex locked.
441 struct stw_framebuffer *
442 stw_framebuffer_from_hdc(HDC hdc)
444 struct stw_framebuffer *fb;
449 stw_lock_framebuffers(stw_dev);
450 fb = stw_framebuffer_from_hdc_locked(hdc);
451 stw_unlock_framebuffers(stw_dev);
458 * Given an HWND, return the corresponding stw_framebuffer.
459 * The returned stw_framebuffer will have its mutex locked.
461 struct stw_framebuffer *
462 stw_framebuffer_from_hwnd(HWND hwnd)
464 struct stw_framebuffer *fb;
466 stw_lock_framebuffers(stw_dev);
467 fb = stw_framebuffer_from_hwnd_locked(hwnd);
468 stw_unlock_framebuffers(stw_dev);
475 DrvSetPixelFormat(HDC hdc, LONG iPixelFormat)
479 struct stw_framebuffer *fb;
484 index = (uint) iPixelFormat - 1;
485 count = stw_pixelformat_get_count(hdc);
489 fb = stw_framebuffer_from_hdc_locked(hdc);
492 * SetPixelFormat must be called only once. However ignore
493 * pbuffers, for which the framebuffer object is created first.
495 boolean bPbuffer = fb->owner == STW_FRAMEBUFFER_PBUFFER;
497 stw_framebuffer_unlock( fb );
502 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info(iPixelFormat);
504 fb = stw_framebuffer_create(WindowFromDC(hdc), pfi, STW_FRAMEBUFFER_WGL_WINDOW, stw_dev->fscreen);
509 stw_framebuffer_unlock( fb );
511 /* Some applications mistakenly use the undocumented wglSetPixelFormat
512 * function instead of SetPixelFormat, so we call SetPixelFormat here to
513 * avoid opengl32.dll's wglCreateContext to fail */
514 if (GetPixelFormat(hdc) == 0) {
515 BOOL bRet = SetPixelFormat(hdc, iPixelFormat, NULL);
517 debug_printf("SetPixelFormat failed\n");
526 stw_pixelformat_get(HDC hdc)
528 int iPixelFormat = 0;
529 struct stw_framebuffer *fb;
531 fb = stw_framebuffer_from_hdc(hdc);
533 iPixelFormat = fb->pfi->iPixelFormat;
534 stw_framebuffer_unlock(fb);
542 DrvPresentBuffers(HDC hdc, LPPRESENTBUFFERS data)
544 struct stw_framebuffer *fb;
545 struct stw_context *ctx;
546 struct pipe_screen *screen;
547 struct pipe_context *pipe;
548 struct pipe_resource *res;
553 fb = stw_framebuffer_from_hdc( hdc );
557 screen = stw_dev->screen;
558 ctx = stw_current_context();
559 pipe = ctx ? ctx->st->pipe : NULL;
561 res = (struct pipe_resource *)data->pPrivData;
563 if (data->hSurface != fb->hSharedSurface) {
564 if (fb->shared_surface) {
565 stw_dev->stw_winsys->shared_surface_close(screen, fb->shared_surface);
566 fb->shared_surface = NULL;
569 fb->hSharedSurface = data->hSurface;
571 if (data->hSurface &&
572 stw_dev->stw_winsys->shared_surface_open) {
574 stw_dev->stw_winsys->shared_surface_open(screen,
579 if (!fb->minimized) {
580 if (fb->shared_surface) {
581 stw_dev->stw_winsys->compose(screen,
585 data->ullPresentToken);
588 stw_dev->stw_winsys->present( screen, pipe, res, hdc );
592 stw_framebuffer_update(fb);
593 stw_notify_current_locked(fb);
595 stw_framebuffer_unlock(fb);
602 * Queue a composition.
604 * The stw_framebuffer object must have its mutex locked. The mutex will
605 * be unlocked here before returning.
608 stw_framebuffer_present_locked(HDC hdc,
609 struct stw_framebuffer *fb,
610 struct pipe_resource *res)
612 if (fb->winsys_framebuffer) {
613 int interval = fb->swap_interval == -1 ? stw_dev->swap_interval : fb->swap_interval;
614 BOOL result = fb->winsys_framebuffer->present(fb->winsys_framebuffer, interval);
616 stw_framebuffer_update(fb);
617 stw_notify_current_locked(fb);
618 stw_framebuffer_unlock(fb);
622 else if (stw_dev->callbacks.pfnPresentBuffers &&
623 stw_dev->stw_winsys->compose) {
624 PRESENTBUFFERSCB data;
626 memset(&data, 0, sizeof data);
628 data.syncType = PRESCB_SYNCTYPE_NONE;
629 data.luidAdapter = stw_dev->AdapterLuid;
630 data.updateRect = fb->client_rect;
631 data.pPrivData = (void *)res;
633 stw_notify_current_locked(fb);
634 stw_framebuffer_unlock(fb);
636 return stw_dev->callbacks.pfnPresentBuffers(hdc, &data);
639 struct pipe_screen *screen = stw_dev->screen;
640 struct stw_context *ctx = stw_current_context();
641 struct pipe_context *pipe = ctx ? ctx->st->pipe : NULL;
643 stw_dev->stw_winsys->present( screen, pipe, res, hdc );
645 stw_framebuffer_update(fb);
646 stw_notify_current_locked(fb);
647 stw_framebuffer_unlock(fb);
655 * This is called just before issuing the buffer swap/present.
656 * We query the current time and determine if we should sleep before
657 * issuing the swap/present.
658 * This is a bit of a hack and is certainly not very accurate but it
660 * This is for the WGL_ARB_swap_interval extension.
663 wait_swap_interval(struct stw_framebuffer *fb, int interval)
665 /* Note: all time variables here are in units of microseconds */
666 int64_t cur_time = os_time_get_nano() / 1000;
668 if (fb->prev_swap_time != 0) {
669 /* Compute time since previous swap */
670 int64_t delta = cur_time - fb->prev_swap_time;
671 int64_t min_swap_period =
672 1.0e6 / stw_dev->refresh_rate * interval;
674 /* If time since last swap is less than wait period, wait.
675 * Note that it's possible for the delta to be negative because of
676 * rollover. See https://bugs.freedesktop.org/show_bug.cgi?id=102241
678 if ((delta >= 0) && (delta < min_swap_period)) {
679 float fudge = 1.75f; /* emperical fudge factor */
680 int64_t wait = (min_swap_period - delta) * fudge;
685 fb->prev_swap_time = cur_time;
689 stw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb)
691 struct stw_context *ctx;
692 if (!(fb->pfi->pfd.dwFlags & PFD_DOUBLEBUFFER)) {
693 stw_framebuffer_unlock(fb);
697 ctx = stw_current_context();
700 /* Display the HUD */
701 struct pipe_resource *back =
702 stw_get_framebuffer_resource(fb->drawable, ST_ATTACHMENT_BACK_LEFT);
704 hud_run(ctx->hud, NULL, back);
708 if (ctx->current_framebuffer == fb) {
709 /* flush current context */
710 stw_st_flush(ctx->st, fb->drawable, ST_FLUSH_END_OF_FRAME);
714 int interval = fb->swap_interval == -1 ? stw_dev->swap_interval : fb->swap_interval;
715 if (interval != 0 && !fb->winsys_framebuffer) {
716 wait_swap_interval(fb, interval);
719 return stw_st_swap_framebuffer_locked(hdc, ctx->st, fb->drawable);
723 DrvSwapBuffers(HDC hdc)
725 struct stw_framebuffer *fb;
730 fb = stw_framebuffer_from_hdc( hdc );
734 return stw_framebuffer_swap_locked(hdc, fb);
739 DrvSwapLayerBuffers(HDC hdc, UINT fuPlanes)
741 if (fuPlanes & WGL_SWAP_MAIN_PLANE)
742 return DrvSwapBuffers(hdc);