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