xm_st.c revision 685bb6941681f89f71a9169594d87c8e314b94d0
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.9 4 * 5 * Copyright (C) 2010 LunarG Inc. 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 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. 24 * 25 * Authors: 26 * Chia-I Wu <olv@lunarg.com> 27 */ 28 29#include "util/u_memory.h" 30#include "util/u_inlines.h" 31 32#include "xm_api.h" 33#include "xm_st.h" 34 35struct xmesa_st_framebuffer { 36 XMesaDisplay display; 37 XMesaBuffer buffer; 38 struct pipe_screen *screen; 39 40 struct st_visual stvis; 41 42 unsigned texture_width, texture_height, texture_mask; 43 struct pipe_resource *textures[ST_ATTACHMENT_COUNT]; 44 45 struct pipe_surface *display_surface; 46}; 47 48static INLINE struct xmesa_st_framebuffer * 49xmesa_st_framebuffer(struct st_framebuffer_iface *stfbi) 50{ 51 return (struct xmesa_st_framebuffer *) stfbi->st_manager_private; 52} 53 54/** 55 * Display an attachment to the xlib_drawable of the framebuffer. 56 */ 57static boolean 58xmesa_st_framebuffer_display(struct st_framebuffer_iface *stfbi, 59 enum st_attachment_type statt) 60{ 61 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 62 struct pipe_resource *ptex = xstfb->textures[statt]; 63 struct pipe_surface *psurf; 64 65 if (!ptex) 66 return TRUE; 67 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); 72 73 psurf = xstfb->screen->get_tex_surface(xstfb->screen, 74 ptex, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET); 75 if (!psurf) 76 return FALSE; 77 78 xstfb->display_surface = psurf; 79 } 80 81 xstfb->screen->flush_frontbuffer(xstfb->screen, psurf, &xstfb->buffer->ws); 82 83 return TRUE; 84} 85 86/** 87 * Copy the contents between the attachments. 88 */ 89static void 90xmesa_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) 95{ 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_subresource subsrc, subdst; 100 struct pipe_context *pipe; 101 102 if (!src_ptex || !dst_ptex) 103 return; 104 105 pipe = xstfb->display->pipe; 106 if (!pipe) { 107 pipe = xstfb->screen->context_create(xstfb->screen, NULL); 108 if (!pipe) 109 return; 110 xstfb->display->pipe = pipe; 111 } 112 113 subsrc.face = 0; 114 subsrc.level = 0; 115 subdst.face = 0; 116 subdst.level = 0; 117 118 if (src_ptex && dst_ptex) 119 pipe->resource_copy_region(pipe, dst_ptex, subdst, x, y, 0, 120 src_ptex, subsrc, x, y, 0, width, height); 121} 122 123/** 124 * Remove outdated textures and create the requested ones. 125 */ 126static boolean 127xmesa_st_framebuffer_validate_textures(struct st_framebuffer_iface *stfbi, 128 unsigned width, unsigned height, 129 unsigned mask) 130{ 131 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 132 struct pipe_resource templ; 133 enum st_attachment_type i; 134 135 /* remove outdated textures */ 136 if (xstfb->texture_width != width || xstfb->texture_height != height) { 137 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 138 pipe_resource_reference(&xstfb->textures[i], NULL); 139 } 140 141 memset(&templ, 0, sizeof(templ)); 142 templ.target = PIPE_TEXTURE_2D; 143 templ.width0 = width; 144 templ.height0 = height; 145 templ.depth0 = 1; 146 templ.last_level = 0; 147 148 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) { 149 enum pipe_format format; 150 unsigned bind; 151 152 /* the texture already exists or not requested */ 153 if (xstfb->textures[i] || !(mask & (1 << i))) { 154 /* remember the texture */ 155 if (xstfb->textures[i]) 156 mask |= (1 << i); 157 continue; 158 } 159 160 switch (i) { 161 case ST_ATTACHMENT_FRONT_LEFT: 162 case ST_ATTACHMENT_BACK_LEFT: 163 case ST_ATTACHMENT_FRONT_RIGHT: 164 case ST_ATTACHMENT_BACK_RIGHT: 165 format = xstfb->stvis.color_format; 166 bind = PIPE_BIND_DISPLAY_TARGET | 167 PIPE_BIND_RENDER_TARGET; 168 break; 169 case ST_ATTACHMENT_DEPTH_STENCIL: 170 format = xstfb->stvis.depth_stencil_format; 171 bind = PIPE_BIND_DEPTH_STENCIL; 172 break; 173 default: 174 format = PIPE_FORMAT_NONE; 175 break; 176 } 177 178 if (format != PIPE_FORMAT_NONE) { 179 templ.format = format; 180 templ.bind = bind; 181 182 xstfb->textures[i] = 183 xstfb->screen->resource_create(xstfb->screen, &templ); 184 if (!xstfb->textures[i]) 185 return FALSE; 186 } 187 } 188 189 xstfb->texture_width = width; 190 xstfb->texture_height = height; 191 xstfb->texture_mask = mask; 192 193 return TRUE; 194} 195 196static boolean 197xmesa_st_framebuffer_validate(struct st_framebuffer_iface *stfbi, 198 const enum st_attachment_type *statts, 199 unsigned count, 200 struct pipe_resource **out) 201{ 202 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 203 unsigned statt_mask, new_mask, i; 204 boolean resized; 205 boolean ret; 206 207 statt_mask = 0x0; 208 for (i = 0; i < count; i++) 209 statt_mask |= 1 << statts[i]; 210 /* record newly allocated textures */ 211 new_mask = statt_mask & ~xstfb->texture_mask; 212 213 resized = (xstfb->buffer->width != xstfb->texture_width || 214 xstfb->buffer->height != xstfb->texture_height); 215 216 /* revalidate textures */ 217 if (resized || new_mask) { 218 ret = xmesa_st_framebuffer_validate_textures(stfbi, 219 xstfb->buffer->width, xstfb->buffer->height, statt_mask); 220 if (!ret) 221 return ret; 222 223 if (!resized) { 224 enum st_attachment_type back, front; 225 226 back = ST_ATTACHMENT_BACK_LEFT; 227 front = ST_ATTACHMENT_FRONT_LEFT; 228 /* copy the contents if front is newly allocated and back is not */ 229 if ((statt_mask & (1 << back)) && 230 (new_mask & (1 << front)) && 231 !(new_mask & (1 << back))) { 232 xmesa_st_framebuffer_copy_textures(stfbi, back, front, 233 0, 0, xstfb->texture_width, xstfb->texture_height); 234 } 235 } 236 } 237 238 for (i = 0; i < count; i++) { 239 out[i] = NULL; 240 pipe_resource_reference(&out[i], xstfb->textures[statts[i]]); 241 } 242 243 return TRUE; 244} 245 246static boolean 247xmesa_st_framebuffer_flush_front(struct st_framebuffer_iface *stfbi, 248 enum st_attachment_type statt) 249{ 250 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 251 boolean ret; 252 253 ret = xmesa_st_framebuffer_display(stfbi, statt); 254 if (ret) 255 xmesa_check_buffer_size(xstfb->buffer); 256 257 return ret; 258} 259 260struct st_framebuffer_iface * 261xmesa_create_st_framebuffer(XMesaDisplay xmdpy, XMesaBuffer b) 262{ 263 struct st_framebuffer_iface *stfbi; 264 struct xmesa_st_framebuffer *xstfb; 265 266 assert(xmdpy->display == b->xm_visual->display); 267 268 stfbi = CALLOC_STRUCT(st_framebuffer_iface); 269 xstfb = CALLOC_STRUCT(xmesa_st_framebuffer); 270 if (!stfbi || !xstfb) { 271 if (stfbi) 272 FREE(stfbi); 273 if (xstfb) 274 FREE(xstfb); 275 return NULL; 276 } 277 278 xstfb->display = xmdpy; 279 xstfb->buffer = b; 280 xstfb->screen = xmdpy->screen; 281 xstfb->stvis = b->xm_visual->stvis; 282 283 stfbi->visual = &xstfb->stvis; 284 stfbi->flush_front = xmesa_st_framebuffer_flush_front; 285 stfbi->validate = xmesa_st_framebuffer_validate; 286 stfbi->st_manager_private = (void *) xstfb; 287 288 return stfbi; 289} 290 291void 292xmesa_destroy_st_framebuffer(struct st_framebuffer_iface *stfbi) 293{ 294 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 295 int i; 296 297 pipe_surface_reference(&xstfb->display_surface, NULL); 298 299 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 300 pipe_resource_reference(&xstfb->textures[i], NULL); 301 302 FREE(xstfb); 303 FREE(stfbi); 304} 305 306void 307xmesa_swap_st_framebuffer(struct st_framebuffer_iface *stfbi) 308{ 309 struct xmesa_st_framebuffer *xstfb = xmesa_st_framebuffer(stfbi); 310 boolean ret; 311 312 ret = xmesa_st_framebuffer_display(stfbi, ST_ATTACHMENT_BACK_LEFT); 313 if (ret) { 314 struct pipe_resource **front, **back, *tmp; 315 316 front = &xstfb->textures[ST_ATTACHMENT_FRONT_LEFT]; 317 back = &xstfb->textures[ST_ATTACHMENT_BACK_LEFT]; 318 /* swap textures only if the front texture has been allocated */ 319 if (*front) { 320 tmp = *front; 321 *front = *back; 322 *back = tmp; 323 } 324 325 xmesa_check_buffer_size(xstfb->buffer); 326 } 327} 328 329void 330xmesa_copy_st_framebuffer(struct st_framebuffer_iface *stfbi, 331 enum st_attachment_type src, 332 enum st_attachment_type dst, 333 int x, int y, int w, int h) 334{ 335 xmesa_st_framebuffer_copy_textures(stfbi, src, dst, x, y, w, h); 336 if (dst == ST_ATTACHMENT_FRONT_LEFT) 337 xmesa_st_framebuffer_display(stfbi, dst); 338} 339