Merge remote branch 'origin/master' into glsl2
[mesa.git] / src / gallium / drivers / llvmpipe / lp_fence.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "lp_debug.h"
32 #include "lp_fence.h"
33
34
35 /**
36 * Create a new fence object.
37 *
38 * The rank will be the number of bins in the scene. Whenever a rendering
39 * thread hits a fence command, it'll increment the fence counter. When
40 * the counter == the rank, the fence is finished.
41 *
42 * \param rank the expected finished value of the fence counter.
43 */
44 struct lp_fence *
45 lp_fence_create(unsigned rank)
46 {
47 struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
48
49 pipe_reference_init(&fence->reference, 1);
50
51 pipe_mutex_init(fence->mutex);
52 pipe_condvar_init(fence->signalled);
53
54 fence->rank = rank;
55
56 return fence;
57 }
58
59
60 /** Destroy a fence. Called when refcount hits zero. */
61 void
62 lp_fence_destroy(struct lp_fence *fence)
63 {
64 pipe_mutex_destroy(fence->mutex);
65 pipe_condvar_destroy(fence->signalled);
66 FREE(fence);
67 }
68
69
70 /**
71 * For reference counting.
72 * This is a Gallium API function.
73 */
74 static void
75 llvmpipe_fence_reference(struct pipe_screen *screen,
76 struct pipe_fence_handle **ptr,
77 struct pipe_fence_handle *fence)
78 {
79 struct lp_fence **old = (struct lp_fence **) ptr;
80 struct lp_fence *f = (struct lp_fence *) fence;
81
82 lp_fence_reference(old, f);
83 }
84
85
86 /**
87 * Has the fence been executed/finished?
88 * This is a Gallium API function.
89 */
90 static int
91 llvmpipe_fence_signalled(struct pipe_screen *screen,
92 struct pipe_fence_handle *fence,
93 unsigned flag)
94 {
95 struct lp_fence *f = (struct lp_fence *) fence;
96
97 return f->count == f->rank;
98 }
99
100
101 /**
102 * Wait for the fence to finish.
103 * This is a Gallium API function.
104 */
105 static int
106 llvmpipe_fence_finish(struct pipe_screen *screen,
107 struct pipe_fence_handle *fence_handle,
108 unsigned flag)
109 {
110 struct lp_fence *fence = (struct lp_fence *) fence_handle;
111
112 pipe_mutex_lock(fence->mutex);
113 while (fence->count < fence->rank) {
114 pipe_condvar_wait(fence->signalled, fence->mutex);
115 }
116 pipe_mutex_unlock(fence->mutex);
117
118 return 0;
119 }
120
121
122 /**
123 * Called by the rendering threads to increment the fence counter.
124 * When the counter == the rank, the fence is finished.
125 */
126 void
127 lp_fence_signal(struct lp_fence *fence)
128 {
129 pipe_mutex_lock(fence->mutex);
130
131 fence->count++;
132 assert(fence->count <= fence->rank);
133
134 LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
135 fence->count, fence->rank);
136
137 pipe_condvar_signal(fence->signalled);
138
139 pipe_mutex_unlock(fence->mutex);
140 }
141
142
143 void
144 llvmpipe_init_screen_fence_funcs(struct pipe_screen *screen)
145 {
146 screen->fence_reference = llvmpipe_fence_reference;
147 screen->fence_signalled = llvmpipe_fence_signalled;
148 screen->fence_finish = llvmpipe_fence_finish;
149 }