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@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 38struct 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 49static uint32_t stwfb_ID = 0; 50 51/** 52 * Is the given mutex held by the calling thread? 53 */ 54bool 55stw_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 */ 75static void 76stw_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 templ.nr_storage_samples = stwfb->stvis.samples;; 99 100 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) { 101 enum pipe_format format; 102 unsigned bind; 103 104 /* the texture already exists or not requested */ 105 if (stwfb->textures[i] || !(mask & (1 << i))) { 106 /* remember the texture */ 107 if (stwfb->textures[i]) 108 mask |= (1 << i); 109 continue; 110 } 111 112 switch (i) { 113 case ST_ATTACHMENT_FRONT_LEFT: 114 case ST_ATTACHMENT_BACK_LEFT: 115 format = stwfb->stvis.color_format; 116 bind = PIPE_BIND_DISPLAY_TARGET | 117 PIPE_BIND_SAMPLER_VIEW | 118 PIPE_BIND_RENDER_TARGET; 119 break; 120 case ST_ATTACHMENT_DEPTH_STENCIL: 121 format = stwfb->stvis.depth_stencil_format; 122 bind = PIPE_BIND_DEPTH_STENCIL; 123 break; 124 default: 125 format = PIPE_FORMAT_NONE; 126 break; 127 } 128 129 if (format != PIPE_FORMAT_NONE) { 130 templ.format = format; 131 templ.bind = bind; 132 133 stwfb->textures[i] = 134 stw_dev->screen->resource_create(stw_dev->screen, &templ); 135 } 136 } 137 138 stwfb->texture_width = width; 139 stwfb->texture_height = height; 140 stwfb->texture_mask = mask; 141} 142 143static boolean 144stw_st_framebuffer_validate(struct st_context_iface *stctx, 145 struct st_framebuffer_iface *stfb, 146 const enum st_attachment_type *statts, 147 unsigned count, 148 struct pipe_resource **out) 149{ 150 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 151 unsigned statt_mask, i; 152 153 statt_mask = 0x0; 154 for (i = 0; i < count; i++) 155 statt_mask |= 1 << statts[i]; 156 157 stw_framebuffer_lock(stwfb->fb); 158 159 if (stwfb->fb->must_resize || (statt_mask & ~stwfb->texture_mask)) { 160 stw_st_framebuffer_validate_locked(&stwfb->base, 161 stwfb->fb->width, stwfb->fb->height, statt_mask); 162 stwfb->fb->must_resize = FALSE; 163 } 164 165 for (i = 0; i < count; i++) 166 pipe_resource_reference(&out[i], stwfb->textures[statts[i]]); 167 168 stw_framebuffer_unlock(stwfb->fb); 169 170 return TRUE; 171} 172 173/** 174 * Present an attachment of the framebuffer. 175 */ 176static boolean 177stw_st_framebuffer_present_locked(HDC hdc, 178 struct st_framebuffer_iface *stfb, 179 enum st_attachment_type statt) 180{ 181 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 182 struct pipe_resource *resource; 183 184 assert(stw_own_mutex(&stwfb->fb->mutex)); 185 186 resource = stwfb->textures[statt]; 187 if (resource) { 188 stw_framebuffer_present_locked(hdc, stwfb->fb, resource); 189 } 190 else { 191 stw_framebuffer_unlock(stwfb->fb); 192 } 193 194 assert(!stw_own_mutex(&stwfb->fb->mutex)); 195 196 return TRUE; 197} 198 199static boolean 200stw_st_framebuffer_flush_front(struct st_context_iface *stctx, 201 struct st_framebuffer_iface *stfb, 202 enum st_attachment_type statt) 203{ 204 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 205 boolean ret; 206 HDC hDC; 207 208 stw_framebuffer_lock(stwfb->fb); 209 210 /* We must not cache HDCs anywhere, as they can be invalidated by the 211 * application, or screen resolution changes. */ 212 213 hDC = GetDC(stwfb->fb->hWnd); 214 215 ret = stw_st_framebuffer_present_locked(hDC, &stwfb->base, statt); 216 217 ReleaseDC(stwfb->fb->hWnd, hDC); 218 219 return ret; 220} 221 222/** 223 * Create a framebuffer interface. 224 */ 225struct st_framebuffer_iface * 226stw_st_create_framebuffer(struct stw_framebuffer *fb) 227{ 228 struct stw_st_framebuffer *stwfb; 229 230 stwfb = CALLOC_STRUCT(stw_st_framebuffer); 231 if (!stwfb) 232 return NULL; 233 234 stwfb->fb = fb; 235 stwfb->stvis = fb->pfi->stvis; 236 stwfb->base.ID = p_atomic_inc_return(&stwfb_ID); 237 stwfb->base.state_manager = stw_dev->smapi; 238 239 stwfb->base.visual = &stwfb->stvis; 240 p_atomic_set(&stwfb->base.stamp, 1); 241 stwfb->base.flush_front = stw_st_framebuffer_flush_front; 242 stwfb->base.validate = stw_st_framebuffer_validate; 243 244 return &stwfb->base; 245} 246 247/** 248 * Destroy a framebuffer interface. 249 */ 250void 251stw_st_destroy_framebuffer_locked(struct st_framebuffer_iface *stfb) 252{ 253 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 254 int i; 255 256 for (i = 0; i < ST_ATTACHMENT_COUNT; i++) 257 pipe_resource_reference(&stwfb->textures[i], NULL); 258 259 /* Notify the st manager that the framebuffer interface is no 260 * longer valid. 261 */ 262 stw_dev->stapi->destroy_drawable(stw_dev->stapi, &stwfb->base); 263 264 FREE(stwfb); 265} 266 267/** 268 * Swap the buffers of the given framebuffer. 269 */ 270boolean 271stw_st_swap_framebuffer_locked(HDC hdc, struct st_framebuffer_iface *stfb) 272{ 273 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 274 unsigned front = ST_ATTACHMENT_FRONT_LEFT, back = ST_ATTACHMENT_BACK_LEFT; 275 struct pipe_resource *ptex; 276 unsigned mask; 277 278 /* swap the textures */ 279 ptex = stwfb->textures[front]; 280 stwfb->textures[front] = stwfb->textures[back]; 281 stwfb->textures[back] = ptex; 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(hdc, &stwfb->base, front); 297} 298 299 300/** 301 * Return the pipe_resource that correspond to given buffer. 302 */ 303struct pipe_resource * 304stw_get_framebuffer_resource(struct st_framebuffer_iface *stfb, 305 enum st_attachment_type att) 306{ 307 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb); 308 return stwfb->textures[att]; 309} 310 311 312/** 313 * Create an st_api of the state tracker. 314 */ 315struct st_api * 316stw_st_create_api(void) 317{ 318 return st_gl_api_create(); 319} 320