1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/**************************************************************************
*
* 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.
*
**************************************************************************/
/*
* Author: Keith Whitwell <keithw@vmware.com>
* Author: Jakob Bornecrantz <wallbraker@gmail.com>
*/
#include "dri_screen.h"
#include "dri_drawable.h"
#include "dri_context.h"
#include "frontend/drm_driver.h"
#include "pipe/p_context.h"
#include "pipe-loader/pipe_loader.h"
#include "state_tracker/st_context.h"
#include "util/u_cpu_detect.h"
#include "util/u_memory.h"
#include "util/u_debug.h"
struct dri_context *
dri_create_context(struct dri_screen *screen,
gl_api api, const struct gl_config *visual,
const struct __DriverContextConfig *ctx_config,
unsigned *error,
struct dri_context *sharedContextPrivate,
void *loaderPrivate)
{
struct dri_context *ctx = NULL;
struct st_context *st_share = NULL;
struct st_context_attribs attribs;
enum st_context_error ctx_err = 0;
unsigned allowed_flags = __DRI_CTX_FLAG_DEBUG |
__DRI_CTX_FLAG_FORWARD_COMPATIBLE;
unsigned allowed_attribs =
__DRIVER_CONTEXT_ATTRIB_PRIORITY |
__DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR |
__DRIVER_CONTEXT_ATTRIB_NO_ERROR;
const __DRIbackgroundCallableExtension *backgroundCallable =
screen->dri2.backgroundCallable;
const struct driOptionCache *optionCache = &screen->dev->option_cache;
/* This is effectively doing error checking for GLX context creation (by both
* Mesa and the X server) when the driver doesn't support the robustness ext.
* EGL already checks, so it won't send us the flags if the ext isn't
* available.
*/
if (screen->has_reset_status_query) {
allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS;
allowed_attribs |= __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
}
if (screen->has_protected_context)
allowed_attribs |= __DRIVER_CONTEXT_ATTRIB_PROTECTED;
if (ctx_config->flags & ~allowed_flags) {
*error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
goto fail;
}
if (ctx_config->attribute_mask & ~allowed_attribs) {
*error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
goto fail;
}
memset(&attribs, 0, sizeof(attribs));
switch (api) {
case API_OPENGLES:
attribs.profile = API_OPENGLES;
break;
case API_OPENGLES2:
attribs.profile = API_OPENGLES2;
break;
case API_OPENGL_COMPAT:
case API_OPENGL_CORE:
if (driQueryOptionb(optionCache, "force_compat_profile")) {
attribs.profile = API_OPENGL_COMPAT;
} else {
attribs.profile = api == API_OPENGL_COMPAT ? API_OPENGL_COMPAT
: API_OPENGL_CORE;
}
attribs.major = ctx_config->major_version;
attribs.minor = ctx_config->minor_version;
if ((ctx_config->flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
break;
default:
*error = __DRI_CTX_ERROR_BAD_API;
goto fail;
}
if ((ctx_config->flags & __DRI_CTX_FLAG_DEBUG) != 0)
attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
if (ctx_config->flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS)
attribs.context_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY)
if (ctx_config->reset_strategy != __DRI_CTX_RESET_NO_NOTIFICATION)
attribs.context_flags |= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET;
if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_NO_ERROR)
attribs.flags |= ctx_config->no_error ? ST_CONTEXT_FLAG_NO_ERROR : 0;
if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_PRIORITY) {
switch (ctx_config->priority) {
case __DRI_CTX_PRIORITY_LOW:
attribs.context_flags |= PIPE_CONTEXT_LOW_PRIORITY;
break;
case __DRI_CTX_PRIORITY_HIGH:
attribs.context_flags |= PIPE_CONTEXT_HIGH_PRIORITY;
break;
default:
break;
}
}
if ((ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR)
&& (ctx_config->release_behavior == __DRI_CTX_RELEASE_BEHAVIOR_NONE))
attribs.flags |= ST_CONTEXT_FLAG_RELEASE_NONE;
if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_PROTECTED)
attribs.context_flags |= PIPE_CONTEXT_PROTECTED;
struct dri_context *share_ctx = NULL;
if (sharedContextPrivate) {
share_ctx = (struct dri_context *)sharedContextPrivate;
st_share = share_ctx->st;
}
ctx = CALLOC_STRUCT(dri_context);
if (ctx == NULL) {
*error = __DRI_CTX_ERROR_NO_MEMORY;
goto fail;
}
ctx->screen = screen;
ctx->loaderPrivate = loaderPrivate;
/* KHR_no_error is likely to crash, overflow memory, etc if an application
* has errors so don't enable it for setuid processes.
*/
if (debug_get_bool_option("MESA_NO_ERROR", false) ||
driQueryOptionb(&screen->dev->option_cache, "mesa_no_error"))
#if !defined(_WIN32)
if (geteuid() == getuid())
#endif
attribs.flags |= ST_CONTEXT_FLAG_NO_ERROR;
attribs.options = screen->options;
dri_fill_st_visual(&attribs.visual, screen, visual);
ctx->st = st_api_create_context(&screen->base, &attribs, &ctx_err,
st_share);
if (ctx->st == NULL) {
switch (ctx_err) {
case ST_CONTEXT_SUCCESS:
*error = __DRI_CTX_ERROR_SUCCESS;
break;
case ST_CONTEXT_ERROR_NO_MEMORY:
*error = __DRI_CTX_ERROR_NO_MEMORY;
break;
case ST_CONTEXT_ERROR_BAD_VERSION:
*error = __DRI_CTX_ERROR_BAD_VERSION;
break;
}
goto fail;
}
ctx->st->frontend_context = (void *) ctx;
if (ctx->st->cso_context) {
ctx->pp = pp_init(ctx->st->pipe, screen->pp_enabled, ctx->st->cso_context,
ctx->st, st_context_invalidate_state);
ctx->hud = hud_create(ctx->st->cso_context,
share_ctx ? share_ctx->hud : NULL,
ctx->st, st_context_invalidate_state);
}
/* order of precedence (least to most):
* - driver setting
* - app setting
* - user setting
*/
bool enable_glthread = driQueryOptionb(&screen->dev->option_cache, "mesa_glthread_driver");
/* always disable glthread by default if fewer than 5 "big" CPUs are active */
unsigned nr_big_cpus = util_get_cpu_caps()->nr_big_cpus;
if (util_get_cpu_caps()->nr_cpus < 4 || (nr_big_cpus && nr_big_cpus < 5))
enable_glthread = false;
int app_enable_glthread = driQueryOptioni(&screen->dev->option_cache, "mesa_glthread_app_profile");
if (app_enable_glthread != -1) {
/* if set (not -1), apply the app setting */
enable_glthread = app_enable_glthread == 1;
}
if (getenv("mesa_glthread")) {
/* only apply the env var if set */
bool user_enable_glthread = debug_get_bool_option("mesa_glthread", false);
if (user_enable_glthread != enable_glthread) {
/* print warning to mimic old behavior */
fprintf(stderr, "ATTENTION: default value of option mesa_glthread overridden by environment.\n");
}
enable_glthread = user_enable_glthread;
}
/* Do this last. */
if (enable_glthread) {
bool safe = true;
/* This is only needed by X11/DRI2, which can be unsafe. */
if (backgroundCallable &&
backgroundCallable->base.version >= 2 &&
backgroundCallable->isThreadSafe &&
!backgroundCallable->isThreadSafe(loaderPrivate))
safe = false;
if (safe)
_mesa_glthread_init(ctx->st->ctx);
}
*error = __DRI_CTX_ERROR_SUCCESS;
return ctx;
fail:
if (ctx && ctx->st)
st_destroy_context(ctx->st);
free(ctx);
return NULL;
}
void
dri_destroy_context(struct dri_context *ctx)
{
/* Wait for glthread to finish because we can't use pipe_context from
* multiple threads.
*/
_mesa_glthread_finish(ctx->st->ctx);
if (ctx->hud) {
hud_destroy(ctx->hud, ctx->st->cso_context);
}
if (ctx->pp)
pp_free(ctx->pp);
/* No particular reason to wait for command completion before
* destroying a context, but we flush the context here
* to avoid having to add code elsewhere to cope with flushing a
* partially destroyed context.
*/
st_context_flush(ctx->st, 0, NULL, NULL, NULL);
st_destroy_context(ctx->st);
free(ctx);
}
/* This is called inside MakeCurrent to unbind the context. */
GLboolean
dri_unbind_context(struct dri_context *ctx)
{
/* dri_util.c ensures cPriv is not null */
struct st_context *st = ctx->st;
if (st == st_api_get_current()) {
_mesa_glthread_finish(st->ctx);
/* Record HUD queries for the duration the context was "current". */
if (ctx->hud)
hud_record_only(ctx->hud, st->pipe);
st_api_make_current(NULL, NULL, NULL);
}
if (ctx->draw || ctx->read) {
assert(ctx->draw);
dri_put_drawable(ctx->draw);
if (ctx->read != ctx->draw)
dri_put_drawable(ctx->read);
ctx->draw = NULL;
ctx->read = NULL;
}
return GL_TRUE;
}
GLboolean
dri_make_current(struct dri_context *ctx,
struct dri_drawable *draw,
struct dri_drawable *read)
{
/* dri_unbind_context() is always called before this, so drawables are
* always NULL here.
*/
assert(!ctx->draw);
assert(!ctx->read);
if ((draw && !read) || (!draw && read))
return GL_FALSE; /* only both non-NULL or both NULL are allowed */
/* Wait for glthread to finish because we can't use st_context from
* multiple threads.
*/
_mesa_glthread_finish(ctx->st->ctx);
/* There are 2 cases that can occur here. Either we bind drawables, or we
* bind NULL for configless and surfaceless contexts.
*/
if (!draw && !read)
return st_api_make_current(ctx->st, NULL, NULL);
/* Bind drawables to the context */
ctx->draw = draw;
ctx->read = read;
dri_get_drawable(draw);
draw->texture_stamp = draw->lastStamp - 1;
if (draw != read) {
dri_get_drawable(read);
read->texture_stamp = read->lastStamp - 1;
}
st_api_make_current(ctx->st, &draw->base, &read->base);
/* This is ok to call here. If they are already init, it's a no-op. */
if (ctx->pp && draw->textures[ST_ATTACHMENT_BACK_LEFT])
pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
return GL_TRUE;
}
struct dri_context *
dri_get_current(void)
{
struct st_context *st = st_api_get_current();
return (struct dri_context *) st ? st->frontend_context : NULL;
}
/* vim: set sw=3 ts=8 sts=3 expandtab: */
|