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 "state_tracker/st_context.h"
43 dri_pp_query(struct dri_context
*ctx
)
47 for (i
= 0; i
< PP_FILTERS
; i
++) {
48 ctx
->pp_enabled
[i
] = driQueryOptioni(&ctx
->optionCache
, pp_filters
[i
].name
);
52 static void dri_fill_st_options(struct st_config_options
*options
,
53 const struct driOptionCache
* optionCache
)
55 options
->force_glsl_extensions_warn
=
56 driQueryOptionb(optionCache
, "force_glsl_extensions_warn");
60 dri_create_context(gl_api api
, const struct gl_config
* visual
,
62 unsigned major_version
,
63 unsigned minor_version
,
66 void *sharedContextPrivate
)
68 __DRIscreen
*sPriv
= cPriv
->driScreenPriv
;
69 struct dri_screen
*screen
= dri_screen(sPriv
);
70 struct st_api
*stapi
= screen
->st_api
;
71 struct dri_context
*ctx
= NULL
;
72 struct st_context_iface
*st_share
= NULL
;
73 struct st_context_attribs attribs
;
74 enum st_context_error ctx_err
= 0;
76 memset(&attribs
, 0, sizeof(attribs
));
79 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
82 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
84 case API_OPENGL_COMPAT
:
86 attribs
.profile
= api
== API_OPENGL_COMPAT
? ST_PROFILE_DEFAULT
87 : ST_PROFILE_OPENGL_CORE
;
88 attribs
.major
= major_version
;
89 attribs
.minor
= minor_version
;
91 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0)
92 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
94 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
95 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
98 *error
= __DRI_CTX_ERROR_BAD_API
;
102 if (sharedContextPrivate
) {
103 st_share
= ((struct dri_context
*)sharedContextPrivate
)->st
;
106 ctx
= CALLOC_STRUCT(dri_context
);
108 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
112 cPriv
->driverPrivate
= ctx
;
116 driParseConfigFiles(&ctx
->optionCache
,
117 &screen
->optionCache
, sPriv
->myNum
, driver_descriptor
.name
);
119 dri_fill_st_options(&attribs
.options
, &ctx
->optionCache
);
120 dri_fill_st_visual(&attribs
.visual
, screen
, visual
);
121 ctx
->st
= stapi
->create_context(stapi
, &screen
->base
, &attribs
, &ctx_err
,
123 if (ctx
->st
== NULL
) {
125 case ST_CONTEXT_SUCCESS
:
126 *error
= __DRI_CTX_ERROR_SUCCESS
;
128 case ST_CONTEXT_ERROR_NO_MEMORY
:
129 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
131 case ST_CONTEXT_ERROR_BAD_API
:
132 *error
= __DRI_CTX_ERROR_BAD_API
;
134 case ST_CONTEXT_ERROR_BAD_VERSION
:
135 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
137 case ST_CONTEXT_ERROR_BAD_FLAG
:
138 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
140 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
141 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
143 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
144 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
149 ctx
->st
->st_manager_private
= (void *) ctx
;
152 // Context successfully created. See if post-processing is requested.
155 if (ctx
->st
->cso_context
) {
156 ctx
->pp
= pp_init(ctx
->st
->pipe
, ctx
->pp_enabled
, ctx
->st
->cso_context
);
159 *error
= __DRI_CTX_ERROR_SUCCESS
;
164 ctx
->st
->destroy(ctx
->st
);
171 dri_destroy_context(__DRIcontext
* cPriv
)
173 struct dri_context
*ctx
= dri_context(cPriv
);
175 /* note: we are freeing values and nothing more because
176 * driParseConfigFiles allocated values only - the rest
177 * is owned by screen optionCache.
179 free(ctx
->optionCache
.values
);
181 /* No particular reason to wait for command completion before
182 * destroying a context, but we flush the context here
183 * to avoid having to add code elsewhere to cope with flushing a
184 * partially destroyed context.
186 ctx
->st
->flush(ctx
->st
, 0, NULL
);
187 ctx
->st
->destroy(ctx
->st
);
189 if (ctx
->pp
) pp_free(ctx
->pp
);
195 dri_unbind_context(__DRIcontext
* cPriv
)
197 /* dri_util.c ensures cPriv is not null */
198 struct dri_screen
*screen
= dri_screen(cPriv
->driScreenPriv
);
199 struct dri_context
*ctx
= dri_context(cPriv
);
200 struct st_api
*stapi
= screen
->st_api
;
202 if (--ctx
->bind_count
== 0) {
203 if (ctx
->st
== ctx
->stapi
->get_current(ctx
->stapi
)) {
204 /* For conformance, unbind is supposed to flush the context.
205 * However, if we do it here we might end up flushing a partially
206 * destroyed context. Instead, we flush in dri_make_current and
207 * in dri_destroy_context which should cover all the cases.
209 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
217 dri_make_current(__DRIcontext
* cPriv
,
218 __DRIdrawable
* driDrawPriv
,
219 __DRIdrawable
* driReadPriv
)
221 /* dri_util.c ensures cPriv is not null */
222 struct dri_context
*ctx
= dri_context(cPriv
);
223 struct dri_drawable
*draw
= dri_drawable(driDrawPriv
);
224 struct dri_drawable
*read
= dri_drawable(driReadPriv
);
225 struct st_context_iface
*old_st
= ctx
->stapi
->get_current(ctx
->stapi
);
227 /* Flush the old context here so we don't have to flush on unbind() */
228 if (old_st
&& old_st
!= ctx
->st
)
229 old_st
->flush(old_st
, ST_FLUSH_FRONT
, NULL
);
233 if (!driDrawPriv
&& !driReadPriv
)
234 return ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, NULL
, NULL
);
235 else if (!driDrawPriv
|| !driReadPriv
)
238 if (ctx
->dPriv
!= driDrawPriv
) {
239 ctx
->dPriv
= driDrawPriv
;
240 draw
->texture_stamp
= driDrawPriv
->lastStamp
- 1;
242 if (ctx
->rPriv
!= driReadPriv
) {
243 ctx
->rPriv
= driReadPriv
;
244 read
->texture_stamp
= driReadPriv
->lastStamp
- 1;
247 ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, &draw
->base
, &read
->base
);
249 // This is ok to call here. If they are already init, it's a no-op.
250 if (draw
->textures
[ST_ATTACHMENT_BACK_LEFT
] && draw
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]
252 pp_init_fbos(ctx
->pp
, draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->width0
,
253 draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->height0
);
259 dri_get_current(__DRIscreen
*sPriv
)
261 struct dri_screen
*screen
= dri_screen(sPriv
);
262 struct st_api
*stapi
= screen
->st_api
;
263 struct st_context_iface
*st
;
265 st
= stapi
->get_current(stapi
);
267 return (struct dri_context
*) (st
) ? st
->st_manager_private
: NULL
;
270 /* vim: set sw=3 ts=8 sts=3 expandtab: */