stw_st.c revision 4c7001462607e6e99e474d6271dd481d3f8f201c
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 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Chia-I Wu <olv@lunarg.com> 26 */ 27 28#include "util/u_memory.h" 29#include "util/u_inlines.h" 30#include "state_tracker/st_gl_api.h" /* for st_gl_api_create */ 31 32#include "stw_st.h" 33#include "stw_device.h" 34#include "stw_framebuffer.h" 35#include "stw_pixelformat.h" 36 37struct stw_st_framebuffer { 38 struct st_framebuffer_iface base; 39 40 struct stw_framebuffer *fb; 41 struct st_visual stvis; 42 43 struct pipe_resource *textures[ST_ATTACHMENT_COUNT]; 44 unsigned texture_width, texture_height; 45 unsigned texture_mask; 46 47 struct pipe_resource *front_res, *back_res; 48}; 49 50static INLINE struct stw_st_framebuffer * 51stw_st_framebuffer(struct st_framebuffer_iface *stfb) 52{ 53 return (struct stw_st_framebuffer *) stfb; 54} 55 56/** 57 * Remove outdated textures and create the requested ones. 58 */ 59static void 60stw_st_framebuffer_validate_locked(struct st_framebuffer_iface *stfb, 61 unsigned width, unsigned height, 62 unsigned mask) 63{ 64 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 65 struct pipe_resource templ; 66 unsigned i; 67 68 /* remove outdated surface */ 69 pipe_resource_reference(&stwfb->front_res, NULL); 70 pipe_resource_reference(&stwfb->back_res, NULL); 71 72 /* remove outdated textures */ 73 if (stwfb->texture_width != width || stwfb->texture_height != height) { 74 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 75 pipe_resource_reference(&stwfb->textures[i], NULL); 76 } 77 78 memset(&templ, 0, sizeof(templ)); 79 templ.target = PIPE_TEXTURE_2D; 80 templ.width0 = width; 81 templ.height0 = height; 82 templ.depth0 = 1; 83 templ.array_size = 1; 84 templ.last_level = 0; 85 86 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) { 87 enum pipe_format format; 88 unsigned bind; 89 90 /* the texture already exists or not requested */ 91 if (stwfb->textures[i] || !(mask & (1 << i))) { 92 /* remember the texture */ 93 if (stwfb->textures[i]) 94 mask |= (1 << i); 95 continue; 96 } 97 98 switch (i) { 99 case ST_ATTACHMENT_FRONT_LEFT: 100 case ST_ATTACHMENT_BACK_LEFT: 101 format = stwfb->stvis.color_format; 102 bind = PIPE_BIND_DISPLAY_TARGET | 103 PIPE_BIND_RENDER_TARGET; 104 break; 105 case ST_ATTACHMENT_DEPTH_STENCIL: 106 format = stwfb->stvis.depth_stencil_format; 107 bind = PIPE_BIND_DEPTH_STENCIL; 108 break; 109 default: 110 format = PIPE_FORMAT_NONE; 111 break; 112 } 113 114 if (format != PIPE_FORMAT_NONE) { 115 templ.format = format; 116 templ.bind = bind; 117 118 stwfb->textures[i] = 119 stw_dev->screen->resource_create(stw_dev->screen, &templ); 120 } 121 } 122 123 stwfb->texture_width = width; 124 stwfb->texture_height = height; 125 stwfb->texture_mask = mask; 126} 127 128static boolean 129stw_st_framebuffer_validate(struct st_framebuffer_iface *stfb, 130 const enum st_attachment_type *statts, 131 unsigned count, 132 struct pipe_resource **out) 133{ 134 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 135 unsigned statt_mask, i; 136 137 statt_mask = 0x0; 138 for (i = 0; i < count; i++) 139 statt_mask |= 1 << statts[i]; 140 141 pipe_mutex_lock(stwfb->fb->mutex); 142 143 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) { 144 stw_st_framebuffer_validate_locked(&stwfb->base, 145 stwfb->fb->width, stwfb->fb->height, statt_mask); 146 stwfb->fb->must_resize = FALSE; 147 } 148 149 for (i = 0; i < count; i++) { 150 out[i] = NULL; 151 pipe_resource_reference(&out[i], stwfb->textures[statts[i]]); 152 } 153 154 stw_framebuffer_release(stwfb->fb); 155 156 return TRUE; 157} 158 159static struct pipe_resource * 160get_present_surface_locked(struct st_framebuffer_iface *stfb, 161 enum st_attachment_type statt) 162{ 163 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 164#if 0 165 /* since we don't really have to get a surface for this we 166 no longer need a cache? */ 167 struct pipe_resource *ptex; 168 struct pipe_resource *pres, **cache; 169 170 ptex = stwfb->textures[statt]; 171 if (!ptex) 172 return NULL; 173 174 pres = NULL; 175 176 switch (statt) { 177 case ST_ATTACHMENT_FRONT_LEFT: 178 cache = &stwfb->front_surface; 179 break; 180 case ST_ATTACHMENT_BACK_LEFT: 181 cache = &stwfb->back_surface; 182 break; 183 default: 184 cache = &pres; 185 break; 186 } 187 188 if (!*cache) { 189 *cache = ptex; 190 } 191 192 if (pres != *cache) 193 pipe_resource_reference(&pres, *cache); 194 195 return pres; 196#else 197 return stwfb->textures[statt]; 198#endif 199} 200 201/** 202 * Present an attachment of the framebuffer. 203 */ 204static boolean 205stw_st_framebuffer_present_locked(struct st_framebuffer_iface *stfb, 206 enum st_attachment_type statt) 207{ 208 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 209 struct pipe_resource *pres; 210 211 pres = get_present_surface_locked(&stwfb->base, statt); 212 if (pres) { 213 stw_framebuffer_present_locked(stwfb->fb->hDC, stwfb->fb, pres); 214 pipe_resource_reference(&pres, NULL); 215 } 216 217 return TRUE; 218} 219 220static boolean 221stw_st_framebuffer_flush_front(struct st_framebuffer_iface *stfb, 222 enum st_attachment_type statt) 223{ 224 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 225 226 pipe_mutex_lock(stwfb->fb->mutex); 227 228 return stw_st_framebuffer_present_locked(&stwfb->base, statt); 229} 230 231/** 232 * Create a framebuffer interface. 233 */ 234struct st_framebuffer_iface * 235stw_st_create_framebuffer(struct stw_framebuffer *fb) 236{ 237 struct stw_st_framebuffer *stwfb; 238 239 stwfb = CALLOC_STRUCT(stw_st_framebuffer); 240 if (!stwfb) 241 return NULL; 242 243 stwfb->fb = fb; 244 stwfb->stvis = fb->pfi->stvis; 245 246 stwfb->base.visual = &stwfb->stvis; 247 stwfb->base.flush_front = stw_st_framebuffer_flush_front; 248 stwfb->base.validate = stw_st_framebuffer_validate; 249 250 return &stwfb->base; 251} 252 253/** 254 * Destroy a framebuffer interface. 255 */ 256void 257stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb) 258{ 259 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 260 int i; 261 262 pipe_resource_reference(&stwfb->front_res, NULL); 263 pipe_resource_reference(&stwfb->back_res, NULL); 264 265 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 266 pipe_resource_reference(&stwfb->textures[i], NULL); 267 268 FREE(stwfb); 269} 270 271/** 272 * Swap the buffers of the given framebuffer. 273 */ 274boolean 275stw_st_swap_framebuffer_locked(struct st_framebuffer_iface *stfb) 276{ 277 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 278 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT; 279 struct pipe_resource *ptex; 280 struct pipe_resource *pres; 281 unsigned mask; 282 283 /* swap the textures */ 284 ptex = stwfb->textures[front]; 285 stwfb->textures[front] = stwfb->textures[back]; 286 stwfb->textures[back] = ptex; 287 288 /* swap the surfaces */ 289 pres = stwfb->front_res; 290 stwfb->front_res = stwfb->back_res; 291 stwfb->back_res = pres; 292 293 /* convert to mask */ 294 front = 1 << front; 295 back = 1 << back; 296 297 /* swap the bits in mask */ 298 mask = stwfb->texture_mask & ~(front | back); 299 if (stwfb->texture_mask & front) 300 mask |= back; 301 if (stwfb->texture_mask & back) 302 mask |= front; 303 stwfb->texture_mask = mask; 304 305 front = ST_ATTACHMENT_FRONT_LEFT; 306 return stw_st_framebuffer_present_locked(&stwfb->base, front); 307} 308 309/** 310 * Create an st_api of the state tracker. 311 */ 312struct st_api * 313stw_st_create_api(void) 314{ 315 return st_gl_api_create(); 316} 317