r300 driver side of color tiling support.
[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 "r200_context.h"
51 #include "radeon_ioctl.h"
52 #include "radeon_macros.h"
53 #include "radeon_reg.h"
54 #include "r200_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 "20040924"
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->IsPCI ? 0 :
78 radeon->radeonScreen->AGPMode;
79 const char* chipname;
80
81 if (IS_FAMILY_R300(radeon))
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 static int get_ust_nop(int64_t * ust)
128 {
129 *ust = 1;
130 return 0;
131 }
132
133 /**
134 * Create and initialize all common fields of the context,
135 * including the Mesa context itself.
136 */
137 GLboolean radeonInitContext(radeonContextPtr radeon,
138 struct dd_function_table* functions,
139 const __GLcontextModes * glVisual,
140 __DRIcontextPrivate * driContextPriv,
141 void *sharedContextPrivate)
142 {
143 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
144 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
145 GLcontext* ctx;
146 GLcontext* shareCtx;
147 int fthrottle_mode;
148
149 /* Fill in additional standard functions. */
150 radeonInitDriverFuncs(functions);
151
152 /* Allocate and initialize the Mesa context */
153 if (sharedContextPrivate)
154 shareCtx = ((radeonContextPtr)sharedContextPrivate)->glCtx;
155 else
156 shareCtx = NULL;
157 radeon->glCtx = _mesa_create_context(glVisual, shareCtx,
158 functions, (void *)radeon);
159 if (!radeon->glCtx)
160 return GL_FALSE;
161
162 ctx = radeon->glCtx;
163 driContextPriv->driverPrivate = radeon;
164
165 /* DRI fields */
166 radeon->dri.context = driContextPriv;
167 radeon->dri.screen = sPriv;
168 radeon->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
169 radeon->dri.hwContext = driContextPriv->hHWContext;
170 radeon->dri.hwLock = &sPriv->pSAREA->lock;
171 radeon->dri.fd = sPriv->fd;
172 radeon->dri.drmMinor = sPriv->drmMinor;
173
174 radeon->radeonScreen = screen;
175 radeon->sarea = (drm_radeon_sarea_t *) ((GLubyte *) sPriv->pSAREA +
176 screen->sarea_priv_offset);
177
178 /* Setup IRQs */
179 fthrottle_mode = driQueryOptioni(&radeon->optionCache, "fthrottle_mode");
180 radeon->iw.irq_seq = -1;
181 radeon->irqsEmitted = 0;
182 radeon->do_irqs = (radeon->dri.drmMinor >= 6 &&
183 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
184 radeon->radeonScreen->irq);
185
186 radeon->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
187
188 if (!radeon->do_irqs)
189 fprintf(stderr,
190 "IRQ's not enabled, falling back to %s: %d %d %d\n",
191 radeon->do_usleeps ? "usleeps" : "busy waits",
192 radeon->dri.drmMinor,
193 fthrottle_mode, radeon->radeonScreen->irq);
194
195 radeon->vblank_flags = (radeon->radeonScreen->irq != 0)
196 ? driGetDefaultVBlankFlags(&radeon->optionCache) : VBLANK_FLAG_NO_IRQ;
197
198 radeon->get_ust =
199 (PFNGLXGETUSTPROC) glXGetProcAddress((const GLubyte *)
200 "__glXGetUST");
201 if (radeon->get_ust == NULL) {
202 radeon->get_ust = get_ust_nop;
203 }
204 (*radeon->get_ust) (&radeon->swap_ust);
205
206 return GL_TRUE;
207 }
208
209
210 /**
211 * Cleanup common context fields.
212 * Called by r200DestroyContext/r300DestroyContext
213 */
214 void radeonCleanupContext(radeonContextPtr radeon)
215 {
216 /* free the Mesa context */
217 radeon->glCtx->DriverCtx = NULL;
218 _mesa_destroy_context(radeon->glCtx);
219
220 if (radeon->state.scissor.pClipRects) {
221 FREE(radeon->state.scissor.pClipRects);
222 radeon->state.scissor.pClipRects = 0;
223 }
224 }
225
226
227 /**
228 * Swap front and back buffer.
229 */
230 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
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 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
241 if (radeon->doPageFlip) {
242 radeonPageFlip(dPriv);
243 } else {
244 radeonCopyBuffer(dPriv);
245 }
246 }
247 } else {
248 /* XXX this shouldn't be an error but we can't handle it for now */
249 _mesa_problem(NULL, "%s: drawable has no context!",
250 __FUNCTION__);
251 }
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->dri.drawable = driDrawPriv;
274
275 #if R200_MERGED
276 if (IS_FAMILY_R200(radeon)) {
277 r200UpdateWindow(radeon->glCtx);
278 r200UpdateViewportOffset(radeon->glCtx);
279 }
280 #endif
281 }
282
283 _mesa_make_current(radeon->glCtx,
284 (GLframebuffer *) driDrawPriv->
285 driverPrivate,
286 (GLframebuffer *) driReadPriv->
287 driverPrivate);
288
289 if (!radeon->glCtx->Viewport.Width) {
290 _mesa_set_viewport(radeon->glCtx, 0, 0,
291 driDrawPriv->w, driDrawPriv->h);
292 }
293
294 _mesa_update_state(radeon->glCtx);
295
296 #if R200_MERGED
297 if (IS_FAMILY_R200(radeon))
298 r200ValidateState(radeon->glCtx);
299 #endif
300
301 } else {
302 if (RADEON_DEBUG & DEBUG_DRI)
303 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
304 _mesa_make_current(0, 0, 0);
305 }
306
307 if (RADEON_DEBUG & DEBUG_DRI)
308 fprintf(stderr, "End %s\n", __FUNCTION__);
309 return GL_TRUE;
310 }
311
312 /* Force the context `c' to be unbound from its buffer.
313 */
314 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv)
315 {
316 radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
317
318 if (RADEON_DEBUG & DEBUG_DRI)
319 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
320 radeon->glCtx);
321
322 return GL_TRUE;
323 }
324