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 pipe_frontend_drawable;
42 struct stw_pixelformat_info;
43 struct pipe_frontend_screen;
44
45 enum stw_framebuffer_owner
46 {
47 /* WGL window framebuffers have no corresponding destroy, and therefore
48 * a window hook is needed to clean them up.
49 */
50 STW_FRAMEBUFFER_WGL_WINDOW,
51 /* PBuffers behave like WGL window framebuffers, except that the window
52 * lifetime is managed by us. We can explicitly clean up the window.
53 */
54 STW_FRAMEBUFFER_PBUFFER,
55 /* EGL window framebuffers do have a corresponding destroy, so they don't
56 * need to be registered in the global framebuffer list. This means they
57 * will only be cleaned up from a destroy, and don't need to live until the
58 * window goes away.
59 */
60 STW_FRAMEBUFFER_EGL_WINDOW,
61 };
62
63 /**
64 * Windows framebuffer.
65 */
66 struct stw_framebuffer
67 {
68 /**
69 * This mutex has two purposes:
70 * - protect the access to the mutable data members below
71 * - prevent the framebuffer from being deleted while being accessed.
72 *
73 * Note: if both this mutex and the stw_device::fb_mutex need to be locked,
74 * the stw_device::fb_mutex needs to be locked first.
75 */
76 CRITICAL_SECTION mutex;
77
78 /*
79 * Immutable members.
80 *
81 * Note that even access to immutable members implies acquiring the mutex
82 * above, to prevent the framebuffer from being destroyed.
83 */
84
85 HWND hWnd;
86
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 pipe_frontend_drawable *drawable;
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 bool must_resize;
104
105 bool 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 bool 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 int swap_interval;
133 int64_t prev_swap_time;
134
135 #ifdef _GAMING_XBOX
136 /* For the WndProc hook chain */
137 WNDPROC prev_wndproc;
138 #endif
139
140 /**
141 * This is protected by stw_device::fb_mutex, not the mutex above.
142 *
143 * Deletions must be done by first acquiring stw_device::fb_mutex, and then
144 * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted.
145 * This ensures that nobody else is reading/writing to the.
146 *
147 * It is not necessary to acquire the mutex above to navigate the linked list
148 * given that deletions are done with stw_device::fb_mutex held, so no other
149 * thread can delete.
150 */
151 struct stw_framebuffer *next;
152 };
153
154 /**
155 * Create a new framebuffer object which will correspond to the given HDC.
156 *
157 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
158 * must be called when done
159 */
160 struct stw_framebuffer *
161 stw_framebuffer_create(HWND hwnd, const struct stw_pixelformat_info *pfi, enum stw_framebuffer_owner owner,
162 struct pipe_frontend_screen *fscreen);
163
164 struct stw_framebuffer *
165 stw_pbuffer_create(const struct stw_pixelformat_info *pfi, int iWidth, int iHeight, struct pipe_frontend_screen *fscreen);
166
167
168 /**
169 * Increase fb reference count. The referenced framebuffer should be locked.
170 *
171 * It's not necessary to hold stw_dev::fb_mutex global lock.
172 */
173 void
174 stw_framebuffer_reference_locked(struct stw_framebuffer *fb);
175
176
177 void
178 stw_framebuffer_release_locked(struct stw_framebuffer *fb,
179 struct st_context *st);
180
181 /**
182 * Search a framebuffer with a matching HWND.
183 *
184 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
185 * must be called when done
186 */
187 struct stw_framebuffer *
188 stw_framebuffer_from_hwnd(HWND hwnd);
189
190 /**
191 * Search a framebuffer with a matching HDC.
192 *
193 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
194 * must be called when done
195 */
196 struct stw_framebuffer *
197 stw_framebuffer_from_hdc(HDC hdc);
198
199 BOOL
200 stw_framebuffer_present_locked(HDC hdc,
201 struct stw_framebuffer *fb,
202 struct pipe_resource *res);
203
204 void
205 stw_framebuffer_update(struct stw_framebuffer *fb);
206
207 BOOL
208 stw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb);
209
210
211 static inline void
stw_framebuffer_lock(struct stw_framebuffer * fb)212 stw_framebuffer_lock(struct stw_framebuffer *fb)
213 {
214 assert(fb);
215 EnterCriticalSection(&fb->mutex);
216 }
217
218
219 /**
220 * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
221 * after calling this function, as it may have been deleted by another thread
222 * in the meanwhile.
223 */
224 void
225 stw_framebuffer_unlock(struct stw_framebuffer *fb);
226
227
228 /**
229 * Cleanup any existing framebuffers when exiting application.
230 */
231 void
232 stw_framebuffer_cleanup(void);
233
234
235 static inline struct stw_st_framebuffer *
stw_st_framebuffer(struct pipe_frontend_drawable * drawable)236 stw_st_framebuffer(struct pipe_frontend_drawable *drawable)
237 {
238 return (struct stw_st_framebuffer *) drawable;
239 }
240
241
242 static inline struct stw_framebuffer *
stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)243 stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
244 {
245 return (struct stw_framebuffer *) hPbuffer;
246 }
247
248
249 #endif /* STW_FRAMEBUFFER_H */
250