gallium: rename st_framebuffer_iface -> pipe_frontend_drawable, etc.
[platform/upstream/mesa.git] / src / gallium / frontends / 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 #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    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    int swap_interval;
133    int64_t prev_swap_time;
134
135    /** 
136     * This is protected by stw_device::fb_mutex, not the mutex above.
137     * 
138     * Deletions must be done by first acquiring stw_device::fb_mutex, and then
139     * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted. 
140     * This ensures that nobody else is reading/writing to the.
141     * 
142     * It is not necessary to acquire the mutex above to navigate the linked list
143     * given that deletions are done with stw_device::fb_mutex held, so no other
144     * thread can delete.
145     */
146    struct stw_framebuffer *next;
147 };
148
149 /**
150  * Create a new framebuffer object which will correspond to the given HDC.
151  * 
152  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
153  * must be called when done 
154  */
155 struct stw_framebuffer *
156 stw_framebuffer_create(HWND hwnd, const struct stw_pixelformat_info *pfi, enum stw_framebuffer_owner owner,
157                        struct pipe_frontend_screen *fscreen);
158
159 struct stw_framebuffer *
160 stw_pbuffer_create(const struct stw_pixelformat_info *pfi, int iWidth, int iHeight, struct pipe_frontend_screen *fscreen);
161
162
163 /**
164  * Increase fb reference count.  The referenced framebuffer should be locked.
165  *
166  * It's not necessary to hold stw_dev::fb_mutex global lock.
167  */
168 void
169 stw_framebuffer_reference_locked(struct stw_framebuffer *fb);
170
171
172 void
173 stw_framebuffer_release_locked(struct stw_framebuffer *fb,
174                                struct st_context *st);
175
176 /**
177  * Search a framebuffer with a matching HWND.
178  * 
179  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
180  * must be called when done 
181  */
182 struct stw_framebuffer *
183 stw_framebuffer_from_hwnd(HWND hwnd);
184
185 /**
186  * Search a framebuffer with a matching HDC.
187  * 
188  * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
189  * must be called when done 
190  */
191 struct stw_framebuffer *
192 stw_framebuffer_from_hdc(HDC hdc);
193
194 BOOL
195 stw_framebuffer_present_locked(HDC hdc,
196                                struct stw_framebuffer *fb,
197                                struct pipe_resource *res);
198
199 void
200 stw_framebuffer_update(struct stw_framebuffer *fb);
201
202 BOOL
203 stw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb);
204
205
206 static inline void
207 stw_framebuffer_lock(struct stw_framebuffer *fb)
208 {
209    assert(fb);
210    EnterCriticalSection(&fb->mutex);
211 }
212
213
214 /**
215  * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
216  * after calling this function, as it may have been deleted by another thread
217  * in the meanwhile.
218  */
219 void
220 stw_framebuffer_unlock(struct stw_framebuffer *fb);
221
222
223 /**
224  * Cleanup any existing framebuffers when exiting application.
225  */
226 void
227 stw_framebuffer_cleanup(void);
228
229
230 static inline struct stw_st_framebuffer *
231 stw_st_framebuffer(struct pipe_frontend_drawable *drawable)
232 {
233    return (struct stw_st_framebuffer *) drawable;
234 }
235
236
237 static inline struct stw_framebuffer *
238 stw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
239 {
240    return (struct stw_framebuffer *) hPbuffer;
241 }
242
243
244 #endif /* STW_FRAMEBUFFER_H */