Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / state_trackers / glx / 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 TUNGSTEN GRAPHICS 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
29 #include "dri_screen.h"
30 #include "dri_context.h"
31 #include "dri_winsys.h"
32
33 #include "state_tracker/st_public.h"
34 #include "state_tracker/st_context.h"
35 #include "pipe/p_context.h"
36
37 #include "util/u_memory.h"
38
39
40 GLboolean
41 dri_create_context(const __GLcontextModes *visual,
42 __DRIcontextPrivate *cPriv,
43 void *sharedContextPrivate)
44 {
45 __DRIscreenPrivate *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
62 driParseConfigFiles(&ctx->optionCache,
63 &screen->optionCache,
64 sPriv->myNum,
65 "dri");
66
67 ctx->pipe = screen->pipe_screen->create_context(screen->pipe_screen,
68 screen->pipe_winsys,
69 hw_winsys );
70 if (ctx->pipe == NULL)
71 goto fail;
72
73 ctx->pipe->priv = ctx; /* I guess */
74
75 ctx->st = st_create_context(ctx->pipe, visual, st_share);
76 if (ctx->st == NULL)
77 goto fail;
78
79 dri_init_extensions( ctx );
80
81 return GL_TRUE;
82
83 fail:
84 if (ctx && ctx->st)
85 st_destroy_context( ctx->st );
86
87 if (ctx && ctx->pipe)
88 ctx->pipe->destroy( ctx->pipe );
89
90 FREE(ctx);
91 return FALSE;
92 }
93
94
95 void
96 dri_destroy_context(__DRIcontextPrivate *cPriv)
97 {
98 struct dri_context *ctx = dri_context(cPriv);
99 struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
100 struct pipe_winsys *winsys = screen->winsys;
101
102 /* No particular reason to wait for command completion before
103 * destroying a context, but it is probably worthwhile flushing it
104 * to avoid having to add code elsewhere to cope with flushing a
105 * partially destroyed context.
106 */
107 st_flush(ctx->st);
108
109 if (screen->dummyContext == ctx)
110 screen->dummyContext = NULL;
111
112 /* Also frees ctx->pipe?
113 */
114 st_destroy_context(ctx->st);
115
116 FREE(ctx);
117 }
118
119
120 GLboolean
121 dri_unbind_context(__DRIcontextPrivate *cPriv)
122 {
123 struct dri_context *ctx = dri_context(cPriv);
124 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
125 /* XXX make_current(NULL)? */
126 return GL_TRUE;
127 }
128
129
130 GLboolean
131 dri_make_current(__DRIcontextPrivate *cPriv,
132 __DRIdrawablePrivate *driDrawPriv,
133 __DRIdrawablePrivate *driReadPriv)
134 {
135 if (cPriv) {
136 struct dri_context *ctx = dri_context(cPriv);
137 struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
138 struct dri_drawable *draw = dri_drawable(driDrawPriv);
139 struct dri_drawable *read = dri_drawable(driReadPriv);
140
141 /* This is for situations in which we need a rendering context but
142 * there may not be any currently bound.
143 */
144 screen->dummyContext = ctx;
145
146 st_make_current( ctx->st,
147 draw->stfb,
148 read->stfb );
149
150 ctx->dPriv = driDrawPriv;
151
152 /* Update window sizes if necessary:
153 */
154 if (draw->stamp != driDrawPriv->lastStamp) {
155 dri_update_window_size( draw );
156 }
157
158 if (read->stamp != driReadPriv->lastStamp) {
159 dri_update_window_size( read );
160 }
161
162 }
163 else {
164 st_make_current(NULL, NULL, NULL);
165 }
166
167 return GL_TRUE;
168 }