17ec681f3Smrg/**************************************************************************
27ec681f3Smrg *
37ec681f3Smrg * Copyright 2008 VMware, Inc.
47ec681f3Smrg * All Rights Reserved.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the
87ec681f3Smrg * "Software"), to deal in the Software without restriction, including
97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to
117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
127ec681f3Smrg * the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the
157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions
167ec681f3Smrg * of the Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
217ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
257ec681f3Smrg *
267ec681f3Smrg **************************************************************************/
277ec681f3Smrg
287ec681f3Smrg#ifndef STW_FRAMEBUFFER_H
297ec681f3Smrg#define STW_FRAMEBUFFER_H
307ec681f3Smrg
317ec681f3Smrg#include <windows.h>
327ec681f3Smrg
337ec681f3Smrg#include <GL/gl.h>
347ec681f3Smrg#include <GL/wglext.h>
357ec681f3Smrg
367ec681f3Smrg#include "util/u_debug.h"
377ec681f3Smrg#include "stw_st.h"
387ec681f3Smrg
397ec681f3Smrg
407ec681f3Smrgstruct pipe_resource;
417ec681f3Smrgstruct st_framebuffer_iface;
427ec681f3Smrgstruct stw_pixelformat_info;
437ec681f3Smrg
447ec681f3Smrgenum stw_framebuffer_owner
457ec681f3Smrg{
467ec681f3Smrg   /* WGL window framebuffers have no corresponding destroy, and therefore
477ec681f3Smrg    * a window hook is needed to clean them up.
487ec681f3Smrg    */
497ec681f3Smrg   STW_FRAMEBUFFER_WGL_WINDOW,
507ec681f3Smrg   /* PBuffers behave like WGL window framebuffers, except that the window
517ec681f3Smrg    * lifetime is managed by us. We can explicitly clean up the window.
527ec681f3Smrg    */
537ec681f3Smrg   STW_FRAMEBUFFER_PBUFFER,
547ec681f3Smrg   /* EGL window framebuffers do have a corresponding destroy, so they don't
557ec681f3Smrg    * need to be registered in the global framebuffer list. This means they
567ec681f3Smrg    * will only be cleaned up from a destroy, and don't need to live until the
577ec681f3Smrg    * window goes away.
587ec681f3Smrg    */
597ec681f3Smrg   STW_FRAMEBUFFER_EGL_WINDOW,
607ec681f3Smrg};
617ec681f3Smrg
627ec681f3Smrg/**
637ec681f3Smrg * Windows framebuffer.
647ec681f3Smrg */
657ec681f3Smrgstruct stw_framebuffer
667ec681f3Smrg{
677ec681f3Smrg   /**
687ec681f3Smrg    * This mutex has two purposes:
697ec681f3Smrg    * - protect the access to the mutable data members below
707ec681f3Smrg    * - prevent the framebuffer from being deleted while being accessed.
717ec681f3Smrg    *
727ec681f3Smrg    * Note: if both this mutex and the stw_device::fb_mutex need to be locked,
737ec681f3Smrg    * the stw_device::fb_mutex needs to be locked first.
747ec681f3Smrg    */
757ec681f3Smrg   CRITICAL_SECTION mutex;
767ec681f3Smrg
777ec681f3Smrg   /*
787ec681f3Smrg    * Immutable members.
797ec681f3Smrg    *
807ec681f3Smrg    * Note that even access to immutable members implies acquiring the mutex
817ec681f3Smrg    * above, to prevent the framebuffer from being destroyed.
827ec681f3Smrg    */
837ec681f3Smrg
847ec681f3Smrg   HWND hWnd;
857ec681f3Smrg
867ec681f3Smrg   int iPixelFormat;
877ec681f3Smrg   const struct stw_pixelformat_info *pfi;
887ec681f3Smrg
897ec681f3Smrg   /* A pixel format that can be used by GDI */
907ec681f3Smrg   int iDisplayablePixelFormat;
917ec681f3Smrg   enum stw_framebuffer_owner owner;
927ec681f3Smrg
937ec681f3Smrg   struct st_framebuffer_iface *stfb;
947ec681f3Smrg
957ec681f3Smrg   /*
967ec681f3Smrg    * Mutable members.
977ec681f3Smrg    */
987ec681f3Smrg
997ec681f3Smrg   unsigned refcnt;
1007ec681f3Smrg
1017ec681f3Smrg
1027ec681f3Smrg   /* FIXME: Make this work for multiple contexts bound to the same framebuffer */
1037ec681f3Smrg   boolean must_resize;
1047ec681f3Smrg
1057ec681f3Smrg   boolean minimized;  /**< Is the window currently minimized? */
1067ec681f3Smrg
1077ec681f3Smrg   unsigned width;
1087ec681f3Smrg   unsigned height;
1097ec681f3Smrg
1107ec681f3Smrg   /** WGL_ARB_render_texture - set at Pbuffer creation time */
1117ec681f3Smrg   unsigned textureFormat;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_RGB[A]_ARB */
1127ec681f3Smrg   unsigned textureTarget;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_1D/2D/
1137ec681f3Smrg                                 CUBE_MAP_ARB */
1147ec681f3Smrg   boolean textureMipmap;   /**< TRUE/FALSE */
1157ec681f3Smrg   /** WGL_ARB_render_texture - set with wglSetPbufferAttribARB() */
1167ec681f3Smrg   unsigned textureLevel;
1177ec681f3Smrg   unsigned textureFace;    /**< [0..6] */
1187ec681f3Smrg
1197ec681f3Smrg   /**
1207ec681f3Smrg    * Client area rectangle, relative to the window upper-left corner.
1217ec681f3Smrg    *
1227ec681f3Smrg    * @sa GLCBPRESENTBUFFERSDATA::rect.
1237ec681f3Smrg    */
1247ec681f3Smrg   RECT client_rect;
1257ec681f3Smrg
1267ec681f3Smrg   HANDLE hSharedSurface;
1277ec681f3Smrg   struct stw_shared_surface *shared_surface;
1287ec681f3Smrg
1297ec681f3Smrg   struct stw_winsys_framebuffer *winsys_framebuffer;
1307ec681f3Smrg
1317ec681f3Smrg   /* For WGL_EXT_swap_control */
1327ec681f3Smrg   int64_t prev_swap_time;
1337ec681f3Smrg
1347ec681f3Smrg   /**
1357ec681f3Smrg    * This is protected by stw_device::fb_mutex, not the mutex above.
1367ec681f3Smrg    *
1377ec681f3Smrg    * Deletions must be done by first acquiring stw_device::fb_mutex, and then
1387ec681f3Smrg    * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted.
1397ec681f3Smrg    * This ensures that nobody else is reading/writing to the.
1407ec681f3Smrg    *
1417ec681f3Smrg    * It is not necessary to acquire the mutex above to navigate the linked list
1427ec681f3Smrg    * given that deletions are done with stw_device::fb_mutex held, so no other
1437ec681f3Smrg    * thread can delete.
1447ec681f3Smrg    */
1457ec681f3Smrg   struct stw_framebuffer *next;
1467ec681f3Smrg};
1477ec681f3Smrg
1487ec681f3Smrg/**
1497ec681f3Smrg * Create a new framebuffer object which will correspond to the given HDC.
1507ec681f3Smrg *
1517ec681f3Smrg * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
1527ec681f3Smrg * must be called when done
1537ec681f3Smrg */
1547ec681f3Smrgstruct stw_framebuffer *
1557ec681f3Smrgstw_framebuffer_create(HWND hwnd, int iPixelFormat, enum stw_framebuffer_owner owner);
1567ec681f3Smrg
1577ec681f3Smrg
1587ec681f3Smrg/**
1597ec681f3Smrg * Increase fb reference count.  The referenced framebuffer should be locked.
1607ec681f3Smrg *
1617ec681f3Smrg * It's not necessary to hold stw_dev::fb_mutex global lock.
1627ec681f3Smrg */
1637ec681f3Smrgvoid
1647ec681f3Smrgstw_framebuffer_reference_locked(struct stw_framebuffer *fb);
1657ec681f3Smrg
1667ec681f3Smrg
1677ec681f3Smrgvoid
1687ec681f3Smrgstw_framebuffer_release_locked(struct stw_framebuffer *fb,
1697ec681f3Smrg                               struct st_context_iface *stctx);
1707ec681f3Smrg
1717ec681f3Smrg/**
1727ec681f3Smrg * Search a framebuffer with a matching HWND.
1737ec681f3Smrg *
1747ec681f3Smrg * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
1757ec681f3Smrg * must be called when done
1767ec681f3Smrg */
1777ec681f3Smrgstruct stw_framebuffer *
1787ec681f3Smrgstw_framebuffer_from_hwnd(HWND hwnd);
1797ec681f3Smrg
1807ec681f3Smrg/**
1817ec681f3Smrg * Search a framebuffer with a matching HDC.
1827ec681f3Smrg *
1837ec681f3Smrg * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
1847ec681f3Smrg * must be called when done
1857ec681f3Smrg */
1867ec681f3Smrgstruct stw_framebuffer *
1877ec681f3Smrgstw_framebuffer_from_hdc(HDC hdc);
1887ec681f3Smrg
1897ec681f3SmrgBOOL
1907ec681f3Smrgstw_framebuffer_present_locked(HDC hdc,
1917ec681f3Smrg                               struct stw_framebuffer *fb,
1927ec681f3Smrg                               struct pipe_resource *res);
1937ec681f3Smrg
1947ec681f3Smrgvoid
1957ec681f3Smrgstw_framebuffer_update(struct stw_framebuffer *fb);
1967ec681f3Smrg
1977ec681f3SmrgBOOL
1987ec681f3Smrgstw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb);
1997ec681f3Smrg
2007ec681f3Smrg
2017ec681f3Smrgstatic inline void
2027ec681f3Smrgstw_framebuffer_lock(struct stw_framebuffer *fb)
2037ec681f3Smrg{
2047ec681f3Smrg   assert(fb);
2057ec681f3Smrg   EnterCriticalSection(&fb->mutex);
2067ec681f3Smrg}
2077ec681f3Smrg
2087ec681f3Smrg
2097ec681f3Smrg/**
2107ec681f3Smrg * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
2117ec681f3Smrg * after calling this function, as it may have been deleted by another thread
2127ec681f3Smrg * in the meanwhile.
2137ec681f3Smrg */
2147ec681f3Smrgvoid
2157ec681f3Smrgstw_framebuffer_unlock(struct stw_framebuffer *fb);
2167ec681f3Smrg
2177ec681f3Smrg
2187ec681f3Smrg/**
2197ec681f3Smrg * Cleanup any existing framebuffers when exiting application.
2207ec681f3Smrg */
2217ec681f3Smrgvoid
2227ec681f3Smrgstw_framebuffer_cleanup(void);
2237ec681f3Smrg
2247ec681f3Smrg
2257ec681f3Smrgstatic inline struct stw_st_framebuffer *
2267ec681f3Smrgstw_st_framebuffer(struct st_framebuffer_iface *stfb)
2277ec681f3Smrg{
2287ec681f3Smrg   return (struct stw_st_framebuffer *) stfb;
2297ec681f3Smrg}
2307ec681f3Smrg
2317ec681f3Smrg
2327ec681f3Smrgstatic inline struct stw_framebuffer *
2337ec681f3Smrgstw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
2347ec681f3Smrg{
2357ec681f3Smrg   return (struct stw_framebuffer *) hPbuffer;
2367ec681f3Smrg}
2377ec681f3Smrg
2387ec681f3Smrg
2397ec681f3Smrg#endif /* STW_FRAMEBUFFER_H */
240