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
45 GLboolean
dri_create_context(gl_api api,const struct gl_config * visual,__DRIcontext * cPriv,const struct __DriverContextConfig * ctx_config,unsigned * error,void * sharedContextPrivate)46 dri_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->hud = hud_create(ctx->st->cso_context,
197 share_ctx ? share_ctx->hud : NULL);
198 }
199
200 /* Do this last. */
201 if (ctx->st->start_thread &&
202 driQueryOptionb(&screen->dev->option_cache, "mesa_glthread")) {
203
204 if (backgroundCallable && backgroundCallable->base.version >= 2 &&
205 backgroundCallable->isThreadSafe) {
206
207 if (backgroundCallable->isThreadSafe(cPriv->loaderPrivate))
208 ctx->st->start_thread(ctx->st);
209 else
210 fprintf(stderr, "dri_create_context: glthread isn't thread safe "
211 "- missing call XInitThreads\n");
212 } else {
213 fprintf(stderr, "dri_create_context: requested glthread but driver "
214 "is missing backgroundCallable V2 extension\n");
215 }
216 }
217
218 *error = __DRI_CTX_ERROR_SUCCESS;
219 return GL_TRUE;
220
221 fail:
222 if (ctx && ctx->st)
223 ctx->st->destroy(ctx->st);
224
225 free(ctx);
226 return GL_FALSE;
227 }
228
229 void
dri_destroy_context(__DRIcontext * cPriv)230 dri_destroy_context(__DRIcontext * cPriv)
231 {
232 struct dri_context *ctx = dri_context(cPriv);
233
234 if (ctx->hud) {
235 hud_destroy(ctx->hud, ctx->st->cso_context);
236 }
237
238 if (ctx->pp)
239 pp_free(ctx->pp);
240
241 /* No particular reason to wait for command completion before
242 * destroying a context, but we flush the context here
243 * to avoid having to add code elsewhere to cope with flushing a
244 * partially destroyed context.
245 */
246 ctx->st->flush(ctx->st, 0, NULL, NULL, NULL);
247 ctx->st->destroy(ctx->st);
248 free(ctx);
249 }
250
251 /* This is called inside MakeCurrent to unbind the context. */
252 GLboolean
dri_unbind_context(__DRIcontext * cPriv)253 dri_unbind_context(__DRIcontext * cPriv)
254 {
255 /* dri_util.c ensures cPriv is not null */
256 struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
257 struct dri_context *ctx = dri_context(cPriv);
258 struct st_context_iface *st = ctx->st;
259 struct st_api *stapi = screen->st_api;
260
261 if (--ctx->bind_count == 0) {
262 if (st == stapi->get_current(stapi)) {
263 if (st->thread_finish)
264 st->thread_finish(st);
265
266 /* Record HUD queries for the duration the context was "current". */
267 if (ctx->hud)
268 hud_record_only(ctx->hud, st->pipe);
269
270 stapi->make_current(stapi, NULL, NULL, NULL);
271 }
272 }
273
274 return GL_TRUE;
275 }
276
277 GLboolean
dri_make_current(__DRIcontext * cPriv,__DRIdrawable * driDrawPriv,__DRIdrawable * driReadPriv)278 dri_make_current(__DRIcontext * cPriv,
279 __DRIdrawable * driDrawPriv,
280 __DRIdrawable * driReadPriv)
281 {
282 /* dri_util.c ensures cPriv is not null */
283 struct dri_context *ctx = dri_context(cPriv);
284 struct dri_drawable *draw = dri_drawable(driDrawPriv);
285 struct dri_drawable *read = dri_drawable(driReadPriv);
286
287 ++ctx->bind_count;
288
289 if (!draw && !read)
290 return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
291 else if (!draw || !read)
292 return GL_FALSE;
293
294 if (ctx->dPriv != driDrawPriv) {
295 ctx->dPriv = driDrawPriv;
296 draw->texture_stamp = driDrawPriv->lastStamp - 1;
297 }
298 if (ctx->rPriv != driReadPriv) {
299 ctx->rPriv = driReadPriv;
300 read->texture_stamp = driReadPriv->lastStamp - 1;
301 }
302
303 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
304
305 /* This is ok to call here. If they are already init, it's a no-op. */
306 if (ctx->pp && draw->textures[ST_ATTACHMENT_BACK_LEFT])
307 pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
308 draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
309
310 return GL_TRUE;
311 }
312
313 struct dri_context *
dri_get_current(__DRIscreen * sPriv)314 dri_get_current(__DRIscreen *sPriv)
315 {
316 struct dri_screen *screen = dri_screen(sPriv);
317 struct st_api *stapi = screen->st_api;
318 struct st_context_iface *st;
319
320 st = stapi->get_current(stapi);
321
322 return (struct dri_context *) st ? st->st_manager_private : NULL;
323 }
324
325 /* vim: set sw=3 ts=8 sts=3 expandtab: */
326