/************************************************************************** * * Copyright 2009 VMware, Inc. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sub license, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice (including the * next paragraph) shall be included in all copies or substantial portions * of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **************************************************************************/ #include "vg_context.h" #include "paint.h" #include "renderer.h" #include "shaders_cache.h" #include "shader.h" #include "asm_util.h" #include "st_inlines.h" #include "vg_manager.h" #include "api.h" #include "pipe/p_context.h" #include "util/u_inlines.h" #include "cso_cache/cso_context.h" #include "util/u_simple_shaders.h" #include "util/u_memory.h" #include "util/u_blit.h" #include "util/u_sampler.h" struct vg_context *_vg_context = 0; struct vg_context * vg_current_context(void) { return _vg_context; } static void init_clear(struct vg_context *st) { struct pipe_context *pipe = st->pipe; /* rasterizer state: bypass clipping */ memset(&st->clear.raster, 0, sizeof(st->clear.raster)); st->clear.raster.gl_rasterization_rules = 1; /* fragment shader state: color pass-through program */ st->clear.fs = util_make_fragment_passthrough_shader(pipe); } /** * A depth/stencil rb will be needed regardless of what the visual says. */ static boolean choose_depth_stencil_format(struct vg_context *ctx) { struct pipe_screen *screen = ctx->pipe->screen; enum pipe_format formats[] = { PIPE_FORMAT_Z24_UNORM_S8_USCALED, PIPE_FORMAT_S8_USCALED_Z24_UNORM, PIPE_FORMAT_NONE }; enum pipe_format *fmt; for (fmt = formats; *fmt != PIPE_FORMAT_NONE; fmt++) { if (screen->is_format_supported(screen, *fmt, PIPE_TEXTURE_2D, 0, PIPE_BIND_DEPTH_STENCIL, 0)) break; } ctx->ds_format = *fmt; return (ctx->ds_format != PIPE_FORMAT_NONE); } void vg_set_current_context(struct vg_context *ctx) { _vg_context = ctx; api_make_dispatch_current((ctx) ? ctx->dispatch : NULL); } struct vg_context * vg_create_context(struct pipe_context *pipe, const void *visual, struct vg_context *share) { struct vg_context *ctx; unsigned i; ctx = CALLOC_STRUCT(vg_context); ctx->pipe = pipe; if (!choose_depth_stencil_format(ctx)) { FREE(ctx); return NULL; } ctx->dispatch = api_create_dispatch(); vg_init_state(&ctx->state.vg); ctx->state.dirty = ALL_DIRTY; ctx->cso_context = cso_create_context(pipe); init_clear(ctx); ctx->default_paint = paint_create(ctx); ctx->state.vg.stroke_paint = ctx->default_paint; ctx->state.vg.fill_paint = ctx->default_paint; ctx->mask.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->mask.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->mask.sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->mask.sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; ctx->mask.sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; ctx->mask.sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; ctx->mask.sampler.normalized_coords = 0; ctx->blend_sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->blend_sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->blend_sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; ctx->blend_sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; ctx->blend_sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; ctx->blend_sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; ctx->blend_sampler.normalized_coords = 0; for (i = 0; i < 2; i++) { ctx->velems[i].src_offset = i * 4 * sizeof(float); ctx->velems[i].instance_divisor = 0; ctx->velems[i].vertex_buffer_index = 0; ctx->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; } vg_set_error(ctx, VG_NO_ERROR); ctx->owned_objects[VG_OBJECT_PAINT] = cso_hash_create(); ctx->owned_objects[VG_OBJECT_IMAGE] = cso_hash_create(); ctx->owned_objects[VG_OBJECT_MASK] = cso_hash_create(); ctx->owned_objects[VG_OBJECT_FONT] = cso_hash_create(); ctx->owned_objects[VG_OBJECT_PATH] = cso_hash_create(); ctx->renderer = renderer_create(ctx); ctx->sc = shaders_cache_create(ctx); ctx->shader = shader_create(ctx); ctx->blit = util_create_blit(ctx->pipe, ctx->cso_context); return ctx; } void vg_destroy_context(struct vg_context *ctx) { struct pipe_resource **cbuf = &ctx->mask.cbuf; struct pipe_resource **vsbuf = &ctx->vs_const_buffer; util_destroy_blit(ctx->blit); renderer_destroy(ctx->renderer); shaders_cache_destroy(ctx->sc); shader_destroy(ctx->shader); paint_destroy(ctx->default_paint); if (*cbuf) pipe_resource_reference(cbuf, NULL); if (*vsbuf) pipe_resource_reference(vsbuf, NULL); if (ctx->clear.fs) { cso_delete_fragment_shader(ctx->cso_context, ctx->clear.fs); ctx->clear.fs = NULL; } if (ctx->plain_vs) { vg_shader_destroy(ctx, ctx->plain_vs); ctx->plain_vs = NULL; } if (ctx->clear_vs) { vg_shader_destroy(ctx, ctx->clear_vs); ctx->clear_vs = NULL; } if (ctx->texture_vs) { vg_shader_destroy(ctx, ctx->texture_vs); ctx->texture_vs = NULL; } if (ctx->pass_through_depth_fs) vg_shader_destroy(ctx, ctx->pass_through_depth_fs); if (ctx->mask.union_fs) vg_shader_destroy(ctx, ctx->mask.union_fs); if (ctx->mask.intersect_fs) vg_shader_destroy(ctx, ctx->mask.intersect_fs); if (ctx->mask.subtract_fs) vg_shader_destroy(ctx, ctx->mask.subtract_fs); if (ctx->mask.set_fs) vg_shader_destroy(ctx, ctx->mask.set_fs); cso_release_all(ctx->cso_context); cso_destroy_context(ctx->cso_context); cso_hash_delete(ctx->owned_objects[VG_OBJECT_PAINT]); cso_hash_delete(ctx->owned_objects[VG_OBJECT_IMAGE]); cso_hash_delete(ctx->owned_objects[VG_OBJECT_MASK]); cso_hash_delete(ctx->owned_objects[VG_OBJECT_FONT]); cso_hash_delete(ctx->owned_objects[VG_OBJECT_PATH]); api_destroy_dispatch(ctx->dispatch); FREE(ctx); } void vg_init_object(struct vg_object *obj, struct vg_context *ctx, enum vg_object_type type) { obj->type = type; obj->ctx = ctx; } VGboolean vg_context_is_object_valid(struct vg_context *ctx, enum vg_object_type type, void *ptr) { if (ctx) { struct cso_hash *hash = ctx->owned_objects[type]; if (!hash) return VG_FALSE; return cso_hash_contains(hash, (unsigned)(long)ptr); } return VG_FALSE; } void vg_context_add_object(struct vg_context *ctx, enum vg_object_type type, void *ptr) { if (ctx) { struct cso_hash *hash = ctx->owned_objects[type]; if (!hash) return; cso_hash_insert(hash, (unsigned)(long)ptr, ptr); } } void vg_context_remove_object(struct vg_context *ctx, enum vg_object_type type, void *ptr) { if (ctx) { struct cso_hash *hash = ctx->owned_objects[type]; if (!hash) return; cso_hash_take(hash, (unsigned)(long)ptr); } } void vg_validate_state(struct vg_context *ctx) { vg_manager_validate_framebuffer(ctx); renderer_validate(ctx->renderer, ctx->state.dirty, ctx->draw_buffer, &ctx->state.vg); ctx->state.dirty = NONE_DIRTY; shader_set_masking(ctx->shader, ctx->state.vg.masking); shader_set_image_mode(ctx->shader, ctx->state.vg.image_mode); } VGboolean vg_object_is_valid(void *ptr, enum vg_object_type type) { struct vg_object *obj = ptr; if (ptr && is_aligned(obj) && obj->type == type) return VG_TRUE; else return VG_FALSE; } void vg_set_error(struct vg_context *ctx, VGErrorCode code) { /*vgGetError returns the oldest error code provided by * an API call on the current context since the previous * call to vgGetError on that context (or since the creation of the context).*/ if (ctx->_error == VG_NO_ERROR) ctx->_error = code; } void vg_prepare_blend_surface(struct vg_context *ctx) { struct pipe_surface *dest_surface = NULL; struct pipe_context *pipe = ctx->pipe; struct pipe_sampler_view *view; struct pipe_sampler_view view_templ; struct st_framebuffer *stfb = ctx->draw_buffer; struct st_renderbuffer *strb = stfb->strb; /* first finish all pending rendering */ vgFinish(); u_sampler_view_default_template(&view_templ, strb->texture, strb->texture->format); view = pipe->create_sampler_view(pipe, strb->texture, &view_templ); dest_surface = pipe->screen->get_tex_surface(pipe->screen, stfb->blend_texture_view->texture, 0, 0, 0, PIPE_BIND_RENDER_TARGET); util_blit_pixels_tex(ctx->blit, view, 0, 0, strb->width, strb->height, dest_surface, 0, 0, strb->width, strb->height, 0.0, PIPE_TEX_MIPFILTER_NEAREST); if (dest_surface) pipe_surface_reference(&dest_surface, NULL); /* make sure it's complete */ vgFinish(); pipe_sampler_view_reference(&view, NULL); } void vg_prepare_blend_surface_from_mask(struct vg_context *ctx) { struct pipe_surface *dest_surface = NULL; struct pipe_context *pipe = ctx->pipe; struct st_framebuffer *stfb = ctx->draw_buffer; struct st_renderbuffer *strb = stfb->strb; vg_validate_state(ctx); /* first finish all pending rendering */ vgFinish(); dest_surface = pipe->screen->get_tex_surface(pipe->screen, stfb->blend_texture_view->texture, 0, 0, 0, PIPE_BIND_RENDER_TARGET); /* flip it, because we want to use it as a sampler */ util_blit_pixels_tex(ctx->blit, stfb->alpha_mask_view, 0, strb->height, strb->width, 0, dest_surface, 0, 0, strb->width, strb->height, 0.0, PIPE_TEX_MIPFILTER_NEAREST); /* make sure it's complete */ vgFinish(); if (dest_surface) pipe_surface_reference(&dest_surface, NULL); } void * vg_plain_vs(struct vg_context *ctx) { if (!ctx->plain_vs) { ctx->plain_vs = shader_create_from_text(ctx->pipe, vs_plain_asm, 200, PIPE_SHADER_VERTEX); } return ctx->plain_vs->driver; } void * vg_clear_vs(struct vg_context *ctx) { if (!ctx->clear_vs) { ctx->clear_vs = shader_create_from_text(ctx->pipe, vs_clear_asm, 200, PIPE_SHADER_VERTEX); } return ctx->clear_vs->driver; } void * vg_texture_vs(struct vg_context *ctx) { if (!ctx->texture_vs) { ctx->texture_vs = shader_create_from_text(ctx->pipe, vs_texture_asm, 200, PIPE_SHADER_VERTEX); } return ctx->texture_vs->driver; }