2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
35 struct xmesa_st_framebuffer
{
38 struct pipe_screen
*screen
;
40 struct st_visual stvis
;
42 unsigned texture_width
, texture_height
, texture_mask
;
43 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
45 struct pipe_surface
*display_surface
;
48 static INLINE
struct xmesa_st_framebuffer
*
49 xmesa_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
51 return (struct xmesa_st_framebuffer
*) stfbi
->st_manager_private
;
55 * Display an attachment to the xlib_drawable of the framebuffer.
58 xmesa_st_framebuffer_display(struct st_framebuffer_iface
*stfbi
,
59 enum st_attachment_type statt
)
61 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
62 struct pipe_resource
*ptex
= xstfb
->textures
[statt
];
63 struct pipe_surface
*psurf
;
68 psurf
= xstfb
->display_surface
;
69 /* (re)allocate the surface for the texture to be displayed */
70 if (!psurf
|| psurf
->texture
!= ptex
) {
71 pipe_surface_reference(&xstfb
->display_surface
, NULL
);
73 psurf
= xstfb
->screen
->get_tex_surface(xstfb
->screen
,
74 ptex
, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET
);
78 xstfb
->display_surface
= psurf
;
81 xstfb
->screen
->flush_frontbuffer(xstfb
->screen
, psurf
, &xstfb
->buffer
->ws
);
87 * Copy the contents between the attachments.
90 xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface
*stfbi
,
91 enum st_attachment_type src_statt
,
92 enum st_attachment_type dst_statt
,
93 unsigned x
, unsigned y
,
94 unsigned width
, unsigned height
)
96 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
97 struct pipe_resource
*src_ptex
= xstfb
->textures
[src_statt
];
98 struct pipe_resource
*dst_ptex
= xstfb
->textures
[dst_statt
];
99 struct pipe_surface
*src
, *dst
;
100 struct pipe_context
*pipe
;
102 if (!src_ptex
|| !dst_ptex
)
105 pipe
= xstfb
->display
->pipe
;
107 pipe
= xstfb
->screen
->context_create(xstfb
->screen
, NULL
);
110 xstfb
->display
->pipe
= pipe
;
113 src
= xstfb
->screen
->get_tex_surface(xstfb
->screen
,
114 src_ptex
, 0, 0, 0, PIPE_BIND_BLIT_SOURCE
);
115 dst
= xstfb
->screen
->get_tex_surface(xstfb
->screen
,
116 dst_ptex
, 0, 0, 0, PIPE_BIND_BLIT_DESTINATION
);
119 pipe
->surface_copy(pipe
, dst
, x
, y
, src
, x
, y
, width
, height
);
121 pipe_surface_reference(&src
, NULL
);
122 pipe_surface_reference(&dst
, NULL
);
126 * Remove outdated textures and create the requested ones.
129 xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface
*stfbi
,
130 unsigned width
, unsigned height
,
133 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
134 struct pipe_resource templ
;
135 enum st_attachment_type i
;
137 /* remove outdated textures */
138 if (xstfb
->texture_width
!= width
|| xstfb
->texture_height
!= height
) {
139 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
140 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
143 memset(&templ
, 0, sizeof(templ
));
144 templ
.target
= PIPE_TEXTURE_2D
;
145 templ
.width0
= width
;
146 templ
.height0
= height
;
148 templ
.last_level
= 0;
150 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
151 enum pipe_format format
;
154 /* the texture already exists or not requested */
155 if (xstfb
->textures
[i
] || !(mask
& (1 << i
))) {
156 /* remember the texture */
157 if (xstfb
->textures
[i
])
163 case ST_ATTACHMENT_FRONT_LEFT
:
164 case ST_ATTACHMENT_BACK_LEFT
:
165 case ST_ATTACHMENT_FRONT_RIGHT
:
166 case ST_ATTACHMENT_BACK_RIGHT
:
167 format
= xstfb
->stvis
.color_format
;
168 bind
= PIPE_BIND_DISPLAY_TARGET
|
169 PIPE_BIND_RENDER_TARGET
;
171 case ST_ATTACHMENT_DEPTH_STENCIL
:
172 format
= xstfb
->stvis
.depth_stencil_format
;
173 bind
= PIPE_BIND_DEPTH_STENCIL
;
176 format
= PIPE_FORMAT_NONE
;
180 if (format
!= PIPE_FORMAT_NONE
) {
181 templ
.format
= format
;
185 xstfb
->screen
->resource_create(xstfb
->screen
, &templ
);
186 if (!xstfb
->textures
[i
])
191 xstfb
->texture_width
= width
;
192 xstfb
->texture_height
= height
;
193 xstfb
->texture_mask
= mask
;
199 xmesa_st_framebuffer_validate(struct st_framebuffer_iface
*stfbi
,
200 const enum st_attachment_type
*statts
,
202 struct pipe_resource
**out
)
204 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
205 unsigned statt_mask
, new_mask
, i
;
210 for (i
= 0; i
< count
; i
++)
211 statt_mask
|= 1 << statts
[i
];
212 /* record newly allocated textures */
213 new_mask
= statt_mask
& ~xstfb
->texture_mask
;
215 resized
= (xstfb
->buffer
->width
!= xstfb
->texture_width
||
216 xstfb
->buffer
->height
!= xstfb
->texture_height
);
218 /* revalidate textures */
219 if (resized
|| new_mask
) {
220 ret
= xmesa_st_framebuffer_validate_textures(stfbi
,
221 xstfb
->buffer
->width
, xstfb
->buffer
->height
, statt_mask
);
226 enum st_attachment_type back
, front
;
228 back
= ST_ATTACHMENT_BACK_LEFT
;
229 front
= ST_ATTACHMENT_FRONT_LEFT
;
230 /* copy the contents if front is newly allocated and back is not */
231 if ((statt_mask
& (1 << back
)) &&
232 (new_mask
& (1 << front
)) &&
233 !(new_mask
& (1 << back
))) {
234 xmesa_st_framebuffer_copy_textures(stfbi
, back
, front
,
235 0, 0, xstfb
->texture_width
, xstfb
->texture_height
);
240 for (i
= 0; i
< count
; i
++) {
242 pipe_resource_reference(&out
[i
], xstfb
->textures
[statts
[i
]]);
249 xmesa_st_framebuffer_flush_front(struct st_framebuffer_iface
*stfbi
,
250 enum st_attachment_type statt
)
252 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
255 ret
= xmesa_st_framebuffer_display(stfbi
, statt
);
257 xmesa_check_buffer_size(xstfb
->buffer
);
262 struct st_framebuffer_iface
*
263 xmesa_create_st_framebuffer(XMesaDisplay xmdpy
, XMesaBuffer b
)
265 struct st_framebuffer_iface
*stfbi
;
266 struct xmesa_st_framebuffer
*xstfb
;
268 assert(xmdpy
->display
== b
->xm_visual
->display
);
270 stfbi
= CALLOC_STRUCT(st_framebuffer_iface
);
271 xstfb
= CALLOC_STRUCT(xmesa_st_framebuffer
);
272 if (!stfbi
|| !xstfb
) {
280 xstfb
->display
= xmdpy
;
282 xstfb
->screen
= xmdpy
->screen
;
283 xstfb
->stvis
= b
->xm_visual
->stvis
;
285 stfbi
->visual
= &xstfb
->stvis
;
286 stfbi
->flush_front
= xmesa_st_framebuffer_flush_front
;
287 stfbi
->validate
= xmesa_st_framebuffer_validate
;
288 stfbi
->st_manager_private
= (void *) xstfb
;
294 xmesa_destroy_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
296 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
299 pipe_surface_reference(&xstfb
->display_surface
, NULL
);
301 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
302 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
309 xmesa_swap_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
311 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
314 ret
= xmesa_st_framebuffer_display(stfbi
, ST_ATTACHMENT_BACK_LEFT
);
316 struct pipe_resource
**front
, **back
, *tmp
;
318 front
= &xstfb
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
319 back
= &xstfb
->textures
[ST_ATTACHMENT_BACK_LEFT
];
320 /* swap textures only if the front texture has been allocated */
327 xmesa_check_buffer_size(xstfb
->buffer
);
332 xmesa_copy_st_framebuffer(struct st_framebuffer_iface
*stfbi
,
333 enum st_attachment_type src
,
334 enum st_attachment_type dst
,
335 int x
, int y
, int w
, int h
)
337 xmesa_st_framebuffer_copy_textures(stfbi
, src
, dst
, x
, y
, w
, h
);
338 if (dst
== ST_ATTACHMENT_FRONT_LEFT
)
339 xmesa_st_framebuffer_display(stfbi
, dst
);