More vblank cleanups.
[mesa.git] / src / mesa / drivers / dri / r300 / radeon_context.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /**
31 * \file radeon_context.c
32 * Common context initialization.
33 *
34 * \author Keith Whitwell <keith@tungstengraphics.com>
35 */
36
37 #include <dlfcn.h>
38
39 #include "glheader.h"
40 #include "imports.h"
41 #include "context.h"
42 #include "state.h"
43 #include "matrix.h"
44 #include "framebuffer.h"
45
46 #include "drivers/common/driverfuncs.h"
47 #include "swrast/swrast.h"
48
49 #include "radeon_screen.h"
50 #include "radeon_ioctl.h"
51 #include "radeon_macros.h"
52 #include "radeon_reg.h"
53
54 #include "radeon_state.h"
55 #include "r300_state.h"
56
57 #include "utils.h"
58 #include "vblank.h"
59 #include "xmlpool.h" /* for symbolic values of enum-type options */
60
61 #define DRIVER_DATE "20060815"
62
63
64 /* Return various strings for glGetString().
65 */
66 static const GLubyte *radeonGetString(GLcontext * ctx, GLenum name)
67 {
68 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
69 static char buffer[128];
70
71 switch (name) {
72 case GL_VENDOR:
73 if (IS_R300_CLASS(radeon->radeonScreen))
74 return (GLubyte *) "DRI R300 Project";
75 else
76 return (GLubyte *) "Tungsten Graphics, Inc.";
77
78 case GL_RENDERER:
79 {
80 unsigned offset;
81 GLuint agp_mode = (radeon->radeonScreen->card_type==RADEON_CARD_PCI) ? 0 :
82 radeon->radeonScreen->AGPMode;
83 const char* chipname;
84
85 if (IS_R300_CLASS(radeon->radeonScreen))
86 chipname = "R300";
87 else
88 chipname = "R200";
89
90 offset = driGetRendererString(buffer, chipname, DRIVER_DATE,
91 agp_mode);
92
93 if (IS_R300_CLASS(radeon->radeonScreen)) {
94 sprintf(&buffer[offset], " %sTCL",
95 (radeon->radeonScreen->chip_flags & RADEON_CHIPSET_TCL)
96 ? "" : "NO-");
97 } else {
98 sprintf(&buffer[offset], " %sTCL",
99 !(radeon->TclFallback & RADEON_TCL_FALLBACK_TCL_DISABLE)
100 ? "" : "NO-");
101 }
102
103 return (GLubyte *) buffer;
104 }
105
106 default:
107 return NULL;
108 }
109 }
110
111 /* Initialize the driver's misc functions.
112 */
113 static void radeonInitDriverFuncs(struct dd_function_table *functions)
114 {
115 functions->GetString = radeonGetString;
116 }
117
118
119 /**
120 * Create and initialize all common fields of the context,
121 * including the Mesa context itself.
122 */
123 GLboolean radeonInitContext(radeonContextPtr radeon,
124 struct dd_function_table* functions,
125 const __GLcontextModes * glVisual,
126 __DRIcontextPrivate * driContextPriv,
127 void *sharedContextPrivate)
128 {
129 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
130 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
131 GLcontext* ctx;
132 GLcontext* shareCtx;
133 int fthrottle_mode;
134
135 /* Fill in additional standard functions. */
136 radeonInitDriverFuncs(functions);
137
138 /* Allocate and initialize the Mesa context */
139 if (sharedContextPrivate)
140 shareCtx = ((radeonContextPtr)sharedContextPrivate)->glCtx;
141 else
142 shareCtx = NULL;
143 radeon->glCtx = _mesa_create_context(glVisual, shareCtx,
144 functions, (void *)radeon);
145 if (!radeon->glCtx)
146 return GL_FALSE;
147
148 ctx = radeon->glCtx;
149 driContextPriv->driverPrivate = radeon;
150
151 /* DRI fields */
152 radeon->dri.context = driContextPriv;
153 radeon->dri.screen = sPriv;
154 radeon->dri.drawable = NULL;
155 radeon->dri.readable = NULL;
156 radeon->dri.hwContext = driContextPriv->hHWContext;
157 radeon->dri.hwLock = &sPriv->pSAREA->lock;
158 radeon->dri.fd = sPriv->fd;
159 radeon->dri.drmMinor = sPriv->drm_version.minor;
160
161 radeon->radeonScreen = screen;
162 radeon->sarea = (drm_radeon_sarea_t *) ((GLubyte *) sPriv->pSAREA +
163 screen->sarea_priv_offset);
164
165 /* Setup IRQs */
166 fthrottle_mode = driQueryOptioni(&radeon->optionCache, "fthrottle_mode");
167 radeon->iw.irq_seq = -1;
168 radeon->irqsEmitted = 0;
169 radeon->do_irqs = (fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
170 radeon->radeonScreen->irq);
171
172 radeon->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
173
174 if (!radeon->do_irqs)
175 fprintf(stderr,
176 "IRQ's not enabled, falling back to %s: %d %d\n",
177 radeon->do_usleeps ? "usleeps" : "busy waits",
178 fthrottle_mode, radeon->radeonScreen->irq);
179
180 (*dri_interface->getUST) (&radeon->swap_ust);
181
182 return GL_TRUE;
183 }
184
185
186 /**
187 * Cleanup common context fields.
188 * Called by r200DestroyContext/r300DestroyContext
189 */
190 void radeonCleanupContext(radeonContextPtr radeon)
191 {
192 /* _mesa_destroy_context() might result in calls to functions that
193 * depend on the DriverCtx, so don't set it to NULL before.
194 *
195 * radeon->glCtx->DriverCtx = NULL;
196 */
197
198 /* free the Mesa context */
199 _mesa_destroy_context(radeon->glCtx);
200
201 if (radeon->state.scissor.pClipRects) {
202 FREE(radeon->state.scissor.pClipRects);
203 radeon->state.scissor.pClipRects = 0;
204 }
205 }
206
207
208 /**
209 * Swap front and back buffer.
210 */
211 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
212 {
213 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
214 radeonContextPtr radeon;
215 GLcontext *ctx;
216
217 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
218 ctx = radeon->glCtx;
219
220 if (ctx->Visual.doubleBufferMode) {
221 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
222 if (radeon->doPageFlip) {
223 radeonPageFlip(dPriv);
224 } else {
225 radeonCopyBuffer(dPriv, NULL);
226 }
227 }
228 } else {
229 /* XXX this shouldn't be an error but we can't handle it for now */
230 _mesa_problem(NULL, "%s: drawable has no context!",
231 __FUNCTION__);
232 }
233 }
234
235 void radeonCopySubBuffer(__DRIdrawablePrivate * dPriv,
236 int x, int y, int w, int h )
237 {
238 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
239 radeonContextPtr radeon;
240 GLcontext *ctx;
241
242 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
243 ctx = radeon->glCtx;
244
245 if (ctx->Visual.doubleBufferMode) {
246 drm_clip_rect_t rect;
247 rect.x1 = x + dPriv->x;
248 rect.y1 = (dPriv->h - y - h) + dPriv->y;
249 rect.x2 = rect.x1 + w;
250 rect.y2 = rect.y1 + h;
251 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
252 radeonCopyBuffer(dPriv, &rect);
253 }
254 } else {
255 /* XXX this shouldn't be an error but we can't handle it for now */
256 _mesa_problem(NULL, "%s: drawable has no context!",
257 __FUNCTION__);
258 }
259 }
260
261 /* Force the context `c' to be the current context and associate with it
262 * buffer `b'.
263 */
264 GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
265 __DRIdrawablePrivate * driDrawPriv,
266 __DRIdrawablePrivate * driReadPriv)
267 {
268 if (driContextPriv) {
269 radeonContextPtr radeon =
270 (radeonContextPtr) driContextPriv->driverPrivate;
271
272 if (RADEON_DEBUG & DEBUG_DRI)
273 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
274 radeon->glCtx);
275
276 if (radeon->dri.drawable != driDrawPriv) {
277 if (driDrawPriv->swap_interval == (unsigned)-1) {
278 driDrawPriv->vblFlags =
279 (radeon->radeonScreen->irq != 0)
280 ? driGetDefaultVBlankFlags(&radeon->
281 optionCache)
282 : VBLANK_FLAG_NO_IRQ;
283
284 driDrawableInitVBlank(driDrawPriv);
285 }
286 }
287
288 radeon->dri.readable = driReadPriv;
289
290 if (radeon->dri.drawable != driDrawPriv ||
291 radeon->lastStamp != driDrawPriv->lastStamp) {
292 radeon->dri.drawable = driDrawPriv;
293
294 radeonSetCliprects(radeon);
295 r300UpdateViewportOffset(radeon->glCtx);
296 }
297
298 _mesa_make_current(radeon->glCtx,
299 (GLframebuffer *) driDrawPriv->
300 driverPrivate,
301 (GLframebuffer *) driReadPriv->
302 driverPrivate);
303
304 _mesa_update_state(radeon->glCtx);
305
306 radeonUpdatePageFlipping(radeon);
307 } else {
308 if (RADEON_DEBUG & DEBUG_DRI)
309 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
310 _mesa_make_current(0, 0, 0);
311 }
312
313 if (RADEON_DEBUG & DEBUG_DRI)
314 fprintf(stderr, "End %s\n", __FUNCTION__);
315 return GL_TRUE;
316 }
317
318 /* Force the context `c' to be unbound from its buffer.
319 */
320 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv)
321 {
322 radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
323
324 if (RADEON_DEBUG & DEBUG_DRI)
325 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
326 radeon->glCtx);
327
328 return GL_TRUE;
329 }
330