vg_manager.c revision a91128030e6c19609f6bb66a871382bbe4e3e2a4
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.9 4 * 5 * Copyright 2009 VMware, Inc. All Rights Reserved. 6 * Copyright (C) 2010 LunarG Inc. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 * DEALINGS IN THE SOFTWARE. 25 * 26 * Authors: 27 * Chia-I Wu <olv@lunarg.com> 28 */ 29 30#include "state_tracker/st_api.h" 31 32#include "pipe/p_context.h" 33#include "pipe/p_screen.h" 34#include "util/u_memory.h" 35#include "util/u_inlines.h" 36#include "util/u_box.h" 37#include "util/u_surface.h" 38 39#include "vg_api.h" 40#include "vg_manager.h" 41#include "vg_context.h" 42#include "api.h" 43 44static boolean 45vg_context_update_color_rb(struct vg_context *ctx, struct pipe_resource *pt) 46{ 47 struct st_renderbuffer *strb = ctx->draw_buffer->strb; 48 struct pipe_context *pipe = ctx->pipe; 49 struct pipe_surface surf_tmpl; 50 51 if (strb->texture == pt) { 52 pipe_resource_reference(&pt, NULL); 53 return FALSE; 54 } 55 56 /* unreference existing ones */ 57 pipe_surface_reference(&strb->surface, NULL); 58 pipe_resource_reference(&strb->texture, NULL); 59 strb->width = strb->height = 0; 60 61 strb->texture = pt; 62 63 memset(&surf_tmpl, 0, sizeof(surf_tmpl)); 64 u_surface_default_template(&surf_tmpl, strb->texture, 65 PIPE_BIND_RENDER_TARGET); 66 strb->surface = pipe->create_surface(pipe, strb->texture, &surf_tmpl); 67 68 if (!strb->surface) { 69 pipe_resource_reference(&strb->texture, NULL); 70 return TRUE; 71 } 72 73 strb->width = pt->width0; 74 strb->height = pt->height0; 75 76 return TRUE; 77} 78 79/** 80 * Flush the front buffer if the current context renders to the front buffer. 81 */ 82void 83vg_manager_flush_frontbuffer(struct vg_context *ctx) 84{ 85 struct st_framebuffer *stfb = ctx->draw_buffer; 86 87 if (!stfb) 88 return; 89 90 switch (stfb->strb_att) { 91 case ST_ATTACHMENT_FRONT_LEFT: 92 case ST_ATTACHMENT_FRONT_RIGHT: 93 stfb->iface->flush_front(stfb->iface, stfb->strb_att); 94 break; 95 default: 96 break; 97 } 98} 99 100/** 101 * Re-validate the framebuffer. 102 */ 103void 104vg_manager_validate_framebuffer(struct vg_context *ctx) 105{ 106 struct st_framebuffer *stfb = ctx->draw_buffer; 107 struct pipe_resource *pt; 108 109 /* no binding surface */ 110 if (!stfb) 111 return; 112 113 if (!p_atomic_read(&ctx->draw_buffer_invalid)) 114 return; 115 116 /* validate the fb */ 117 if (!stfb->iface->validate(stfb->iface, &stfb->strb_att, 1, &pt) || !pt) 118 return; 119 120 p_atomic_set(&ctx->draw_buffer_invalid, FALSE); 121 122 if (vg_context_update_color_rb(ctx, pt) || 123 stfb->width != pt->width0 || 124 stfb->height != pt->height0) 125 ctx->state.dirty |= FRAMEBUFFER_DIRTY; 126 127 stfb->width = pt->width0; 128 stfb->height = pt->height0; 129} 130 131static void 132vg_context_notify_invalid_framebuffer(struct st_context_iface *stctxi, 133 struct st_framebuffer_iface *stfbi) 134{ 135 struct vg_context *ctx = (struct vg_context *) stctxi; 136 p_atomic_set(&ctx->draw_buffer_invalid, TRUE); 137} 138 139static void 140vg_context_flush(struct st_context_iface *stctxi, unsigned flags, 141 struct pipe_fence_handle **fence) 142{ 143 struct vg_context *ctx = (struct vg_context *) stctxi; 144 ctx->pipe->flush(ctx->pipe, flags, fence); 145 if (flags & PIPE_FLUSH_RENDER_CACHE) 146 vg_manager_flush_frontbuffer(ctx); 147} 148 149static void 150vg_context_destroy(struct st_context_iface *stctxi) 151{ 152 struct vg_context *ctx = (struct vg_context *) stctxi; 153 struct pipe_context *pipe = ctx->pipe; 154 155 vg_destroy_context(ctx); 156 pipe->destroy(pipe); 157} 158 159static struct st_context_iface * 160vg_api_create_context(struct st_api *stapi, struct st_manager *smapi, 161 const struct st_context_attribs *attribs, 162 struct st_context_iface *shared_stctxi) 163{ 164 struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi; 165 struct vg_context *ctx; 166 struct pipe_context *pipe; 167 168 if (!(stapi->profile_mask & (1 << attribs->profile))) 169 return NULL; 170 171 /* only 1.0 is supported */ 172 if (attribs->major > 1 || (attribs->major == 1 && attribs->minor > 0)) 173 return NULL; 174 175 pipe = smapi->screen->context_create(smapi->screen, NULL); 176 if (!pipe) 177 return NULL; 178 ctx = vg_create_context(pipe, NULL, shared_ctx); 179 if (!ctx) { 180 pipe->destroy(pipe); 181 return NULL; 182 } 183 184 ctx->iface.destroy = vg_context_destroy; 185 186 ctx->iface.notify_invalid_framebuffer = 187 vg_context_notify_invalid_framebuffer; 188 ctx->iface.flush = vg_context_flush; 189 190 ctx->iface.teximage = NULL; 191 ctx->iface.copy = NULL; 192 193 ctx->iface.st_context_private = (void *) smapi; 194 195 return &ctx->iface; 196} 197 198static struct st_renderbuffer * 199create_renderbuffer(enum pipe_format format) 200{ 201 struct st_renderbuffer *strb; 202 203 strb = CALLOC_STRUCT(st_renderbuffer); 204 if (strb) 205 strb->format = format; 206 207 return strb; 208} 209 210static void 211destroy_renderbuffer(struct st_renderbuffer *strb) 212{ 213 pipe_surface_reference(&strb->surface, NULL); 214 pipe_resource_reference(&strb->texture, NULL); 215 FREE(strb); 216} 217 218/** 219 * Decide the buffer to render to. 220 */ 221static enum st_attachment_type 222choose_attachment(struct st_framebuffer_iface *stfbi) 223{ 224 enum st_attachment_type statt; 225 226 statt = stfbi->visual->render_buffer; 227 if (statt != ST_ATTACHMENT_INVALID) { 228 /* use the buffer given by the visual, unless it is unavailable */ 229 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) { 230 switch (statt) { 231 case ST_ATTACHMENT_BACK_LEFT: 232 statt = ST_ATTACHMENT_FRONT_LEFT; 233 break; 234 case ST_ATTACHMENT_BACK_RIGHT: 235 statt = ST_ATTACHMENT_FRONT_RIGHT; 236 break; 237 default: 238 break; 239 } 240 241 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) 242 statt = ST_ATTACHMENT_INVALID; 243 } 244 } 245 246 return statt; 247} 248 249/** 250 * Bind the context to the given framebuffers. 251 */ 252static boolean 253vg_context_bind_framebuffers(struct st_context_iface *stctxi, 254 struct st_framebuffer_iface *stdrawi, 255 struct st_framebuffer_iface *streadi) 256{ 257 struct vg_context *ctx = (struct vg_context *) stctxi; 258 struct st_framebuffer *stfb; 259 enum st_attachment_type strb_att; 260 261 /* the draw and read framebuffers must be the same */ 262 if (stdrawi != streadi) 263 return FALSE; 264 265 p_atomic_set(&ctx->draw_buffer_invalid, TRUE); 266 267 strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID; 268 269 if (ctx->draw_buffer) { 270 stfb = ctx->draw_buffer; 271 272 /* free the existing fb */ 273 if (!stdrawi || 274 stfb->strb_att != strb_att || 275 stfb->strb->format != stdrawi->visual->color_format) { 276 destroy_renderbuffer(stfb->strb); 277 destroy_renderbuffer(stfb->dsrb); 278 FREE(stfb); 279 280 ctx->draw_buffer = NULL; 281 } 282 } 283 284 if (!stdrawi) 285 return TRUE; 286 287 if (strb_att == ST_ATTACHMENT_INVALID) 288 return FALSE; 289 290 /* create a new fb */ 291 if (!ctx->draw_buffer) { 292 stfb = CALLOC_STRUCT(st_framebuffer); 293 if (!stfb) 294 return FALSE; 295 296 stfb->strb = create_renderbuffer(stdrawi->visual->color_format); 297 if (!stfb->strb) { 298 FREE(stfb); 299 return FALSE; 300 } 301 302 stfb->dsrb = create_renderbuffer(ctx->ds_format); 303 if (!stfb->dsrb) { 304 FREE(stfb->strb); 305 FREE(stfb); 306 return FALSE; 307 } 308 309 stfb->width = 0; 310 stfb->height = 0; 311 stfb->strb_att = strb_att; 312 313 ctx->draw_buffer = stfb; 314 } 315 316 ctx->draw_buffer->iface = stdrawi; 317 318 return TRUE; 319} 320 321static boolean 322vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi, 323 struct st_framebuffer_iface *stdrawi, 324 struct st_framebuffer_iface *streadi) 325{ 326 struct vg_context *ctx = (struct vg_context *) stctxi; 327 328 if (stctxi) 329 vg_context_bind_framebuffers(stctxi, stdrawi, streadi); 330 vg_set_current_context(ctx); 331 332 return TRUE; 333} 334 335static struct st_context_iface * 336vg_api_get_current(struct st_api *stapi) 337{ 338 struct vg_context *ctx = vg_current_context(); 339 340 return (ctx) ? &ctx->iface : NULL; 341} 342 343static st_proc_t 344vg_api_get_proc_address(struct st_api *stapi, const char *procname) 345{ 346 return api_get_proc_address(procname); 347} 348 349static void 350vg_api_destroy(struct st_api *stapi) 351{ 352} 353 354static const struct st_api vg_api = { 355 "Vega " VEGA_VERSION_STRING, 356 ST_API_OPENVG, 357 ST_PROFILE_DEFAULT_MASK, 358 vg_api_destroy, 359 vg_api_get_proc_address, 360 vg_api_create_context, 361 vg_api_make_current, 362 vg_api_get_current, 363}; 364 365const struct st_api * 366vg_api_get(void) 367{ 368 return &vg_api; 369} 370