Merge branch 'glsl-to-tgsi'
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_lock.c
1 /**************************************************************************
2
3 Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
4 VA Linux Systems Inc., Fremont, California.
5 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
6
7 The Weather Channel (TM) funded Tungsten Graphics to develop the
8 initial release of the Radeon 8500 driver under the XFree86 license.
9 This notice must be preserved.
10
11 All Rights Reserved.
12
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to
18 permit persons to whom the Software is furnished to do so, subject to
19 the following conditions:
20
21 The above copyright notice and this permission notice (including the
22 next paragraph) shall be included in all copies or substantial
23 portions of the Software.
24
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
29 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32
33 **************************************************************************/
34
35 /*
36 * Authors:
37 * Gareth Hughes <gareth@valinux.com>
38 * Keith Whitwell <keith@tungstengraphics.com>
39 * Kevin E. Martin <martin@valinux.com>
40 */
41
42 #include "main/glheader.h"
43 #include "main/mtypes.h"
44 #include "main/colormac.h"
45 #include "dri_util.h"
46 #include "radeon_screen.h"
47 #include "radeon_common.h"
48 #include "radeon_lock.h"
49
50 /* Update the hardware state. This is called if another context has
51 * grabbed the hardware lock, which includes the X server. This
52 * function also updates the driver's window state after the X server
53 * moves, resizes or restacks a window -- the change will be reflected
54 * in the drawable position and clip rects. Since the X server grabs
55 * the hardware lock when it changes the window state, this routine will
56 * automatically be called after such a change.
57 */
58 void radeonGetLock(radeonContextPtr rmesa, GLuint flags)
59 {
60 __DRIdrawable *const drawable = radeon_get_drawable(rmesa);
61 __DRIdrawable *const readable = radeon_get_readable(rmesa);
62 __DRIscreen *sPriv = rmesa->dri.screen;
63
64 drmGetLock(rmesa->dri.fd, rmesa->dri.hwContext, flags);
65
66 /* The window might have moved, so we might need to get new clip
67 * rects.
68 *
69 * NOTE: This releases and regrabs the hw lock to allow the X server
70 * to respond to the DRI protocol request for new drawable info.
71 * Since the hardware state depends on having the latest drawable
72 * clip rects, all state checking must be done _after_ this call.
73 */
74 if (drawable)
75 DRI_VALIDATE_DRAWABLE_INFO(sPriv, drawable);
76 if (readable && drawable != readable) {
77 DRI_VALIDATE_DRAWABLE_INFO(sPriv, readable);
78 }
79
80 if (drawable && (rmesa->lastStamp != drawable->lastStamp)) {
81 radeon_window_moved(rmesa);
82 rmesa->lastStamp = drawable->lastStamp;
83 }
84
85 rmesa->vtbl.get_lock(rmesa);
86 }
87 #ifndef NDEBUG
88 struct lock_debug {
89 const char* function;
90 const char* file;
91 int line;
92 };
93
94 static struct lock_debug ldebug = {0};
95 #endif
96
97 #if 0
98 /** TODO: use atomic operations for reference counting **/
99 /** gcc 4.2 has builtin functios for this **/
100 #define ATOMIC_INC_AND_FETCH(atomic) __sync_add_and_fetch(&atomic, 1)
101 #define ATOMIC_DEC_AND_FETCH(atomic) __sync_sub_and_fetch(&atomic, 1)
102 #else
103 #define ATOMIC_INC_AND_FETCH(atomic) (++atomic)
104 #define ATOMIC_DEC_AND_FETCH(atomic) (--atomic)
105 #endif
106
107
108 void radeon_lock_hardware(radeonContextPtr radeon
109 #ifndef NDEBUG
110 ,const char* function
111 ,const char* file
112 ,const int line
113 #endif
114 )
115 {
116 char ret = 0;
117
118 if (!radeon->radeonScreen->driScreen->dri2.enabled) {
119 if (ATOMIC_INC_AND_FETCH(radeon->dri.hwLockCount) > 1)
120 {
121 #ifndef NDEBUG
122 if ( RADEON_DEBUG & RADEON_SANITY )
123 fprintf(stderr, "*** %d times of recursive call to %s ***\n"
124 "Original call was from %s (file: %s line: %d)\n"
125 "Now call is coming from %s (file: %s line: %d)\n"
126 , radeon->dri.hwLockCount, __FUNCTION__
127 , ldebug.function, ldebug.file, ldebug.line
128 , function, file, line
129 );
130 #endif
131 return;
132 }
133 DRM_CAS(radeon->dri.hwLock, radeon->dri.hwContext,
134 (DRM_LOCK_HELD | radeon->dri.hwContext), ret );
135 if (ret)
136 radeonGetLock(radeon, 0);
137 #ifndef NDEBUG
138 ldebug.function = function;
139 ldebug.file = file;
140 ldebug.line = line;
141 #endif
142 }
143 }
144
145 void radeon_unlock_hardware(radeonContextPtr radeon)
146 {
147 if (!radeon->radeonScreen->driScreen->dri2.enabled) {
148 if (ATOMIC_DEC_AND_FETCH(radeon->dri.hwLockCount) > 0)
149 {
150 return;
151 }
152 DRM_UNLOCK( radeon->dri.fd,
153 radeon->dri.hwLock,
154 radeon->dri.hwContext );
155 }
156 }