Imported Upstream version 1.15.1
[deb_xorg-server.git] / miext / sync / misync.c
1 /*
2 * Copyright © 2010 NVIDIA Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
26 #endif
27
28 #include "scrnintstr.h"
29 #include "misync.h"
30 #include "misyncstr.h"
31
32 DevPrivateKeyRec miSyncScreenPrivateKey;
33
34 /* Default implementations of the sync screen functions */
35 void
36 miSyncScreenCreateFence(ScreenPtr pScreen, SyncFence * pFence,
37 Bool initially_triggered)
38 {
39 (void) pScreen;
40
41 pFence->triggered = initially_triggered;
42 }
43
44 void
45 miSyncScreenDestroyFence(ScreenPtr pScreen, SyncFence * pFence)
46 {
47 (void) pScreen;
48 (void) pFence;
49 }
50
51 /* Default implementations of the per-object functions */
52 void
53 miSyncFenceSetTriggered(SyncFence * pFence)
54 {
55 pFence->triggered = TRUE;
56 }
57
58 void
59 miSyncFenceReset(SyncFence * pFence)
60 {
61 pFence->triggered = FALSE;
62 }
63
64 Bool
65 miSyncFenceCheckTriggered(SyncFence * pFence)
66 {
67 return pFence->triggered;
68 }
69
70 void
71 miSyncFenceAddTrigger(SyncTrigger * pTrigger)
72 {
73 (void) pTrigger;
74
75 return;
76 }
77
78 void
79 miSyncFenceDeleteTrigger(SyncTrigger * pTrigger)
80 {
81 (void) pTrigger;
82
83 return;
84 }
85
86 /* Machine independent portion of the fence sync object implementation */
87 void
88 miSyncInitFence(ScreenPtr pScreen, SyncFence * pFence, Bool initially_triggered)
89 {
90 SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
91
92 static const SyncFenceFuncsRec miSyncFenceFuncs = {
93 &miSyncFenceSetTriggered,
94 &miSyncFenceReset,
95 &miSyncFenceCheckTriggered,
96 &miSyncFenceAddTrigger,
97 &miSyncFenceDeleteTrigger
98 };
99
100 pFence->pScreen = pScreen;
101 pFence->funcs = miSyncFenceFuncs;
102
103 pScreenPriv->funcs.CreateFence(pScreen, pFence, initially_triggered);
104 }
105
106 void
107 miSyncDestroyFence(SyncFence * pFence)
108 {
109 ScreenPtr pScreen = pFence->pScreen;
110 SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
111 SyncTriggerList *ptl, *pNext;
112
113 pFence->sync.beingDestroyed = TRUE;
114 /* tell all the fence's triggers that the counter has been destroyed */
115 for (ptl = pFence->sync.pTriglist; ptl; ptl = pNext) {
116 (*ptl->pTrigger->CounterDestroyed) (ptl->pTrigger);
117 pNext = ptl->next;
118 free(ptl); /* destroy the trigger list as we go */
119 }
120
121 pScreenPriv->funcs.DestroyFence(pScreen, pFence);
122
123 dixFreeObjectWithPrivates(pFence, PRIVATE_SYNC_FENCE);
124 }
125
126 void
127 miSyncTriggerFence(SyncFence * pFence)
128 {
129 SyncTriggerList *ptl, *pNext;
130 CARD64 unused;
131
132 pFence->funcs.SetTriggered(pFence);
133
134 XSyncIntToValue(&unused, 0L);
135
136 /* run through triggers to see if any fired */
137 for (ptl = pFence->sync.pTriglist; ptl; ptl = pNext) {
138 pNext = ptl->next;
139 if ((*ptl->pTrigger->CheckTrigger) (ptl->pTrigger, unused))
140 (*ptl->pTrigger->TriggerFired) (ptl->pTrigger);
141 }
142 }
143
144 SyncScreenFuncsPtr
145 miSyncGetScreenFuncs(ScreenPtr pScreen)
146 {
147 SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
148
149 return &pScreenPriv->funcs;
150 }
151
152 static Bool
153 SyncCloseScreen(ScreenPtr pScreen)
154 {
155 SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
156
157 pScreen->CloseScreen = pScreenPriv->CloseScreen;
158
159 return (*pScreen->CloseScreen) (pScreen);
160 }
161
162 Bool
163 miSyncSetup(ScreenPtr pScreen)
164 {
165 SyncScreenPrivPtr pScreenPriv;
166
167 static const SyncScreenFuncsRec miSyncScreenFuncs = {
168 &miSyncScreenCreateFence,
169 &miSyncScreenDestroyFence
170 };
171
172 if (!dixPrivateKeyRegistered(&miSyncScreenPrivateKey)) {
173 if (!dixRegisterPrivateKey(&miSyncScreenPrivateKey, PRIVATE_SCREEN,
174 sizeof(SyncScreenPrivRec)))
175 return FALSE;
176 }
177
178 pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
179
180 if (!pScreenPriv->funcs.CreateFence) {
181 pScreenPriv->funcs = miSyncScreenFuncs;
182
183 /* Wrap CloseScreen to clean up */
184 pScreenPriv->CloseScreen = pScreen->CloseScreen;
185 pScreen->CloseScreen = SyncCloseScreen;
186 }
187
188 return TRUE;
189 }