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 "state_tracker/drm_driver.h"
39 #include "pipe/p_context.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "state_tracker/st_context.h"
44 dri_create_context(gl_api api
, const struct gl_config
* visual
,
46 const struct __DriverContextConfig
*ctx_config
,
48 void *sharedContextPrivate
)
50 __DRIscreen
*sPriv
= cPriv
->driScreenPriv
;
51 struct dri_screen
*screen
= dri_screen(sPriv
);
52 struct st_api
*stapi
= screen
->st_api
;
53 struct dri_context
*ctx
= NULL
;
54 struct st_context_iface
*st_share
= NULL
;
55 struct st_context_attribs attribs
;
56 enum st_context_error ctx_err
= 0;
57 unsigned allowed_flags
= __DRI_CTX_FLAG_DEBUG
|
58 __DRI_CTX_FLAG_FORWARD_COMPATIBLE
|
59 __DRI_CTX_FLAG_NO_ERROR
;
60 unsigned allowed_attribs
=
61 __DRIVER_CONTEXT_ATTRIB_PRIORITY
|
62 __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR
;
63 const __DRIbackgroundCallableExtension
*backgroundCallable
=
64 screen
->sPriv
->dri2
.backgroundCallable
;
66 if (screen
->has_reset_status_query
) {
67 allowed_flags
|= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
;
68 allowed_attribs
|= __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY
;
71 if (ctx_config
->flags
& ~allowed_flags
) {
72 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
76 if (ctx_config
->attribute_mask
& ~allowed_attribs
) {
77 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
81 memset(&attribs
, 0, sizeof(attribs
));
84 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
87 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
89 case API_OPENGL_COMPAT
:
91 attribs
.profile
= api
== API_OPENGL_COMPAT
? ST_PROFILE_DEFAULT
92 : ST_PROFILE_OPENGL_CORE
;
93 attribs
.major
= ctx_config
->major_version
;
94 attribs
.minor
= ctx_config
->minor_version
;
96 if ((ctx_config
->flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
97 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
100 *error
= __DRI_CTX_ERROR_BAD_API
;
104 if ((ctx_config
->flags
& __DRI_CTX_FLAG_DEBUG
) != 0)
105 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
107 if (ctx_config
->flags
& __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
)
108 attribs
.flags
|= ST_CONTEXT_FLAG_ROBUST_ACCESS
;
110 if (ctx_config
->attribute_mask
& __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY
)
111 if (ctx_config
->reset_strategy
!= __DRI_CTX_RESET_NO_NOTIFICATION
)
112 attribs
.flags
|= ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
;
114 if (ctx_config
->flags
& __DRI_CTX_FLAG_NO_ERROR
)
115 attribs
.flags
|= ST_CONTEXT_FLAG_NO_ERROR
;
117 if (ctx_config
->attribute_mask
& __DRIVER_CONTEXT_ATTRIB_PRIORITY
) {
118 switch (ctx_config
->priority
) {
119 case __DRI_CTX_PRIORITY_LOW
:
120 attribs
.flags
|= ST_CONTEXT_FLAG_LOW_PRIORITY
;
122 case __DRI_CTX_PRIORITY_HIGH
:
123 attribs
.flags
|= ST_CONTEXT_FLAG_HIGH_PRIORITY
;
130 if ((ctx_config
->attribute_mask
& __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR
)
131 && (ctx_config
->release_behavior
== __DRI_CTX_RELEASE_BEHAVIOR_NONE
))
132 attribs
.flags
|= ST_CONTEXT_FLAG_RELEASE_NONE
;
134 struct dri_context
*share_ctx
= NULL
;
135 if (sharedContextPrivate
) {
136 share_ctx
= (struct dri_context
*)sharedContextPrivate
;
137 st_share
= share_ctx
->st
;
140 ctx
= CALLOC_STRUCT(dri_context
);
142 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
146 cPriv
->driverPrivate
= ctx
;
150 if (driQueryOptionb(&screen
->dev
->option_cache
, "mesa_no_error"))
151 attribs
.flags
|= ST_CONTEXT_FLAG_NO_ERROR
;
153 attribs
.options
= screen
->options
;
154 dri_fill_st_visual(&attribs
.visual
, screen
, visual
);
155 ctx
->st
= stapi
->create_context(stapi
, &screen
->base
, &attribs
, &ctx_err
,
157 if (ctx
->st
== NULL
) {
159 case ST_CONTEXT_SUCCESS
:
160 *error
= __DRI_CTX_ERROR_SUCCESS
;
162 case ST_CONTEXT_ERROR_NO_MEMORY
:
163 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
165 case ST_CONTEXT_ERROR_BAD_API
:
166 *error
= __DRI_CTX_ERROR_BAD_API
;
168 case ST_CONTEXT_ERROR_BAD_VERSION
:
169 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
171 case ST_CONTEXT_ERROR_BAD_FLAG
:
172 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
174 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
175 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
177 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
178 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
183 ctx
->st
->st_manager_private
= (void *) ctx
;
186 if (ctx
->st
->cso_context
) {
187 ctx
->pp
= pp_init(ctx
->st
->pipe
, screen
->pp_enabled
, ctx
->st
->cso_context
);
188 ctx
->hud
= hud_create(ctx
->st
->cso_context
,
189 share_ctx
? share_ctx
->hud
: NULL
);
193 if (ctx
->st
->start_thread
&&
194 driQueryOptionb(&screen
->dev
->option_cache
, "mesa_glthread")) {
196 if (backgroundCallable
&& backgroundCallable
->base
.version
>= 2 &&
197 backgroundCallable
->isThreadSafe
) {
199 if (backgroundCallable
->isThreadSafe(cPriv
->loaderPrivate
))
200 ctx
->st
->start_thread(ctx
->st
);
202 fprintf(stderr
, "dri_create_context: glthread isn't thread safe "
203 "- missing call XInitThreads\n");
205 fprintf(stderr
, "dri_create_context: requested glthread but driver "
206 "is missing backgroundCallable V2 extension\n");
210 *error
= __DRI_CTX_ERROR_SUCCESS
;
215 ctx
->st
->destroy(ctx
->st
);
222 dri_destroy_context(__DRIcontext
* cPriv
)
224 struct dri_context
*ctx
= dri_context(cPriv
);
227 hud_destroy(ctx
->hud
, ctx
->st
->cso_context
);
233 /* No particular reason to wait for command completion before
234 * destroying a context, but we flush the context here
235 * to avoid having to add code elsewhere to cope with flushing a
236 * partially destroyed context.
238 ctx
->st
->flush(ctx
->st
, 0, NULL
);
239 ctx
->st
->destroy(ctx
->st
);
243 /* This is called inside MakeCurrent to unbind the context. */
245 dri_unbind_context(__DRIcontext
* cPriv
)
247 /* dri_util.c ensures cPriv is not null */
248 struct dri_screen
*screen
= dri_screen(cPriv
->driScreenPriv
);
249 struct dri_context
*ctx
= dri_context(cPriv
);
250 struct st_context_iface
*st
= ctx
->st
;
251 struct st_api
*stapi
= screen
->st_api
;
253 if (--ctx
->bind_count
== 0) {
254 if (st
== stapi
->get_current(stapi
)) {
255 if (st
->thread_finish
)
256 st
->thread_finish(st
);
258 /* Record HUD queries for the duration the context was "current". */
260 hud_record_only(ctx
->hud
, st
->pipe
);
262 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
270 dri_make_current(__DRIcontext
* cPriv
,
271 __DRIdrawable
* driDrawPriv
,
272 __DRIdrawable
* driReadPriv
)
274 /* dri_util.c ensures cPriv is not null */
275 struct dri_context
*ctx
= dri_context(cPriv
);
276 struct dri_drawable
*draw
= dri_drawable(driDrawPriv
);
277 struct dri_drawable
*read
= dri_drawable(driReadPriv
);
282 return ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, NULL
, NULL
);
283 else if (!draw
|| !read
)
286 if (ctx
->dPriv
!= driDrawPriv
) {
287 ctx
->dPriv
= driDrawPriv
;
288 draw
->texture_stamp
= driDrawPriv
->lastStamp
- 1;
290 if (ctx
->rPriv
!= driReadPriv
) {
291 ctx
->rPriv
= driReadPriv
;
292 read
->texture_stamp
= driReadPriv
->lastStamp
- 1;
295 ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, &draw
->base
, &read
->base
);
297 /* This is ok to call here. If they are already init, it's a no-op. */
298 if (ctx
->pp
&& draw
->textures
[ST_ATTACHMENT_BACK_LEFT
])
299 pp_init_fbos(ctx
->pp
, draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->width0
,
300 draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->height0
);
306 dri_get_current(__DRIscreen
*sPriv
)
308 struct dri_screen
*screen
= dri_screen(sPriv
);
309 struct st_api
*stapi
= screen
->st_api
;
310 struct st_context_iface
*st
;
312 st
= stapi
->get_current(stapi
);
314 return (struct dri_context
*) st
? st
->st_manager_private
: NULL
;
317 /* vim: set sw=3 ts=8 sts=3 expandtab: */