1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
34 #include "dri_screen.h"
35 #include "dri_drawable.h"
36 #include "dri_context.h"
37 #include "frontend/drm_driver.h"
39 #include "pipe/p_context.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "state_tracker/st_context.h"
43 #include "util/u_memory.h"
46 dri_create_context(gl_api api
, const struct gl_config
* visual
,
48 const struct __DriverContextConfig
*ctx_config
,
50 void *sharedContextPrivate
)
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
;
69 if (screen
->has_reset_status_query
) {
70 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
71 allowed_attribs
|= __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY
;
74 if (ctx_config
->flags
& ~allowed_flags
) {
75 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
79 if (ctx_config
->attribute_mask
& ~allowed_attribs
) {
80 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
84 memset(&attribs
, 0, sizeof(attribs
));
87 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
90 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
92 case API_OPENGL_COMPAT
:
94 if (driQueryOptionb(optionCache
, "force_compat_profile")) {
95 attribs
.profile
= ST_PROFILE_DEFAULT
;
97 attribs
.profile
= api
== API_OPENGL_COMPAT
? ST_PROFILE_DEFAULT
98 : ST_PROFILE_OPENGL_CORE
;
101 attribs
.major
= ctx_config
->major_version
;
102 attribs
.minor
= ctx_config
->minor_version
;
104 if ((ctx_config
->flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
105 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
108 *error
= __DRI_CTX_ERROR_BAD_API
;
112 if ((ctx_config
->flags
& __DRI_CTX_FLAG_DEBUG
) != 0)
113 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
115 if (ctx_config
->flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)
116 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
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
;
122 if (ctx_config
->flags
& __DRI_CTX_FLAG_NO_ERROR
)
123 attribs
.flags
|= ST_CONTEXT_FLAG_NO_ERROR
;
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
;
130 case __DRI_CTX_PRIORITY_HIGH
:
131 attribs
.flags
|= ST_CONTEXT_FLAG_HIGH_PRIORITY
;
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
;
142 struct dri_context
*share_ctx
= NULL
;
143 if (sharedContextPrivate
) {
144 share_ctx
= (struct dri_context
*)sharedContextPrivate
;
145 st_share
= share_ctx
->st
;
148 ctx
= CALLOC_STRUCT(dri_context
);
150 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
154 cPriv
->driverPrivate
= ctx
;
158 if (driQueryOptionb(&screen
->dev
->option_cache
, "mesa_no_error"))
159 attribs
.flags
|= ST_CONTEXT_FLAG_NO_ERROR
;
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
,
165 if (ctx
->st
== NULL
) {
167 case ST_CONTEXT_SUCCESS
:
168 *error
= __DRI_CTX_ERROR_SUCCESS
;
170 case ST_CONTEXT_ERROR_NO_MEMORY
:
171 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
173 case ST_CONTEXT_ERROR_BAD_API
:
174 *error
= __DRI_CTX_ERROR_BAD_API
;
176 case ST_CONTEXT_ERROR_BAD_VERSION
:
177 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
179 case ST_CONTEXT_ERROR_BAD_FLAG
:
180 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
182 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
183 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
185 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
186 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
191 ctx
->st
->st_manager_private
= (void *) ctx
;
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
);
201 if (ctx
->st
->start_thread
&&
202 driQueryOptionb(&screen
->dev
->option_cache
, "mesa_glthread")) {
204 if (backgroundCallable
&& backgroundCallable
->base
.version
>= 2 &&
205 backgroundCallable
->isThreadSafe
) {
207 if (backgroundCallable
->isThreadSafe(cPriv
->loaderPrivate
))
208 ctx
->st
->start_thread(ctx
->st
);
210 fprintf(stderr
, "dri_create_context: glthread isn't thread safe "
211 "- missing call XInitThreads\n");
213 fprintf(stderr
, "dri_create_context: requested glthread but driver "
214 "is missing backgroundCallable V2 extension\n");
218 *error
= __DRI_CTX_ERROR_SUCCESS
;
223 ctx
->st
->destroy(ctx
->st
);
230 dri_destroy_context(__DRIcontext
* cPriv
)
232 struct dri_context
*ctx
= dri_context(cPriv
);
235 hud_destroy(ctx
->hud
, ctx
->st
->cso_context
);
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.
246 ctx
->st
->flush(ctx
->st
, 0, NULL
, NULL
, NULL
);
247 ctx
->st
->destroy(ctx
->st
);
251 /* This is called inside MakeCurrent to unbind the context. */
253 dri_unbind_context(__DRIcontext
* cPriv
)
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
;
261 if (--ctx
->bind_count
== 0) {
262 if (st
== stapi
->get_current(stapi
)) {
263 if (st
->thread_finish
)
264 st
->thread_finish(st
);
266 /* Record HUD queries for the duration the context was "current". */
268 hud_record_only(ctx
->hud
, st
->pipe
);
270 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
278 dri_make_current(__DRIcontext
* cPriv
,
279 __DRIdrawable
* driDrawPriv
,
280 __DRIdrawable
* driReadPriv
)
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
);
290 return ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, NULL
, NULL
);
291 else if (!draw
|| !read
)
294 if (ctx
->dPriv
!= driDrawPriv
) {
295 ctx
->dPriv
= driDrawPriv
;
296 draw
->texture_stamp
= driDrawPriv
->lastStamp
- 1;
298 if (ctx
->rPriv
!= driReadPriv
) {
299 ctx
->rPriv
= driReadPriv
;
300 read
->texture_stamp
= driReadPriv
->lastStamp
- 1;
303 ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, &draw
->base
, &read
->base
);
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
);
314 dri_get_current(__DRIscreen
*sPriv
)
316 struct dri_screen
*screen
= dri_screen(sPriv
);
317 struct st_api
*stapi
= screen
->st_api
;
318 struct st_context_iface
*st
;
320 st
= stapi
->get_current(stapi
);
322 return (struct dri_context
*) st
? st
->st_manager_private
: NULL
;
325 /* vim: set sw=3 ts=8 sts=3 expandtab: */