e09657d31f7e1339f695de84e29707ede4d23241
[deb_xorg-server.git] / present / present_fence.c
1 /*
2 * Copyright © 2013 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #ifdef HAVE_XORG_CONFIG_H
24 #include <xorg-config.h>
25 #endif
26
27 #include "present_priv.h"
28 #include <gcstruct.h>
29 #include <misync.h>
30 #include <misyncstr.h>
31
32 /*
33 * Wraps SyncFence objects so we can add a SyncTrigger to find out
34 * when the SyncFence gets destroyed and clean up appropriately
35 */
36
37 struct present_fence {
38 SyncTrigger trigger;
39 SyncFence *fence;
40 void (*callback)(void *param);
41 void *param;
42 };
43
44 /*
45 * SyncTrigger callbacks
46 */
47 static Bool
48 present_fence_sync_check_trigger(SyncTrigger *trigger, XSyncValue oldval)
49 {
50 struct present_fence *present_fence = container_of(trigger, struct present_fence, trigger);
51
52 return present_fence->callback != NULL;
53 }
54
55 static void
56 present_fence_sync_trigger_fired(SyncTrigger *trigger)
57 {
58 struct present_fence *present_fence = container_of(trigger, struct present_fence, trigger);
59
60 if (present_fence->callback)
61 (*present_fence->callback)(present_fence->param);
62 }
63
64 static void
65 present_fence_sync_counter_destroyed(SyncTrigger *trigger)
66 {
67 struct present_fence *present_fence = container_of(trigger, struct present_fence, trigger);
68
69 present_fence->fence = NULL;
70 }
71
72 struct present_fence *
73 present_fence_create(SyncFence *fence)
74 {
75 struct present_fence *present_fence;
76
77 present_fence = calloc (1, sizeof (struct present_fence));
78 if (!present_fence)
79 return NULL;
80
81 present_fence->fence = fence;
82 present_fence->trigger.pSync = (SyncObject *) fence;
83 present_fence->trigger.CheckTrigger = present_fence_sync_check_trigger;
84 present_fence->trigger.TriggerFired = present_fence_sync_trigger_fired;
85 present_fence->trigger.CounterDestroyed = present_fence_sync_counter_destroyed;
86
87 if (SyncAddTriggerToSyncObject(&present_fence->trigger) != Success) {
88 free (present_fence);
89 return NULL;
90 }
91 return present_fence;
92 }
93
94 void
95 present_fence_destroy(struct present_fence *present_fence)
96 {
97 if (present_fence) {
98 if (present_fence->fence)
99 SyncDeleteTriggerFromSyncObject(&present_fence->trigger);
100 free(present_fence);
101 }
102 }
103
104 void
105 present_fence_set_triggered(struct present_fence *present_fence)
106 {
107 if (present_fence)
108 if (present_fence->fence)
109 (*present_fence->fence->funcs.SetTriggered) (present_fence->fence);
110 }
111
112 Bool
113 present_fence_check_triggered(struct present_fence *present_fence)
114 {
115 if (!present_fence)
116 return TRUE;
117 if (!present_fence->fence)
118 return TRUE;
119 return (*present_fence->fence->funcs.CheckTriggered)(present_fence->fence);
120 }
121
122 void
123 present_fence_set_callback(struct present_fence *present_fence,
124 void (*callback) (void *param),
125 void *param)
126 {
127 present_fence->callback = callback;
128 present_fence->param = param;
129 }
130
131 XID
132 present_fence_id(struct present_fence *present_fence)
133 {
134 if (!present_fence)
135 return None;
136 if (!present_fence->fence)
137 return None;
138 return present_fence->fence->sync.id;
139 }