Merge branch '7.8' into master
[mesa.git] / src / gallium / state_trackers / dri / 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 "dri_screen.h"
33
34 #include "dri_drawable.h"
35 #include "state_tracker/drm_api.h"
36 #include "state_tracker/dri1_api.h"
37 #include "state_tracker/st_public.h"
38 #include "pipe/p_context.h"
39
40 #include "dri_context.h"
41
42 #include "util/u_memory.h"
43
44 GLboolean
45 dri_create_context(const __GLcontextModes * visual,
46 __DRIcontext * cPriv, void *sharedContextPrivate)
47 {
48 __DRIscreen *sPriv = cPriv->driScreenPriv;
49 struct dri_screen *screen = dri_screen(sPriv);
50 struct dri_context *ctx = NULL;
51 struct st_context *st_share = NULL;
52
53 if (sharedContextPrivate) {
54 st_share = ((struct dri_context *)sharedContextPrivate)->st;
55 }
56
57 ctx = CALLOC_STRUCT(dri_context);
58 if (ctx == NULL)
59 goto fail;
60
61 cPriv->driverPrivate = ctx;
62 ctx->cPriv = cPriv;
63 ctx->sPriv = sPriv;
64 ctx->lock = screen->drmLock;
65 ctx->d_stamp = -1;
66 ctx->r_stamp = -1;
67
68 driParseConfigFiles(&ctx->optionCache,
69 &screen->optionCache, sPriv->myNum, "dri");
70
71 ctx->pipe = screen->pipe_screen->context_create( screen->pipe_screen,
72 ctx );
73
74 if (ctx->pipe == NULL)
75 goto fail;
76
77 ctx->st = st_create_context(ctx->pipe, visual, st_share);
78 if (ctx->st == NULL)
79 goto fail;
80
81 dri_init_extensions(ctx);
82
83 return GL_TRUE;
84
85 fail:
86 if (ctx && ctx->st)
87 st_destroy_context(ctx->st);
88
89 if (ctx && ctx->pipe)
90 ctx->pipe->destroy(ctx->pipe);
91
92 FREE(ctx);
93 return FALSE;
94 }
95
96 void
97 dri_destroy_context(__DRIcontext * cPriv)
98 {
99 struct dri_context *ctx = dri_context(cPriv);
100
101 /* note: we are freeing values and nothing more because
102 * driParseConfigFiles allocated values only - the rest
103 * is owned by screen optionCache.
104 */
105 FREE(ctx->optionCache.values);
106
107 /* No particular reason to wait for command completion before
108 * destroying a context, but it is probably worthwhile flushing it
109 * to avoid having to add code elsewhere to cope with flushing a
110 * partially destroyed context.
111 */
112 st_flush(ctx->st, 0, NULL);
113
114 /* Also frees ctx->pipe?
115 */
116 st_destroy_context(ctx->st);
117
118 FREE(ctx);
119 }
120
121 GLboolean
122 dri_unbind_context(__DRIcontext * cPriv)
123 {
124 if (cPriv) {
125 struct dri_context *ctx = dri_context(cPriv);
126
127 if (--ctx->bind_count == 0) {
128 if (ctx->st && ctx->st == st_get_current()) {
129 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
130 st_make_current(NULL, NULL, NULL, NULL);
131 }
132 }
133 }
134
135 return GL_TRUE;
136 }
137
138 GLboolean
139 dri_make_current(__DRIcontext * cPriv,
140 __DRIdrawable * driDrawPriv,
141 __DRIdrawable * driReadPriv)
142 {
143 if (cPriv) {
144 struct dri_context *ctx = dri_context(cPriv);
145 struct dri_drawable *draw = dri_drawable(driDrawPriv);
146 struct dri_drawable *read = dri_drawable(driReadPriv);
147 struct st_context *old_st = st_get_current();
148
149 if (old_st && old_st != ctx->st)
150 st_flush(old_st, PIPE_FLUSH_RENDER_CACHE, NULL);
151
152 ++ctx->bind_count;
153
154 if (ctx->dPriv != driDrawPriv) {
155 ctx->dPriv = driDrawPriv;
156 ctx->d_stamp = driDrawPriv->lastStamp - 1;
157 }
158 if (ctx->rPriv != driReadPriv) {
159 ctx->rPriv = driReadPriv;
160 ctx->r_stamp = driReadPriv->lastStamp - 1;
161 }
162
163 /* DRI co-state tracker currently overrides flush_frontbuffer.
164 * When this is fixed, will need to pass the drawable in the
165 * fourth parameter here so that when Mesa calls
166 * flush_frontbuffer directly (in front-buffer rendering), it
167 * will have access to the drawable argument:
168 */
169 st_make_current(ctx->st, draw->stfb, read->stfb, ctx);
170
171 if (__dri1_api_hooks) {
172 dri1_update_drawables(ctx, draw, read);
173 } else {
174 dri_update_buffer(ctx->pipe->screen,
175 ctx->pipe->priv);
176 }
177 } else {
178 st_make_current(NULL, NULL, NULL, NULL);
179 }
180
181 return GL_TRUE;
182 }
183
184 static void
185 st_dri_lock(struct pipe_context *pipe)
186 {
187 dri_lock((struct dri_context *)pipe->priv);
188 }
189
190 static void
191 st_dri_unlock(struct pipe_context *pipe)
192 {
193 dri_unlock((struct dri_context *)pipe->priv);
194 }
195
196 static boolean
197 st_dri_is_locked(struct pipe_context *pipe)
198 {
199 return ((struct dri_context *)pipe->priv)->isLocked;
200 }
201
202 static boolean
203 st_dri_lost_lock(struct pipe_context *pipe)
204 {
205 return ((struct dri_context *)pipe->priv)->wsLostLock;
206 }
207
208 static void
209 st_dri_clear_lost_lock(struct pipe_context *pipe)
210 {
211 ((struct dri_context *)pipe->priv)->wsLostLock = FALSE;
212 }
213
214 struct dri1_api_lock_funcs dri1_lf = {
215 .lock = st_dri_lock,
216 .unlock = st_dri_unlock,
217 .is_locked = st_dri_is_locked,
218 .is_lock_lost = st_dri_lost_lock,
219 .clear_lost_lock = st_dri_clear_lost_lock
220 };
221
222 /* vim: set sw=3 ts=8 sts=3 expandtab: */