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"
33 #include "util/u_memory.h"
35 struct xmesa_st_framebuffer
{
38 struct pipe_screen
*screen
;
40 struct st_visual stvis
;
41 enum pipe_texture_target target
;
43 unsigned texture_width
, texture_height
, texture_mask
;
44 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
46 struct pipe_resource
*display_resource
;
50 static inline struct xmesa_st_framebuffer
*
51 xmesa_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
53 return (struct xmesa_st_framebuffer
*) stfbi
->st_manager_private
;
58 * Display (present) an attachment to the xlib_drawable of the framebuffer.
61 xmesa_st_framebuffer_display(struct st_framebuffer_iface
*stfbi
,
62 enum st_attachment_type statt
)
64 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
65 struct pipe_resource
*ptex
= xstfb
->textures
[statt
];
66 struct pipe_resource
*pres
;
71 pres
= xstfb
->display_resource
;
72 /* (re)allocate the surface for the texture to be displayed */
73 if (!pres
|| pres
!= ptex
) {
74 pipe_resource_reference(&xstfb
->display_resource
, ptex
);
75 pres
= xstfb
->display_resource
;
78 xstfb
->screen
->flush_frontbuffer(xstfb
->screen
, pres
, 0, 0, &xstfb
->buffer
->ws
, NULL
);
84 * Copy the contents between the attachments.
87 xmesa_st_framebuffer_copy_textures(struct st_framebuffer_iface
*stfbi
,
88 enum st_attachment_type src_statt
,
89 enum st_attachment_type dst_statt
,
90 unsigned x
, unsigned y
,
91 unsigned width
, unsigned height
)
93 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
94 struct pipe_resource
*src_ptex
= xstfb
->textures
[src_statt
];
95 struct pipe_resource
*dst_ptex
= xstfb
->textures
[dst_statt
];
96 struct pipe_box src_box
;
97 struct pipe_context
*pipe
;
99 if (!src_ptex
|| !dst_ptex
)
102 pipe
= xmesa_get_context(stfbi
);
104 u_box_2d(x
, y
, width
, height
, &src_box
);
106 if (src_ptex
&& dst_ptex
)
107 pipe
->resource_copy_region(pipe
, dst_ptex
, 0, x
, y
, 0,
108 src_ptex
, 0, &src_box
);
113 * Remove outdated textures and create the requested ones.
114 * This is a helper used during framebuffer validation.
117 xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface
*stfbi
,
118 unsigned width
, unsigned height
,
121 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
122 struct pipe_resource templ
;
123 enum st_attachment_type i
;
125 /* remove outdated textures */
126 if (xstfb
->texture_width
!= width
|| xstfb
->texture_height
!= height
) {
127 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
128 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
131 memset(&templ
, 0, sizeof(templ
));
132 templ
.target
= xstfb
->target
;
133 templ
.width0
= width
;
134 templ
.height0
= height
;
136 templ
.array_size
= 1;
137 templ
.last_level
= 0;
138 templ
.nr_samples
= xstfb
->stvis
.samples
;
139 templ
.nr_storage_samples
= xstfb
->stvis
.samples
;
141 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++) {
142 enum pipe_format format
;
145 /* the texture already exists or not requested */
146 if (xstfb
->textures
[i
] || !(mask
& (1 << i
))) {
147 /* remember the texture */
148 if (xstfb
->textures
[i
])
154 case ST_ATTACHMENT_FRONT_LEFT
:
155 case ST_ATTACHMENT_BACK_LEFT
:
156 case ST_ATTACHMENT_FRONT_RIGHT
:
157 case ST_ATTACHMENT_BACK_RIGHT
:
158 format
= xstfb
->stvis
.color_format
;
159 bind
= PIPE_BIND_DISPLAY_TARGET
|
160 PIPE_BIND_RENDER_TARGET
;
162 case ST_ATTACHMENT_DEPTH_STENCIL
:
163 format
= xstfb
->stvis
.depth_stencil_format
;
164 bind
= PIPE_BIND_DEPTH_STENCIL
;
167 format
= PIPE_FORMAT_NONE
;
171 if (format
!= PIPE_FORMAT_NONE
) {
172 templ
.format
= format
;
176 xstfb
->screen
->resource_create(xstfb
->screen
, &templ
);
177 if (!xstfb
->textures
[i
])
182 xstfb
->texture_width
= width
;
183 xstfb
->texture_height
= height
;
184 xstfb
->texture_mask
= mask
;
191 * Check that a framebuffer's attachments match the window's size.
193 * Called via st_framebuffer_iface::validate()
195 * \param statts array of framebuffer attachments
196 * \param count number of framebuffer attachments in statts[]
197 * \param out returns resources for each of the attachments
200 xmesa_st_framebuffer_validate(struct st_context_iface
*stctx
,
201 struct st_framebuffer_iface
*stfbi
,
202 const enum st_attachment_type
*statts
,
204 struct pipe_resource
**out
)
206 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
207 unsigned statt_mask
, new_mask
, i
;
211 /* build mask of ST_ATTACHMENT bits */
213 for (i
= 0; i
< count
; i
++)
214 statt_mask
|= 1 << statts
[i
];
216 /* record newly allocated textures */
217 new_mask
= statt_mask
& ~xstfb
->texture_mask
;
219 /* If xmesa_strict_invalidate is not set, we will not yet have
220 * called XGetGeometry(). Do so here:
222 if (!xmesa_strict_invalidate
)
223 xmesa_check_buffer_size(xstfb
->buffer
);
225 resized
= (xstfb
->buffer
->width
!= xstfb
->texture_width
||
226 xstfb
->buffer
->height
!= xstfb
->texture_height
);
228 /* revalidate textures */
229 if (resized
|| new_mask
) {
230 ret
= xmesa_st_framebuffer_validate_textures(stfbi
,
231 xstfb
->buffer
->width
, xstfb
->buffer
->height
, statt_mask
);
236 enum st_attachment_type back
, front
;
238 back
= ST_ATTACHMENT_BACK_LEFT
;
239 front
= ST_ATTACHMENT_FRONT_LEFT
;
240 /* copy the contents if front is newly allocated and back is not */
241 if ((statt_mask
& (1 << back
)) &&
242 (new_mask
& (1 << front
)) &&
243 !(new_mask
& (1 << back
))) {
244 xmesa_st_framebuffer_copy_textures(stfbi
, back
, front
,
245 0, 0, xstfb
->texture_width
, xstfb
->texture_height
);
250 for (i
= 0; i
< count
; i
++)
251 pipe_resource_reference(&out
[i
], xstfb
->textures
[statts
[i
]]);
258 * Called via st_framebuffer_iface::flush_front()
261 xmesa_st_framebuffer_flush_front(struct st_context_iface
*stctx
,
262 struct st_framebuffer_iface
*stfbi
,
263 enum st_attachment_type statt
)
265 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
268 ret
= xmesa_st_framebuffer_display(stfbi
, statt
);
270 if (ret
&& xmesa_strict_invalidate
)
271 xmesa_check_buffer_size(xstfb
->buffer
);
276 static uint32_t xmesa_stfbi_ID
= 0;
278 struct st_framebuffer_iface
*
279 xmesa_create_st_framebuffer(XMesaDisplay xmdpy
, XMesaBuffer b
)
281 struct st_framebuffer_iface
*stfbi
;
282 struct xmesa_st_framebuffer
*xstfb
;
284 assert(xmdpy
->display
== b
->xm_visual
->display
);
286 stfbi
= CALLOC_STRUCT(st_framebuffer_iface
);
287 xstfb
= CALLOC_STRUCT(xmesa_st_framebuffer
);
288 if (!stfbi
|| !xstfb
) {
294 xstfb
->display
= xmdpy
;
296 xstfb
->screen
= xmdpy
->screen
;
297 xstfb
->stvis
= b
->xm_visual
->stvis
;
298 if (xstfb
->screen
->get_param(xstfb
->screen
, PIPE_CAP_NPOT_TEXTURES
))
299 xstfb
->target
= PIPE_TEXTURE_2D
;
301 xstfb
->target
= PIPE_TEXTURE_RECT
;
303 stfbi
->visual
= &xstfb
->stvis
;
304 stfbi
->flush_front
= xmesa_st_framebuffer_flush_front
;
305 stfbi
->validate
= xmesa_st_framebuffer_validate
;
306 stfbi
->ID
= p_atomic_inc_return(&xmesa_stfbi_ID
);
307 stfbi
->state_manager
= xmdpy
->smapi
;
308 p_atomic_set(&stfbi
->stamp
, 1);
309 stfbi
->st_manager_private
= (void *) xstfb
;
316 xmesa_destroy_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
318 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
321 pipe_resource_reference(&xstfb
->display_resource
, NULL
);
323 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
324 pipe_resource_reference(&xstfb
->textures
[i
], NULL
);
332 * Return the pipe_surface which corresponds to the given
333 * framebuffer attachment.
335 struct pipe_resource
*
336 xmesa_get_framebuffer_resource(struct st_framebuffer_iface
*stfbi
,
337 enum st_attachment_type att
)
339 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
340 return xstfb
->textures
[att
];
345 xmesa_swap_st_framebuffer(struct st_framebuffer_iface
*stfbi
)
347 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
350 ret
= xmesa_st_framebuffer_display(stfbi
, ST_ATTACHMENT_BACK_LEFT
);
352 struct pipe_resource
**front
, **back
, *tmp
;
354 front
= &xstfb
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
355 back
= &xstfb
->textures
[ST_ATTACHMENT_BACK_LEFT
];
356 /* swap textures only if the front texture has been allocated */
362 /* the current context should validate the buffer after swapping */
363 if (!xmesa_strict_invalidate
)
364 xmesa_notify_invalid_buffer(xstfb
->buffer
);
367 if (xmesa_strict_invalidate
)
368 xmesa_check_buffer_size(xstfb
->buffer
);
374 xmesa_copy_st_framebuffer(struct st_framebuffer_iface
*stfbi
,
375 enum st_attachment_type src
,
376 enum st_attachment_type dst
,
377 int x
, int y
, int w
, int h
)
379 xmesa_st_framebuffer_copy_textures(stfbi
, src
, dst
, x
, y
, w
, h
);
380 if (dst
== ST_ATTACHMENT_FRONT_LEFT
)
381 xmesa_st_framebuffer_display(stfbi
, dst
);
385 struct pipe_resource
*
386 xmesa_get_attachment(struct st_framebuffer_iface
*stfbi
,
387 enum st_attachment_type st_attachment
)
389 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
390 struct pipe_resource
*res
;
392 res
= xstfb
->textures
[st_attachment
];
398 xmesa_get_context(struct st_framebuffer_iface
*stfbi
)
400 struct pipe_context
*pipe
;
401 struct xmesa_st_framebuffer
*xstfb
= xmesa_st_framebuffer(stfbi
);
403 pipe
= xstfb
->display
->pipe
;
405 pipe
= xstfb
->screen
->context_create(xstfb
->screen
, NULL
, 0);
408 xstfb
->display
->pipe
= pipe
;