st/dri: Use enum st_api_type internally.
[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_visual stvis;
61
62 switch (api) {
63 case API_OPENGL:
64 stapi = screen->st_api[ST_API_OPENGL];
65 break;
66 case API_OPENGLES:
67 stapi = screen->st_api[ST_API_OPENGL_ES1];
68 break;
69 case API_OPENGLES2:
70 stapi = screen->st_api[ST_API_OPENGL_ES2];
71 break;
72 default:
73 stapi = NULL;
74 break;
75 }
76 if (!stapi)
77 return GL_FALSE;
78
79 if (sharedContextPrivate) {
80 st_share = ((struct dri_context *)sharedContextPrivate)->st;
81 }
82
83 ctx = CALLOC_STRUCT(dri_context);
84 if (ctx == NULL)
85 goto fail;
86
87 cPriv->driverPrivate = ctx;
88 ctx->cPriv = cPriv;
89 ctx->sPriv = sPriv;
90 ctx->lock = screen->drmLock;
91
92 driParseConfigFiles(&ctx->optionCache,
93 &screen->optionCache, sPriv->myNum, "dri");
94
95 dri_fill_st_visual(&stvis, screen, visual);
96 ctx->st = stapi->create_context(stapi, &screen->base, &stvis, st_share);
97 if (ctx->st == NULL)
98 goto fail;
99 ctx->st->st_manager_private = (void *) ctx;
100 ctx->stapi = stapi;
101
102 dri_init_extensions(ctx);
103
104 return GL_TRUE;
105
106 fail:
107 if (ctx && ctx->st)
108 ctx->st->destroy(ctx->st);
109
110 FREE(ctx);
111 return GL_FALSE;
112 }
113
114 void
115 dri_destroy_context(__DRIcontext * cPriv)
116 {
117 struct dri_context *ctx = dri_context(cPriv);
118
119 /* note: we are freeing values and nothing more because
120 * driParseConfigFiles allocated values only - the rest
121 * is owned by screen optionCache.
122 */
123 FREE(ctx->optionCache.values);
124
125 /* No particular reason to wait for command completion before
126 * destroying a context, but it is probably worthwhile flushing it
127 * to avoid having to add code elsewhere to cope with flushing a
128 * partially destroyed context.
129 */
130 ctx->st->flush(ctx->st, 0, NULL);
131 ctx->st->destroy(ctx->st);
132
133 FREE(ctx);
134 }
135
136 GLboolean
137 dri_unbind_context(__DRIcontext * cPriv)
138 {
139 /* dri_util.c ensures cPriv is not null */
140 struct dri_context *ctx = dri_context(cPriv);
141
142 if (--ctx->bind_count == 0) {
143 if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
144 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
145 ctx->stapi->make_current(ctx->stapi, NULL, NULL, NULL);
146 }
147 }
148
149 return GL_TRUE;
150 }
151
152 GLboolean
153 dri_make_current(__DRIcontext * cPriv,
154 __DRIdrawable * driDrawPriv,
155 __DRIdrawable * driReadPriv)
156 {
157 /* dri_util.c ensures cPriv is not null */
158 struct dri_context *ctx = dri_context(cPriv);
159 struct dri_drawable *draw = dri_drawable(driDrawPriv);
160 struct dri_drawable *read = dri_drawable(driReadPriv);
161 struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
162
163 if (old_st && old_st != ctx->st)
164 old_st->flush(old_st, PIPE_FLUSH_RENDER_CACHE, NULL);
165
166 ++ctx->bind_count;
167
168 if (ctx->dPriv != driDrawPriv) {
169 ctx->dPriv = driDrawPriv;
170 draw->texture_stamp = driDrawPriv->lastStamp - 1;
171 }
172 if (ctx->rPriv != driReadPriv) {
173 ctx->rPriv = driReadPriv;
174 read->texture_stamp = driReadPriv->lastStamp - 1;
175 }
176
177 ctx->stapi->make_current(ctx->stapi, ctx->st, &draw->base, &read->base);
178
179 return GL_TRUE;
180 }
181
182 struct dri_context *
183 dri_get_current(__DRIscreen *sPriv)
184 {
185 struct dri_screen *screen = dri_screen(sPriv);
186 struct st_api *stapi;
187 struct st_context_iface *st = NULL;
188 gl_api api;
189
190 /* XXX: How do we do this when the screen supports
191 multiple rendering API's? Pick the first one,
192 like this? (NB: all three API's use the same
193 implementation of get_current (see st_manager.c),
194 so maybe it doesn't matter right now since
195 they'll all return the same result.) */
196 for (api = API_OPENGL; api <= API_OPENGLES2; ++api) {
197 stapi = screen->st_api[api];
198 if (!stapi)
199 continue;
200 st = stapi->get_current(stapi);
201 if (st)
202 break;
203 }
204
205 return (struct dri_context *) (st) ? st->st_manager_private : NULL;
206 }
207
208 /* vim: set sw=3 ts=8 sts=3 expandtab: */