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