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
;
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
;
99 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
100 enum pipe_format format
;
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
])
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
;
119 case ST_ATTACHMENT_DEPTH_STENCIL
:
120 format
= stwfb
->stvis
.depth_stencil_format
;
121 bind
= PIPE_BIND_DEPTH_STENCIL
;
124 format
= PIPE_FORMAT_NONE
;
128 if (format
!= PIPE_FORMAT_NONE
) {
129 templ
.format
= format
;
133 stw_dev
->screen
->resource_create(stw_dev
->screen
, &templ
);
137 stwfb
->texture_width
= width
;
138 stwfb
->texture_height
= height
;
139 stwfb
->texture_mask
= mask
;
143 stw_st_framebuffer_validate(struct st_context_iface
*stctx
,
144 struct st_framebuffer_iface
*stfb
,
145 const enum st_attachment_type
*statts
,
147 struct pipe_resource
**out
)
149 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
150 unsigned statt_mask
, i
;
153 for (i
= 0; i
< count
; i
++)
154 statt_mask
|= 1 << statts
[i
];
156 stw_framebuffer_lock(stwfb
->fb
);
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
;
164 for (i
= 0; i
< count
; i
++) {
166 pipe_resource_reference(&out
[i
], stwfb
->textures
[statts
[i
]]);
169 stw_framebuffer_unlock(stwfb
->fb
);
175 * Present an attachment of the framebuffer.
178 stw_st_framebuffer_present_locked(HDC hdc
,
179 struct st_framebuffer_iface
*stfb
,
180 enum st_attachment_type statt
)
182 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
183 struct pipe_resource
*resource
;
185 assert(stw_own_mutex(&stwfb
->fb
->mutex
));
187 resource
= stwfb
->textures
[statt
];
189 stw_framebuffer_present_locked(hdc
, stwfb
->fb
, resource
);
192 stw_framebuffer_unlock(stwfb
->fb
);
195 assert(!stw_own_mutex(&stwfb
->fb
->mutex
));
201 stw_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
202 struct st_framebuffer_iface
*stfb
,
203 enum st_attachment_type statt
)
205 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
209 stw_framebuffer_lock(stwfb
->fb
);
211 /* We must not cache HDCs anywhere, as they can be invalidated by the
212 * application, or screen resolution changes. */
214 hDC
= GetDC(stwfb
->fb
->hWnd
);
216 ret
= stw_st_framebuffer_present_locked(hDC
, &stwfb
->base
, statt
);
218 ReleaseDC(stwfb
->fb
->hWnd
, hDC
);
224 * Create a framebuffer interface.
226 struct st_framebuffer_iface
*
227 stw_st_create_framebuffer(struct stw_framebuffer
*fb
)
229 struct stw_st_framebuffer
*stwfb
;
231 stwfb
= CALLOC_STRUCT(stw_st_framebuffer
);
236 stwfb
->stvis
= fb
->pfi
->stvis
;
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
;
247 * Destroy a framebuffer interface.
250 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface
*stfb
)
252 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
255 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
256 pipe_resource_reference(&stwfb
->textures
[i
], NULL
);
262 * Swap the buffers of the given framebuffer.
265 stw_st_swap_framebuffer_locked(HDC hdc
, struct st_framebuffer_iface
*stfb
)
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
;
272 /* swap the textures */
273 ptex
= stwfb
->textures
[front
];
274 stwfb
->textures
[front
] = stwfb
->textures
[back
];
275 stwfb
->textures
[back
] = ptex
;
277 /* convert to mask */
281 /* swap the bits in mask */
282 mask
= stwfb
->texture_mask
& ~(front
| back
);
283 if (stwfb
->texture_mask
& front
)
285 if (stwfb
->texture_mask
& back
)
287 stwfb
->texture_mask
= mask
;
289 front
= ST_ATTACHMENT_FRONT_LEFT
;
290 return stw_st_framebuffer_present_locked(hdc
, &stwfb
->base
, front
);
295 * Return the pipe_resource that correspond to given buffer.
297 struct pipe_resource
*
298 stw_get_framebuffer_resource(struct st_framebuffer_iface
*stfb
,
299 enum st_attachment_type att
)
301 struct stw_st_framebuffer
*stwfb
= stw_st_framebuffer(stfb
);
302 return stwfb
->textures
[att
];
307 * Create an st_api of the state tracker.
310 stw_st_create_api(void)
312 return st_gl_api_create();