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>
38 #include "main/glheader.h"
39 #include "main/imports.h"
40 #include "main/macros.h"
41 #include "main/context.h"
42 #include "swrast/swrast.h"
43 #include "r300_context.h"
44 #include "radeon_ioctl.h"
45 #include "r300_ioctl.h"
46 #include "r300_state.h"
47 #include "radeon_reg.h"
49 #include "drirenderbuffer.h"
52 static void radeonWaitForIdle(radeonContextPtr radeon
);
54 /* ================================================================
55 * SwapBuffers with client-side throttling
58 static uint32_t radeonGetLastFrame(radeonContextPtr radeon
)
60 drm_radeon_getparam_t gp
;
64 gp
.param
= RADEON_PARAM_LAST_FRAME
;
65 gp
.value
= (int *)&frame
;
66 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_GETPARAM
,
69 fprintf(stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
,
77 uint32_t radeonGetAge(radeonContextPtr radeon
)
79 drm_radeon_getparam_t gp
;
83 gp
.param
= RADEON_PARAM_LAST_CLEAR
;
84 gp
.value
= (int *)&age
;
85 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_GETPARAM
,
88 fprintf(stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
,
96 static void radeonEmitIrqLocked(radeonContextPtr radeon
)
98 drm_radeon_irq_emit_t ie
;
101 ie
.irq_seq
= &radeon
->iw
.irq_seq
;
102 ret
= drmCommandWriteRead(radeon
->dri
.fd
, DRM_RADEON_IRQ_EMIT
,
105 fprintf(stderr
, "%s: drmRadeonIrqEmit: %d\n", __FUNCTION__
,
111 static void radeonWaitIrq(radeonContextPtr radeon
)
116 ret
= drmCommandWrite(radeon
->dri
.fd
, DRM_RADEON_IRQ_WAIT
,
117 &radeon
->iw
, sizeof(radeon
->iw
));
118 } while (ret
&& (errno
== EINTR
|| errno
== EBUSY
));
121 fprintf(stderr
, "%s: drmRadeonIrqWait: %d\n", __FUNCTION__
,
127 static void radeonWaitForFrameCompletion(radeonContextPtr radeon
)
129 drm_radeon_sarea_t
*sarea
= radeon
->sarea
;
131 if (radeon
->do_irqs
) {
132 if (radeonGetLastFrame(radeon
) < sarea
->last_frame
) {
133 if (!radeon
->irqsEmitted
) {
134 while (radeonGetLastFrame(radeon
) <
137 UNLOCK_HARDWARE(radeon
);
138 radeonWaitIrq(radeon
);
139 LOCK_HARDWARE(radeon
);
141 radeon
->irqsEmitted
= 10;
144 if (radeon
->irqsEmitted
) {
145 radeonEmitIrqLocked(radeon
);
146 radeon
->irqsEmitted
--;
149 while (radeonGetLastFrame(radeon
) < sarea
->last_frame
) {
150 UNLOCK_HARDWARE(radeon
);
151 if (radeon
->do_usleeps
)
153 LOCK_HARDWARE(radeon
);
158 /* Copy the back color buffer to the front color buffer.
160 void radeonCopyBuffer(__DRIdrawablePrivate
* dPriv
,
161 const drm_clip_rect_t
* rect
)
163 radeonContextPtr radeon
;
165 GLboolean missed_target
;
167 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
170 assert(dPriv
->driContextPriv
);
171 assert(dPriv
->driContextPriv
->driverPrivate
);
173 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
175 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
176 fprintf(stderr
, "\n%s( %p )\n\n", __FUNCTION__
,
177 (void *)radeon
->glCtx
);
180 r300Flush(radeon
->glCtx
);
182 LOCK_HARDWARE(radeon
);
184 /* Throttle the frame rate -- only allow one pending swap buffers
187 radeonWaitForFrameCompletion(radeon
);
190 UNLOCK_HARDWARE(radeon
);
191 driWaitForVBlank(dPriv
, &missed_target
);
192 LOCK_HARDWARE(radeon
);
195 nbox
= dPriv
->numClipRects
; /* must be in locked region */
197 for (i
= 0; i
< nbox
;) {
198 GLint nr
= MIN2(i
+ RADEON_NR_SAREA_CLIPRECTS
, nbox
);
199 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
200 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
203 for ( ; i
< nr
; i
++ ) {
209 if (rect
->x1
> b
->x1
)
211 if (rect
->y1
> b
->y1
)
213 if (rect
->x2
< b
->x2
)
215 if (rect
->y2
< b
->y2
)
218 if (b
->x1
>= b
->x2
|| b
->y1
>= b
->y2
)
225 radeon
->sarea
->nbox
= n
;
230 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_SWAP
);
233 fprintf(stderr
, "DRM_RADEON_SWAP: return = %d\n",
235 UNLOCK_HARDWARE(radeon
);
240 UNLOCK_HARDWARE(radeon
);
243 ((r300ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
245 radeon
->swap_count
++;
246 (*psp
->systemTime
->getUST
) (&ust
);
248 radeon
->swap_missed_count
++;
249 radeon
->swap_missed_ust
= ust
- radeon
->swap_ust
;
252 radeon
->swap_ust
= ust
;
258 void radeonPageFlip(__DRIdrawablePrivate
* dPriv
)
260 radeonContextPtr radeon
;
262 GLboolean missed_target
;
263 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
266 assert(dPriv
->driContextPriv
);
267 assert(dPriv
->driContextPriv
->driverPrivate
);
269 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
271 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
272 fprintf(stderr
, "%s: pfCurrentPage: %d\n", __FUNCTION__
,
273 radeon
->sarea
->pfCurrentPage
);
276 r300Flush(radeon
->glCtx
);
277 LOCK_HARDWARE(radeon
);
279 if (!dPriv
->numClipRects
) {
280 UNLOCK_HARDWARE(radeon
);
281 usleep(10000); /* throttle invisible client 10ms */
285 /* Need to do this for the perf box placement:
288 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
289 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
291 radeon
->sarea
->nbox
= 1;
294 /* Throttle the frame rate -- only allow a few pending swap buffers
297 radeonWaitForFrameCompletion(radeon
);
298 UNLOCK_HARDWARE(radeon
);
299 driWaitForVBlank(dPriv
, &missed_target
);
301 radeon
->swap_missed_count
++;
302 (void)(*psp
->systemTime
->getUST
) (&radeon
->swap_missed_ust
);
304 LOCK_HARDWARE(radeon
);
306 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_FLIP
);
308 UNLOCK_HARDWARE(radeon
);
311 fprintf(stderr
, "DRM_RADEON_FLIP: return = %d\n", ret
);
315 radeon
->swap_count
++;
316 (void)(*psp
->systemTime
->getUST
) (&radeon
->swap_ust
);
318 driFlipRenderbuffers(radeon
->glCtx
->WinSysDrawBuffer
,
319 radeon
->sarea
->pfCurrentPage
);
321 if (radeon
->sarea
->pfCurrentPage
== 1) {
322 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->frontOffset
;
323 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->frontPitch
;
325 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->backOffset
;
326 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->backPitch
;
329 if (IS_R300_CLASS(radeon
->radeonScreen
)) {
330 r300ContextPtr r300
= (r300ContextPtr
)radeon
;
331 R300_STATECHANGE(r300
, cb
);
332 r300
->hw
.cb
.cmd
[R300_CB_OFFSET
] = r300
->radeon
.state
.color
.drawOffset
+
333 r300
->radeon
.radeonScreen
->fbLocation
;
334 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] = r300
->radeon
.state
.color
.drawPitch
;
336 if (r300
->radeon
.radeonScreen
->cpp
== 4)
337 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_ARGB8888
;
339 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_RGB565
;
341 if (r300
->radeon
.sarea
->tiling_enabled
)
342 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_TILE_ENABLE
;
346 void radeonWaitForIdleLocked(radeonContextPtr radeon
)
352 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_CP_IDLE
);
355 } while (ret
&& ++i
< 100);
358 UNLOCK_HARDWARE(radeon
);
359 fprintf(stderr
, "Error: R300 timed out... exiting\n");
364 static void radeonWaitForIdle(radeonContextPtr radeon
)
366 LOCK_HARDWARE(radeon
);
367 radeonWaitForIdleLocked(radeon
);
368 UNLOCK_HARDWARE(radeon
);
371 void radeonFlush(GLcontext
* ctx
)
373 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
375 if (IS_R300_CLASS(radeon
->radeonScreen
))
380 /* Make sure all commands have been sent to the hardware and have
381 * completed processing.
383 void radeonFinish(GLcontext
* ctx
)
385 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
389 if (radeon
->do_irqs
) {
390 LOCK_HARDWARE(radeon
);
391 radeonEmitIrqLocked(radeon
);
392 UNLOCK_HARDWARE(radeon
);
393 radeonWaitIrq(radeon
);
395 radeonWaitForIdle(radeon
);