1 /**************************************************************************
2  *
3  * Copyright 2008 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 #ifndef STW_FRAMEBUFFER_H
29 #define STW_FRAMEBUFFER_H
30 
31 #include <windows.h>
32 
33 #include <GL/gl.h>
34 #include <GL/wglext.h>
35 
36 #include "util/u_debug.h"
37 #include "stw_st.h"
38 
39 
40 struct pipe_resource;
41 struct st_framebuffer_iface;
42 struct stw_pixelformat_info;
43 
44 enum stw_framebuffer_owner
45 {
46    /* WGL window framebuffers have no corresponding destroy, and therefore
47     * a window hook is needed to clean them up.
48     */
49    STW_FRAMEBUFFER_WGL_WINDOW,
50    /* PBuffers behave like WGL window framebuffers, except that the window
51     * lifetime is managed by us. We can explicitly clean up the window.
52     */
53    STW_FRAMEBUFFER_PBUFFER,
54    /* EGL window framebuffers do have a corresponding destroy, so they don't
55     * need to be registered in the global framebuffer list. This means they
56     * will only be cleaned up from a destroy, and don't need to live until the
57     * window goes away.
58     */
59    STW_FRAMEBUFFER_EGL_WINDOW,
60 };
61 
62 /**
63  * Windows framebuffer.
64  */
65 struct stw_framebuffer
66 {
67    /**
68     * This mutex has two purposes:
69     * - protect the access to the mutable data members below
70     * - prevent the framebuffer from being deleted while being accessed.
71     *
72     * Note: if both this mutex and the stw_device::fb_mutex need to be locked,
73     * the stw_device::fb_mutex needs to be locked first.
74     */
75    CRITICAL_SECTION mutex;
76 
77    /*
78     * Immutable members.
79     *
80     * Note that even access to immutable members implies acquiring the mutex
81     * above, to prevent the framebuffer from being destroyed.
82     */
83 
84    HWND hWnd;
85 
86    int iPixelFormat;
87    const struct stw_pixelformat_info *pfi;
88 
89    /* A pixel format that can be used by GDI */
90    int iDisplayablePixelFormat;
91    enum stw_framebuffer_owner owner;
92 
93    struct st_framebuffer_iface *stfb;
94 
95    /*
96     * Mutable members.
97     */
98 
99    unsigned refcnt;
100 
101 
102    /* FIXME: Make this work for multiple contexts bound to the same framebuffer */
103    boolean must_resize;
104 
105    boolean minimized;  /**< Is the window currently minimized? */
106 
107    unsigned width;
108    unsigned height;
109 
110    /** WGL_ARB_render_texture - set at Pbuffer creation time */
111    unsigned textureFormat;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_RGB[A]_ARB */
112    unsigned textureTarget;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_1D/2D/
113                                  CUBE_MAP_ARB */
114    boolean textureMipmap;   /**< TRUE/FALSE */
115    /** WGL_ARB_render_texture - set with wglSetPbufferAttribARB() */
116    unsigned textureLevel;
117    unsigned textureFace;    /**< [0..6] */
118 
119    /**
120     * Client area rectangle, relative to the window upper-left corner.
121     *
122     * @sa GLCBPRESENTBUFFERSDATA::rect.
123     */
124    RECT client_rect;
125 
126    HANDLE hSharedSurface;
127    struct stw_shared_surface *shared_surface;
128 
129    struct stw_winsys_framebuffer *winsys_framebuffer;
130 
131    /* For WGL_EXT_swap_control */
132    int64_t prev_swap_time;
133 
134    /**
135     * This is protected by stw_device::fb_mutex, not the mutex above.
136     *
137     * Deletions must be done by first acquiring stw_device::fb_mutex, and then
138     * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted.
139     * This ensures that nobody else is reading/writing to the.
140     *
141     * It is not necessary to acquire the mutex above to navigate the linked list
142     * given that deletions are done with stw_device::fb_mutex held, so no other
143     * thread can delete.
144     */
145    struct stw_framebuffer *next;
146 };
147 
148 /**
149  * Create a new framebuffer object which will correspond to the given HDC.
150  *
151  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
152  * must be called when done
153  */
154 struct stw_framebuffer *
155 stw_framebuffer_create(HWND hwnd, int iPixelFormat, enum stw_framebuffer_owner owner);
156 
157 
158 /**
159  * Increase fb reference count.  The referenced framebuffer should be locked.
160  *
161  * It's not necessary to hold stw_dev::fb_mutex global lock.
162  */
163 void
164 stw_framebuffer_reference_locked(struct stw_framebuffer *fb);
165 
166 
167 void
168 stw_framebuffer_release_locked(struct stw_framebuffer *fb,
169                                struct st_context_iface *stctx);
170 
171 /**
172  * Search a framebuffer with a matching HWND.
173  *
174  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
175  * must be called when done
176  */
177 struct stw_framebuffer *
178 stw_framebuffer_from_hwnd(HWND hwnd);
179 
180 /**
181  * Search a framebuffer with a matching HDC.
182  *
183  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
184  * must be called when done
185  */
186 struct stw_framebuffer *
187 stw_framebuffer_from_hdc(HDC hdc);
188 
189 BOOL
190 stw_framebuffer_present_locked(HDC hdc,
191                                struct stw_framebuffer *fb,
192                                struct pipe_resource *res);
193 
194 void
195 stw_framebuffer_update(struct stw_framebuffer *fb);
196 
197 BOOL
198 stw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb);
199 
200 
201 static inline void
stw_framebuffer_lock(struct stw_framebuffer * fb)202 stw_framebuffer_lock(struct stw_framebuffer *fb)
203 {
204    assert(fb);
205    EnterCriticalSection(&fb->mutex);
206 }
207 
208 
209 /**
210  * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
211  * after calling this function, as it may have been deleted by another thread
212  * in the meanwhile.
213  */
214 void
215 stw_framebuffer_unlock(struct stw_framebuffer *fb);
216 
217 
218 /**
219  * Cleanup any existing framebuffers when exiting application.
220  */
221 void
222 stw_framebuffer_cleanup(void);
223 
224 
225 static inline struct stw_st_framebuffer *
stw_st_framebuffer(struct st_framebuffer_iface * stfb)226 stw_st_framebuffer(struct st_framebuffer_iface *stfb)
227 {
228    return (struct stw_st_framebuffer *) stfb;
229 }
230 
231 
232 static inline struct stw_framebuffer *
stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)233 stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
234 {
235    return (struct stw_framebuffer *) hPbuffer;
236 }
237 
238 
239 #endif /* STW_FRAMEBUFFER_H */
240