1 /* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 2010 LunarG Inc. 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR 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 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Chia-I Wu <olv (at) lunarg.com> 26 */ 27 28 #include "util/u_memory.h" 29 #include "util/u_inlines.h" 30 #include "util/u_atomic.h" 31 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */ 32 33 #include "stw_st.h" 34 #include "stw_device.h" 35 #include "stw_framebuffer.h" 36 #include "stw_pixelformat.h" 37 38 struct stw_st_framebuffer { 39 struct st_framebuffer_iface base; 40 41 struct stw_framebuffer *fb; 42 struct st_visual stvis; 43 44 struct pipe_resource *textures[ST_ATTACHMENT_COUNT]; 45 unsigned texture_width, texture_height; 46 unsigned texture_mask; 47 }; 48 49 static uint32_t stwfb_ID = 0; 50 51 /** 52 * Is the given mutex held by the calling thread? 53 */ 54 bool 55 stw_own_mutex(const CRITICAL_SECTION *cs) 56 { 57 // We can't compare OwningThread with our thread handle/id (see 58 // http://stackoverflow.com/a/12675635 ) but we can compare with the 59 // OwningThread member of a critical section we know we own. 60 CRITICAL_SECTION dummy; 61 InitializeCriticalSection(&dummy); 62 EnterCriticalSection(&dummy); 63 if (0) 64 _debug_printf("%p %p\n", cs->OwningThread, dummy.OwningThread); 65 bool ret = cs->OwningThread == dummy.OwningThread; 66 LeaveCriticalSection(&dummy); 67 DeleteCriticalSection(&dummy); 68 return ret; 69 } 70 71 72 /** 73 * Remove outdated textures and create the requested ones. 74 */ 75 static void 76 stw_st_framebuffer_validate_locked(struct st_framebuffer_iface *stfb, 77 unsigned width, unsigned height, 78 unsigned mask) 79 { 80 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 81 struct pipe_resource templ; 82 unsigned i; 83 84 /* remove outdated textures */ 85 if (stwfb->texture_width != width || stwfb->texture_height != height) { 86 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 87 pipe_resource_reference(&stwfb->textures[i], NULL); 88 } 89 90 memset(&templ, 0, sizeof(templ)); 91 templ.target = PIPE_TEXTURE_2D; 92 templ.width0 = width; 93 templ.height0 = height; 94 templ.depth0 = 1; 95 templ.array_size = 1; 96 templ.last_level = 0; 97 templ.nr_samples = stwfb->stvis.samples; 98 99 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) { 100 enum pipe_format format; 101 unsigned bind; 102 103 /* the texture already exists or not requested */ 104 if (stwfb->textures[i] || !(mask & (1 << i))) { 105 /* remember the texture */ 106 if (stwfb->textures[i]) 107 mask |= (1 << i); 108 continue; 109 } 110 111 switch (i) { 112 case ST_ATTACHMENT_FRONT_LEFT: 113 case ST_ATTACHMENT_BACK_LEFT: 114 format = stwfb->stvis.color_format; 115 bind = PIPE_BIND_DISPLAY_TARGET | 116 PIPE_BIND_SAMPLER_VIEW | 117 PIPE_BIND_RENDER_TARGET; 118 break; 119 case ST_ATTACHMENT_DEPTH_STENCIL: 120 format = stwfb->stvis.depth_stencil_format; 121 bind = PIPE_BIND_DEPTH_STENCIL; 122 break; 123 default: 124 format = PIPE_FORMAT_NONE; 125 break; 126 } 127 128 if (format != PIPE_FORMAT_NONE) { 129 templ.format = format; 130 templ.bind = bind; 131 132 stwfb->textures[i] = 133 stw_dev->screen->resource_create(stw_dev->screen, &templ); 134 } 135 } 136 137 stwfb->texture_width = width; 138 stwfb->texture_height = height; 139 stwfb->texture_mask = mask; 140 } 141 142 static boolean 143 stw_st_framebuffer_validate(struct st_context_iface *stctx, 144 struct st_framebuffer_iface *stfb, 145 const enum st_attachment_type *statts, 146 unsigned count, 147 struct pipe_resource **out) 148 { 149 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 150 unsigned statt_mask, i; 151 152 statt_mask = 0x0; 153 for (i = 0; i < count; i++) 154 statt_mask |= 1 << statts[i]; 155 156 stw_framebuffer_lock(stwfb->fb); 157 158 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) { 159 stw_st_framebuffer_validate_locked(&stwfb->base, 160 stwfb->fb->width, stwfb->fb->height, statt_mask); 161 stwfb->fb->must_resize = FALSE; 162 } 163 164 for (i = 0; i < count; i++) 165 pipe_resource_reference(&out[i], stwfb->textures[statts[i]]); 166 167 stw_framebuffer_unlock(stwfb->fb); 168 169 return TRUE; 170 } 171 172 /** 173 * Present an attachment of the framebuffer. 174 */ 175 static boolean 176 stw_st_framebuffer_present_locked(HDC hdc, 177 struct st_framebuffer_iface *stfb, 178 enum st_attachment_type statt) 179 { 180 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 181 struct pipe_resource *resource; 182 183 assert(stw_own_mutex(&stwfb->fb->mutex)); 184 185 resource = stwfb->textures[statt]; 186 if (resource) { 187 stw_framebuffer_present_locked(hdc, stwfb->fb, resource); 188 } 189 else { 190 stw_framebuffer_unlock(stwfb->fb); 191 } 192 193 assert(!stw_own_mutex(&stwfb->fb->mutex)); 194 195 return TRUE; 196 } 197 198 static boolean 199 stw_st_framebuffer_flush_front(struct st_context_iface *stctx, 200 struct st_framebuffer_iface *stfb, 201 enum st_attachment_type statt) 202 { 203 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 204 boolean ret; 205 HDC hDC; 206 207 stw_framebuffer_lock(stwfb->fb); 208 209 /* We must not cache HDCs anywhere, as they can be invalidated by the 210 * application, or screen resolution changes. */ 211 212 hDC = GetDC(stwfb->fb->hWnd); 213 214 ret = stw_st_framebuffer_present_locked(hDC, &stwfb->base, statt); 215 216 ReleaseDC(stwfb->fb->hWnd, hDC); 217 218 return ret; 219 } 220 221 /** 222 * Create a framebuffer interface. 223 */ 224 struct st_framebuffer_iface * 225 stw_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 stwfb->base.ID = p_atomic_inc_return(&stwfb_ID); 236 stwfb->base.state_manager = stw_dev->smapi; 237 238 stwfb->base.visual = &stwfb->stvis; 239 p_atomic_set(&stwfb->base.stamp, 1); 240 stwfb->base.flush_front = stw_st_framebuffer_flush_front; 241 stwfb->base.validate = stw_st_framebuffer_validate; 242 243 return &stwfb->base; 244 } 245 246 /** 247 * Destroy a framebuffer interface. 248 */ 249 void 250 stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb) 251 { 252 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 253 int i; 254 255 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 256 pipe_resource_reference(&stwfb->textures[i], NULL); 257 258 /* Notify the st manager that the framebuffer interface is no 259 * longer valid. 260 */ 261 stw_dev->stapi->destroy_drawable(stw_dev->stapi, &stwfb->base); 262 263 FREE(stwfb); 264 } 265 266 /** 267 * Swap the buffers of the given framebuffer. 268 */ 269 boolean 270 stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb) 271 { 272 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 273 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT; 274 struct pipe_resource *ptex; 275 unsigned mask; 276 277 /* swap the textures */ 278 ptex = stwfb->textures[front]; 279 stwfb->textures[front] = stwfb->textures[back]; 280 stwfb->textures[back] = ptex; 281 282 /* convert to mask */ 283 front = 1 << front; 284 back = 1 << back; 285 286 /* swap the bits in mask */ 287 mask = stwfb->texture_mask & ~(front | back); 288 if (stwfb->texture_mask & front) 289 mask |= back; 290 if (stwfb->texture_mask & back) 291 mask |= front; 292 stwfb->texture_mask = mask; 293 294 front = ST_ATTACHMENT_FRONT_LEFT; 295 return stw_st_framebuffer_present_locked(hdc, &stwfb->base, front); 296 } 297 298 299 /** 300 * Return the pipe_resource that correspond to given buffer. 301 */ 302 struct pipe_resource * 303 stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb, 304 enum st_attachment_type att) 305 { 306 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 307 return stwfb->textures[att]; 308 } 309 310 311 /** 312 * Create an st_api of the state tracker. 313 */ 314 struct st_api * 315 stw_st_create_api(void) 316 { 317 return st_gl_api_create(); 318 } 319