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 "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
;
169 assert(dPriv
->driContextPriv
);
170 assert(dPriv
->driContextPriv
->driverPrivate
);
172 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
174 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
175 fprintf(stderr
, "\n%s( %p )\n\n", __FUNCTION__
,
176 (void *)radeon
->glCtx
);
179 r300Flush(radeon
->glCtx
);
181 LOCK_HARDWARE(radeon
);
183 /* Throttle the frame rate -- only allow one pending swap buffers
186 radeonWaitForFrameCompletion(radeon
);
189 UNLOCK_HARDWARE(radeon
);
190 driWaitForVBlank(dPriv
, &missed_target
);
191 LOCK_HARDWARE(radeon
);
194 nbox
= dPriv
->numClipRects
; /* must be in locked region */
196 for (i
= 0; i
< nbox
;) {
197 GLint nr
= MIN2(i
+ RADEON_NR_SAREA_CLIPRECTS
, nbox
);
198 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
199 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
202 for ( ; i
< nr
; i
++ ) {
208 if (rect
->x1
> b
->x1
)
210 if (rect
->y1
> b
->y1
)
212 if (rect
->x2
< b
->x2
)
214 if (rect
->y2
< b
->y2
)
217 if (b
->x1
< b
->x2
&& b
->y1
< b
->y2
)
225 radeon
->sarea
->nbox
= n
;
227 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_SWAP
);
230 fprintf(stderr
, "DRM_RADEON_SWAP: return = %d\n",
232 UNLOCK_HARDWARE(radeon
);
237 UNLOCK_HARDWARE(radeon
);
240 ((r300ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
242 radeon
->swap_count
++;
243 (*dri_interface
->getUST
) (&ust
);
245 radeon
->swap_missed_count
++;
246 radeon
->swap_missed_ust
= ust
- radeon
->swap_ust
;
249 radeon
->swap_ust
= ust
;
255 void radeonPageFlip(__DRIdrawablePrivate
* dPriv
)
257 radeonContextPtr radeon
;
259 GLboolean missed_target
;
262 assert(dPriv
->driContextPriv
);
263 assert(dPriv
->driContextPriv
->driverPrivate
);
265 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
267 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
268 fprintf(stderr
, "%s: pfCurrentPage: %d\n", __FUNCTION__
,
269 radeon
->sarea
->pfCurrentPage
);
272 r300Flush(radeon
->glCtx
);
273 LOCK_HARDWARE(radeon
);
275 if (!dPriv
->numClipRects
) {
276 UNLOCK_HARDWARE(radeon
);
277 usleep(10000); /* throttle invisible client 10ms */
281 /* Need to do this for the perf box placement:
284 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
285 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
287 radeon
->sarea
->nbox
= 1;
290 /* Throttle the frame rate -- only allow a few pending swap buffers
293 radeonWaitForFrameCompletion(radeon
);
294 UNLOCK_HARDWARE(radeon
);
295 driWaitForVBlank(dPriv
, &missed_target
);
297 radeon
->swap_missed_count
++;
298 (void)(*dri_interface
->getUST
) (&radeon
->swap_missed_ust
);
300 LOCK_HARDWARE(radeon
);
302 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_FLIP
);
304 UNLOCK_HARDWARE(radeon
);
307 fprintf(stderr
, "DRM_RADEON_FLIP: return = %d\n", ret
);
311 radeon
->swap_count
++;
312 (void)(*dri_interface
->getUST
) (&radeon
->swap_ust
);
314 driFlipRenderbuffers(radeon
->glCtx
->WinSysDrawBuffer
,
315 radeon
->sarea
->pfCurrentPage
);
317 if (radeon
->sarea
->pfCurrentPage
== 1) {
318 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->frontOffset
;
319 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->frontPitch
;
321 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->backOffset
;
322 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->backPitch
;
325 if (IS_R300_CLASS(radeon
->radeonScreen
)) {
326 r300ContextPtr r300
= (r300ContextPtr
)radeon
;
327 R300_STATECHANGE(r300
, cb
);
328 r300
->hw
.cb
.cmd
[R300_CB_OFFSET
] = r300
->radeon
.state
.color
.drawOffset
+
329 r300
->radeon
.radeonScreen
->fbLocation
;
330 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] = r300
->radeon
.state
.color
.drawPitch
;
332 if (r300
->radeon
.radeonScreen
->cpp
== 4)
333 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_ARGB8888
;
335 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_RGB565
;
337 if (r300
->radeon
.sarea
->tiling_enabled
)
338 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_TILE_ENABLE
;
342 void radeonWaitForIdleLocked(radeonContextPtr radeon
)
348 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_CP_IDLE
);
351 } while (ret
&& ++i
< 100);
354 UNLOCK_HARDWARE(radeon
);
355 fprintf(stderr
, "Error: R300 timed out... exiting\n");
360 static void radeonWaitForIdle(radeonContextPtr radeon
)
362 LOCK_HARDWARE(radeon
);
363 radeonWaitForIdleLocked(radeon
);
364 UNLOCK_HARDWARE(radeon
);
367 void radeonFlush(GLcontext
* ctx
)
369 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
371 if (IS_R300_CLASS(radeon
->radeonScreen
))
376 /* Make sure all commands have been sent to the hardware and have
377 * completed processing.
379 void radeonFinish(GLcontext
* ctx
)
381 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
385 if (radeon
->do_irqs
) {
386 LOCK_HARDWARE(radeon
);
387 radeonEmitIrqLocked(radeon
);
388 UNLOCK_HARDWARE(radeon
);
389 radeonWaitIrq(radeon
);
391 radeonWaitForIdle(radeon
);