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");
57 options
->disable_glsl_line_continuations
=
58 driQueryOptionb(optionCache
, "disable_glsl_line_continuations");
59 options
->disable_blend_func_extended
=
60 driQueryOptionb(optionCache
, "disable_blend_func_extended");
61 options
->force_s3tc_enable
=
62 driQueryOptionb(optionCache
, "force_s3tc_enable");
66 dri_create_context(gl_api api
, const struct gl_config
* visual
,
68 unsigned major_version
,
69 unsigned minor_version
,
72 void *sharedContextPrivate
)
74 __DRIscreen
*sPriv
= cPriv
->driScreenPriv
;
75 struct dri_screen
*screen
= dri_screen(sPriv
);
76 struct st_api
*stapi
= screen
->st_api
;
77 struct dri_context
*ctx
= NULL
;
78 struct st_context_iface
*st_share
= NULL
;
79 struct st_context_attribs attribs
;
80 enum st_context_error ctx_err
= 0;
82 memset(&attribs
, 0, sizeof(attribs
));
85 attribs
.profile
= ST_PROFILE_OPENGL_ES1
;
88 attribs
.profile
= ST_PROFILE_OPENGL_ES2
;
90 case API_OPENGL_COMPAT
:
92 attribs
.profile
= api
== API_OPENGL_COMPAT
? ST_PROFILE_DEFAULT
93 : ST_PROFILE_OPENGL_CORE
;
94 attribs
.major
= major_version
;
95 attribs
.minor
= minor_version
;
97 if ((flags
& __DRI_CTX_FLAG_DEBUG
) != 0)
98 attribs
.flags
|= ST_CONTEXT_FLAG_DEBUG
;
100 if ((flags
& __DRI_CTX_FLAG_FORWARD_COMPATIBLE
) != 0)
101 attribs
.flags
|= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
;
104 *error
= __DRI_CTX_ERROR_BAD_API
;
108 if (sharedContextPrivate
) {
109 st_share
= ((struct dri_context
*)sharedContextPrivate
)->st
;
112 ctx
= CALLOC_STRUCT(dri_context
);
114 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
118 cPriv
->driverPrivate
= ctx
;
122 driParseConfigFiles(&ctx
->optionCache
,
123 &screen
->optionCacheDefaults
,
124 sPriv
->myNum
, driver_descriptor
.name
);
126 dri_fill_st_options(&attribs
.options
, &ctx
->optionCache
);
127 dri_fill_st_visual(&attribs
.visual
, screen
, visual
);
128 ctx
->st
= stapi
->create_context(stapi
, &screen
->base
, &attribs
, &ctx_err
,
130 if (ctx
->st
== NULL
) {
132 case ST_CONTEXT_SUCCESS
:
133 *error
= __DRI_CTX_ERROR_SUCCESS
;
135 case ST_CONTEXT_ERROR_NO_MEMORY
:
136 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
138 case ST_CONTEXT_ERROR_BAD_API
:
139 *error
= __DRI_CTX_ERROR_BAD_API
;
141 case ST_CONTEXT_ERROR_BAD_VERSION
:
142 *error
= __DRI_CTX_ERROR_BAD_VERSION
;
144 case ST_CONTEXT_ERROR_BAD_FLAG
:
145 *error
= __DRI_CTX_ERROR_BAD_FLAG
;
147 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE
:
148 *error
= __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
;
150 case ST_CONTEXT_ERROR_UNKNOWN_FLAG
:
151 *error
= __DRI_CTX_ERROR_UNKNOWN_FLAG
;
156 ctx
->st
->st_manager_private
= (void *) ctx
;
159 // Context successfully created. See if post-processing is requested.
162 if (ctx
->st
->cso_context
) {
163 ctx
->pp
= pp_init(ctx
->st
->pipe
, ctx
->pp_enabled
, ctx
->st
->cso_context
);
164 ctx
->hud
= hud_create(ctx
->st
->pipe
, ctx
->st
->cso_context
);
167 *error
= __DRI_CTX_ERROR_SUCCESS
;
172 ctx
->st
->destroy(ctx
->st
);
179 dri_destroy_context(__DRIcontext
* cPriv
)
181 struct dri_context
*ctx
= dri_context(cPriv
);
184 hud_destroy(ctx
->hud
);
187 /* note: we are freeing values and nothing more because
188 * driParseConfigFiles allocated values only - the rest
189 * is owned by screen optionCacheDefaults.
191 free(ctx
->optionCache
.values
);
193 /* No particular reason to wait for command completion before
194 * destroying a context, but we flush the context here
195 * to avoid having to add code elsewhere to cope with flushing a
196 * partially destroyed context.
198 ctx
->st
->flush(ctx
->st
, 0, NULL
);
199 ctx
->st
->destroy(ctx
->st
);
201 if (ctx
->pp
) pp_free(ctx
->pp
);
207 dri_unbind_context(__DRIcontext
* cPriv
)
209 /* dri_util.c ensures cPriv is not null */
210 struct dri_screen
*screen
= dri_screen(cPriv
->driScreenPriv
);
211 struct dri_context
*ctx
= dri_context(cPriv
);
212 struct st_api
*stapi
= screen
->st_api
;
214 if (--ctx
->bind_count
== 0) {
215 if (ctx
->st
== ctx
->stapi
->get_current(ctx
->stapi
)) {
216 /* For conformance, unbind is supposed to flush the context.
217 * However, if we do it here we might end up flushing a partially
218 * destroyed context. Instead, we flush in dri_make_current and
219 * in dri_destroy_context which should cover all the cases.
221 stapi
->make_current(stapi
, NULL
, NULL
, NULL
);
229 dri_make_current(__DRIcontext
* cPriv
,
230 __DRIdrawable
* driDrawPriv
,
231 __DRIdrawable
* driReadPriv
)
233 /* dri_util.c ensures cPriv is not null */
234 struct dri_context
*ctx
= dri_context(cPriv
);
235 struct dri_drawable
*draw
= dri_drawable(driDrawPriv
);
236 struct dri_drawable
*read
= dri_drawable(driReadPriv
);
237 struct st_context_iface
*old_st
= ctx
->stapi
->get_current(ctx
->stapi
);
239 /* Flush the old context here so we don't have to flush on unbind() */
240 if (old_st
&& old_st
!= ctx
->st
)
241 old_st
->flush(old_st
, ST_FLUSH_FRONT
, NULL
);
245 if (!driDrawPriv
&& !driReadPriv
)
246 return ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, NULL
, NULL
);
247 else if (!driDrawPriv
|| !driReadPriv
)
250 if (ctx
->dPriv
!= driDrawPriv
) {
251 ctx
->dPriv
= driDrawPriv
;
252 draw
->texture_stamp
= driDrawPriv
->lastStamp
- 1;
254 if (ctx
->rPriv
!= driReadPriv
) {
255 ctx
->rPriv
= driReadPriv
;
256 read
->texture_stamp
= driReadPriv
->lastStamp
- 1;
259 ctx
->stapi
->make_current(ctx
->stapi
, ctx
->st
, &draw
->base
, &read
->base
);
261 // This is ok to call here. If they are already init, it's a no-op.
262 if (draw
->textures
[ST_ATTACHMENT_BACK_LEFT
] && draw
->textures
[ST_ATTACHMENT_DEPTH_STENCIL
]
264 pp_init_fbos(ctx
->pp
, draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->width0
,
265 draw
->textures
[ST_ATTACHMENT_BACK_LEFT
]->height0
);
271 dri_get_current(__DRIscreen
*sPriv
)
273 struct dri_screen
*screen
= dri_screen(sPriv
);
274 struct st_api
*stapi
= screen
->st_api
;
275 struct st_context_iface
*st
;
277 st
= stapi
->get_current(stapi
);
279 return (struct dri_context
*) (st
) ? st
->st_manager_private
: NULL
;
282 /* vim: set sw=3 ts=8 sts=3 expandtab: */