1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#ifndef STW_FRAMEBUFFER_H
29bf215546Sopenharmony_ci#define STW_FRAMEBUFFER_H
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <windows.h>
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include <GL/gl.h>
34bf215546Sopenharmony_ci#include <GL/wglext.h>
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include "util/u_debug.h"
37bf215546Sopenharmony_ci#include "stw_st.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistruct pipe_resource;
41bf215546Sopenharmony_cistruct st_framebuffer_iface;
42bf215546Sopenharmony_cistruct stw_pixelformat_info;
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_cienum stw_framebuffer_owner
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   /* WGL window framebuffers have no corresponding destroy, and therefore
47bf215546Sopenharmony_ci    * a window hook is needed to clean them up.
48bf215546Sopenharmony_ci    */
49bf215546Sopenharmony_ci   STW_FRAMEBUFFER_WGL_WINDOW,
50bf215546Sopenharmony_ci   /* PBuffers behave like WGL window framebuffers, except that the window
51bf215546Sopenharmony_ci    * lifetime is managed by us. We can explicitly clean up the window.
52bf215546Sopenharmony_ci    */
53bf215546Sopenharmony_ci   STW_FRAMEBUFFER_PBUFFER,
54bf215546Sopenharmony_ci   /* EGL window framebuffers do have a corresponding destroy, so they don't
55bf215546Sopenharmony_ci    * need to be registered in the global framebuffer list. This means they
56bf215546Sopenharmony_ci    * will only be cleaned up from a destroy, and don't need to live until the
57bf215546Sopenharmony_ci    * window goes away.
58bf215546Sopenharmony_ci    */
59bf215546Sopenharmony_ci   STW_FRAMEBUFFER_EGL_WINDOW,
60bf215546Sopenharmony_ci};
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci/**
63bf215546Sopenharmony_ci * Windows framebuffer.
64bf215546Sopenharmony_ci */
65bf215546Sopenharmony_cistruct stw_framebuffer
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   /**
68bf215546Sopenharmony_ci    * This mutex has two purposes:
69bf215546Sopenharmony_ci    * - protect the access to the mutable data members below
70bf215546Sopenharmony_ci    * - prevent the framebuffer from being deleted while being accessed.
71bf215546Sopenharmony_ci    *
72bf215546Sopenharmony_ci    * Note: if both this mutex and the stw_device::fb_mutex need to be locked,
73bf215546Sopenharmony_ci    * the stw_device::fb_mutex needs to be locked first.
74bf215546Sopenharmony_ci    */
75bf215546Sopenharmony_ci   CRITICAL_SECTION mutex;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   /*
78bf215546Sopenharmony_ci    * Immutable members.
79bf215546Sopenharmony_ci    *
80bf215546Sopenharmony_ci    * Note that even access to immutable members implies acquiring the mutex
81bf215546Sopenharmony_ci    * above, to prevent the framebuffer from being destroyed.
82bf215546Sopenharmony_ci    */
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   HWND hWnd;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   int iPixelFormat;
87bf215546Sopenharmony_ci   const struct stw_pixelformat_info *pfi;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   /* A pixel format that can be used by GDI */
90bf215546Sopenharmony_ci   int iDisplayablePixelFormat;
91bf215546Sopenharmony_ci   enum stw_framebuffer_owner owner;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   struct st_framebuffer_iface *stfb;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   /*
96bf215546Sopenharmony_ci    * Mutable members.
97bf215546Sopenharmony_ci    */
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   unsigned refcnt;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   /* FIXME: Make this work for multiple contexts bound to the same framebuffer */
103bf215546Sopenharmony_ci   boolean must_resize;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   boolean minimized;  /**< Is the window currently minimized? */
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   unsigned width;
108bf215546Sopenharmony_ci   unsigned height;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci   /** WGL_ARB_render_texture - set at Pbuffer creation time */
111bf215546Sopenharmony_ci   unsigned textureFormat;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_RGB[A]_ARB */
112bf215546Sopenharmony_ci   unsigned textureTarget;  /**< WGL_NO_TEXTURE or WGL_TEXTURE_1D/2D/
113bf215546Sopenharmony_ci                                 CUBE_MAP_ARB */
114bf215546Sopenharmony_ci   boolean textureMipmap;   /**< TRUE/FALSE */
115bf215546Sopenharmony_ci   /** WGL_ARB_render_texture - set with wglSetPbufferAttribARB() */
116bf215546Sopenharmony_ci   unsigned textureLevel;
117bf215546Sopenharmony_ci   unsigned textureFace;    /**< [0..6] */
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   /**
120bf215546Sopenharmony_ci    * Client area rectangle, relative to the window upper-left corner.
121bf215546Sopenharmony_ci    *
122bf215546Sopenharmony_ci    * @sa GLCBPRESENTBUFFERSDATA::rect.
123bf215546Sopenharmony_ci    */
124bf215546Sopenharmony_ci   RECT client_rect;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   HANDLE hSharedSurface;
127bf215546Sopenharmony_ci   struct stw_shared_surface *shared_surface;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   struct stw_winsys_framebuffer *winsys_framebuffer;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   /* For WGL_EXT_swap_control */
132bf215546Sopenharmony_ci   int64_t prev_swap_time;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   /**
135bf215546Sopenharmony_ci    * This is protected by stw_device::fb_mutex, not the mutex above.
136bf215546Sopenharmony_ci    *
137bf215546Sopenharmony_ci    * Deletions must be done by first acquiring stw_device::fb_mutex, and then
138bf215546Sopenharmony_ci    * acquiring the stw_framebuffer::mutex of the framebuffer to be deleted.
139bf215546Sopenharmony_ci    * This ensures that nobody else is reading/writing to the.
140bf215546Sopenharmony_ci    *
141bf215546Sopenharmony_ci    * It is not necessary to acquire the mutex above to navigate the linked list
142bf215546Sopenharmony_ci    * given that deletions are done with stw_device::fb_mutex held, so no other
143bf215546Sopenharmony_ci    * thread can delete.
144bf215546Sopenharmony_ci    */
145bf215546Sopenharmony_ci   struct stw_framebuffer *next;
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci/**
149bf215546Sopenharmony_ci * Create a new framebuffer object which will correspond to the given HDC.
150bf215546Sopenharmony_ci *
151bf215546Sopenharmony_ci * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
152bf215546Sopenharmony_ci * must be called when done
153bf215546Sopenharmony_ci */
154bf215546Sopenharmony_cistruct stw_framebuffer *
155bf215546Sopenharmony_cistw_framebuffer_create(HWND hwnd, int iPixelFormat, enum stw_framebuffer_owner owner);
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci/**
159bf215546Sopenharmony_ci * Increase fb reference count.  The referenced framebuffer should be locked.
160bf215546Sopenharmony_ci *
161bf215546Sopenharmony_ci * It's not necessary to hold stw_dev::fb_mutex global lock.
162bf215546Sopenharmony_ci */
163bf215546Sopenharmony_civoid
164bf215546Sopenharmony_cistw_framebuffer_reference_locked(struct stw_framebuffer *fb);
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_civoid
168bf215546Sopenharmony_cistw_framebuffer_release_locked(struct stw_framebuffer *fb,
169bf215546Sopenharmony_ci                               struct st_context_iface *stctx);
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci/**
172bf215546Sopenharmony_ci * Search a framebuffer with a matching HWND.
173bf215546Sopenharmony_ci *
174bf215546Sopenharmony_ci * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
175bf215546Sopenharmony_ci * must be called when done
176bf215546Sopenharmony_ci */
177bf215546Sopenharmony_cistruct stw_framebuffer *
178bf215546Sopenharmony_cistw_framebuffer_from_hwnd(HWND hwnd);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci/**
181bf215546Sopenharmony_ci * Search a framebuffer with a matching HDC.
182bf215546Sopenharmony_ci *
183bf215546Sopenharmony_ci * This function will acquire stw_framebuffer::mutex. stw_framebuffer_unlock
184bf215546Sopenharmony_ci * must be called when done
185bf215546Sopenharmony_ci */
186bf215546Sopenharmony_cistruct stw_framebuffer *
187bf215546Sopenharmony_cistw_framebuffer_from_hdc(HDC hdc);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ciBOOL
190bf215546Sopenharmony_cistw_framebuffer_present_locked(HDC hdc,
191bf215546Sopenharmony_ci                               struct stw_framebuffer *fb,
192bf215546Sopenharmony_ci                               struct pipe_resource *res);
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_civoid
195bf215546Sopenharmony_cistw_framebuffer_update(struct stw_framebuffer *fb);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ciBOOL
198bf215546Sopenharmony_cistw_framebuffer_swap_locked(HDC hdc, struct stw_framebuffer *fb);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_cistatic inline void
202bf215546Sopenharmony_cistw_framebuffer_lock(struct stw_framebuffer *fb)
203bf215546Sopenharmony_ci{
204bf215546Sopenharmony_ci   assert(fb);
205bf215546Sopenharmony_ci   EnterCriticalSection(&fb->mutex);
206bf215546Sopenharmony_ci}
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci/**
210bf215546Sopenharmony_ci * Release stw_framebuffer::mutex lock. This framebuffer must not be accessed
211bf215546Sopenharmony_ci * after calling this function, as it may have been deleted by another thread
212bf215546Sopenharmony_ci * in the meanwhile.
213bf215546Sopenharmony_ci */
214bf215546Sopenharmony_civoid
215bf215546Sopenharmony_cistw_framebuffer_unlock(struct stw_framebuffer *fb);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci/**
219bf215546Sopenharmony_ci * Cleanup any existing framebuffers when exiting application.
220bf215546Sopenharmony_ci */
221bf215546Sopenharmony_civoid
222bf215546Sopenharmony_cistw_framebuffer_cleanup(void);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_cistatic inline struct stw_st_framebuffer *
226bf215546Sopenharmony_cistw_st_framebuffer(struct st_framebuffer_iface *stfb)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci   return (struct stw_st_framebuffer *) stfb;
229bf215546Sopenharmony_ci}
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_cistatic inline struct stw_framebuffer *
233bf215546Sopenharmony_cistw_framebuffer_from_HPBUFFERARB(HPBUFFERARB hPbuffer)
234bf215546Sopenharmony_ci{
235bf215546Sopenharmony_ci   return (struct stw_framebuffer *) hPbuffer;
236bf215546Sopenharmony_ci}
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci#endif /* STW_FRAMEBUFFER_H */
240