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 OR
17 * 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 OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
31 #include "util/u_inlines.h"
32 #include "util/u_atomic.h"
34 struct xmesa_st_framebuffer
{
37 struct pipe_screen
*screen
;
39 struct st_visual stvis
;
40 enum pipe_texture_target target
;
42 unsigned texture_width
, texture_height
, texture_mask
;
43 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
45 struct pipe_resource
*display_resource
;
49 static inline struct xmesa_st_framebuffer
*
50 xmesa_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
52 return (struct xmesa_st_framebuffer
*) stfbi
->st_manager_private
;
57 * Display (present) an attachment to the xlib_drawable of the framebuffer.
60 xmesa_st_framebuffer_display(struct st_framebuffer_iface
*stfbi
,
61 enum st_attachment_type statt
)
63 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
64 struct pipe_resource
*ptex
= xstfb
->textures
[statt
];
65 struct pipe_resource
*pres
;
70 pres
= xstfb
->display_resource
;
71 /* (re)allocate the surface for the texture to be displayed */
72 if (!pres
|| pres
!= ptex
) {
73 pipe_resource_reference(&xstfb
->display_resource
, ptex
);
74 pres
= xstfb
->display_resource
;
77 xstfb
->screen
->flush_frontbuffer(xstfb
->screen
, pres
, 0, 0, &xstfb
->buffer
->ws
, NULL
);
83 * Copy the contents between the attachments.
86 xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface
*stfbi
,
87 enum st_attachment_type src_statt
,
88 enum st_attachment_type dst_statt
,
89 unsigned x
, unsigned y
,
90 unsigned width
, unsigned height
)
92 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
93 struct pipe_resource
*src_ptex
= xstfb
->textures
[src_statt
];
94 struct pipe_resource
*dst_ptex
= xstfb
->textures
[dst_statt
];
95 struct pipe_box src_box
;
96 struct pipe_context
*pipe
;
98 if (!src_ptex
|| !dst_ptex
)
101 pipe
= xmesa_get_context(stfbi
);
103 u_box_2d(x
, y
, width
, height
, &src_box
);
105 if (src_ptex
&& dst_ptex
)
106 pipe
->resource_copy_region(pipe
, dst_ptex
, 0, x
, y
, 0,
107 src_ptex
, 0, &src_box
);
112 * Remove outdated textures and create the requested ones.
113 * This is a helper used during framebuffer validation.
116 xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface
*stfbi
,
117 unsigned width
, unsigned height
,
120 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
121 struct pipe_resource templ
;
122 enum st_attachment_type i
;
124 /* remove outdated textures */
125 if (xstfb
->texture_width
!= width
|| xstfb
->texture_height
!= height
) {
126 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
127 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
130 memset(&templ
, 0, sizeof(templ
));
131 templ
.target
= xstfb
->target
;
132 templ
.width0
= width
;
133 templ
.height0
= height
;
135 templ
.array_size
= 1;
136 templ
.last_level
= 0;
138 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
139 enum pipe_format format
;
142 /* the texture already exists or not requested */
143 if (xstfb
->textures
[i
] || !(mask
& (1 << i
))) {
144 /* remember the texture */
145 if (xstfb
->textures
[i
])
151 case ST_ATTACHMENT_FRONT_LEFT
:
152 case ST_ATTACHMENT_BACK_LEFT
:
153 case ST_ATTACHMENT_FRONT_RIGHT
:
154 case ST_ATTACHMENT_BACK_RIGHT
:
155 format
= xstfb
->stvis
.color_format
;
156 bind
= PIPE_BIND_DISPLAY_TARGET
|
157 PIPE_BIND_RENDER_TARGET
;
159 case ST_ATTACHMENT_DEPTH_STENCIL
:
160 format
= xstfb
->stvis
.depth_stencil_format
;
161 bind
= PIPE_BIND_DEPTH_STENCIL
;
164 format
= PIPE_FORMAT_NONE
;
168 if (format
!= PIPE_FORMAT_NONE
) {
169 templ
.format
= format
;
173 xstfb
->screen
->resource_create(xstfb
->screen
, &templ
);
174 if (!xstfb
->textures
[i
])
179 xstfb
->texture_width
= width
;
180 xstfb
->texture_height
= height
;
181 xstfb
->texture_mask
= mask
;
188 * Check that a framebuffer's attachments match the window's size.
190 * Called via st_framebuffer_iface::validate()
192 * \param statts array of framebuffer attachments
193 * \param count number of framebuffer attachments in statts[]
194 * \param out returns resources for each of the attachments
197 xmesa_st_framebuffer_validate(struct st_context_iface
*stctx
,
198 struct st_framebuffer_iface
*stfbi
,
199 const enum st_attachment_type
*statts
,
201 struct pipe_resource
**out
)
203 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
204 unsigned statt_mask
, new_mask
, i
;
208 /* build mask of ST_ATTACHMENT bits */
210 for (i
= 0; i
< count
; i
++)
211 statt_mask
|= 1 << statts
[i
];
213 /* record newly allocated textures */
214 new_mask
= statt_mask
& ~xstfb
->texture_mask
;
216 /* If xmesa_strict_invalidate is not set, we will not yet have
217 * called XGetGeometry(). Do so here:
219 if (!xmesa_strict_invalidate
)
220 xmesa_check_buffer_size(xstfb
->buffer
);
222 resized
= (xstfb
->buffer
->width
!= xstfb
->texture_width
||
223 xstfb
->buffer
->height
!= xstfb
->texture_height
);
225 /* revalidate textures */
226 if (resized
|| new_mask
) {
227 ret
= xmesa_st_framebuffer_validate_textures(stfbi
,
228 xstfb
->buffer
->width
, xstfb
->buffer
->height
, statt_mask
);
233 enum st_attachment_type back
, front
;
235 back
= ST_ATTACHMENT_BACK_LEFT
;
236 front
= ST_ATTACHMENT_FRONT_LEFT
;
237 /* copy the contents if front is newly allocated and back is not */
238 if ((statt_mask
& (1 << back
)) &&
239 (new_mask
& (1 << front
)) &&
240 !(new_mask
& (1 << back
))) {
241 xmesa_st_framebuffer_copy_textures(stfbi
, back
, front
,
242 0, 0, xstfb
->texture_width
, xstfb
->texture_height
);
247 for (i
= 0; i
< count
; i
++) {
249 pipe_resource_reference(&out
[i
], xstfb
->textures
[statts
[i
]]);
257 * Called via st_framebuffer_iface::flush_front()
260 xmesa_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
261 struct st_framebuffer_iface
*stfbi
,
262 enum st_attachment_type statt
)
264 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
267 ret
= xmesa_st_framebuffer_display(stfbi
, statt
);
269 if (ret
&& xmesa_strict_invalidate
)
270 xmesa_check_buffer_size(xstfb
->buffer
);
276 struct st_framebuffer_iface
*
277 xmesa_create_st_framebuffer(XMesaDisplay xmdpy
, XMesaBuffer b
)
279 struct st_framebuffer_iface
*stfbi
;
280 struct xmesa_st_framebuffer
*xstfb
;
282 assert(xmdpy
->display
== b
->xm_visual
->display
);
284 stfbi
= CALLOC_STRUCT(st_framebuffer_iface
);
285 xstfb
= CALLOC_STRUCT(xmesa_st_framebuffer
);
286 if (!stfbi
|| !xstfb
) {
292 xstfb
->display
= xmdpy
;
294 xstfb
->screen
= xmdpy
->screen
;
295 xstfb
->stvis
= b
->xm_visual
->stvis
;
296 if (xstfb
->screen
->get_param(xstfb
->screen
, PIPE_CAP_NPOT_TEXTURES
))
297 xstfb
->target
= PIPE_TEXTURE_2D
;
299 xstfb
->target
= PIPE_TEXTURE_RECT
;
301 stfbi
->visual
= &xstfb
->stvis
;
302 stfbi
->flush_front
= xmesa_st_framebuffer_flush_front
;
303 stfbi
->validate
= xmesa_st_framebuffer_validate
;
304 p_atomic_set(&stfbi
->stamp
, 1);
305 stfbi
->st_manager_private
= (void *) xstfb
;
312 xmesa_destroy_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
314 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
317 pipe_resource_reference(&xstfb
->display_resource
, NULL
);
319 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
320 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
328 * Return the pipe_surface which corresponds to the given
329 * framebuffer attachment.
331 struct pipe_resource
*
332 xmesa_get_framebuffer_resource(struct st_framebuffer_iface
*stfbi
,
333 enum st_attachment_type att
)
335 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
336 return xstfb
->textures
[att
];
341 xmesa_swap_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
343 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
346 ret
= xmesa_st_framebuffer_display(stfbi
, ST_ATTACHMENT_BACK_LEFT
);
348 struct pipe_resource
**front
, **back
, *tmp
;
350 front
= &xstfb
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
351 back
= &xstfb
->textures
[ST_ATTACHMENT_BACK_LEFT
];
352 /* swap textures only if the front texture has been allocated */
358 /* the current context should validate the buffer after swapping */
359 if (!xmesa_strict_invalidate
)
360 xmesa_notify_invalid_buffer(xstfb
->buffer
);
363 if (xmesa_strict_invalidate
)
364 xmesa_check_buffer_size(xstfb
->buffer
);
370 xmesa_copy_st_framebuffer(struct st_framebuffer_iface
*stfbi
,
371 enum st_attachment_type src
,
372 enum st_attachment_type dst
,
373 int x
, int y
, int w
, int h
)
375 xmesa_st_framebuffer_copy_textures(stfbi
, src
, dst
, x
, y
, w
, h
);
376 if (dst
== ST_ATTACHMENT_FRONT_LEFT
)
377 xmesa_st_framebuffer_display(stfbi
, dst
);
381 struct pipe_resource
*
382 xmesa_get_attachment(struct st_framebuffer_iface
*stfbi
,
383 enum st_attachment_type st_attachment
)
385 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
386 struct pipe_resource
*res
;
388 res
= xstfb
->textures
[st_attachment
];
394 xmesa_get_context(struct st_framebuffer_iface
*stfbi
)
396 struct pipe_context
*pipe
;
397 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
399 pipe
= xstfb
->display
->pipe
;
401 pipe
= xstfb
->screen
->context_create(xstfb
->screen
, NULL
, 0);
404 xstfb
->display
->pipe
= pipe
;