bfedae4dbdb8946fd8b7d02d3494d96c7b9ae364
[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 "r300_state.h"
55
56 #include "utils.h"
57 #include "vblank.h"
58 #include "xmlpool.h" /* for symbolic values of enum-type options */
59
60 #define DRIVER_DATE "20060815"
61
62
63 /* Return various strings for glGetString().
64 */
65 static const GLubyte *radeonGetString(GLcontext * ctx, GLenum name)
66 {
67 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
68 static char buffer[128];
69
70 switch (name) {
71 case GL_VENDOR:
72 return (GLubyte *) "Tungsten Graphics, Inc.";
73
74 case GL_RENDERER:
75 {
76 unsigned offset;
77 GLuint agp_mode = (radeon->radeonScreen->card_type==RADEON_CARD_PCI) ? 0 :
78 radeon->radeonScreen->AGPMode;
79 const char* chipname;
80
81 if (IS_R300_CLASS(radeon->radeonScreen))
82 chipname = "R300";
83 else
84 chipname = "R200";
85
86 offset = driGetRendererString(buffer, chipname, DRIVER_DATE,
87 agp_mode);
88
89 sprintf(&buffer[offset], " %sTCL",
90 !(radeon->TclFallback & RADEON_TCL_FALLBACK_TCL_DISABLE)
91 ? "" : "NO-");
92
93 return (GLubyte *) buffer;
94 }
95
96 default:
97 return NULL;
98 }
99 }
100
101
102 /* Return the width and height of the given buffer.
103 */
104 static void radeonGetBufferSize(GLframebuffer * buffer,
105 GLuint * width, GLuint * height)
106 {
107 GET_CURRENT_CONTEXT(ctx);
108 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
109
110 LOCK_HARDWARE(radeon);
111 *width = radeon->dri.drawable->w;
112 *height = radeon->dri.drawable->h;
113 UNLOCK_HARDWARE(radeon);
114 }
115
116
117 /* Initialize the driver's misc functions.
118 */
119 static void radeonInitDriverFuncs(struct dd_function_table *functions)
120 {
121 functions->GetBufferSize = radeonGetBufferSize;
122 functions->ResizeBuffers = _mesa_resize_framebuffer;
123 functions->GetString = radeonGetString;
124 }
125
126
127 /**
128 * Create and initialize all common fields of the context,
129 * including the Mesa context itself.
130 */
131 GLboolean radeonInitContext(radeonContextPtr radeon,
132 struct dd_function_table* functions,
133 const __GLcontextModes * glVisual,
134 __DRIcontextPrivate * driContextPriv,
135 void *sharedContextPrivate)
136 {
137 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
138 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
139 GLcontext* ctx;
140 GLcontext* shareCtx;
141 int fthrottle_mode;
142
143 /* Fill in additional standard functions. */
144 radeonInitDriverFuncs(functions);
145
146 /* Allocate and initialize the Mesa context */
147 if (sharedContextPrivate)
148 shareCtx = ((radeonContextPtr)sharedContextPrivate)->glCtx;
149 else
150 shareCtx = NULL;
151 radeon->glCtx = _mesa_create_context(glVisual, shareCtx,
152 functions, (void *)radeon);
153 if (!radeon->glCtx)
154 return GL_FALSE;
155
156 ctx = radeon->glCtx;
157 driContextPriv->driverPrivate = radeon;
158
159 /* DRI fields */
160 radeon->dri.context = driContextPriv;
161 radeon->dri.screen = sPriv;
162 radeon->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
163 radeon->dri.hwContext = driContextPriv->hHWContext;
164 radeon->dri.hwLock = &sPriv->pSAREA->lock;
165 radeon->dri.fd = sPriv->fd;
166 radeon->dri.drmMinor = sPriv->drmMinor;
167
168 radeon->radeonScreen = screen;
169 radeon->sarea = (drm_radeon_sarea_t *) ((GLubyte *) sPriv->pSAREA +
170 screen->sarea_priv_offset);
171
172 /* Setup IRQs */
173 fthrottle_mode = driQueryOptioni(&radeon->optionCache, "fthrottle_mode");
174 radeon->iw.irq_seq = -1;
175 radeon->irqsEmitted = 0;
176 radeon->do_irqs = (fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
177 radeon->radeonScreen->irq);
178
179 radeon->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
180
181 if (!radeon->do_irqs)
182 fprintf(stderr,
183 "IRQ's not enabled, falling back to %s: %d %d\n",
184 radeon->do_usleeps ? "usleeps" : "busy waits",
185 fthrottle_mode, radeon->radeonScreen->irq);
186
187 radeon->vblank_flags = (radeon->radeonScreen->irq != 0)
188 ? driGetDefaultVBlankFlags(&radeon->optionCache) : VBLANK_FLAG_NO_IRQ;
189
190 (*dri_interface->getUST) (&radeon->swap_ust);
191
192 return GL_TRUE;
193 }
194
195
196 /**
197 * Cleanup common context fields.
198 * Called by r200DestroyContext/r300DestroyContext
199 */
200 void radeonCleanupContext(radeonContextPtr radeon)
201 {
202 /* _mesa_destroy_context() might result in calls to functions that
203 * depend on the DriverCtx, so don't set it to NULL before.
204 *
205 * radeon->glCtx->DriverCtx = NULL;
206 */
207
208 /* free the Mesa context */
209 _mesa_destroy_context(radeon->glCtx);
210
211 if (radeon->state.scissor.pClipRects) {
212 FREE(radeon->state.scissor.pClipRects);
213 radeon->state.scissor.pClipRects = 0;
214 }
215 }
216
217
218 /**
219 * Swap front and back buffer.
220 */
221 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
222 {
223 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
224 radeonContextPtr radeon;
225 GLcontext *ctx;
226
227 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
228 ctx = radeon->glCtx;
229
230 if (ctx->Visual.doubleBufferMode) {
231 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
232 if (radeon->doPageFlip) {
233 radeonPageFlip(dPriv);
234 } else {
235 radeonCopyBuffer(dPriv, NULL);
236 }
237 }
238 } else {
239 /* XXX this shouldn't be an error but we can't handle it for now */
240 _mesa_problem(NULL, "%s: drawable has no context!",
241 __FUNCTION__);
242 }
243 }
244
245 void radeonCopySubBuffer(__DRIdrawablePrivate * dPriv,
246 int x, int y, int w, int h )
247 {
248 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
249 radeonContextPtr radeon;
250 GLcontext *ctx;
251
252 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
253 ctx = radeon->glCtx;
254
255 if (ctx->Visual.doubleBufferMode) {
256 drm_clip_rect_t rect;
257 rect.x1 = x + dPriv->x;
258 rect.y1 = (dPriv->h - y - h) + dPriv->y;
259 rect.x2 = rect.x1 + w;
260 rect.y2 = rect.y1 + h;
261 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
262 radeonCopyBuffer(dPriv, &rect);
263 }
264 } else {
265 /* XXX this shouldn't be an error but we can't handle it for now */
266 _mesa_problem(NULL, "%s: drawable has no context!",
267 __FUNCTION__);
268 }
269 }
270
271 /* Force the context `c' to be the current context and associate with it
272 * buffer `b'.
273 */
274 GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
275 __DRIdrawablePrivate * driDrawPriv,
276 __DRIdrawablePrivate * driReadPriv)
277 {
278 if (driContextPriv) {
279 radeonContextPtr radeon =
280 (radeonContextPtr) driContextPriv->driverPrivate;
281
282 if (RADEON_DEBUG & DEBUG_DRI)
283 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
284 radeon->glCtx);
285
286 if (radeon->dri.drawable != driDrawPriv) {
287 driDrawableInitVBlank(driDrawPriv,
288 radeon->vblank_flags,
289 &radeon->vbl_seq);
290 radeon->dri.drawable = driDrawPriv;
291
292 r300UpdateWindow(radeon->glCtx);
293 r300UpdateViewportOffset(radeon->glCtx);
294 }
295
296 _mesa_make_current(radeon->glCtx,
297 (GLframebuffer *) driDrawPriv->
298 driverPrivate,
299 (GLframebuffer *) driReadPriv->
300 driverPrivate);
301
302 if (!radeon->glCtx->Viewport.Width) {
303 _mesa_set_viewport(radeon->glCtx, 0, 0,
304 driDrawPriv->w, driDrawPriv->h);
305 }
306
307 _mesa_update_state(radeon->glCtx);
308 } else {
309 if (RADEON_DEBUG & DEBUG_DRI)
310 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
311 _mesa_make_current(0, 0, 0);
312 }
313
314 if (RADEON_DEBUG & DEBUG_DRI)
315 fprintf(stderr, "End %s\n", __FUNCTION__);
316 return GL_TRUE;
317 }
318
319 /* Force the context `c' to be unbound from its buffer.
320 */
321 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv)
322 {
323 radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
324
325 if (RADEON_DEBUG & DEBUG_DRI)
326 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
327 radeon->glCtx);
328
329 return GL_TRUE;
330 }
331