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