state_trackers/dri/sw: Convert to automake
[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 }
58
59 GLboolean
60 dri_create_context(gl_api api, const struct gl_config * visual,
61 __DRIcontext * cPriv,
62 unsigned major_version,
63 unsigned minor_version,
64 uint32_t flags,
65 unsigned *error,
66 void *sharedContextPrivate)
67 {
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;
75
76 memset(&attribs, 0, sizeof(attribs));
77 switch (api) {
78 case API_OPENGLES:
79 attribs.profile = ST_PROFILE_OPENGL_ES1;
80 break;
81 case API_OPENGLES2:
82 attribs.profile = ST_PROFILE_OPENGL_ES2;
83 break;
84 case API_OPENGL_COMPAT:
85 case API_OPENGL_CORE:
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;
90
91 if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
92 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
93
94 if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
95 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
96 break;
97 default:
98 *error = __DRI_CTX_ERROR_BAD_API;
99 goto fail;
100 }
101
102 if (sharedContextPrivate) {
103 st_share = ((struct dri_context *)sharedContextPrivate)->st;
104 }
105
106 ctx = CALLOC_STRUCT(dri_context);
107 if (ctx == NULL) {
108 *error = __DRI_CTX_ERROR_NO_MEMORY;
109 goto fail;
110 }
111
112 cPriv->driverPrivate = ctx;
113 ctx->cPriv = cPriv;
114 ctx->sPriv = sPriv;
115
116 driParseConfigFiles(&ctx->optionCache,
117 &screen->optionCache, sPriv->myNum, driver_descriptor.name);
118
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,
122 st_share);
123 if (ctx->st == NULL) {
124 switch (ctx_err) {
125 case ST_CONTEXT_SUCCESS:
126 *error = __DRI_CTX_ERROR_SUCCESS;
127 break;
128 case ST_CONTEXT_ERROR_NO_MEMORY:
129 *error = __DRI_CTX_ERROR_NO_MEMORY;
130 break;
131 case ST_CONTEXT_ERROR_BAD_API:
132 *error = __DRI_CTX_ERROR_BAD_API;
133 break;
134 case ST_CONTEXT_ERROR_BAD_VERSION:
135 *error = __DRI_CTX_ERROR_BAD_VERSION;
136 break;
137 case ST_CONTEXT_ERROR_BAD_FLAG:
138 *error = __DRI_CTX_ERROR_BAD_FLAG;
139 break;
140 case ST_CONTEXT_ERROR_UNKNOWN_ATTRIBUTE:
141 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
142 break;
143 case ST_CONTEXT_ERROR_UNKNOWN_FLAG:
144 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
145 break;
146 }
147 goto fail;
148 }
149 ctx->st->st_manager_private = (void *) ctx;
150 ctx->stapi = stapi;
151
152 // Context successfully created. See if post-processing is requested.
153 dri_pp_query(ctx);
154
155 if (ctx->st->cso_context) {
156 ctx->pp = pp_init(ctx->st->pipe, ctx->pp_enabled, ctx->st->cso_context);
157 }
158
159 *error = __DRI_CTX_ERROR_SUCCESS;
160 return GL_TRUE;
161
162 fail:
163 if (ctx && ctx->st)
164 ctx->st->destroy(ctx->st);
165
166 free(ctx);
167 return GL_FALSE;
168 }
169
170 void
171 dri_destroy_context(__DRIcontext * cPriv)
172 {
173 struct dri_context *ctx = dri_context(cPriv);
174
175 /* note: we are freeing values and nothing more because
176 * driParseConfigFiles allocated values only - the rest
177 * is owned by screen optionCache.
178 */
179 free(ctx->optionCache.values);
180
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.
185 */
186 ctx->st->flush(ctx->st, 0, NULL);
187 ctx->st->destroy(ctx->st);
188
189 if (ctx->pp) pp_free(ctx->pp);
190
191 free(ctx);
192 }
193
194 GLboolean
195 dri_unbind_context(__DRIcontext * cPriv)
196 {
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;
201
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.
208 */
209 stapi->make_current(stapi, NULL, NULL, NULL);
210 }
211 }
212
213 return GL_TRUE;
214 }
215
216 GLboolean
217 dri_make_current(__DRIcontext * cPriv,
218 __DRIdrawable * driDrawPriv,
219 __DRIdrawable * driReadPriv)
220 {
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);
226
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);
230
231 ++ctx->bind_count;
232
233 if (!driDrawPriv && !driReadPriv)
234 return ctx->stapi->make_current(ctx->stapi, ctx->st, NULL, NULL);
235 else if (!driDrawPriv || !driReadPriv)
236 return GL_FALSE;
237
238 if (ctx->dPriv != driDrawPriv) {
239 ctx->dPriv = driDrawPriv;
240 draw->texture_stamp = driDrawPriv->lastStamp - 1;
241 }
242 if (ctx->rPriv != driReadPriv) {
243 ctx->rPriv = driReadPriv;
244 read->texture_stamp = driReadPriv->lastStamp - 1;
245 }
246
247 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
248
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]
251 && ctx->pp)
252 pp_init_fbos(ctx->pp, draw->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
253 draw->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
254
255 return GL_TRUE;
256 }
257
258 struct dri_context *
259 dri_get_current(__DRIscreen *sPriv)
260 {
261 struct dri_screen *screen = dri_screen(sPriv);
262 struct st_api *stapi = screen->st_api;
263 struct st_context_iface *st;
264
265 st = stapi->get_current(stapi);
266
267 return (struct dri_context *) (st) ? st->st_manager_private : NULL;
268 }
269
270 /* vim: set sw=3 ts=8 sts=3 expandtab: */