gallium: Add context profile support to st_api.
[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
38 #include "pipe/p_context.h"
39 #include "state_tracker/st_context.h"
40
41 static void
42 dri_init_extensions(struct dri_context *ctx)
43 {
44 struct st_context *st = (struct st_context *) ctx->st;
45
46 /* New extensions should be added in mesa/state_tracker/st_extensions.c
47 * and not in this file. */
48 driInitExtensions(st->ctx, NULL, GL_FALSE);
49 }
50
51 GLboolean
52 dri_create_context(gl_api api, const __GLcontextModes * visual,
53 __DRIcontext * cPriv, void *sharedContextPrivate)
54 {
55 __DRIscreen *sPriv = cPriv->driScreenPriv;
56 struct dri_screen *screen = dri_screen(sPriv);
57 struct st_api *stapi;
58 struct dri_context *ctx = NULL;
59 struct st_context_iface *st_share = NULL;
60 struct st_context_attribs attribs;
61
62 memset(&attribs, 0, sizeof(attribs));
63 switch (api) {
64 case API_OPENGL:
65 stapi = screen->st_api[ST_API_OPENGL];
66 attribs.profile = ST_PROFILE_DEFAULT;
67 break;
68 case API_OPENGLES:
69 stapi = screen->st_api[ST_API_OPENGL_ES1];
70 attribs.profile = ST_PROFILE_OPENGL_ES1;
71 break;
72 case API_OPENGLES2:
73 stapi = screen->st_api[ST_API_OPENGL_ES2];
74 attribs.profile = ST_PROFILE_OPENGL_ES2;
75 break;
76 default:
77 stapi = NULL;
78 break;
79 }
80 if (!stapi)
81 return GL_FALSE;
82
83 if (sharedContextPrivate) {
84 st_share = ((struct dri_context *)sharedContextPrivate)->st;
85 }
86
87 ctx = CALLOC_STRUCT(dri_context);
88 if (ctx == NULL)
89 goto fail;
90
91 cPriv->driverPrivate = ctx;
92 ctx->cPriv = cPriv;
93 ctx->sPriv = sPriv;
94 ctx->lock = screen->drmLock;
95
96 driParseConfigFiles(&ctx->optionCache,
97 &screen->optionCache, sPriv->myNum, "dri");
98
99 dri_fill_st_visual(&attribs.visual, screen, visual);
100 ctx->st = stapi->create_context(stapi, &screen->base, &attribs, st_share);
101 if (ctx->st == NULL)
102 goto fail;
103 ctx->st->st_manager_private = (void *) ctx;
104 ctx->stapi = stapi;
105
106 /*
107 * libmesagallium.a that this state tracker will be linked to expects
108 * OpenGL's _glapi_table. That is, it expects libGL.so instead of
109 * libGLESv1_CM.so or libGLESv2.so. As there is no clean way to know the
110 * shared library the app links to, use the api as a simple check.
111 * It might be as well to simply remove this function call though.
112 */
113 if (api == API_OPENGL)
114 dri_init_extensions(ctx);
115
116 return GL_TRUE;
117
118 fail:
119 if (ctx && ctx->st)
120 ctx->st->destroy(ctx->st);
121
122 FREE(ctx);
123 return GL_FALSE;
124 }
125
126 void
127 dri_destroy_context(__DRIcontext * cPriv)
128 {
129 struct dri_context *ctx = dri_context(cPriv);
130
131 /* note: we are freeing values and nothing more because
132 * driParseConfigFiles allocated values only - the rest
133 * is owned by screen optionCache.
134 */
135 FREE(ctx->optionCache.values);
136
137 /* No particular reason to wait for command completion before
138 * destroying a context, but it is probably worthwhile flushing it
139 * to avoid having to add code elsewhere to cope with flushing a
140 * partially destroyed context.
141 */
142 ctx->st->flush(ctx->st, 0, NULL);
143 ctx->st->destroy(ctx->st);
144
145 FREE(ctx);
146 }
147
148 GLboolean
149 dri_unbind_context(__DRIcontext * cPriv)
150 {
151 /* dri_util.c ensures cPriv is not null */
152 struct dri_context *ctx = dri_context(cPriv);
153
154 if (--ctx->bind_count == 0) {
155 if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
156 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
157 ctx->stapi->make_current(ctx->stapi, NULL, NULL, NULL);
158 }
159 }
160
161 return GL_TRUE;
162 }
163
164 GLboolean
165 dri_make_current(__DRIcontext * cPriv,
166 __DRIdrawable * driDrawPriv,
167 __DRIdrawable * driReadPriv)
168 {
169 /* dri_util.c ensures cPriv is not null */
170 struct dri_context *ctx = dri_context(cPriv);
171 struct dri_drawable *draw = dri_drawable(driDrawPriv);
172 struct dri_drawable *read = dri_drawable(driReadPriv);
173 struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
174
175 if (old_st && old_st != ctx->st)
176 old_st->flush(old_st, PIPE_FLUSH_RENDER_CACHE, NULL);
177
178 ++ctx->bind_count;
179
180 if (ctx->dPriv != driDrawPriv) {
181 ctx->dPriv = driDrawPriv;
182 draw->texture_stamp = driDrawPriv->lastStamp - 1;
183 }
184 if (ctx->rPriv != driReadPriv) {
185 ctx->rPriv = driReadPriv;
186 read->texture_stamp = driReadPriv->lastStamp - 1;
187 }
188
189 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
190
191 return GL_TRUE;
192 }
193
194 struct dri_context *
195 dri_get_current(__DRIscreen *sPriv)
196 {
197 struct dri_screen *screen = dri_screen(sPriv);
198 struct st_api *stapi;
199 struct st_context_iface *st = NULL;
200 gl_api api;
201
202 /* XXX: How do we do this when the screen supports
203 multiple rendering API's? Pick the first one,
204 like this? (NB: all three API's use the same
205 implementation of get_current (see st_manager.c),
206 so maybe it doesn't matter right now since
207 they'll all return the same result.) */
208 for (api = API_OPENGL; api <= API_OPENGLES2; ++api) {
209 stapi = screen->st_api[api];
210 if (!stapi)
211 continue;
212 st = stapi->get_current(stapi);
213 if (st)
214 break;
215 }
216
217 return (struct dri_context *) (st) ? st->st_manager_private : NULL;
218 }
219
220 /* vim: set sw=3 ts=8 sts=3 expandtab: */