1 /**************************************************************************
2  *
3  * Copyright 2008-2009 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28 #include <windows.h>
29 
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 "frontend/api.h"
35 
36 #include <GL/gl.h>
37 #include "gldrv.h"
38 #include "stw_framebuffer.h"
39 #include "stw_device.h"
40 #include "stw_winsys.h"
41 #include "stw_tls.h"
42 #include "stw_context.h"
43 #include "stw_st.h"
44 
45 
46 /**
47  * Search the framebuffer with the matching HWND while holding the
48  * stw_dev::fb_mutex global lock.
49  * If a stw_framebuffer is found, lock it and return the pointer.
50  * Else, return NULL.
51  */
52 static struct stw_framebuffer *
stw_framebuffer_from_hwnd_locked(HWND hwnd)53 stw_framebuffer_from_hwnd_locked(HWND hwnd)
54 {
55    struct stw_framebuffer *fb;
56 
57    for (fb = stw_dev->fb_head; fb != NULL; fb = fb->next)
58       if (fb->hWnd == hwnd) {
59          stw_framebuffer_lock(fb);
60          assert(fb->mutex.RecursionCount == 1);
61          return fb;
62       }
63 
64    return NULL;
65 }
66 
67 
68 /**
69  * Decrement the reference count on the given stw_framebuffer object.
70  * If the reference count hits zero, destroy the object.
71  *
72  * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already be
73  * locked.  After this function completes, the fb's mutex will be unlocked.
74  */
75 void
stw_framebuffer_release_locked(struct stw_framebuffer * fb)76 stw_framebuffer_release_locked(struct stw_framebuffer *fb)
77 {
78    struct stw_framebuffer **link;
79 
80    assert(fb);
81    assert(stw_own_mutex(&fb->mutex));
82    assert(stw_own_mutex(&stw_dev->fb_mutex));
83 
84    /* check the reference count */
85    fb->refcnt--;
86    if (fb->refcnt) {
87       stw_framebuffer_unlock(fb);
88       return;
89    }
90 
91    /* remove this stw_framebuffer from the device's linked list */
92    link = &stw_dev->fb_head;
93    while (*link != fb)
94       link = &(*link)->next;
95    assert(*link);
96    *link = fb->next;
97    fb->next = NULL;
98 
99    if (fb->shared_surface)
100       stw_dev->stw_winsys->shared_surface_close(stw_dev->screen,
101                                                 fb->shared_surface);
102 
103    stw_st_destroy_framebuffer_locked(fb->stfb);
104 
105    stw_framebuffer_unlock(fb);
106 
107    DeleteCriticalSection(&fb->mutex);
108 
109    FREE( fb );
110 }
111 
112 
113 /**
114  * Query the size of the given framebuffer's on-screen window and update
115  * the stw_framebuffer's width/height.
116  */
117 static void
stw_framebuffer_get_size(struct stw_framebuffer * fb)118 stw_framebuffer_get_size(struct stw_framebuffer *fb)
119 {
120    LONG width, height;
121    RECT client_rect;
122    RECT window_rect;
123    POINT client_pos;
124 
125    /*
126     * Sanity checking.
127     */
128    assert(fb->hWnd);
129    assert(fb->width && fb->height);
130    assert(fb->client_rect.right  == fb->client_rect.left + fb->width);
131    assert(fb->client_rect.bottom == fb->client_rect.top  + fb->height);
132 
133    /*
134     * Get the client area size.
135     */
136    if (!GetClientRect(fb->hWnd, &client_rect)) {
137       return;
138    }
139 
140    assert(client_rect.left == 0);
141    assert(client_rect.top == 0);
142    width  = client_rect.right  - client_rect.left;
143    height = client_rect.bottom - client_rect.top;
144 
145    fb->minimized = width == 0 || height == 0;
146 
147    if (width <= 0 || height <= 0) {
148       /*
149        * When the window is minimized GetClientRect will return zeros.  Simply
150        * preserve the current window size, until the window is restored or
151        * maximized again.
152        */
153       return;
154    }
155 
156    if (width != fb->width || height != fb->height) {
157       fb->must_resize = TRUE;
158       fb->width = width;
159       fb->height = height;
160    }
161 
162    client_pos.x = 0;
163    client_pos.y = 0;
164    if (ClientToScreen(fb->hWnd, &client_pos) &&
165        GetWindowRect(fb->hWnd, &window_rect)) {
166       fb->client_rect.left = client_pos.x - window_rect.left;
167       fb->client_rect.top  = client_pos.y - window_rect.top;
168    }
169 
170    fb->client_rect.right  = fb->client_rect.left + fb->width;
171    fb->client_rect.bottom = fb->client_rect.top  + fb->height;
172 
173 #if 0
174    debug_printf("\n");
175    debug_printf("%s: hwnd = %p\n", __FUNCTION__, fb->hWnd);
176    debug_printf("%s: client_position = (%li, %li)\n",
177                 __FUNCTION__, client_pos.x, client_pos.y);
178    debug_printf("%s: window_rect = (%li, %li) - (%li, %li)\n",
179                 __FUNCTION__,
180                 window_rect.left, window_rect.top,
181                 window_rect.right, window_rect.bottom);
182    debug_printf("%s: client_rect = (%li, %li) - (%li, %li)\n",
183                 __FUNCTION__,
184                 fb->client_rect.left, fb->client_rect.top,
185                 fb->client_rect.right, fb->client_rect.bottom);
186 #endif
187 }
188 
189 
190 /**
191  * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
192  * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
193  */
194 LRESULT CALLBACK
stw_call_window_proc(int nCode,WPARAM wParam,LPARAM lParam)195 stw_call_window_proc(int nCode, WPARAM wParam, LPARAM lParam)
196 {
197    struct stw_tls_data *tls_data;
198    PCWPSTRUCT pParams = (PCWPSTRUCT)lParam;
199    struct stw_framebuffer *fb;
200 
201    tls_data = stw_tls_get_data();
202    if (!tls_data)
203       return 0;
204 
205    if (nCode < 0 || !stw_dev)
206        return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
207 
208    /* We check that the stw_dev object is initialized before we try to do
209     * anything with it.  Otherwise, in multi-threaded programs there's a
210     * chance of executing this code before the stw_dev object is fully
211     * initialized.
212     */
213    if (stw_dev && stw_dev->initialized) {
214       if (pParams->message == WM_WINDOWPOSCHANGED) {
215          /* We handle WM_WINDOWPOSCHANGED instead of WM_SIZE because according
216           * to http://blogs.msdn.com/oldnewthing/archive/2008/01/15/7113860.aspx
217           * WM_SIZE is generated from WM_WINDOWPOSCHANGED by DefWindowProc so it
218           * can be masked out by the application.
219           */
220          LPWINDOWPOS lpWindowPos = (LPWINDOWPOS)pParams->lParam;
221          if ((lpWindowPos->flags & SWP_SHOWWINDOW) ||
222              !(lpWindowPos->flags & SWP_NOMOVE) ||
223              !(lpWindowPos->flags & SWP_NOSIZE)) {
224             fb = stw_framebuffer_from_hwnd( pParams->hwnd );
225             if (fb) {
226                /* Size in WINDOWPOS includes the window frame, so get the size
227                 * of the client area via GetClientRect.
228                 */
229                stw_framebuffer_get_size(fb);
230                stw_framebuffer_unlock(fb);
231             }
232          }
233       }
234       else if (pParams->message == WM_DESTROY) {
235          stw_lock_framebuffers(stw_dev);
236          fb = stw_framebuffer_from_hwnd_locked( pParams->hwnd );
237          if (fb)
238             stw_framebuffer_release_locked(fb);
239          stw_unlock_framebuffers(stw_dev);
240       }
241    }
242 
243    return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
244 }
245 
246 
247 /**
248  * Create a new stw_framebuffer object which corresponds to the given
249  * HDC/window.  If successful, we return the new stw_framebuffer object
250  * with its mutex locked.
251  */
252 struct stw_framebuffer *
stw_framebuffer_create(HDC hdc,int iPixelFormat)253 stw_framebuffer_create(HDC hdc, int iPixelFormat)
254 {
255    HWND hWnd;
256    struct stw_framebuffer *fb;
257    const struct stw_pixelformat_info *pfi;
258 
259    /* We only support drawing to a window. */
260    hWnd = WindowFromDC( hdc );
261    if (!hWnd)
262       return NULL;
263 
264    fb = CALLOC_STRUCT( stw_framebuffer );
265    if (fb == NULL)
266       return NULL;
267 
268    fb->hWnd = hWnd;
269    fb->iPixelFormat = iPixelFormat;
270 
271    /*
272     * We often need a displayable pixel format to make GDI happy. Set it
273     * here (always 1, i.e., out first pixel format) where appropriate.
274     */
275    fb->iDisplayablePixelFormat = iPixelFormat <= stw_dev->pixelformat_count
276       ? iPixelFormat : 1;
277 
278    fb->pfi = pfi = stw_pixelformat_get_info( iPixelFormat );
279    fb->stfb = stw_st_create_framebuffer( fb );
280    if (!fb->stfb) {
281       FREE( fb );
282       return NULL;
283    }
284 
285    fb->refcnt = 1;
286 
287    /*
288     * Windows can be sometimes have zero width and or height, but we ensure
289     * a non-zero framebuffer size at all times.
290     */
291 
292    fb->must_resize = TRUE;
293    fb->width  = 1;
294    fb->height = 1;
295    fb->client_rect.left   = 0;
296    fb->client_rect.top    = 0;
297    fb->client_rect.right  = fb->client_rect.left + fb->width;
298    fb->client_rect.bottom = fb->client_rect.top  + fb->height;
299 
300    stw_framebuffer_get_size(fb);
301 
302    InitializeCriticalSection(&fb->mutex);
303 
304    /* This is the only case where we lock the stw_framebuffer::mutex before
305     * stw_dev::fb_mutex, since no other thread can know about this framebuffer
306     * and we must prevent any other thread from destroying it before we return.
307     */
308    stw_framebuffer_lock(fb);
309 
310    stw_lock_framebuffers(stw_dev);
311    fb->next = stw_dev->fb_head;
312    stw_dev->fb_head = fb;
313    stw_unlock_framebuffers(stw_dev);
314 
315    return fb;
316 }
317 
318 
319 /**
320  * Update the framebuffer's size if necessary.
321  */
322 void
stw_framebuffer_update(struct stw_framebuffer * fb)323 stw_framebuffer_update(struct stw_framebuffer *fb)
324 {
325    assert(fb->stfb);
326    assert(fb->height);
327    assert(fb->width);
328 
329    /* XXX: It would be nice to avoid checking the size again -- in theory
330     * stw_call_window_proc would have cought the resize and stored the right
331     * size already, but unfortunately threads created before the DllMain is
332     * called don't get a DLL_THREAD_ATTACH notification, and there is no way
333     * to know of their existing without using the not very portable PSAPI.
334     */
335    stw_framebuffer_get_size(fb);
336 }
337 
338 
339 /**
340  * Try to free all stw_framebuffer objects associated with the device.
341  */
342 void
stw_framebuffer_cleanup(void)343 stw_framebuffer_cleanup(void)
344 {
345    struct stw_framebuffer *fb;
346    struct stw_framebuffer *next;
347 
348    if (!stw_dev)
349       return;
350 
351    stw_lock_framebuffers(stw_dev);
352 
353    fb = stw_dev->fb_head;
354    while (fb) {
355       next = fb->next;
356 
357       stw_framebuffer_lock(fb);
358       stw_framebuffer_release_locked(fb);
359 
360       fb = next;
361    }
362    stw_dev->fb_head = NULL;
363 
364    stw_unlock_framebuffers(stw_dev);
365 }
366 
367 
368 /**
369  * Given an hdc, return the corresponding stw_framebuffer.
370  * The returned stw_framebuffer will have its mutex locked.
371  */
372 static struct stw_framebuffer *
stw_framebuffer_from_hdc_locked(HDC hdc)373 stw_framebuffer_from_hdc_locked(HDC hdc)
374 {
375    HWND hwnd;
376 
377    hwnd = WindowFromDC(hdc);
378    if (!hwnd) {
379       return NULL;
380    }
381 
382    return stw_framebuffer_from_hwnd_locked(hwnd);
383 }
384 
385 
386 /**
387  * Given an HDC, return the corresponding stw_framebuffer.
388  * The returned stw_framebuffer will have its mutex locked.
389  */
390 struct stw_framebuffer *
stw_framebuffer_from_hdc(HDC hdc)391 stw_framebuffer_from_hdc(HDC hdc)
392 {
393    struct stw_framebuffer *fb;
394 
395    if (!stw_dev)
396       return NULL;
397 
398    stw_lock_framebuffers(stw_dev);
399    fb = stw_framebuffer_from_hdc_locked(hdc);
400    stw_unlock_framebuffers(stw_dev);
401 
402    return fb;
403 }
404 
405 
406 /**
407  * Given an HWND, return the corresponding stw_framebuffer.
408  * The returned stw_framebuffer will have its mutex locked.
409  */
410 struct stw_framebuffer *
stw_framebuffer_from_hwnd(HWND hwnd)411 stw_framebuffer_from_hwnd(HWND hwnd)
412 {
413    struct stw_framebuffer *fb;
414 
415    stw_lock_framebuffers(stw_dev);
416    fb = stw_framebuffer_from_hwnd_locked(hwnd);
417    stw_unlock_framebuffers(stw_dev);
418 
419    return fb;
420 }
421 
422 
423 BOOL APIENTRY
DrvSetPixelFormat(HDC hdc,LONG iPixelFormat)424 DrvSetPixelFormat(HDC hdc, LONG iPixelFormat)
425 {
426    uint count;
427    uint index;
428    struct stw_framebuffer *fb;
429 
430    if (!stw_dev)
431       return FALSE;
432 
433    index = (uint) iPixelFormat - 1;
434    count = stw_pixelformat_get_count();
435    if (index >= count)
436       return FALSE;
437 
438    fb = stw_framebuffer_from_hdc_locked(hdc);
439    if (fb) {
440       /*
441        * SetPixelFormat must be called only once.  However ignore
442        * pbuffers, for which the framebuffer object is created first.
443        */
444       boolean bPbuffer = fb->bPbuffer;
445 
446       stw_framebuffer_unlock( fb );
447 
448       return bPbuffer;
449    }
450 
451    fb = stw_framebuffer_create(hdc, iPixelFormat);
452    if (!fb) {
453       return FALSE;
454    }
455 
456    stw_framebuffer_unlock( fb );
457 
458    /* Some applications mistakenly use the undocumented wglSetPixelFormat
459     * function instead of SetPixelFormat, so we call SetPixelFormat here to
460     * avoid opengl32.dll's wglCreateContext to fail */
461    if (GetPixelFormat(hdc) == 0) {
462       BOOL bRet = SetPixelFormat(hdc, iPixelFormat, NULL);
463       if (!bRet) {
464 	  debug_printf("SetPixelFormat failed\n");
465       }
466    }
467 
468    return TRUE;
469 }
470 
471 
472 int
stw_pixelformat_get(HDC hdc)473 stw_pixelformat_get(HDC hdc)
474 {
475    int iPixelFormat = 0;
476    struct stw_framebuffer *fb;
477 
478    fb = stw_framebuffer_from_hdc(hdc);
479    if (fb) {
480       iPixelFormat = fb->iPixelFormat;
481       stw_framebuffer_unlock(fb);
482    }
483 
484    return iPixelFormat;
485 }
486 
487 
488 BOOL APIENTRY
DrvPresentBuffers(HDC hdc,LPPRESENTBUFFERS data)489 DrvPresentBuffers(HDC hdc, LPPRESENTBUFFERS data)
490 {
491    struct stw_framebuffer *fb;
492    struct pipe_screen *screen;
493    struct pipe_resource *res;
494 
495    if (!stw_dev)
496       return FALSE;
497 
498    fb = stw_framebuffer_from_hdc( hdc );
499    if (fb == NULL)
500       return FALSE;
501 
502    screen = stw_dev->screen;
503 
504    res = (struct pipe_resource *)data->pPrivData;
505 
506    if (data->hSurface != fb->hSharedSurface) {
507       if (fb->shared_surface) {
508          stw_dev->stw_winsys->shared_surface_close(screen, fb->shared_surface);
509          fb->shared_surface = NULL;
510       }
511 
512       fb->hSharedSurface = data->hSurface;
513 
514       if (data->hSurface &&
515          stw_dev->stw_winsys->shared_surface_open) {
516          fb->shared_surface =
517             stw_dev->stw_winsys->shared_surface_open(screen,
518                                                      fb->hSharedSurface);
519       }
520    }
521 
522    if (!fb->minimized) {
523       if (fb->shared_surface) {
524          stw_dev->stw_winsys->compose(screen,
525                                       res,
526                                       fb->shared_surface,
527                                       &fb->client_rect,
528                                       data->ullPresentToken);
529       }
530       else {
531          stw_dev->stw_winsys->present( screen, res, hdc );
532       }
533    }
534 
535    stw_framebuffer_update(fb);
536    stw_notify_current_locked(fb);
537 
538    stw_framebuffer_unlock(fb);
539 
540    return TRUE;
541 }
542 
543 
544 /**
545  * Queue a composition.
546  *
547  * The stw_framebuffer object must have its mutex locked.  The mutex will
548  * be unlocked here before returning.
549  */
550 BOOL
stw_framebuffer_present_locked(HDC hdc,struct stw_framebuffer * fb,struct pipe_resource * res)551 stw_framebuffer_present_locked(HDC hdc,
552                                struct stw_framebuffer *fb,
553                                struct pipe_resource *res)
554 {
555    if (stw_dev->callbacks.pfnPresentBuffers &&
556       stw_dev->stw_winsys->compose) {
557       PRESENTBUFFERSCB data;
558 
559       memset(&data, 0, sizeof data);
560       data.nVersion = 2;
561       data.syncType = PRESCB_SYNCTYPE_NONE;
562       data.luidAdapter = stw_dev->AdapterLuid;
563       data.updateRect = fb->client_rect;
564       data.pPrivData = (void *)res;
565 
566       stw_notify_current_locked(fb);
567       stw_framebuffer_unlock(fb);
568 
569       return stw_dev->callbacks.pfnPresentBuffers(hdc, &data);
570    }
571    else {
572       struct pipe_screen *screen = stw_dev->screen;
573 
574       stw_dev->stw_winsys->present( screen, res, hdc );
575 
576       stw_framebuffer_update(fb);
577       stw_notify_current_locked(fb);
578       stw_framebuffer_unlock(fb);
579 
580       return TRUE;
581    }
582 }
583 
584 
585 /**
586  * This is called just before issuing the buffer swap/present.
587  * We query the current time and determine if we should sleep before
588  * issuing the swap/present.
589  * This is a bit of a hack and is certainly not very accurate but it
590  * basically works.
591  * This is for the WGL_ARB_swap_interval extension.
592  */
593 static void
wait_swap_interval(struct stw_framebuffer * fb)594 wait_swap_interval(struct stw_framebuffer *fb)
595 {
596    /* Note: all time variables here are in units of microseconds */
597    int64_t cur_time = os_time_get_nano() / 1000;
598 
599    if (fb->prev_swap_time != 0) {
600       /* Compute time since previous swap */
601       int64_t delta = cur_time - fb->prev_swap_time;
602       int64_t min_swap_period =
603          1.0e6 / stw_dev->refresh_rate * stw_dev->swap_interval;
604 
605       /* If time since last swap is less than wait period, wait.
606        * Note that it's possible for the delta to be negative because of
607        * rollover.  See https://bugs.freedesktop.org/show_bug.cgi?id=102241
608        */
609       if ((delta >= 0) && (delta < min_swap_period)) {
610          float fudge = 1.75f;  /* emperical fudge factor */
611          int64_t wait = (min_swap_period - delta) * fudge;
612          os_time_sleep(wait);
613       }
614    }
615 
616    fb->prev_swap_time = cur_time;
617 }
618 
619 
620 BOOL APIENTRY
DrvSwapBuffers(HDC hdc)621 DrvSwapBuffers(HDC hdc)
622 {
623    struct stw_context *ctx;
624    struct stw_framebuffer *fb;
625 
626    if (!stw_dev)
627       return FALSE;
628 
629    fb = stw_framebuffer_from_hdc( hdc );
630    if (fb == NULL)
631       return FALSE;
632 
633    if (!(fb->pfi->pfd.dwFlags & PFD_DOUBLEBUFFER)) {
634       stw_framebuffer_unlock(fb);
635       return TRUE;
636    }
637 
638    ctx = stw_current_context();
639    if (ctx) {
640       if (ctx->hud) {
641          /* Display the HUD */
642          struct pipe_resource *back =
643             stw_get_framebuffer_resource(fb->stfb, ST_ATTACHMENT_BACK_LEFT);
644          if (back) {
645             hud_run(ctx->hud, NULL, back);
646          }
647       }
648 
649       if (ctx->current_framebuffer == fb) {
650          /* flush current context */
651          ctx->st->flush(ctx->st, ST_FLUSH_END_OF_FRAME, NULL, NULL, NULL);
652       }
653    }
654 
655    if (stw_dev->swap_interval != 0) {
656       wait_swap_interval(fb);
657    }
658 
659    return stw_st_swap_framebuffer_locked(hdc, ctx->st, fb->stfb);
660 }
661 
662 
663 BOOL APIENTRY
DrvSwapLayerBuffers(HDC hdc,UINT fuPlanes)664 DrvSwapLayerBuffers(HDC hdc, UINT fuPlanes)
665 {
666    if (fuPlanes & WGL_SWAP_MAIN_PLANE)
667       return DrvSwapBuffers(hdc);
668 
669    return FALSE;
670 }
671