vg_manager.c revision 635fe3e1927f812a69a68ec3e03d9ab7a2c3a5d9
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_sampler.h" 37 38#include "vg_api.h" 39#include "vg_manager.h" 40#include "vg_context.h" 41#include "image.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_screen *screen = ctx->pipe->screen; 49 50 if (strb->texture == pt) { 51 pipe_resource_reference(&pt, NULL); 52 return FALSE; 53 } 54 55 /* unreference existing ones */ 56 pipe_surface_reference(&strb->surface, NULL); 57 pipe_resource_reference(&strb->texture, NULL); 58 strb->width = strb->height = 0; 59 60 strb->texture = pt; 61 strb->surface = screen->get_tex_surface(screen, strb->texture, 0, 0, 0, 62 PIPE_BIND_RENDER_TARGET); 63 if (!strb->surface) { 64 pipe_resource_reference(&strb->texture, NULL); 65 return TRUE; 66 } 67 68 strb->width = pt->width0; 69 strb->height = pt->height0; 70 71 return TRUE; 72} 73 74/** 75 * Flush the front buffer if the current context renders to the front buffer. 76 */ 77void 78vg_manager_flush_frontbuffer(struct vg_context *ctx) 79{ 80 struct st_framebuffer *stfb = ctx->draw_buffer; 81 82 if (!stfb) 83 return; 84 85 switch (stfb->strb_att) { 86 case ST_ATTACHMENT_FRONT_LEFT: 87 case ST_ATTACHMENT_FRONT_RIGHT: 88 stfb->iface->flush_front(stfb->iface, stfb->strb_att); 89 break; 90 default: 91 break; 92 } 93} 94 95/** 96 * Re-validate the framebuffer. 97 */ 98void 99vg_manager_validate_framebuffer(struct vg_context *ctx) 100{ 101 struct st_framebuffer *stfb = ctx->draw_buffer; 102 struct pipe_resource *pt; 103 104 /* no binding surface */ 105 if (!stfb) 106 return; 107 108 if (!p_atomic_read(&ctx->draw_buffer_invalid)) 109 return; 110 111 /* validate the fb */ 112 if (!stfb->iface->validate(stfb->iface, &stfb->strb_att, 1, &pt) || !pt) 113 return; 114 115 p_atomic_set(&ctx->draw_buffer_invalid, FALSE); 116 117 if (vg_context_update_color_rb(ctx, pt) || 118 stfb->width != pt->width0 || 119 stfb->height != pt->height0) 120 ctx->state.dirty |= FRAMEBUFFER_DIRTY; 121 122 stfb->width = pt->width0; 123 stfb->height = pt->height0; 124} 125 126static void 127vg_context_notify_invalid_framebuffer(struct st_context_iface *stctxi, 128 struct st_framebuffer_iface *stfbi) 129{ 130 struct vg_context *ctx = (struct vg_context *) stctxi; 131 p_atomic_set(&ctx->draw_buffer_invalid, TRUE); 132} 133 134static void 135vg_context_flush(struct st_context_iface *stctxi, unsigned flags, 136 struct pipe_fence_handle **fence) 137{ 138 struct vg_context *ctx = (struct vg_context *) stctxi; 139 ctx->pipe->flush(ctx->pipe, flags, fence); 140 if (flags & PIPE_FLUSH_RENDER_CACHE) 141 vg_manager_flush_frontbuffer(ctx); 142} 143 144static void 145vg_context_destroy(struct st_context_iface *stctxi) 146{ 147 struct vg_context *ctx = (struct vg_context *) stctxi; 148 vg_destroy_context(ctx); 149} 150 151static struct st_context_iface * 152vg_api_create_context(struct st_api *stapi, struct st_manager *smapi, 153 const struct st_context_attribs *attribs, 154 struct st_context_iface *shared_stctxi) 155{ 156 struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi; 157 struct vg_context *ctx; 158 struct pipe_context *pipe; 159 160 if (!(stapi->profile_mask & (1 << attribs->profile))) 161 return NULL; 162 163 /* only 1.0 is supported */ 164 if (attribs->major > 1 || (attribs->major == 1 && attribs->minor > 0)) 165 return NULL; 166 167 pipe = smapi->screen->context_create(smapi->screen, NULL); 168 if (!pipe) 169 return NULL; 170 ctx = vg_create_context(pipe, NULL, shared_ctx); 171 if (!ctx) { 172 pipe->destroy(pipe); 173 return NULL; 174 } 175 176 ctx->iface.destroy = vg_context_destroy; 177 178 ctx->iface.notify_invalid_framebuffer = 179 vg_context_notify_invalid_framebuffer; 180 ctx->iface.flush = vg_context_flush; 181 182 ctx->iface.teximage = NULL; 183 ctx->iface.copy = NULL; 184 185 ctx->iface.st_context_private = (void *) smapi; 186 187 return &ctx->iface; 188} 189 190static struct st_renderbuffer * 191create_renderbuffer(enum pipe_format format) 192{ 193 struct st_renderbuffer *strb; 194 195 strb = CALLOC_STRUCT(st_renderbuffer); 196 if (strb) 197 strb->format = format; 198 199 return strb; 200} 201 202static void 203destroy_renderbuffer(struct st_renderbuffer *strb) 204{ 205 pipe_surface_reference(&strb->surface, NULL); 206 pipe_resource_reference(&strb->texture, NULL); 207 FREE(strb); 208} 209 210/** 211 * Decide the buffer to render to. 212 */ 213static enum st_attachment_type 214choose_attachment(struct st_framebuffer_iface *stfbi) 215{ 216 enum st_attachment_type statt; 217 218 statt = stfbi->visual->render_buffer; 219 if (statt != ST_ATTACHMENT_INVALID) { 220 /* use the buffer given by the visual, unless it is unavailable */ 221 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) { 222 switch (statt) { 223 case ST_ATTACHMENT_BACK_LEFT: 224 statt = ST_ATTACHMENT_FRONT_LEFT; 225 break; 226 case ST_ATTACHMENT_BACK_RIGHT: 227 statt = ST_ATTACHMENT_FRONT_RIGHT; 228 break; 229 default: 230 break; 231 } 232 233 if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) 234 statt = ST_ATTACHMENT_INVALID; 235 } 236 } 237 238 return statt; 239} 240 241/** 242 * Bind the context to the given framebuffers. 243 */ 244static boolean 245vg_context_bind_framebuffers(struct st_context_iface *stctxi, 246 struct st_framebuffer_iface *stdrawi, 247 struct st_framebuffer_iface *streadi) 248{ 249 struct vg_context *ctx = (struct vg_context *) stctxi; 250 struct st_framebuffer *stfb; 251 enum st_attachment_type strb_att; 252 253 /* the draw and read framebuffers must be the same */ 254 if (stdrawi != streadi) 255 return FALSE; 256 257 p_atomic_set(&ctx->draw_buffer_invalid, TRUE); 258 259 strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID; 260 261 if (ctx->draw_buffer) { 262 stfb = ctx->draw_buffer; 263 264 /* free the existing fb */ 265 if (!stdrawi || 266 stfb->strb_att != strb_att || 267 stfb->strb->format != stdrawi->visual->color_format) { 268 destroy_renderbuffer(stfb->strb); 269 destroy_renderbuffer(stfb->dsrb); 270 FREE(stfb); 271 272 ctx->draw_buffer = NULL; 273 } 274 } 275 276 if (!stdrawi) 277 return TRUE; 278 279 if (strb_att == ST_ATTACHMENT_INVALID) 280 return FALSE; 281 282 /* create a new fb */ 283 if (!ctx->draw_buffer) { 284 stfb = CALLOC_STRUCT(st_framebuffer); 285 if (!stfb) 286 return FALSE; 287 288 stfb->strb = create_renderbuffer(stdrawi->visual->color_format); 289 if (!stfb->strb) { 290 FREE(stfb); 291 return FALSE; 292 } 293 294 stfb->dsrb = create_renderbuffer(ctx->ds_format); 295 if (!stfb->dsrb) { 296 FREE(stfb->strb); 297 FREE(stfb); 298 return FALSE; 299 } 300 301 stfb->width = 0; 302 stfb->height = 0; 303 stfb->strb_att = strb_att; 304 305 ctx->draw_buffer = stfb; 306 } 307 308 ctx->draw_buffer->iface = stdrawi; 309 310 return TRUE; 311} 312 313static boolean 314vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi, 315 struct st_framebuffer_iface *stdrawi, 316 struct st_framebuffer_iface *streadi) 317{ 318 struct vg_context *ctx = (struct vg_context *) stctxi; 319 320 if (stctxi) 321 vg_context_bind_framebuffers(stctxi, stdrawi, streadi); 322 vg_set_current_context(ctx); 323 324 return TRUE; 325} 326 327static struct st_context_iface * 328vg_api_get_current(struct st_api *stapi) 329{ 330 struct vg_context *ctx = vg_current_context(); 331 332 return (ctx) ? &ctx->iface : NULL; 333} 334 335static st_proc_t 336vg_api_get_proc_address(struct st_api *stapi, const char *procname) 337{ 338 return api_get_proc_address(procname); 339} 340 341static void 342vg_api_destroy(struct st_api *stapi) 343{ 344} 345 346static const struct st_api vg_api = { 347 "Vega " VEGA_VERSION_STRING, 348 ST_API_OPENVG, 349 ST_PROFILE_DEFAULT_MASK, 350 vg_api_destroy, 351 vg_api_get_proc_address, 352 vg_api_create_context, 353 vg_api_make_current, 354 vg_api_get_current, 355}; 356 357const struct st_api * 358vg_api_get(void) 359{ 360 return &vg_api; 361} 362