Merge branch 'master' into i915-unification
[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->drmMinor;
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 radeon->vblank_flags = (radeon->radeonScreen->irq != 0)
181 ? driGetDefaultVBlankFlags(&radeon->optionCache) : VBLANK_FLAG_NO_IRQ;
182
183 (*dri_interface->getUST) (&radeon->swap_ust);
184
185 return GL_TRUE;
186 }
187
188
189 /**
190 * Cleanup common context fields.
191 * Called by r200DestroyContext/r300DestroyContext
192 */
193 void radeonCleanupContext(radeonContextPtr radeon)
194 {
195 /* _mesa_destroy_context() might result in calls to functions that
196 * depend on the DriverCtx, so don't set it to NULL before.
197 *
198 * radeon->glCtx->DriverCtx = NULL;
199 */
200
201 /* free the Mesa context */
202 _mesa_destroy_context(radeon->glCtx);
203
204 if (radeon->state.scissor.pClipRects) {
205 FREE(radeon->state.scissor.pClipRects);
206 radeon->state.scissor.pClipRects = 0;
207 }
208 }
209
210
211 /**
212 * Swap front and back buffer.
213 */
214 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
215 {
216 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
217 radeonContextPtr radeon;
218 GLcontext *ctx;
219
220 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
221 ctx = radeon->glCtx;
222
223 if (ctx->Visual.doubleBufferMode) {
224 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
225 if (radeon->doPageFlip) {
226 radeonPageFlip(dPriv);
227 } else {
228 radeonCopyBuffer(dPriv, NULL);
229 }
230 }
231 } else {
232 /* XXX this shouldn't be an error but we can't handle it for now */
233 _mesa_problem(NULL, "%s: drawable has no context!",
234 __FUNCTION__);
235 }
236 }
237
238 void radeonCopySubBuffer(__DRIdrawablePrivate * dPriv,
239 int x, int y, int w, int h )
240 {
241 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
242 radeonContextPtr radeon;
243 GLcontext *ctx;
244
245 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
246 ctx = radeon->glCtx;
247
248 if (ctx->Visual.doubleBufferMode) {
249 drm_clip_rect_t rect;
250 rect.x1 = x + dPriv->x;
251 rect.y1 = (dPriv->h - y - h) + dPriv->y;
252 rect.x2 = rect.x1 + w;
253 rect.y2 = rect.y1 + h;
254 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
255 radeonCopyBuffer(dPriv, &rect);
256 }
257 } else {
258 /* XXX this shouldn't be an error but we can't handle it for now */
259 _mesa_problem(NULL, "%s: drawable has no context!",
260 __FUNCTION__);
261 }
262 }
263
264 /* Force the context `c' to be the current context and associate with it
265 * buffer `b'.
266 */
267 GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
268 __DRIdrawablePrivate * driDrawPriv,
269 __DRIdrawablePrivate * driReadPriv)
270 {
271 if (driContextPriv) {
272 radeonContextPtr radeon =
273 (radeonContextPtr) driContextPriv->driverPrivate;
274
275 if (RADEON_DEBUG & DEBUG_DRI)
276 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
277 radeon->glCtx);
278
279 if (radeon->dri.drawable != driDrawPriv) {
280 driDrawableInitVBlank(driDrawPriv,
281 radeon->vblank_flags,
282 &radeon->vbl_seq);
283 }
284
285 radeon->dri.readable = driReadPriv;
286
287 if (radeon->dri.drawable != driDrawPriv ||
288 radeon->lastStamp != driDrawPriv->lastStamp) {
289 radeon->dri.drawable = driDrawPriv;
290
291 radeonSetCliprects(radeon);
292 r300UpdateViewportOffset(radeon->glCtx);
293 }
294
295 _mesa_make_current(radeon->glCtx,
296 (GLframebuffer *) driDrawPriv->
297 driverPrivate,
298 (GLframebuffer *) driReadPriv->
299 driverPrivate);
300
301 _mesa_update_state(radeon->glCtx);
302
303 radeonUpdatePageFlipping(radeon);
304 } else {
305 if (RADEON_DEBUG & DEBUG_DRI)
306 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
307 _mesa_make_current(0, 0, 0);
308 }
309
310 if (RADEON_DEBUG & DEBUG_DRI)
311 fprintf(stderr, "End %s\n", __FUNCTION__);
312 return GL_TRUE;
313 }
314
315 /* Force the context `c' to be unbound from its buffer.
316 */
317 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv)
318 {
319 radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
320
321 if (RADEON_DEBUG & DEBUG_DRI)
322 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
323 radeon->glCtx);
324
325 return GL_TRUE;
326 }
327