stw_st.c revision 192f06adca5e79b4824d92dc41186592ed57f71e
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_manager.h" /* for st_manager_create_api */ 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_texture *textures[ST_ATTACHMENT_COUNT]; 44 unsigned texture_width, texture_height; 45 unsigned texture_mask; 46 47 struct pipe_surface *front_surface, *back_surface; 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_texture templ; 66 unsigned i; 67 68 /* remove outdated textures */ 69 if (stwfb->texture_width != width || stwfb->texture_height != height) { 70 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 71 pipe_texture_reference(&stwfb->textures[i], NULL); 72 } 73 74 memset(&templ, 0, sizeof(templ)); 75 templ.target = PIPE_TEXTURE_2D; 76 templ.width0 = width; 77 templ.height0 = height; 78 templ.depth0 = 1; 79 templ.last_level = 0; 80 81 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) { 82 enum pipe_format format; 83 unsigned tex_usage; 84 85 /* the texture already exists or not requested */ 86 if (stwfb->textures[i] || !(mask & (1 << i))) { 87 /* remember the texture */ 88 if (stwfb->textures[i]) 89 mask |= (1 << i); 90 continue; 91 } 92 93 switch (i) { 94 case ST_ATTACHMENT_FRONT_LEFT: 95 case ST_ATTACHMENT_BACK_LEFT: 96 format = stwfb->stvis.color_format; 97 tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET | 98 PIPE_TEXTURE_USAGE_RENDER_TARGET; 99 break; 100 case ST_ATTACHMENT_DEPTH_STENCIL: 101 format = stwfb->stvis.depth_stencil_format; 102 tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL; 103 break; 104 default: 105 format = PIPE_FORMAT_NONE; 106 break; 107 } 108 109 if (format != PIPE_FORMAT_NONE) { 110 templ.format = format; 111 templ.tex_usage = tex_usage; 112 113 stwfb->textures[i] = 114 stw_dev->screen->texture_create(stw_dev->screen, &templ); 115 } 116 } 117 118 stwfb->texture_width = width; 119 stwfb->texture_height = height; 120 stwfb->texture_mask = mask; 121} 122 123static boolean 124stw_st_framebuffer_validate(struct st_framebuffer_iface *stfb, 125 const enum st_attachment_type *statts, 126 unsigned count, 127 struct pipe_texture **out) 128{ 129 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 130 unsigned statt_mask, i; 131 132 statt_mask = 0x0; 133 for (i = 0; i < count; i++) 134 statt_mask |= 1 << statts[i]; 135 136 pipe_mutex_lock(stwfb->fb->mutex); 137 138 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) { 139 stw_st_framebuffer_validate_locked(&stwfb->base, 140 statt_mask, stwfb->fb->width, stwfb->fb->height); 141 stwfb->fb->must_resize = FALSE; 142 } 143 144 for (i = 0; i < count; i++) { 145 out[i] = NULL; 146 pipe_texture_reference(&out[i], stwfb->textures[statts[i]]); 147 } 148 149 stw_framebuffer_release(stwfb->fb); 150 151 return TRUE; 152} 153 154static struct pipe_surface * 155get_present_surface_locked(struct st_framebuffer_iface *stfb, 156 enum st_attachment_type statt) 157{ 158 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 159 struct pipe_texture *ptex; 160 struct pipe_surface *psurf, **cache; 161 162 ptex = stwfb->textures[statt]; 163 if (!ptex) 164 return NULL; 165 166 psurf = NULL; 167 168 switch (statt) { 169 case ST_ATTACHMENT_FRONT_LEFT: 170 cache = &stwfb->front_surface; 171 break; 172 case ST_ATTACHMENT_BACK_LEFT: 173 cache = &stwfb->back_surface; 174 break; 175 default: 176 cache = &psurf; 177 break; 178 } 179 180 if (!*cache) { 181 *cache = stw_dev->screen->get_tex_surface(stw_dev->screen, 182 ptex, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ); 183 } 184 185 if (psurf != *cache) 186 pipe_surface_reference(&psurf, *cache); 187 188 return psurf; 189} 190 191/** 192 * Present an attachment of the framebuffer. 193 */ 194static boolean 195stw_st_framebuffer_present_locked(struct st_framebuffer_iface *stfb, 196 enum st_attachment_type statt) 197{ 198 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 199 struct pipe_surface *psurf; 200 201 psurf = get_present_surface_locked(&stwfb->base, statt); 202 if (psurf) { 203 stw_framebuffer_present_locked(stwfb->fb->hDC, stwfb->fb, psurf); 204 pipe_surface_reference(&psurf, NULL); 205 } 206 207 return TRUE; 208} 209 210static boolean 211stw_st_framebuffer_flush_front(struct st_framebuffer_iface *stfb, 212 enum st_attachment_type statt) 213{ 214 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 215 216 pipe_mutex_lock(stwfb->fb->mutex); 217 218 return stw_st_framebuffer_present_locked(&stwfb->base, statt); 219} 220 221/** 222 * Create a framebuffer interface. 223 */ 224struct st_framebuffer_iface * 225stw_st_create_framebuffer(struct stw_framebuffer *fb) 226{ 227 struct stw_st_framebuffer *stwfb; 228 229 stwfb = CALLOC_STRUCT(stw_st_framebuffer); 230 if (!stwfb) 231 return NULL; 232 233 stwfb->fb = fb; 234 stwfb->stvis = fb->pfi->stvis; 235 236 stwfb->base.visual = &stwfb->stvis; 237 stwfb->base.flush_front = stw_st_framebuffer_flush_front; 238 stwfb->base.validate = stw_st_framebuffer_validate; 239 240 return &stwfb->base; 241} 242 243/** 244 * Destroy a framebuffer interface. 245 */ 246void 247stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb) 248{ 249 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 250 int i; 251 252 pipe_surface_reference(&stwfb->front_surface, NULL); 253 pipe_surface_reference(&stwfb->back_surface, NULL); 254 255 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 256 pipe_texture_reference(&stwfb->textures[i], NULL); 257 258 FREE(stwfb); 259} 260 261/** 262 * Swap the buffers of the given framebuffer. 263 */ 264boolean 265stw_st_swap_framebuffer_locked(struct st_framebuffer_iface *stfb) 266{ 267 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 268 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT; 269 struct pipe_texture *ptex; 270 struct pipe_surface *psurf; 271 unsigned mask; 272 273 /* swap the textures */ 274 ptex = stwfb->textures[front]; 275 stwfb->textures[front] = stwfb->textures[back]; 276 stwfb->textures[back] = ptex; 277 278 /* swap the surfaces */ 279 psurf = stwfb->front_surface; 280 stwfb->front_surface = stwfb->back_surface; 281 stwfb->back_surface = psurf; 282 283 /* convert to mask */ 284 front = 1 << front; 285 back = 1 << back; 286 287 /* swap the bits in mask */ 288 mask = stwfb->texture_mask & ~(front | back); 289 if (stwfb->texture_mask & front) 290 mask |= back; 291 if (stwfb->texture_mask & back) 292 mask |= front; 293 stwfb->texture_mask = mask; 294 295 front = ST_ATTACHMENT_FRONT_LEFT; 296 return stw_st_framebuffer_present_locked(&stwfb->base, front); 297} 298 299/** 300 * Create an st_api of the state tracker. 301 */ 302struct st_api * 303stw_st_create_api(void) 304{ 305 return st_manager_create_api(); 306} 307