dri_context.c revision 7ec681f3
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#include "frontend/drm_driver.h" 38 39#include "pipe/p_context.h" 40#include "pipe-loader/pipe_loader.h" 41#include "state_tracker/st_context.h" 42 43#include "util/u_memory.h" 44 45GLboolean 46dri_create_context(gl_api api, const struct gl_config * visual, 47 __DRIcontext * cPriv, 48 const struct __DriverContextConfig *ctx_config, 49 unsigned *error, 50 void *sharedContextPrivate) 51{ 52 __DRIscreen *sPriv = cPriv->driScreenPriv; 53 struct dri_screen *screen = dri_screen(sPriv); 54 struct st_api *stapi = screen->st_api; 55 struct dri_context *ctx = NULL; 56 struct st_context_iface *st_share = NULL; 57 struct st_context_attribs attribs; 58 enum st_context_error ctx_err = 0; 59 unsigned allowed_flags = __DRI_CTX_FLAG_DEBUG | 60 __DRI_CTX_FLAG_FORWARD_COMPATIBLE | 61 __DRI_CTX_FLAG_NO_ERROR; 62 unsigned allowed_attribs = 63 __DRIVER_CONTEXT_ATTRIB_PRIORITY | 64 __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR; 65 const __DRIbackgroundCallableExtension *backgroundCallable = 66 screen->sPriv->dri2.backgroundCallable; 67 const struct driOptionCache *optionCache = &screen->dev->option_cache; 68 69 if (screen->has_reset_status_query) { 70 allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS; 71 allowed_attribs |= __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY; 72 } 73 74 if (ctx_config->flags & ~allowed_flags) { 75 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG; 76 goto fail; 77 } 78 79 if (ctx_config->attribute_mask & ~allowed_attribs) { 80 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE; 81 goto fail; 82 } 83 84 memset(&attribs, 0, sizeof(attribs)); 85 switch (api) { 86 case API_OPENGLES: 87 attribs.profile = ST_PROFILE_OPENGL_ES1; 88 break; 89 case API_OPENGLES2: 90 attribs.profile = ST_PROFILE_OPENGL_ES2; 91 break; 92 case API_OPENGL_COMPAT: 93 case API_OPENGL_CORE: 94 if (driQueryOptionb(optionCache, "force_compat_profile")) { 95 attribs.profile = ST_PROFILE_DEFAULT; 96 } else { 97 attribs.profile = api == API_OPENGL_COMPAT ? ST_PROFILE_DEFAULT 98 : ST_PROFILE_OPENGL_CORE; 99 } 100 101 attribs.major = ctx_config->major_version; 102 attribs.minor = ctx_config->minor_version; 103 104 if ((ctx_config->flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) 105 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE; 106 break; 107 default: 108 *error = __DRI_CTX_ERROR_BAD_API; 109 goto fail; 110 } 111 112 if ((ctx_config->flags & __DRI_CTX_FLAG_DEBUG) != 0) 113 attribs.flags |= ST_CONTEXT_FLAG_DEBUG; 114 115 if (ctx_config->flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS) 116 attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS; 117 118 if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY) 119 if (ctx_config->reset_strategy != __DRI_CTX_RESET_NO_NOTIFICATION) 120 attribs.flags |= ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED; 121 122 if (ctx_config->flags & __DRI_CTX_FLAG_NO_ERROR) 123 attribs.flags |= ST_CONTEXT_FLAG_NO_ERROR; 124 125 if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_PRIORITY) { 126 switch (ctx_config->priority) { 127 case __DRI_CTX_PRIORITY_LOW: 128 attribs.flags |= ST_CONTEXT_FLAG_LOW_PRIORITY; 129 break; 130 case __DRI_CTX_PRIORITY_HIGH: 131 attribs.flags |= ST_CONTEXT_FLAG_HIGH_PRIORITY; 132 break; 133 default: 134 break; 135 } 136 } 137 138 if ((ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR) 139 && (ctx_config->release_behavior == __DRI_CTX_RELEASE_BEHAVIOR_NONE)) 140 attribs.flags |= ST_CONTEXT_FLAG_RELEASE_NONE; 141 142 struct dri_context *share_ctx = NULL; 143 if (sharedContextPrivate) { 144 share_ctx = (struct dri_context *)sharedContextPrivate; 145 st_share = share_ctx->st; 146 } 147 148 ctx = CALLOC_STRUCT(dri_context); 149 if (ctx == NULL) { 150 *error = __DRI_CTX_ERROR_NO_MEMORY; 151 goto fail; 152 } 153 154 cPriv->driverPrivate = ctx; 155 ctx->cPriv = cPriv; 156 ctx->sPriv = sPriv; 157 158 if (driQueryOptionb(&screen->dev->option_cache, "mesa_no_error")) 159 attribs.flags |= ST_CONTEXT_FLAG_NO_ERROR; 160 161 attribs.options = screen->options; 162 dri_fill_st_visual(&attribs.visual, screen, visual); 163 ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err, 164 st_share); 165 if (ctx->st == NULL) { 166 switch (ctx_err) { 167 case ST_CONTEXT_SUCCESS: 168 *error = __DRI_CTX_ERROR_SUCCESS; 169 break; 170 case ST_CONTEXT_ERROR_NO_MEMORY: 171 *error = __DRI_CTX_ERROR_NO_MEMORY; 172 break; 173 case ST_CONTEXT_ERROR_BAD_API: 174 *error = __DRI_CTX_ERROR_BAD_API; 175 break; 176 case ST_CONTEXT_ERROR_BAD_VERSION: 177 *error = __DRI_CTX_ERROR_BAD_VERSION; 178 break; 179 case ST_CONTEXT_ERROR_BAD_FLAG: 180 *error = __DRI_CTX_ERROR_BAD_FLAG; 181 break; 182 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE: 183 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE; 184 break; 185 case ST_CONTEXT_ERROR_UNKNOWN_FLAG: 186 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG; 187 break; 188 } 189 goto fail; 190 } 191 ctx->st->st_manager_private = (void *) ctx; 192 ctx->stapi = stapi; 193 194 if (ctx->st->cso_context) { 195 ctx->pp = pp_init(ctx->st->pipe, screen->pp_enabled, ctx->st->cso_context, 196 ctx->st); 197 ctx->hud = hud_create(ctx->st->cso_context, ctx->st, 198 share_ctx ? share_ctx->hud : NULL); 199 } 200 201 /* Do this last. */ 202 if (ctx->st->start_thread && 203 driQueryOptionb(&screen->dev->option_cache, "mesa_glthread")) { 204 205 if (backgroundCallable && backgroundCallable->base.version >= 2 && 206 backgroundCallable->isThreadSafe) { 207 208 if (backgroundCallable->isThreadSafe(cPriv->loaderPrivate)) 209 ctx->st->start_thread(ctx->st); 210 else 211 fprintf(stderr, "dri_create_context: glthread isn't thread safe " 212 "- missing call XInitThreads\n"); 213 } else { 214 fprintf(stderr, "dri_create_context: requested glthread but driver " 215 "is missing backgroundCallable V2 extension\n"); 216 } 217 } 218 219 *error = __DRI_CTX_ERROR_SUCCESS; 220 return GL_TRUE; 221 222 fail: 223 if (ctx && ctx->st) 224 ctx->st->destroy(ctx->st); 225 226 free(ctx); 227 return GL_FALSE; 228} 229 230void 231dri_destroy_context(__DRIcontext * cPriv) 232{ 233 struct dri_context *ctx = dri_context(cPriv); 234 235 if (ctx->hud) { 236 hud_destroy(ctx->hud, ctx->st->cso_context); 237 } 238 239 if (ctx->pp) 240 pp_free(ctx->pp); 241 242 /* No particular reason to wait for command completion before 243 * destroying a context, but we flush the context here 244 * to avoid having to add code elsewhere to cope with flushing a 245 * partially destroyed context. 246 */ 247 ctx->st->flush(ctx->st, 0, NULL, NULL, NULL); 248 ctx->st->destroy(ctx->st); 249 free(ctx); 250} 251 252/* This is called inside MakeCurrent to unbind the context. */ 253GLboolean 254dri_unbind_context(__DRIcontext * cPriv) 255{ 256 /* dri_util.c ensures cPriv is not null */ 257 struct dri_screen *screen = dri_screen(cPriv->driScreenPriv); 258 struct dri_context *ctx = dri_context(cPriv); 259 struct st_context_iface *st = ctx->st; 260 struct st_api *stapi = screen->st_api; 261 262 if (--ctx->bind_count == 0) { 263 if (st == stapi->get_current(stapi)) { 264 if (st->thread_finish) 265 st->thread_finish(st); 266 267 /* Record HUD queries for the duration the context was "current". */ 268 if (ctx->hud) 269 hud_record_only(ctx->hud, st->pipe); 270 271 stapi->make_current(stapi, NULL, NULL, NULL); 272 } 273 } 274 ctx->dPriv = NULL; 275 ctx->rPriv = NULL; 276 277 return GL_TRUE; 278} 279 280GLboolean 281dri_make_current(__DRIcontext * cPriv, 282 __DRIdrawable * driDrawPriv, 283 __DRIdrawable * driReadPriv) 284{ 285 /* dri_util.c ensures cPriv is not null */ 286 struct dri_context *ctx = dri_context(cPriv); 287 struct dri_drawable *draw = dri_drawable(driDrawPriv); 288 struct dri_drawable *read = dri_drawable(driReadPriv); 289 290 ++ctx->bind_count; 291 292 if (!draw && !read) 293 return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL); 294 else if (!draw || !read) 295 return GL_FALSE; 296 297 if (ctx->dPriv != driDrawPriv) { 298 ctx->dPriv = driDrawPriv; 299 draw->texture_stamp = driDrawPriv->lastStamp - 1; 300 } 301 if (ctx->rPriv != driReadPriv) { 302 ctx->rPriv = driReadPriv; 303 read->texture_stamp = driReadPriv->lastStamp - 1; 304 } 305 306 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base); 307 308 /* This is ok to call here. If they are already init, it's a no-op. */ 309 if (ctx->pp && draw->textures[ST_ATTACHMENT_BACK_LEFT]) 310 pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0, 311 draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0); 312 313 return GL_TRUE; 314} 315 316struct dri_context * 317dri_get_current(__DRIscreen *sPriv) 318{ 319 struct dri_screen *screen = dri_screen(sPriv); 320 struct st_api *stapi = screen->st_api; 321 struct st_context_iface *st; 322 323 st = stapi->get_current(stapi); 324 325 return (struct dri_context *) st ? st->st_manager_private : NULL; 326} 327 328/* vim: set sw=3 ts=8 sts=3 expandtab: */ 329