2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
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 */
34 #include "stw_device.h"
35 #include "stw_framebuffer.h"
36 #include "stw_pixelformat.h"
38 struct stw_st_framebuffer
{
39 struct st_framebuffer_iface base
;
41 struct stw_framebuffer
*fb
;
42 struct st_visual stvis
;
44 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
45 unsigned texture_width
, texture_height
;
46 unsigned texture_mask
;
49 static uint32_t stwfb_ID
= 0;
52 * Is the given mutex held by the calling thread?
55 stw_own_mutex(const CRITICAL_SECTION
*cs
)
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
);
64 _debug_printf("%p %p\n", cs
->OwningThread
, dummy
.OwningThread
);
65 bool ret
= cs
->OwningThread
== dummy
.OwningThread
;
66 LeaveCriticalSection(&dummy
);
67 DeleteCriticalSection(&dummy
);
73 * Remove outdated textures and create the requested ones.
76 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface
*stfb
,
77 unsigned width
, unsigned height
,
80 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
81 struct pipe_resource templ
;
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
);
90 memset(&templ
, 0, sizeof(templ
));
91 templ
.target
= PIPE_TEXTURE_2D
;
93 templ
.height0
= height
;
97 templ
.nr_samples
= stwfb
->stvis
.samples
;
98 templ
.nr_storage_samples
= stwfb
->stvis
.samples
;;
100 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
101 enum pipe_format format
;
104 /* the texture already exists or not requested */
105 if (stwfb
->textures
[i
] || !(mask
& (1 << i
))) {
106 /* remember the texture */
107 if (stwfb
->textures
[i
])
113 case ST_ATTACHMENT_FRONT_LEFT
:
114 case ST_ATTACHMENT_BACK_LEFT
:
115 format
= stwfb
->stvis
.color_format
;
116 bind
= PIPE_BIND_DISPLAY_TARGET
|
117 PIPE_BIND_SAMPLER_VIEW
|
118 PIPE_BIND_RENDER_TARGET
;
120 case ST_ATTACHMENT_DEPTH_STENCIL
:
121 format
= stwfb
->stvis
.depth_stencil_format
;
122 bind
= PIPE_BIND_DEPTH_STENCIL
;
125 format
= PIPE_FORMAT_NONE
;
129 if (format
!= PIPE_FORMAT_NONE
) {
130 templ
.format
= format
;
134 stw_dev
->screen
->resource_create(stw_dev
->screen
, &templ
);
138 stwfb
->texture_width
= width
;
139 stwfb
->texture_height
= height
;
140 stwfb
->texture_mask
= mask
;
144 stw_st_framebuffer_validate(struct st_context_iface
*stctx
,
145 struct st_framebuffer_iface
*stfb
,
146 const enum st_attachment_type
*statts
,
148 struct pipe_resource
**out
)
150 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
151 unsigned statt_mask
, i
;
154 for (i
= 0; i
< count
; i
++)
155 statt_mask
|= 1 << statts
[i
];
157 stw_framebuffer_lock(stwfb
->fb
);
159 if (stwfb
->fb
->must_resize
|| (statt_mask
& ~stwfb
->texture_mask
)) {
160 stw_st_framebuffer_validate_locked(&stwfb
->base
,
161 stwfb
->fb
->width
, stwfb
->fb
->height
, statt_mask
);
162 stwfb
->fb
->must_resize
= FALSE
;
165 for (i
= 0; i
< count
; i
++)
166 pipe_resource_reference(&out
[i
], stwfb
->textures
[statts
[i
]]);
168 stw_framebuffer_unlock(stwfb
->fb
);
174 * Present an attachment of the framebuffer.
177 stw_st_framebuffer_present_locked(HDC hdc
,
178 struct st_framebuffer_iface
*stfb
,
179 enum st_attachment_type statt
)
181 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
182 struct pipe_resource
*resource
;
184 assert(stw_own_mutex(&stwfb
->fb
->mutex
));
186 resource
= stwfb
->textures
[statt
];
188 stw_framebuffer_present_locked(hdc
, stwfb
->fb
, resource
);
191 stw_framebuffer_unlock(stwfb
->fb
);
194 assert(!stw_own_mutex(&stwfb
->fb
->mutex
));
200 stw_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
201 struct st_framebuffer_iface
*stfb
,
202 enum st_attachment_type statt
)
204 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
208 stw_framebuffer_lock(stwfb
->fb
);
210 /* We must not cache HDCs anywhere, as they can be invalidated by the
211 * application, or screen resolution changes. */
213 hDC
= GetDC(stwfb
->fb
->hWnd
);
215 ret
= stw_st_framebuffer_present_locked(hDC
, &stwfb
->base
, statt
);
217 ReleaseDC(stwfb
->fb
->hWnd
, hDC
);
223 * Create a framebuffer interface.
225 struct st_framebuffer_iface
*
226 stw_st_create_framebuffer(struct stw_framebuffer
*fb
)
228 struct stw_st_framebuffer
*stwfb
;
230 stwfb
= CALLOC_STRUCT(stw_st_framebuffer
);
235 stwfb
->stvis
= fb
->pfi
->stvis
;
236 stwfb
->base
.ID
= p_atomic_inc_return(&stwfb_ID
);
237 stwfb
->base
.state_manager
= stw_dev
->smapi
;
239 stwfb
->base
.visual
= &stwfb
->stvis
;
240 p_atomic_set(&stwfb
->base
.stamp
, 1);
241 stwfb
->base
.flush_front
= stw_st_framebuffer_flush_front
;
242 stwfb
->base
.validate
= stw_st_framebuffer_validate
;
248 * Destroy a framebuffer interface.
251 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface
*stfb
)
253 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
256 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
257 pipe_resource_reference(&stwfb
->textures
[i
], NULL
);
259 /* Notify the st manager that the framebuffer interface is no
262 stw_dev
->stapi
->destroy_drawable(stw_dev
->stapi
, &stwfb
->base
);
268 * Swap the buffers of the given framebuffer.
271 stw_st_swap_framebuffer_locked(HDC hdc
, struct st_framebuffer_iface
*stfb
)
273 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
274 unsigned front
= ST_ATTACHMENT_FRONT_LEFT
, back
= ST_ATTACHMENT_BACK_LEFT
;
275 struct pipe_resource
*ptex
;
278 /* swap the textures */
279 ptex
= stwfb
->textures
[front
];
280 stwfb
->textures
[front
] = stwfb
->textures
[back
];
281 stwfb
->textures
[back
] = ptex
;
283 /* convert to mask */
287 /* swap the bits in mask */
288 mask
= stwfb
->texture_mask
& ~(front
| back
);
289 if (stwfb
->texture_mask
& front
)
291 if (stwfb
->texture_mask
& back
)
293 stwfb
->texture_mask
= mask
;
295 front
= ST_ATTACHMENT_FRONT_LEFT
;
296 return stw_st_framebuffer_present_locked(hdc
, &stwfb
->base
, front
);
301 * Return the pipe_resource that correspond to given buffer.
303 struct pipe_resource
*
304 stw_get_framebuffer_resource(struct st_framebuffer_iface
*stfb
,
305 enum st_attachment_type att
)
307 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
308 return stwfb
->textures
[att
];
313 * Create an st_api of the state tracker.
316 stw_st_create_api(void)
318 return st_gl_api_create();