merge current trunk into vbo branch
[mesa.git] / src / mesa / drivers / dri / r300 / radeon_lock.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 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 */
34 #include <string.h>
35
36 #include "radeon_lock.h"
37 #include "radeon_ioctl.h"
38 #include "radeon_state.h"
39 #include "r300_context.h"
40 #include "r300_state.h"
41
42 #include "framebuffer.h"
43
44 #include "drirenderbuffer.h"
45
46 #if DEBUG_LOCKING
47 char *prevLockFile = NULL;
48 int prevLockLine = 0;
49 #endif
50
51 /* Turn on/off page flipping according to the flags in the sarea:
52 */
53 static void radeonUpdatePageFlipping(radeonContextPtr radeon)
54 {
55 int use_back;
56
57 radeon->doPageFlip = radeon->sarea->pfState;
58 if (!radeon->doPageFlip) {
59 driFlipRenderbuffers(radeon->glCtx->WinSysDrawBuffer, GL_FALSE);
60 }
61
62 use_back = (radeon->glCtx->DrawBuffer->_ColorDrawBufferMask[0] == BUFFER_BIT_BACK_LEFT);
63 use_back ^= (radeon->sarea->pfCurrentPage == 1);
64
65 if (use_back) {
66 radeon->state.color.drawOffset = radeon->radeonScreen->backOffset;
67 radeon->state.color.drawPitch = radeon->radeonScreen->backPitch;
68 } else {
69 radeon->state.color.drawOffset = radeon->radeonScreen->frontOffset;
70 radeon->state.color.drawPitch = radeon->radeonScreen->frontPitch;
71 }
72 }
73
74 /**
75 * Called by radeonGetLock() after the lock has been obtained.
76 */
77 static void r300RegainedLock(radeonContextPtr radeon)
78 {
79 int i;
80 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
81 r300ContextPtr r300 = (r300ContextPtr)radeon;
82 drm_radeon_sarea_t *sarea = radeon->sarea;
83
84 if ( radeon->lastStamp != drawable->lastStamp ) {
85 radeonUpdatePageFlipping(radeon);
86 radeonSetCliprects(radeon);
87 #if 1
88 r300UpdateViewportOffset( radeon->glCtx );
89 driUpdateFramebufferSize(radeon->glCtx, drawable);
90 #else
91 radeonUpdateScissor(radeon->glCtx);
92 #endif
93 radeon->lastStamp = drawable->lastStamp;
94 }
95
96 if (sarea->ctx_owner != radeon->dri.hwContext) {
97 sarea->ctx_owner = radeon->dri.hwContext;
98
99 for (i = 0; i < r300->nr_heaps; i++) {
100 DRI_AGE_TEXTURES(r300->texture_heaps[i]);
101 }
102 }
103 }
104
105 /* Update the hardware state. This is called if another context has
106 * grabbed the hardware lock, which includes the X server. This
107 * function also updates the driver's window state after the X server
108 * moves, resizes or restacks a window -- the change will be reflected
109 * in the drawable position and clip rects. Since the X server grabs
110 * the hardware lock when it changes the window state, this routine will
111 * automatically be called after such a change.
112 */
113 void radeonGetLock(radeonContextPtr radeon, GLuint flags)
114 {
115 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
116 __DRIdrawablePrivate *const readable = radeon->dri.readable;
117 __DRIscreenPrivate *sPriv = radeon->dri.screen;
118
119 assert (drawable != NULL);
120
121 drmGetLock(radeon->dri.fd, radeon->dri.hwContext, flags);
122
123 /* The window might have moved, so we might need to get new clip
124 * rects.
125 *
126 * NOTE: This releases and regrabs the hw lock to allow the X server
127 * to respond to the DRI protocol request for new drawable info.
128 * Since the hardware state depends on having the latest drawable
129 * clip rects, all state checking must be done _after_ this call.
130 */
131 DRI_VALIDATE_DRAWABLE_INFO( sPriv, drawable );
132 if (drawable != readable) {
133 DRI_VALIDATE_DRAWABLE_INFO( sPriv, readable );
134 }
135
136 if (IS_R300_CLASS(radeon->radeonScreen))
137 r300RegainedLock(radeon);
138
139 radeon->lost_context = GL_TRUE;
140 }