st/dri: implement the driconf option force_s3tc_enable properly
[mesa.git] / src / gallium / state_trackers / dri / common / dri_context.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "utils.h"
33
34 #include "dri_screen.h"
35 #include "dri_drawable.h"
36 #include "dri_context.h"
37 #include "state_tracker/drm_driver.h"
38
39 #include "pipe/p_context.h"
40 #include "state_tracker/st_context.h"
41
42 static void
43 dri_pp_query(struct dri_context *ctx)
44 {
45 unsigned int i;
46
47 for (i = 0; i < PP_FILTERS; i++) {
48 ctx->pp_enabled[i] = driQueryOptioni(&ctx->optionCache, pp_filters[i].name);
49 }
50 }
51
52 static void dri_fill_st_options(struct st_config_options *options,
53 const struct driOptionCache * optionCache)
54 {
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");
63 }
64
65 GLboolean
66 dri_create_context(gl_api api, const struct gl_config * visual,
67 __DRIcontext * cPriv,
68 unsigned major_version,
69 unsigned minor_version,
70 uint32_t flags,
71 unsigned *error,
72 void *sharedContextPrivate)
73 {
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;
81
82 memset(&attribs, 0, sizeof(attribs));
83 switch (api) {
84 case API_OPENGLES:
85 attribs.profile = ST_PROFILE_OPENGL_ES1;
86 break;
87 case API_OPENGLES2:
88 attribs.profile = ST_PROFILE_OPENGL_ES2;
89 break;
90 case API_OPENGL_COMPAT:
91 case API_OPENGL_CORE:
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;
96
97 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
98 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
99
100 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
101 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
102 break;
103 default:
104 *error = __DRI_CTX_ERROR_BAD_API;
105 goto fail;
106 }
107
108 if (sharedContextPrivate) {
109 st_share = ((struct dri_context *)sharedContextPrivate)->st;
110 }
111
112 ctx = CALLOC_STRUCT(dri_context);
113 if (ctx == NULL) {
114 *error = __DRI_CTX_ERROR_NO_MEMORY;
115 goto fail;
116 }
117
118 cPriv->driverPrivate = ctx;
119 ctx->cPriv = cPriv;
120 ctx->sPriv = sPriv;
121
122 driParseConfigFiles(&ctx->optionCache,
123 &screen->optionCacheDefaults,
124 sPriv->myNum, driver_descriptor.name);
125
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,
129 st_share);
130 if (ctx->st == NULL) {
131 switch (ctx_err) {
132 case ST_CONTEXT_SUCCESS:
133 *error = __DRI_CTX_ERROR_SUCCESS;
134 break;
135 case ST_CONTEXT_ERROR_NO_MEMORY:
136 *error = __DRI_CTX_ERROR_NO_MEMORY;
137 break;
138 case ST_CONTEXT_ERROR_BAD_API:
139 *error = __DRI_CTX_ERROR_BAD_API;
140 break;
141 case ST_CONTEXT_ERROR_BAD_VERSION:
142 *error = __DRI_CTX_ERROR_BAD_VERSION;
143 break;
144 case ST_CONTEXT_ERROR_BAD_FLAG:
145 *error = __DRI_CTX_ERROR_BAD_FLAG;
146 break;
147 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
148 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
149 break;
150 case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
151 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
152 break;
153 }
154 goto fail;
155 }
156 ctx->st->st_manager_private = (void *) ctx;
157 ctx->stapi = stapi;
158
159 // Context successfully created. See if post-processing is requested.
160 dri_pp_query(ctx);
161
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);
165 }
166
167 *error = __DRI_CTX_ERROR_SUCCESS;
168 return GL_TRUE;
169
170 fail:
171 if (ctx && ctx->st)
172 ctx->st->destroy(ctx->st);
173
174 free(ctx);
175 return GL_FALSE;
176 }
177
178 void
179 dri_destroy_context(__DRIcontext * cPriv)
180 {
181 struct dri_context *ctx = dri_context(cPriv);
182
183 if (ctx->hud) {
184 hud_destroy(ctx->hud);
185 }
186
187 /* note: we are freeing values and nothing more because
188 * driParseConfigFiles allocated values only - the rest
189 * is owned by screen optionCacheDefaults.
190 */
191 free(ctx->optionCache.values);
192
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.
197 */
198 ctx->st->flush(ctx->st, 0, NULL);
199 ctx->st->destroy(ctx->st);
200
201 if (ctx->pp) pp_free(ctx->pp);
202
203 free(ctx);
204 }
205
206 GLboolean
207 dri_unbind_context(__DRIcontext * cPriv)
208 {
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;
213
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.
220 */
221 stapi->make_current(stapi, NULL, NULL, NULL);
222 }
223 }
224
225 return GL_TRUE;
226 }
227
228 GLboolean
229 dri_make_current(__DRIcontext * cPriv,
230 __DRIdrawable * driDrawPriv,
231 __DRIdrawable * driReadPriv)
232 {
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);
238
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);
242
243 ++ctx->bind_count;
244
245 if (!driDrawPriv && !driReadPriv)
246 return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
247 else if (!driDrawPriv || !driReadPriv)
248 return GL_FALSE;
249
250 if (ctx->dPriv != driDrawPriv) {
251 ctx->dPriv = driDrawPriv;
252 draw->texture_stamp = driDrawPriv->lastStamp - 1;
253 }
254 if (ctx->rPriv != driReadPriv) {
255 ctx->rPriv = driReadPriv;
256 read->texture_stamp = driReadPriv->lastStamp - 1;
257 }
258
259 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
260
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]
263 && ctx->pp)
264 pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
265 draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
266
267 return GL_TRUE;
268 }
269
270 struct dri_context *
271 dri_get_current(__DRIscreen *sPriv)
272 {
273 struct dri_screen *screen = dri_screen(sPriv);
274 struct st_api *stapi = screen->st_api;
275 struct st_context_iface *st;
276
277 st = stapi->get_current(stapi);
278
279 return (struct dri_context *) (st) ? st->st_manager_private : NULL;
280 }
281
282 /* vim: set sw=3 ts=8 sts=3 expandtab: */