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