lp_fence.c revision 8239fd4baa8ba6ce546960ad41a8c1c6088b3de3
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 "util/u_inlines.h"
32#include "lp_debug.h"
33#include "lp_fence.h"
34
35
36/**
37 * Create a new fence object.
38 *
39 * The rank will be the number of bins in the scene.  Whenever a rendering
40 * thread hits a fence command, it'll increment the fence counter.  When
41 * the counter == the rank, the fence is finished.
42 *
43 * \param rank  the expected finished value of the fence counter.
44 */
45struct lp_fence *
46lp_fence_create(unsigned rank)
47{
48   struct lp_fence *fence = CALLOC_STRUCT(lp_fence);
49
50   pipe_reference_init(&fence->reference, 1);
51
52   pipe_mutex_init(fence->mutex);
53   pipe_condvar_init(fence->signalled);
54
55   fence->rank = rank;
56
57   return fence;
58}
59
60
61/** Destroy a fence.  Called when refcount hits zero. */
62static void
63lp_fence_destroy(struct lp_fence *fence)
64{
65   pipe_mutex_destroy(fence->mutex);
66   pipe_condvar_destroy(fence->signalled);
67   FREE(fence);
68}
69
70
71/**
72 * For reference counting.
73 * This is a Gallium API function.
74 */
75static void
76llvmpipe_fence_reference(struct pipe_screen *screen,
77                         struct pipe_fence_handle **ptr,
78                         struct pipe_fence_handle *fence)
79{
80   struct lp_fence *old = (struct lp_fence *) *ptr;
81   struct lp_fence *f = (struct lp_fence *) fence;
82
83   if (pipe_reference(&old->reference, &f->reference)) {
84      lp_fence_destroy(old);
85   }
86}
87
88
89/**
90 * Has the fence been executed/finished?
91 * This is a Gallium API function.
92 */
93static int
94llvmpipe_fence_signalled(struct pipe_screen *screen,
95                         struct pipe_fence_handle *fence,
96                         unsigned flag)
97{
98   struct lp_fence *f = (struct lp_fence *) fence;
99
100   return f->count == f->rank;
101}
102
103
104/**
105 * Wait for the fence to finish.
106 * This is a Gallium API function.
107 */
108static int
109llvmpipe_fence_finish(struct pipe_screen *screen,
110                      struct pipe_fence_handle *fence_handle,
111                      unsigned flag)
112{
113   struct lp_fence *fence = (struct lp_fence *) fence_handle;
114
115   pipe_mutex_lock(fence->mutex);
116   while (fence->count < fence->rank) {
117      pipe_condvar_wait(fence->signalled, fence->mutex);
118   }
119   pipe_mutex_unlock(fence->mutex);
120
121   return 0;
122}
123
124
125/**
126 * Called by the rendering threads to increment the fence counter.
127 * When the counter == the rank, the fence is finished.
128 */
129void
130lp_fence_signal(struct lp_fence *fence)
131{
132   pipe_mutex_lock(fence->mutex);
133
134   fence->count++;
135   assert(fence->count <= fence->rank);
136
137   LP_DBG(DEBUG_RAST, "%s count=%u rank=%u\n", __FUNCTION__,
138          fence->count, fence->rank);
139
140   pipe_condvar_signal(fence->signalled);
141
142   pipe_mutex_unlock(fence->mutex);
143}
144
145
146void
147llvmpipe_init_screen_fence_funcs(struct pipe_screen *screen)
148{
149   screen->fence_reference = llvmpipe_fence_reference;
150   screen->fence_signalled = llvmpipe_fence_signalled;
151   screen->fence_finish = llvmpipe_fence_finish;
152}
153