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
== EBUSY
));
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
,
168 const drm_clip_rect_t
* rect
)
170 radeonContextPtr radeon
;
172 GLboolean missed_target
;
176 assert(dPriv
->driContextPriv
);
177 assert(dPriv
->driContextPriv
->driverPrivate
);
179 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
181 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
182 fprintf(stderr
, "\n%s( %p )\n\n", __FUNCTION__
,
183 (void *)radeon
->glCtx
);
186 if (IS_R200_CLASS(radeon
->radeonScreen
))
187 R200_FIREVERTICES((r200ContextPtr
)radeon
);
189 r300Flush(radeon
->glCtx
);
191 LOCK_HARDWARE(radeon
);
193 /* Throttle the frame rate -- only allow one pending swap buffers
196 radeonWaitForFrameCompletion(radeon
);
199 UNLOCK_HARDWARE(radeon
);
200 driWaitForVBlank(dPriv
, &radeon
->vbl_seq
, radeon
->vblank_flags
,
202 LOCK_HARDWARE(radeon
);
205 nbox
= dPriv
->numClipRects
; /* must be in locked region */
207 for (i
= 0; i
< nbox
;) {
208 GLint nr
= MIN2(i
+ RADEON_NR_SAREA_CLIPRECTS
, nbox
);
209 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
210 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
213 for ( ; i
< nr
; i
++ ) {
219 if (rect
->x1
> b
->x1
)
221 if (rect
->y1
> b
->y1
)
223 if (rect
->x2
< b
->x2
)
225 if (rect
->y2
< b
->y2
)
228 if (b
->x1
< b
->x2
&& b
->y1
< b
->y2
)
236 radeon
->sarea
->nbox
= n
;
238 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_SWAP
);
241 fprintf(stderr
, "DRM_RADEON_SWAP: return = %d\n",
243 UNLOCK_HARDWARE(radeon
);
248 UNLOCK_HARDWARE(radeon
);
251 if (IS_R200_CLASS(radeon
->radeonScreen
))
252 ((r200ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
254 ((r300ContextPtr
)radeon
)->hw
.all_dirty
= GL_TRUE
;
256 radeon
->swap_count
++;
257 (*dri_interface
->getUST
) (&ust
);
259 radeon
->swap_missed_count
++;
260 radeon
->swap_missed_ust
= ust
- radeon
->swap_ust
;
263 radeon
->swap_ust
= ust
;
269 void radeonPageFlip(const __DRIdrawablePrivate
* dPriv
)
271 radeonContextPtr radeon
;
273 GLboolean missed_target
;
276 assert(dPriv
->driContextPriv
);
277 assert(dPriv
->driContextPriv
->driverPrivate
);
279 radeon
= (radeonContextPtr
) dPriv
->driContextPriv
->driverPrivate
;
281 if (RADEON_DEBUG
& DEBUG_IOCTL
) {
282 fprintf(stderr
, "%s: pfCurrentPage: %d\n", __FUNCTION__
,
283 radeon
->sarea
->pfCurrentPage
);
286 if (IS_R200_CLASS(radeon
->radeonScreen
))
287 R200_FIREVERTICES((r200ContextPtr
)radeon
);
289 r300Flush(radeon
->glCtx
);
290 LOCK_HARDWARE(radeon
);
292 if (!dPriv
->numClipRects
) {
293 UNLOCK_HARDWARE(radeon
);
294 usleep(10000); /* throttle invisible client 10ms */
298 /* Need to do this for the perf box placement:
301 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
302 drm_clip_rect_t
*b
= radeon
->sarea
->boxes
;
304 radeon
->sarea
->nbox
= 1;
307 /* Throttle the frame rate -- only allow a few pending swap buffers
310 radeonWaitForFrameCompletion(radeon
);
311 UNLOCK_HARDWARE(radeon
);
312 driWaitForVBlank(dPriv
, &radeon
->vbl_seq
, radeon
->vblank_flags
,
315 radeon
->swap_missed_count
++;
316 (void)(*dri_interface
->getUST
) (&radeon
->swap_missed_ust
);
318 LOCK_HARDWARE(radeon
);
320 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_FLIP
);
322 UNLOCK_HARDWARE(radeon
);
325 fprintf(stderr
, "DRM_RADEON_FLIP: return = %d\n", ret
);
329 radeon
->swap_count
++;
330 (void)(*dri_interface
->getUST
) (&radeon
->swap_ust
);
332 driFlipRenderbuffers(radeon
->glCtx
->WinSysDrawBuffer
,
333 radeon
->sarea
->pfCurrentPage
);
335 if (radeon
->sarea
->pfCurrentPage
== 1) {
336 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->frontOffset
;
337 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->frontPitch
;
339 radeon
->state
.color
.drawOffset
= radeon
->radeonScreen
->backOffset
;
340 radeon
->state
.color
.drawPitch
= radeon
->radeonScreen
->backPitch
;
343 if (IS_R200_CLASS(radeon
->radeonScreen
)) {
344 r200ContextPtr r200
= (r200ContextPtr
)radeon
;
346 R200_STATECHANGE(r200
, ctx
);
347 r200
->hw
.ctx
.cmd
[CTX_RB3D_COLOROFFSET
] = radeon
->state
.color
.drawOffset
348 + radeon
->radeonScreen
->fbLocation
;
349 r200
->hw
.ctx
.cmd
[CTX_RB3D_COLORPITCH
] = radeon
->state
.color
.drawPitch
;
351 if (IS_R300_CLASS(radeon
->radeonScreen
)) {
352 r300ContextPtr r300
= (r300ContextPtr
)radeon
;
353 R300_STATECHANGE(r300
, cb
);
354 r300
->hw
.cb
.cmd
[R300_CB_OFFSET
] = r300
->radeon
.state
.color
.drawOffset
+
355 r300
->radeon
.radeonScreen
->fbLocation
;
356 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] = r300
->radeon
.state
.color
.drawPitch
;
358 if (r300
->radeon
.radeonScreen
->cpp
== 4)
359 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_ARGB8888
;
361 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_FORMAT_RGB565
;
363 if (r300
->radeon
.sarea
->tiling_enabled
)
364 r300
->hw
.cb
.cmd
[R300_CB_PITCH
] |= R300_COLOR_TILE_ENABLE
;
368 void radeonWaitForIdleLocked(radeonContextPtr radeon
)
374 ret
= drmCommandNone(radeon
->dri
.fd
, DRM_RADEON_CP_IDLE
);
377 } while (ret
&& ++i
< 100);
380 UNLOCK_HARDWARE(radeon
);
381 fprintf(stderr
, "Error: R200 timed out... exiting\n");
386 static void radeonWaitForIdle(radeonContextPtr radeon
)
388 LOCK_HARDWARE(radeon
);
389 radeonWaitForIdleLocked(radeon
);
390 UNLOCK_HARDWARE(radeon
);
393 void radeonFlush(GLcontext
* ctx
)
395 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
397 if (IS_R300_CLASS(radeon
->radeonScreen
))
406 /* Make sure all commands have been sent to the hardware and have
407 * completed processing.
409 void radeonFinish(GLcontext
* ctx
)
411 radeonContextPtr radeon
= RADEON_CONTEXT(ctx
);
415 if (radeon
->do_irqs
) {
416 LOCK_HARDWARE(radeon
);
417 radeonEmitIrqLocked(radeon
);
418 UNLOCK_HARDWARE(radeon
);
419 radeonWaitIrq(radeon
);
421 radeonWaitForIdle(radeon
);