• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2010 LunarG Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Chia-I Wu <olv@lunarg.com>
26  */
27 
28 #include "util/u_memory.h"
29 #include "util/u_inlines.h"
30 #include "util/u_atomic.h"
31 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
32 
33 #include "stw_st.h"
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
37 
38 struct stw_st_framebuffer {
39    struct st_framebuffer_iface base;
40 
41    struct stw_framebuffer *fb;
42    struct st_visual stvis;
43 
44    struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
45    unsigned texture_width, texture_height;
46    unsigned texture_mask;
47 };
48 
49 
50 
51 /**
52  * Is the given mutex held by the calling thread?
53  */
54 bool
stw_own_mutex(const CRITICAL_SECTION * cs)55 stw_own_mutex(const CRITICAL_SECTION *cs)
56 {
57    // We can't compare OwningThread with our thread handle/id (see
58    // http://stackoverflow.com/a/12675635 ) but we can compare with the
59    // OwningThread member of a critical section we know we own.
60    CRITICAL_SECTION dummy;
61    InitializeCriticalSection(&dummy);
62    EnterCriticalSection(&dummy);
63    if (0)
64       _debug_printf("%p %p\n", cs->OwningThread, dummy.OwningThread);
65    bool ret = cs->OwningThread == dummy.OwningThread;
66    LeaveCriticalSection(&dummy);
67    DeleteCriticalSection(&dummy);
68    return ret;
69 }
70 
71 
72 /**
73  * Remove outdated textures and create the requested ones.
74  */
75 static void
stw_st_framebuffer_validate_locked(struct st_framebuffer_iface * stfb,unsigned width,unsigned height,unsigned mask)76 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface *stfb,
77                                    unsigned width, unsigned height,
78                                    unsigned mask)
79 {
80    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
81    struct pipe_resource templ;
82    unsigned i;
83 
84    /* remove outdated textures */
85    if (stwfb->texture_width != width || stwfb->texture_height != height) {
86       for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
87          pipe_resource_reference(&stwfb->textures[i], NULL);
88    }
89 
90    memset(&templ, 0, sizeof(templ));
91    templ.target = PIPE_TEXTURE_2D;
92    templ.width0 = width;
93    templ.height0 = height;
94    templ.depth0 = 1;
95    templ.array_size = 1;
96    templ.last_level = 0;
97    templ.nr_samples = stwfb->stvis.samples;
98 
99    for (i = 0; i < ST_ATTACHMENT_COUNT; i++) {
100       enum pipe_format format;
101       unsigned bind;
102 
103       /* the texture already exists or not requested */
104       if (stwfb->textures[i] || !(mask & (1 << i))) {
105          /* remember the texture */
106          if (stwfb->textures[i])
107             mask |= (1 << i);
108          continue;
109       }
110 
111       switch (i) {
112       case ST_ATTACHMENT_FRONT_LEFT:
113       case ST_ATTACHMENT_BACK_LEFT:
114          format = stwfb->stvis.color_format;
115          bind = PIPE_BIND_DISPLAY_TARGET |
116                 PIPE_BIND_SAMPLER_VIEW |
117                 PIPE_BIND_RENDER_TARGET;
118          break;
119       case ST_ATTACHMENT_DEPTH_STENCIL:
120          format = stwfb->stvis.depth_stencil_format;
121          bind = PIPE_BIND_DEPTH_STENCIL;
122          break;
123       default:
124          format = PIPE_FORMAT_NONE;
125          break;
126       }
127 
128       if (format != PIPE_FORMAT_NONE) {
129          templ.format = format;
130          templ.bind = bind;
131 
132          stwfb->textures[i] =
133             stw_dev->screen->resource_create(stw_dev->screen, &templ);
134       }
135    }
136 
137    stwfb->texture_width = width;
138    stwfb->texture_height = height;
139    stwfb->texture_mask = mask;
140 }
141 
142 static boolean
stw_st_framebuffer_validate(struct st_context_iface * stctx,struct st_framebuffer_iface * stfb,const enum st_attachment_type * statts,unsigned count,struct pipe_resource ** out)143 stw_st_framebuffer_validate(struct st_context_iface *stctx,
144                             struct st_framebuffer_iface *stfb,
145                             const enum st_attachment_type *statts,
146                             unsigned count,
147                             struct pipe_resource **out)
148 {
149    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
150    unsigned statt_mask, i;
151 
152    statt_mask = 0x0;
153    for (i = 0; i < count; i++)
154       statt_mask |= 1 << statts[i];
155 
156    stw_framebuffer_lock(stwfb->fb);
157 
158    if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) {
159       stw_st_framebuffer_validate_locked(&stwfb->base,
160             stwfb->fb->width, stwfb->fb->height, statt_mask);
161       stwfb->fb->must_resize = FALSE;
162    }
163 
164    for (i = 0; i < count; i++) {
165       out[i] = NULL;
166       pipe_resource_reference(&out[i], stwfb->textures[statts[i]]);
167    }
168 
169    stw_framebuffer_unlock(stwfb->fb);
170 
171    return TRUE;
172 }
173 
174 /**
175  * Present an attachment of the framebuffer.
176  */
177 static boolean
stw_st_framebuffer_present_locked(HDC hdc,struct st_framebuffer_iface * stfb,enum st_attachment_type statt)178 stw_st_framebuffer_present_locked(HDC hdc,
179                                   struct st_framebuffer_iface *stfb,
180                                   enum st_attachment_type statt)
181 {
182    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
183    struct pipe_resource *resource;
184 
185    assert(stw_own_mutex(&stwfb->fb->mutex));
186 
187    resource = stwfb->textures[statt];
188    if (resource) {
189       stw_framebuffer_present_locked(hdc, stwfb->fb, resource);
190    }
191    else {
192       stw_framebuffer_unlock(stwfb->fb);
193    }
194 
195    assert(!stw_own_mutex(&stwfb->fb->mutex));
196 
197    return TRUE;
198 }
199 
200 static boolean
stw_st_framebuffer_flush_front(struct st_context_iface * stctx,struct st_framebuffer_iface * stfb,enum st_attachment_type statt)201 stw_st_framebuffer_flush_front(struct st_context_iface *stctx,
202                                struct st_framebuffer_iface *stfb,
203                                enum st_attachment_type statt)
204 {
205    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
206    boolean ret;
207    HDC hDC;
208 
209    stw_framebuffer_lock(stwfb->fb);
210 
211    /* We must not cache HDCs anywhere, as they can be invalidated by the
212     * application, or screen resolution changes. */
213 
214    hDC = GetDC(stwfb->fb->hWnd);
215 
216    ret = stw_st_framebuffer_present_locked(hDC, &stwfb->base, statt);
217 
218    ReleaseDC(stwfb->fb->hWnd, hDC);
219 
220    return ret;
221 }
222 
223 /**
224  * Create a framebuffer interface.
225  */
226 struct st_framebuffer_iface *
stw_st_create_framebuffer(struct stw_framebuffer * fb)227 stw_st_create_framebuffer(struct stw_framebuffer *fb)
228 {
229    struct stw_st_framebuffer *stwfb;
230 
231    stwfb = CALLOC_STRUCT(stw_st_framebuffer);
232    if (!stwfb)
233       return NULL;
234 
235    stwfb->fb = fb;
236    stwfb->stvis = fb->pfi->stvis;
237 
238    stwfb->base.visual = &stwfb->stvis;
239    p_atomic_set(&stwfb->base.stamp, 1);
240    stwfb->base.flush_front = stw_st_framebuffer_flush_front;
241    stwfb->base.validate = stw_st_framebuffer_validate;
242 
243    return &stwfb->base;
244 }
245 
246 /**
247  * Destroy a framebuffer interface.
248  */
249 void
stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface * stfb)250 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb)
251 {
252    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
253    int i;
254 
255    for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
256       pipe_resource_reference(&stwfb->textures[i], NULL);
257 
258    FREE(stwfb);
259 }
260 
261 /**
262  * Swap the buffers of the given framebuffer.
263  */
264 boolean
stw_st_swap_framebuffer_locked(HDC hdc,struct st_framebuffer_iface * stfb)265 stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb)
266 {
267    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
268    unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT;
269    struct pipe_resource *ptex;
270    unsigned mask;
271 
272    /* swap the textures */
273    ptex = stwfb->textures[front];
274    stwfb->textures[front] = stwfb->textures[back];
275    stwfb->textures[back] = ptex;
276 
277    /* convert to mask */
278    front = 1 << front;
279    back = 1 << back;
280 
281    /* swap the bits in mask */
282    mask = stwfb->texture_mask & ~(front | back);
283    if (stwfb->texture_mask & front)
284       mask |= back;
285    if (stwfb->texture_mask & back)
286       mask |= front;
287    stwfb->texture_mask = mask;
288 
289    front = ST_ATTACHMENT_FRONT_LEFT;
290    return stw_st_framebuffer_present_locked(hdc, &stwfb->base, front);
291 }
292 
293 
294 /**
295  * Return the pipe_resource that correspond to given buffer.
296  */
297 struct pipe_resource *
stw_get_framebuffer_resource(struct st_framebuffer_iface * stfb,enum st_attachment_type att)298 stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb,
299                              enum st_attachment_type att)
300 {
301    struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
302    return stwfb->textures[att];
303 }
304 
305 
306 /**
307  * Create an st_api of the state tracker.
308  */
309 struct st_api *
stw_st_create_api(void)310 stw_st_create_api(void)
311 {
312    return st_gl_api_create();
313 }
314