dri_context.c revision d18152028e1825c05c7de33acacee3336350a096
1/************************************************************************** 2 * 3 * Copyright 2009, VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27/* 28 * Author: Keith Whitwell <keithw@vmware.com> 29 * Author: Jakob Bornecrantz <wallbraker@gmail.com> 30 */ 31 32#include "utils.h" 33 34#include "dri_screen.h" 35#include "dri_drawable.h" 36#include "dri_context.h" 37 38#include "pipe/p_context.h" 39#include "state_tracker/st_context.h" 40 41static void 42dri_pp_query(struct dri_context *ctx) 43{ 44 unsigned int i; 45 46 for (i = 0; i < PP_FILTERS; i++) { 47 ctx->pp_enabled[i] = driQueryOptioni(&ctx->optionCache, pp_filters[i].name); 48 } 49} 50 51GLboolean 52dri_create_context(gl_api api, const struct gl_config * visual, 53 __DRIcontext * cPriv, void *sharedContextPrivate) 54{ 55 __DRIscreen *sPriv = cPriv->driScreenPriv; 56 struct dri_screen *screen = dri_screen(sPriv); 57 struct st_api *stapi = screen->st_api; 58 struct dri_context *ctx = NULL; 59 struct st_context_iface *st_share = NULL; 60 struct st_context_attribs attribs; 61 enum st_context_error ctx_err = 0; 62 63 memset(&attribs, 0, sizeof(attribs)); 64 switch (api) { 65 case API_OPENGLES: 66 attribs.profile = ST_PROFILE_OPENGL_ES1; 67 break; 68 case API_OPENGLES2: 69 attribs.profile = ST_PROFILE_OPENGL_ES2; 70 break; 71 default: 72 attribs.profile = ST_PROFILE_DEFAULT; 73 break; 74 } 75 76 if (sharedContextPrivate) { 77 st_share = ((struct dri_context *)sharedContextPrivate)->st; 78 } 79 80 ctx = CALLOC_STRUCT(dri_context); 81 if (ctx == NULL) 82 goto fail; 83 84 cPriv->driverPrivate = ctx; 85 ctx->cPriv = cPriv; 86 ctx->sPriv = sPriv; 87 88 driParseConfigFiles(&ctx->optionCache, 89 &screen->optionCache, sPriv->myNum, "dri"); 90 91 dri_fill_st_visual(&attribs.visual, screen, visual); 92 ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err, 93 st_share); 94 if (ctx->st == NULL) 95 goto fail; 96 ctx->st->st_manager_private = (void *) ctx; 97 ctx->stapi = stapi; 98 99 // Context successfully created. See if post-processing is requested. 100 dri_pp_query(ctx); 101 102 ctx->pp = pp_init(screen->base.screen, ctx->pp_enabled); 103 104 return GL_TRUE; 105 106 fail: 107 if (ctx && ctx->st) 108 ctx->st->destroy(ctx->st); 109 110 FREE(ctx); 111 return GL_FALSE; 112} 113 114void 115dri_destroy_context(__DRIcontext * cPriv) 116{ 117 struct dri_context *ctx = dri_context(cPriv); 118 119 /* note: we are freeing values and nothing more because 120 * driParseConfigFiles allocated values only - the rest 121 * is owned by screen optionCache. 122 */ 123 FREE(ctx->optionCache.values); 124 125 /* No particular reason to wait for command completion before 126 * destroying a context, but it is probably worthwhile flushing it 127 * to avoid having to add code elsewhere to cope with flushing a 128 * partially destroyed context. 129 */ 130 ctx->st->flush(ctx->st, 0, NULL); 131 ctx->st->destroy(ctx->st); 132 133 if (ctx->pp) pp_free(ctx->pp); 134 135 FREE(ctx); 136} 137 138GLboolean 139dri_unbind_context(__DRIcontext * cPriv) 140{ 141 /* dri_util.c ensures cPriv is not null */ 142 struct dri_screen *screen = dri_screen(cPriv->driScreenPriv); 143 struct dri_context *ctx = dri_context(cPriv); 144 struct st_api *stapi = screen->st_api; 145 146 if (--ctx->bind_count == 0) { 147 if (ctx->st == ctx->stapi->get_current(ctx->stapi)) { 148 ctx->st->flush(ctx->st, ST_FLUSH_FRONT, NULL); 149 stapi->make_current(stapi, NULL, NULL, NULL); 150 } 151 } 152 153 return GL_TRUE; 154} 155 156GLboolean 157dri_make_current(__DRIcontext * cPriv, 158 __DRIdrawable * driDrawPriv, 159 __DRIdrawable * driReadPriv) 160{ 161 /* dri_util.c ensures cPriv is not null */ 162 struct dri_context *ctx = dri_context(cPriv); 163 struct dri_drawable *draw = dri_drawable(driDrawPriv); 164 struct dri_drawable *read = dri_drawable(driReadPriv); 165 struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi); 166 167 if (old_st && old_st != ctx->st) 168 old_st->flush(old_st, ST_FLUSH_FRONT, NULL); 169 170 ++ctx->bind_count; 171 172 if (!driDrawPriv && !driReadPriv) 173 return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL); 174 else if (!driDrawPriv || !driReadPriv) 175 return GL_FALSE; 176 177 if (ctx->dPriv != driDrawPriv) { 178 ctx->dPriv = driDrawPriv; 179 draw->texture_stamp = driDrawPriv->lastStamp - 1; 180 } 181 if (ctx->rPriv != driReadPriv) { 182 ctx->rPriv = driReadPriv; 183 read->texture_stamp = driReadPriv->lastStamp - 1; 184 } 185 186 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base); 187 188 // This is ok to call here. If they are already init, it's a no-op. 189 if (draw->textures[ST_ATTACHMENT_BACK_LEFT] && draw->textures[ST_ATTACHMENT_DEPTH_STENCIL] 190 && ctx->pp) 191 pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0, 192 draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0, 193 draw->textures[ST_ATTACHMENT_DEPTH_STENCIL]); 194 195 return GL_TRUE; 196} 197 198struct dri_context * 199dri_get_current(__DRIscreen *sPriv) 200{ 201 struct dri_screen *screen = dri_screen(sPriv); 202 struct st_api *stapi = screen->st_api; 203 struct st_context_iface *st; 204 205 st = stapi->get_current(stapi); 206 207 return (struct dri_context *) (st) ? st->st_manager_private : NULL; 208} 209 210/* vim: set sw=3 ts=8 sts=3 expandtab: */ 211