vg_context.c revision f8e0dd246b26281d31d4f37799985e27368ba2f4
1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * 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 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 **************************************************************************/ 26 27#include "vg_context.h" 28 29#include "paint.h" 30#include "renderer.h" 31#include "shaders_cache.h" 32#include "shader.h" 33#include "asm_util.h" 34#include "vg_manager.h" 35#include "api.h" 36#include "mask.h" 37 38#include "pipe/p_context.h" 39#include "util/u_inlines.h" 40 41#include "cso_cache/cso_context.h" 42 43#include "util/u_simple_shaders.h" 44#include "util/u_memory.h" 45#include "util/u_blit.h" 46#include "util/u_sampler.h" 47#include "util/u_format.h" 48 49struct vg_context *_vg_context = 0; 50 51struct vg_context * vg_current_context(void) 52{ 53 return _vg_context; 54} 55 56/** 57 * A depth/stencil rb will be needed regardless of what the visual says. 58 */ 59static boolean 60choose_depth_stencil_format(struct vg_context *ctx) 61{ 62 struct pipe_screen *screen = ctx->pipe->screen; 63 enum pipe_format formats[] = { 64 PIPE_FORMAT_Z24_UNORM_S8_USCALED, 65 PIPE_FORMAT_S8_USCALED_Z24_UNORM, 66 PIPE_FORMAT_NONE 67 }; 68 enum pipe_format *fmt; 69 70 for (fmt = formats; *fmt != PIPE_FORMAT_NONE; fmt++) { 71 if (screen->is_format_supported(screen, *fmt, 72 PIPE_TEXTURE_2D, 0, PIPE_BIND_DEPTH_STENCIL, 0)) 73 break; 74 } 75 76 ctx->ds_format = *fmt; 77 78 return (ctx->ds_format != PIPE_FORMAT_NONE); 79} 80 81void vg_set_current_context(struct vg_context *ctx) 82{ 83 _vg_context = ctx; 84 api_make_dispatch_current((ctx) ? ctx->dispatch : NULL); 85} 86 87struct vg_context * vg_create_context(struct pipe_context *pipe, 88 const void *visual, 89 struct vg_context *share) 90{ 91 struct vg_context *ctx; 92 93 ctx = CALLOC_STRUCT(vg_context); 94 95 ctx->pipe = pipe; 96 if (!choose_depth_stencil_format(ctx)) { 97 FREE(ctx); 98 return NULL; 99 } 100 101 ctx->dispatch = api_create_dispatch(); 102 103 vg_init_state(&ctx->state.vg); 104 ctx->state.dirty = ALL_DIRTY; 105 106 ctx->cso_context = cso_create_context(pipe); 107 108 ctx->default_paint = paint_create(ctx); 109 ctx->state.vg.stroke_paint = ctx->default_paint; 110 ctx->state.vg.fill_paint = ctx->default_paint; 111 112 113 ctx->mask.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 114 ctx->mask.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 115 ctx->mask.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 116 ctx->mask.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 117 ctx->mask.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 118 ctx->mask.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 119 ctx->mask.sampler.normalized_coords = 0; 120 121 ctx->blend_sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 122 ctx->blend_sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 123 ctx->blend_sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 124 ctx->blend_sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 125 ctx->blend_sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 126 ctx->blend_sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 127 ctx->blend_sampler.normalized_coords = 0; 128 129 vg_set_error(ctx, VG_NO_ERROR); 130 131 ctx->owned_objects[VG_OBJECT_PAINT] = cso_hash_create(); 132 ctx->owned_objects[VG_OBJECT_IMAGE] = cso_hash_create(); 133 ctx->owned_objects[VG_OBJECT_MASK] = cso_hash_create(); 134 ctx->owned_objects[VG_OBJECT_FONT] = cso_hash_create(); 135 ctx->owned_objects[VG_OBJECT_PATH] = cso_hash_create(); 136 137 ctx->renderer = renderer_create(ctx); 138 ctx->sc = shaders_cache_create(ctx); 139 ctx->shader = shader_create(ctx); 140 141 ctx->blit = util_create_blit(ctx->pipe, ctx->cso_context); 142 143 return ctx; 144} 145 146void vg_destroy_context(struct vg_context *ctx) 147{ 148 struct pipe_resource **cbuf = &ctx->mask.cbuf; 149 150 util_destroy_blit(ctx->blit); 151 renderer_destroy(ctx->renderer); 152 shaders_cache_destroy(ctx->sc); 153 shader_destroy(ctx->shader); 154 paint_destroy(ctx->default_paint); 155 156 if (*cbuf) 157 pipe_resource_reference(cbuf, NULL); 158 159 if (ctx->mask.union_fs) 160 vg_shader_destroy(ctx, ctx->mask.union_fs); 161 if (ctx->mask.intersect_fs) 162 vg_shader_destroy(ctx, ctx->mask.intersect_fs); 163 if (ctx->mask.subtract_fs) 164 vg_shader_destroy(ctx, ctx->mask.subtract_fs); 165 if (ctx->mask.set_fs) 166 vg_shader_destroy(ctx, ctx->mask.set_fs); 167 168 cso_release_all(ctx->cso_context); 169 cso_destroy_context(ctx->cso_context); 170 171 cso_hash_delete(ctx->owned_objects[VG_OBJECT_PAINT]); 172 cso_hash_delete(ctx->owned_objects[VG_OBJECT_IMAGE]); 173 cso_hash_delete(ctx->owned_objects[VG_OBJECT_MASK]); 174 cso_hash_delete(ctx->owned_objects[VG_OBJECT_FONT]); 175 cso_hash_delete(ctx->owned_objects[VG_OBJECT_PATH]); 176 177 api_destroy_dispatch(ctx->dispatch); 178 179 FREE(ctx); 180} 181 182void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type) 183{ 184 obj->type = type; 185 obj->ctx = ctx; 186} 187 188VGboolean vg_context_is_object_valid(struct vg_context *ctx, 189 enum vg_object_type type, 190 void *ptr) 191{ 192 if (ctx) { 193 struct cso_hash *hash = ctx->owned_objects[type]; 194 if (!hash) 195 return VG_FALSE; 196 return cso_hash_contains(hash, (unsigned)(long)ptr); 197 } 198 return VG_FALSE; 199} 200 201void vg_context_add_object(struct vg_context *ctx, 202 enum vg_object_type type, 203 void *ptr) 204{ 205 if (ctx) { 206 struct cso_hash *hash = ctx->owned_objects[type]; 207 if (!hash) 208 return; 209 cso_hash_insert(hash, (unsigned)(long)ptr, ptr); 210 } 211} 212 213void vg_context_remove_object(struct vg_context *ctx, 214 enum vg_object_type type, 215 void *ptr) 216{ 217 if (ctx) { 218 struct cso_hash *hash = ctx->owned_objects[type]; 219 if (!hash) 220 return; 221 cso_hash_take(hash, (unsigned)(long)ptr); 222 } 223} 224 225static struct pipe_resource * 226create_texture(struct pipe_context *pipe, enum pipe_format format, 227 VGint width, VGint height) 228{ 229 struct pipe_resource templ; 230 231 memset(&templ, 0, sizeof(templ)); 232 233 if (format != PIPE_FORMAT_NONE) { 234 templ.format = format; 235 } 236 else { 237 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM; 238 } 239 240 templ.target = PIPE_TEXTURE_2D; 241 templ.width0 = width; 242 templ.height0 = height; 243 templ.depth0 = 1; 244 templ.last_level = 0; 245 246 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) { 247 templ.bind = PIPE_BIND_DEPTH_STENCIL; 248 } else { 249 templ.bind = (PIPE_BIND_DISPLAY_TARGET | 250 PIPE_BIND_RENDER_TARGET | 251 PIPE_BIND_SAMPLER_VIEW); 252 } 253 254 return pipe->screen->resource_create(pipe->screen, &templ); 255} 256 257static struct pipe_sampler_view * 258create_tex_and_view(struct pipe_context *pipe, enum pipe_format format, 259 VGint width, VGint height) 260{ 261 struct pipe_resource *texture; 262 struct pipe_sampler_view view_templ; 263 struct pipe_sampler_view *view; 264 265 texture = create_texture(pipe, format, width, height); 266 267 if (!texture) 268 return NULL; 269 270 u_sampler_view_default_template(&view_templ, texture, texture->format); 271 view = pipe->create_sampler_view(pipe, texture, &view_templ); 272 /* want the texture to go away if the view is freed */ 273 pipe_resource_reference(&texture, NULL); 274 275 return view; 276} 277 278static void 279vg_context_update_alpha_mask_view(struct vg_context *ctx, 280 uint width, uint height) 281{ 282 struct st_framebuffer *stfb = ctx->draw_buffer; 283 struct pipe_sampler_view *old_sampler_view = stfb->alpha_mask_view; 284 struct pipe_context *pipe = ctx->pipe; 285 286 if (old_sampler_view && 287 old_sampler_view->texture->width0 == width && 288 old_sampler_view->texture->height0 == height) 289 return; 290 291 /* 292 we use PIPE_FORMAT_B8G8R8A8_UNORM because we want to render to 293 this texture and use it as a sampler, so while this wastes some 294 space it makes both of those a lot simpler 295 */ 296 stfb->alpha_mask_view = create_tex_and_view(pipe, 297 PIPE_FORMAT_B8G8R8A8_UNORM, width, height); 298 299 if (!stfb->alpha_mask_view) { 300 if (old_sampler_view) 301 pipe_sampler_view_reference(&old_sampler_view, NULL); 302 return; 303 } 304 305 /* XXX could this call be avoided? */ 306 vg_validate_state(ctx); 307 308 /* alpha mask starts with 1.f alpha */ 309 mask_fill(0, 0, width, height, 1.f); 310 311 /* if we had an old surface copy it over */ 312 if (old_sampler_view) { 313 struct pipe_subresource subsurf, subold_surf; 314 subsurf.face = 0; 315 subsurf.level = 0; 316 subold_surf.face = 0; 317 subold_surf.level = 0; 318 pipe->resource_copy_region(pipe, 319 stfb->alpha_mask_view->texture, 320 subsurf, 321 0, 0, 0, 322 old_sampler_view->texture, 323 subold_surf, 324 0, 0, 0, 325 MIN2(old_sampler_view->texture->width0, 326 stfb->alpha_mask_view->texture->width0), 327 MIN2(old_sampler_view->texture->height0, 328 stfb->alpha_mask_view->texture->height0)); 329 } 330 331 /* Free the old texture 332 */ 333 if (old_sampler_view) 334 pipe_sampler_view_reference(&old_sampler_view, NULL); 335} 336 337static void 338vg_context_update_blend_texture_view(struct vg_context *ctx, 339 uint width, uint height) 340{ 341 struct pipe_context *pipe = ctx->pipe; 342 struct st_framebuffer *stfb = ctx->draw_buffer; 343 struct pipe_sampler_view *old = stfb->blend_texture_view; 344 345 if (old && 346 old->texture->width0 == width && 347 old->texture->height0 == height) 348 return; 349 350 stfb->blend_texture_view = create_tex_and_view(pipe, 351 PIPE_FORMAT_B8G8R8A8_UNORM, width, height); 352 353 pipe_sampler_view_reference(&old, NULL); 354} 355 356static boolean 357vg_context_update_depth_stencil_rb(struct vg_context * ctx, 358 uint width, uint height) 359{ 360 struct st_renderbuffer *dsrb = ctx->draw_buffer->dsrb; 361 struct pipe_context *pipe = ctx->pipe; 362 unsigned surface_usage; 363 364 if ((dsrb->width == width && dsrb->height == height) && dsrb->texture) 365 return FALSE; 366 367 /* unreference existing ones */ 368 pipe_surface_reference(&dsrb->surface, NULL); 369 pipe_resource_reference(&dsrb->texture, NULL); 370 dsrb->width = dsrb->height = 0; 371 372 /* Probably need dedicated flags for surface usage too: 373 */ 374 surface_usage = PIPE_BIND_DEPTH_STENCIL; /* XXX: was: RENDER_TARGET */ 375 376 dsrb->texture = create_texture(pipe, dsrb->format, width, height); 377 if (!dsrb->texture) 378 return TRUE; 379 380 dsrb->surface = pipe->screen->get_tex_surface(pipe->screen, 381 dsrb->texture, 382 0, 0, 0, 383 surface_usage); 384 if (!dsrb->surface) { 385 pipe_resource_reference(&dsrb->texture, NULL); 386 return TRUE; 387 } 388 389 dsrb->width = width; 390 dsrb->height = height; 391 392 assert(dsrb->surface->width == width); 393 assert(dsrb->surface->height == height); 394 395 return TRUE; 396} 397 398void vg_validate_state(struct vg_context *ctx) 399{ 400 struct st_framebuffer *stfb = ctx->draw_buffer; 401 402 vg_manager_validate_framebuffer(ctx); 403 404 if (vg_context_update_depth_stencil_rb(ctx, stfb->width, stfb->height)) 405 ctx->state.dirty |= DEPTH_STENCIL_DIRTY; 406 407 /* TODO create as needed */ 408 vg_context_update_alpha_mask_view(ctx, stfb->width, stfb->height); 409 vg_context_update_blend_texture_view(ctx, stfb->width, stfb->height); 410 411 renderer_validate(ctx->renderer, ctx->state.dirty, 412 ctx->draw_buffer, &ctx->state.vg); 413 414 ctx->state.dirty = NONE_DIRTY; 415 416 shader_set_masking(ctx->shader, ctx->state.vg.masking); 417 shader_set_image_mode(ctx->shader, ctx->state.vg.image_mode); 418 shader_set_color_transform(ctx->shader, ctx->state.vg.color_transform); 419} 420 421VGboolean vg_object_is_valid(void *ptr, enum vg_object_type type) 422{ 423 struct vg_object *obj = ptr; 424 if (ptr && is_aligned(obj) && obj->type == type) 425 return VG_TRUE; 426 else 427 return VG_FALSE; 428} 429 430void vg_set_error(struct vg_context *ctx, 431 VGErrorCode code) 432{ 433 /*vgGetError returns the oldest error code provided by 434 * an API call on the current context since the previous 435 * call to vgGetError on that context (or since the creation 436 of the context).*/ 437 if (ctx->_error == VG_NO_ERROR) 438 ctx->_error = code; 439} 440 441void vg_prepare_blend_surface(struct vg_context *ctx) 442{ 443 struct pipe_surface *dest_surface = NULL; 444 struct pipe_context *pipe = ctx->pipe; 445 struct pipe_sampler_view *view; 446 struct pipe_sampler_view view_templ; 447 struct st_framebuffer *stfb = ctx->draw_buffer; 448 struct st_renderbuffer *strb = stfb->strb; 449 450 /* first finish all pending rendering */ 451 vgFinish(); 452 453 u_sampler_view_default_template(&view_templ, strb->texture, strb->texture->format); 454 view = pipe->create_sampler_view(pipe, strb->texture, &view_templ); 455 456 dest_surface = pipe->screen->get_tex_surface(pipe->screen, 457 stfb->blend_texture_view->texture, 458 0, 0, 0, 459 PIPE_BIND_RENDER_TARGET); 460 util_blit_pixels_tex(ctx->blit, 461 view, 462 0, 0, 463 strb->width, strb->height, 464 dest_surface, 465 0, 0, 466 strb->width, strb->height, 467 0.0, PIPE_TEX_MIPFILTER_NEAREST); 468 469 if (dest_surface) 470 pipe_surface_reference(&dest_surface, NULL); 471 472 /* make sure it's complete */ 473 vgFinish(); 474 475 pipe_sampler_view_reference(&view, NULL); 476} 477 478 479void vg_prepare_blend_surface_from_mask(struct vg_context *ctx) 480{ 481 struct pipe_surface *dest_surface = NULL; 482 struct pipe_context *pipe = ctx->pipe; 483 struct st_framebuffer *stfb = ctx->draw_buffer; 484 struct st_renderbuffer *strb = stfb->strb; 485 486 vg_validate_state(ctx); 487 488 /* first finish all pending rendering */ 489 vgFinish(); 490 491 dest_surface = pipe->screen->get_tex_surface(pipe->screen, 492 stfb->blend_texture_view->texture, 493 0, 0, 0, 494 PIPE_BIND_RENDER_TARGET); 495 496 util_blit_pixels_tex(ctx->blit, 497 stfb->alpha_mask_view, 498 0, 0, 499 strb->width, strb->height, 500 dest_surface, 501 0, 0, 502 strb->width, strb->height, 503 0.0, PIPE_TEX_MIPFILTER_NEAREST); 504 505 /* make sure it's complete */ 506 vgFinish(); 507 508 if (dest_surface) 509 pipe_surface_reference(&dest_surface, NULL); 510} 511 512/** 513 * A transformation from window coordinates to paint coordinates. 514 */ 515VGboolean vg_get_paint_matrix(struct vg_context *ctx, 516 const struct matrix *paint_to_user, 517 const struct matrix *user_to_surface, 518 struct matrix *mat) 519{ 520 struct matrix tmp; 521 522 /* get user-to-paint matrix */ 523 memcpy(mat, paint_to_user, sizeof(*paint_to_user)); 524 if (!matrix_invert(mat)) 525 return VG_FALSE; 526 527 /* get surface-to-user matrix */ 528 memcpy(&tmp, user_to_surface, sizeof(*user_to_surface)); 529 if (!matrix_invert(&tmp)) 530 return VG_FALSE; 531 532 matrix_mult(mat, &tmp); 533 534 return VG_TRUE; 535} 536