2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
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.
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:
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.
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.
28 **************************************************************************/
32 * Keith Whitwell <keith@tungstengraphics.com>
42 #include "swrast/swrast.h"
43 #include "r200_context.h"
44 #include "r300_context.h"
45 #include "r200_state.h"
46 #include "radeon_ioctl.h"
47 #include "r200_ioctl.h"
48 #include "r300_ioctl.h"
51 #include "r200_sanity.h"
53 #include "r300_state.h"
54 #include "radeon_reg.h"
56 #include "drirenderbuffer.h"
59 static void radeonWaitForIdle(radeonContextPtr radeon
);
61 /* ================================================================
62 * SwapBuffers with client-side throttling
65 static uint32_t radeonGetLastFrame(radeonContextPtr radeon
)
67 drm_radeon_getparam_t gp
;
71 gp
.param
= RADEON_PARAM_LAST_FRAME
;
72 gp
.value
= (int *)&frame
;
73 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_GETPARAM
,
76 fprintf(stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
,
84 uint32_t radeonGetAge(radeonContextPtr radeon
)
86 drm_radeon_getparam_t gp
;
90 gp
.param
= RADEON_PARAM_LAST_CLEAR
;
91 gp
.value
= (int *)&age
;
92 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_GETPARAM
,
95 fprintf(stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
,
103 static void radeonEmitIrqLocked(radeonContextPtr radeon
)
105 drm_radeon_irq_emit_t ie
;
108 ie
.irq_seq
= &radeon
->iw
.irq_seq
;
109 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_IRQ_EMIT
,
112 fprintf(stderr
, "%s: drmRadeonIrqEmit: %d\n", __FUNCTION__
,
118 static void radeonWaitIrq(radeonContextPtr radeon
)
123 ret
= drmCommandWrite(radeon
->dri
.fd
, DRM_RADEON_IRQ_WAIT
,
124 &radeon
->iw
, sizeof(radeon
->iw
));
125 } while (ret
&& (errno
== EINTR
|| errno
== EAGAIN
));
128 fprintf(stderr
, "%s: drmRadeonIrqWait: %d\n", __FUNCTION__
,
134 static void radeonWaitForFrameCompletion(radeonContextPtr radeon
)
136 drm_radeon_sarea_t
*sarea
= radeon
->sarea
;
138 if (radeon
->do_irqs
) {
139 if (radeonGetLastFrame(radeon
) < sarea
->last_frame
) {
140 if (!radeon
->irqsEmitted
) {
141 while (radeonGetLastFrame(radeon
) <
144 UNLOCK_HARDWARE(radeon
);
145 radeonWaitIrq(radeon
);
146 LOCK_HARDWARE(radeon
);
148 radeon
->irqsEmitted
= 10;
151 if (radeon
->irqsEmitted
) {
152 radeonEmitIrqLocked(radeon
);
153 radeon
->irqsEmitted
--;
156 while (radeonGetLastFrame(radeon
) < sarea
->last_frame
) {
157 UNLOCK_HARDWARE(radeon
);
158 if (radeon
->do_usleeps
)
160 LOCK_HARDWARE(radeon
);
165 /* Copy the back color buffer to the front color buffer.
167 void radeonCopyBuffer(const __DRIdrawablePrivate
* dPriv
)
169 radeonContextPtr radeon
;
171 GLboolean missed_target
;
175 assert(dPriv
->driContextPriv
);
176 assert(dPriv
->driContextPriv
->driverPrivate
);
178 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
180 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
181 fprintf(stderr
, "\n%s( %p )\n\n", __FUNCTION__
,
182 (void *)radeon
->glCtx
);
185 if (IS_R200_CLASS(radeon
->radeonScreen
))
186 R200_FIREVERTICES((r200ContextPtr
)radeon
);
188 r300Flush(radeon
->glCtx
);
190 LOCK_HARDWARE(radeon
);
192 /* Throttle the frame rate -- only allow one pending swap buffers
195 radeonWaitForFrameCompletion(radeon
);
196 UNLOCK_HARDWARE(radeon
);
197 driWaitForVBlank(dPriv
, &radeon
->vbl_seq
, radeon
->vblank_flags
,
199 LOCK_HARDWARE(radeon
);
201 nbox
= dPriv
->numClipRects
; /* must be in locked region */
203 for (i
= 0; i
< nbox
;) {
204 GLint nr
= MIN2(i
+ RADEON_NR_SAREA_CLIPRECTS
, nbox
);
205 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
206 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
209 for (; i
< nr
; i
++) {
213 radeon
->sarea
->nbox
= n
;
215 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_SWAP
);
218 fprintf(stderr
, "DRM_RADEON_SWAP: return = %d\n",
220 UNLOCK_HARDWARE(radeon
);
225 UNLOCK_HARDWARE(radeon
);
227 if (IS_R200_CLASS(radeon
->radeonScreen
))
228 ((r200ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
230 ((r300ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
232 radeon
->swap_count
++;
233 (*dri_interface
->getUST
) (&ust
);
235 radeon
->swap_missed_count
++;
236 radeon
->swap_missed_ust
= ust
- radeon
->swap_ust
;
239 radeon
->swap_ust
= ust
;
244 void radeonPageFlip(const __DRIdrawablePrivate
* dPriv
)
246 radeonContextPtr radeon
;
248 GLboolean missed_target
;
251 assert(dPriv
->driContextPriv
);
252 assert(dPriv
->driContextPriv
->driverPrivate
);
254 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
256 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
257 fprintf(stderr
, "%s: pfCurrentPage: %d\n", __FUNCTION__
,
258 radeon
->sarea
->pfCurrentPage
);
261 if (IS_R200_CLASS(radeon
->radeonScreen
))
262 R200_FIREVERTICES((r200ContextPtr
)radeon
);
264 r300Flush(radeon
->glCtx
);
265 LOCK_HARDWARE(radeon
);
267 if (!dPriv
->numClipRects
) {
268 UNLOCK_HARDWARE(radeon
);
269 usleep(10000); /* throttle invisible client 10ms */
273 /* Need to do this for the perf box placement:
276 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
277 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
279 radeon
->sarea
->nbox
= 1;
282 /* Throttle the frame rate -- only allow a few pending swap buffers
285 radeonWaitForFrameCompletion(radeon
);
286 UNLOCK_HARDWARE(radeon
);
287 driWaitForVBlank(dPriv
, &radeon
->vbl_seq
, radeon
->vblank_flags
,
290 radeon
->swap_missed_count
++;
291 (void)(*dri_interface
->getUST
) (&radeon
->swap_missed_ust
);
293 LOCK_HARDWARE(radeon
);
295 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_FLIP
);
297 UNLOCK_HARDWARE(radeon
);
300 fprintf(stderr
, "DRM_RADEON_FLIP: return = %d\n", ret
);
304 radeon
->swap_count
++;
305 (void)(*dri_interface
->getUST
) (&radeon
->swap_ust
);
307 driFlipRenderbuffers(radeon
->glCtx
->WinSysDrawBuffer
,
308 radeon
->sarea
->pfCurrentPage
);
310 if (radeon
->sarea
->pfCurrentPage
== 1) {
311 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->frontOffset
;
312 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->frontPitch
;
314 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->backOffset
;
315 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->backPitch
;
318 if (IS_R200_CLASS(radeon
->radeonScreen
)) {
319 r200ContextPtr r200
= (r200ContextPtr
)radeon
;
321 R200_STATECHANGE(r200
, ctx
);
322 r200
->hw
.ctx
.cmd
[CTX_RB3D_COLOROFFSET
] = radeon
->state
.color
.drawOffset
323 + radeon
->radeonScreen
->fbLocation
;
324 r200
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] = radeon
->state
.color
.drawPitch
;
326 if (IS_R300_CLASS(radeon
->radeonScreen
)) {
327 r300ContextPtr r300
= (r300ContextPtr
)radeon
;
328 R300_STATECHANGE(r300
, cb
);
329 r300
->hw
.cb
.cmd
[R300_CB_OFFSET
] = r300
->radeon
.state
.color
.drawOffset
+
330 r300
->radeon
.radeonScreen
->fbLocation
;
331 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] = r300
->radeon
.state
.color
.drawPitch
;
333 if (r300
->radeon
.radeonScreen
->cpp
== 4)
334 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_ARGB8888
;
336 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_RGB565
;
338 if (r300
->radeon
.sarea
->tiling_enabled
)
339 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_TILE_ENABLE
;
343 void radeonWaitForIdleLocked(radeonContextPtr radeon
)
349 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_CP_IDLE
);
352 } while (ret
&& ++i
< 100);
355 UNLOCK_HARDWARE(radeon
);
356 fprintf(stderr
, "Error: R200 timed out... exiting\n");
361 static void radeonWaitForIdle(radeonContextPtr radeon
)
363 LOCK_HARDWARE(radeon
);
364 radeonWaitForIdleLocked(radeon
);
365 UNLOCK_HARDWARE(radeon
);
368 void radeonFlush(GLcontext
* ctx
)
370 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
372 if (IS_R300_CLASS(radeon
->radeonScreen
))
381 /* Make sure all commands have been sent to the hardware and have
382 * completed processing.
384 void radeonFinish(GLcontext
* ctx
)
386 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
390 if (radeon
->do_irqs
) {
391 LOCK_HARDWARE(radeon
);
392 radeonEmitIrqLocked(radeon
);
393 UNLOCK_HARDWARE(radeon
);
394 radeonWaitIrq(radeon
);
396 radeonWaitForIdle(radeon
);