st/dri: Reshuffle files and make it obvious which files are shared
[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 "dri_st_api.h"
36
37 #include "pipe/p_context.h"
38 #include "util/u_memory.h"
39
40 GLboolean
41 dri_create_context(const __GLcontextModes * visual,
42 __DRIcontext * cPriv, void *sharedContextPrivate)
43 {
44 struct st_api *stapi = dri_get_st_api();
45 __DRIscreen *sPriv = cPriv->driScreenPriv;
46 struct dri_screen *screen = dri_screen(sPriv);
47 struct dri_context *ctx = NULL;
48 struct st_context_iface *st_share = NULL;
49 struct st_visual stvis;
50
51 if (sharedContextPrivate) {
52 st_share = ((struct dri_context *)sharedContextPrivate)->st;
53 }
54
55 ctx = CALLOC_STRUCT(dri_context);
56 if (ctx == NULL)
57 goto fail;
58
59 cPriv->driverPrivate = ctx;
60 ctx->cPriv = cPriv;
61 ctx->sPriv = sPriv;
62 ctx->lock = screen->drmLock;
63
64 driParseConfigFiles(&ctx->optionCache,
65 &screen->optionCache, sPriv->myNum, "dri");
66
67 dri_fill_st_visual(&stvis, screen, visual);
68 ctx->st = stapi->create_context(stapi, screen->smapi, &stvis, st_share);
69 if (ctx->st == NULL)
70 goto fail;
71 ctx->st->st_manager_private = (void *) ctx;
72
73 dri_init_extensions(ctx);
74
75 return GL_TRUE;
76
77 fail:
78 if (ctx && ctx->st)
79 ctx->st->destroy(ctx->st);
80
81 FREE(ctx);
82 return FALSE;
83 }
84
85 void
86 dri_destroy_context(__DRIcontext * cPriv)
87 {
88 struct dri_context *ctx = dri_context(cPriv);
89
90 /* note: we are freeing values and nothing more because
91 * driParseConfigFiles allocated values only - the rest
92 * is owned by screen optionCache.
93 */
94 FREE(ctx->optionCache.values);
95
96 /* No particular reason to wait for command completion before
97 * destroying a context, but it is probably worthwhile flushing it
98 * to avoid having to add code elsewhere to cope with flushing a
99 * partially destroyed context.
100 */
101 ctx->st->flush(ctx->st, 0, NULL);
102 ctx->st->destroy(ctx->st);
103
104 FREE(ctx);
105 }
106
107 GLboolean
108 dri_unbind_context(__DRIcontext * cPriv)
109 {
110 struct st_api *stapi = dri_get_st_api();
111
112 if (cPriv) {
113 struct dri_context *ctx = dri_context(cPriv);
114
115 if (--ctx->bind_count == 0) {
116 if (ctx->st == stapi->get_current(stapi)) {
117 ctx->st->flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
118 stapi->make_current(stapi, NULL, NULL, NULL);
119 }
120 }
121 }
122
123 return GL_TRUE;
124 }
125
126 GLboolean
127 dri_make_current(__DRIcontext * cPriv,
128 __DRIdrawable * driDrawPriv,
129 __DRIdrawable * driReadPriv)
130 {
131 struct st_api *stapi = dri_get_st_api();
132
133 if (cPriv) {
134 struct dri_context *ctx = dri_context(cPriv);
135 struct dri_drawable *draw = dri_drawable(driDrawPriv);
136 struct dri_drawable *read = dri_drawable(driReadPriv);
137 struct st_context_iface *old_st;
138
139 old_st = stapi->get_current(stapi);
140 if (old_st && old_st != ctx->st)
141 ctx->st->flush(old_st, PIPE_FLUSH_RENDER_CACHE, NULL);
142
143 ++ctx->bind_count;
144
145 if (ctx->dPriv != driDrawPriv) {
146 ctx->dPriv = driDrawPriv;
147 draw->texture_stamp = driDrawPriv->lastStamp - 1;
148 }
149 if (ctx->rPriv != driReadPriv) {
150 ctx->rPriv = driReadPriv;
151 read->texture_stamp = driReadPriv->lastStamp - 1;
152 }
153
154 stapi->make_current(stapi, ctx->st, draw->stfb, read->stfb);
155 }
156 else {
157 stapi->make_current(stapi, NULL, NULL, NULL);
158 }
159
160 return GL_TRUE;
161 }
162
163 struct dri_context *
164 dri_get_current(void)
165 {
166 struct st_api *stapi = dri_get_st_api();
167 struct st_context_iface *st;
168
169 st = stapi->get_current(stapi);
170
171 return (struct dri_context *) (st) ? st->st_manager_private : NULL;
172 }
173
174 /* vim: set sw=3 ts=8 sts=3 expandtab: */