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