st/wgl: add support for WGL_ARB_render_texture
[mesa.git] / src / gallium / state_trackers / wgl / stw_framebuffer.h
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
38
39 struct pipe_resource;
40 struct st_framebuffer_iface;
41 struct stw_pixelformat_info;
42
43 /**
44 * Windows framebuffer.
45 */
46 struct stw_framebuffer
47 {
48 /**
49 * This mutex has two purposes:
50 * - protect the access to the mutable data members below
51 * - prevent the framebuffer from being deleted while being accessed.
52 *
53 * Note: if both this mutex and the stw_device::fb_mutex need to be locked,
54 * the stw_device::fb_mutex needs to be locked first.
55 */
56 CRITICAL_SECTION mutex;
57
58 /*
59 * Immutable members.
60 *
61 * Note that even access to immutable members implies acquiring the mutex
62 * above, to prevent the framebuffer from being destroyed.
63 */
64
65 HWND hWnd;
66
67 int iPixelFormat;
68 const struct stw_pixelformat_info *pfi;
69
70 /* A pixel format that can be used by GDI */
71 int iDisplayablePixelFormat;
72 boolean bPbuffer;
73
74 struct st_framebuffer_iface *stfb;
75
76 /*
77 * Mutable members.
78 */
79
80 unsigned refcnt;
81
82
83 /* FIXME: Make this work for multiple contexts bound to the same framebuffer */
84 boolean must_resize;
85
86 boolean minimized; /**< Is the window currently minimized? */
87
88 unsigned width;
89 unsigned height;
90
91 /** WGL_ARB_render_texture - set at Pbuffer creation time */
92 unsigned textureFormat; /**< WGL_NO_TEXTURE or WGL_TEXTURE_RGB[A]_ARB */
93 unsigned textureTarget; /**< WGL_NO_TEXTURE or WGL_TEXTURE_1D/2D/
94 CUBE_MAP_ARB */
95 boolean textureMipmap; /**< TRUE/FALSE */
96 /** WGL_ARB_render_texture - set with wglSetPbufferAttribARB() */
97 unsigned textureLevel;
98 unsigned textureFace; /**< [0..6] */
99
100 /**
101 * Client area rectangle, relative to the window upper-left corner.
102 *
103 * @sa GLCBPRESENTBUFFERSDATA::rect.
104 */
105 RECT client_rect;
106
107 HANDLE hSharedSurface;
108 struct stw_shared_surface *shared_surface;
109
110 /**
111 * This is protected by stw_device::fb_mutex, not the mutex above.
112 *
113 * Deletions must be done by first acquiring stw_device::fb_mutex, and then
114 * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted.
115 * This ensures that nobody else is reading/writing to the.
116 *
117 * It is not necessary to acquire the mutex above to navigate the linked list
118 * given that deletions are done with stw_device::fb_mutex held, so no other
119 * thread can delete.
120 */
121 struct stw_framebuffer *next;
122 };
123
124
125 /**
126 * Create a new framebuffer object which will correspond to the given HDC.
127 *
128 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
129 * must be called when done
130 */
131 struct stw_framebuffer *
132 stw_framebuffer_create(HDC hdc, int iPixelFormat);
133
134 void
135 stw_framebuffer_reference(struct stw_framebuffer **ptr,
136 struct stw_framebuffer *fb);
137
138 /**
139 * Search a framebuffer with a matching HWND.
140 *
141 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
142 * must be called when done
143 */
144 struct stw_framebuffer *
145 stw_framebuffer_from_hwnd(HWND hwnd);
146
147 /**
148 * Search a framebuffer with a matching HDC.
149 *
150 * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
151 * must be called when done
152 */
153 struct stw_framebuffer *
154 stw_framebuffer_from_hdc(HDC hdc);
155
156 BOOL
157 stw_framebuffer_present_locked(HDC hdc,
158 struct stw_framebuffer *fb,
159 struct pipe_resource *res);
160
161 void
162 stw_framebuffer_update(struct stw_framebuffer *fb);
163
164
165 static inline void
166 stw_framebuffer_lock(struct stw_framebuffer *fb)
167 {
168 assert(fb);
169 EnterCriticalSection(&fb->mutex);
170 }
171
172
173 /**
174 * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
175 * after calling this function, as it may have been deleted by another thread
176 * in the meanwhile.
177 */
178 static inline void
179 stw_framebuffer_unlock(struct stw_framebuffer *fb)
180 {
181 assert(fb);
182 LeaveCriticalSection(&fb->mutex);
183 }
184
185
186 /**
187 * Cleanup any existing framebuffers when exiting application.
188 */
189 void
190 stw_framebuffer_cleanup(void);
191
192
193 static inline struct stw_st_framebuffer *
194 stw_st_framebuffer(struct st_framebuffer_iface *stfb)
195 {
196 return (struct stw_st_framebuffer *) stfb;
197 }
198
199
200 static inline struct stw_framebuffer *
201 stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
202 {
203 return (struct stw_framebuffer *) hPbuffer;
204 }
205
206
207 #endif /* STW_FRAMEBUFFER_H */