1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#include "ws_dri_fencemgr.h"
#include "glthread.h"
#include <xf86mm.h>
#include <string.h>
#include <unistd.h>
/*
* Note: Locking order is
* _DriFenceObject::mutex
* _DriFenceMgr::mutex
*/
struct _DriFenceMgr {
/*
* Constant members. Need no mutex protection.
*/
struct _DriFenceMgrCreateInfo info;
void *private;
/*
* These members are protected by this->mutex
*/
_glthread_Mutex mutex;
int refCount;
drmMMListHead *heads;
};
struct _DriFenceObject {
/*
* These members are constant and need no mutex protection.
*/
struct _DriFenceMgr *mgr;
uint32_t fence_class;
uint32_t fence_type;
/*
* These members are protected by mgr->mutex.
*/
drmMMListHead head;
int refCount;
/*
* These members are protected by this->mutex.
*/
_glthread_Mutex mutex;
uint32_t signaled_type;
void *private;
};
uint32_t
driFenceType(struct _DriFenceObject *fence)
{
return fence->fence_type;
}
struct _DriFenceMgr *
driFenceMgrCreate(const struct _DriFenceMgrCreateInfo *info)
{
struct _DriFenceMgr *tmp;
uint32_t i;
tmp = calloc(1, sizeof(*tmp));
if (!tmp)
return NULL;
_glthread_INIT_MUTEX(tmp->mutex);
_glthread_LOCK_MUTEX(tmp->mutex);
tmp->refCount = 1;
tmp->info = *info;
tmp->heads = calloc(tmp->info.num_classes, sizeof(*tmp->heads));
if (!tmp->heads)
goto out_err;
for (i=0; i<tmp->info.num_classes; ++i) {
DRMINITLISTHEAD(&tmp->heads[i]);
}
_glthread_UNLOCK_MUTEX(tmp->mutex);
return tmp;
out_err:
if (tmp)
free(tmp);
return NULL;
}
static void
driFenceMgrUnrefUnlock(struct _DriFenceMgr **pMgr)
{
struct _DriFenceMgr *mgr = *pMgr;
*pMgr = NULL;
if (--mgr->refCount == 0)
free(mgr);
else
_glthread_UNLOCK_MUTEX(mgr->mutex);
}
void
driFenceMgrUnReference(struct _DriFenceMgr **pMgr)
{
_glthread_LOCK_MUTEX((*pMgr)->mutex);
driFenceMgrUnrefUnlock(pMgr);
}
static void
driFenceUnReferenceLocked(struct _DriFenceObject **pFence)
{
struct _DriFenceObject *fence = *pFence;
struct _DriFenceMgr *mgr = fence->mgr;
*pFence = NULL;
if (--fence->refCount == 0) {
DRMLISTDELINIT(&fence->head);
if (fence->private)
mgr->info.unreference(mgr, &fence->private);
fence->mgr = NULL;
--mgr->refCount;
free(fence);
}
}
static void
driSignalPreviousFencesLocked(struct _DriFenceMgr *mgr,
drmMMListHead *list,
uint32_t fence_class,
uint32_t fence_type)
{
struct _DriFenceObject *entry;
drmMMListHead *prev;
while(list != &mgr->heads[fence_class]) {
entry = DRMLISTENTRY(struct _DriFenceObject, list, head);
/*
* Up refcount so that entry doesn't disappear from under us
* when we unlock-relock mgr to get the correct locking order.
*/
++entry->refCount;
_glthread_UNLOCK_MUTEX(mgr->mutex);
_glthread_LOCK_MUTEX(entry->mutex);
_glthread_LOCK_MUTEX(mgr->mutex);
prev = list->prev;
if (list->prev == list) {
/*
* Somebody else removed the entry from the list.
*/
_glthread_UNLOCK_MUTEX(entry->mutex);
driFenceUnReferenceLocked(&entry);
return;
}
entry->signaled_type |= (fence_type & entry->fence_type);
if (entry->signaled_type == entry->fence_type) {
DRMLISTDELINIT(list);
mgr->info.unreference(mgr, &entry->private);
}
_glthread_UNLOCK_MUTEX(entry->mutex);
driFenceUnReferenceLocked(&entry);
list = prev;
}
}
int
driFenceFinish(struct _DriFenceObject *fence, uint32_t fence_type,
int lazy_hint)
{
struct _DriFenceMgr *mgr = fence->mgr;
int ret = 0;
_glthread_LOCK_MUTEX(fence->mutex);
if ((fence->signaled_type & fence_type) == fence_type)
goto out0;
ret = mgr->info.finish(mgr, fence->private, fence_type, lazy_hint);
if (ret)
goto out0;
_glthread_LOCK_MUTEX(mgr->mutex);
_glthread_UNLOCK_MUTEX(fence->mutex);
driSignalPreviousFencesLocked(mgr, &fence->head, fence->fence_class,
fence_type);
_glthread_UNLOCK_MUTEX(mgr->mutex);
return 0;
out0:
_glthread_UNLOCK_MUTEX(fence->mutex);
return ret;
}
uint32_t driFenceSignaledTypeCached(struct _DriFenceObject *fence)
{
uint32_t ret;
_glthread_LOCK_MUTEX(fence->mutex);
ret = fence->signaled_type;
_glthread_UNLOCK_MUTEX(fence->mutex);
return ret;
}
int
driFenceSignaledType(struct _DriFenceObject *fence, uint32_t flush_type,
uint32_t *signaled)
{
int ret = 0;
struct _DriFenceMgr *mgr;
_glthread_LOCK_MUTEX(fence->mutex);
mgr = fence->mgr;
*signaled = fence->signaled_type;
if ((fence->signaled_type & flush_type) == flush_type)
goto out0;
ret = mgr->info.signaled(mgr, fence->private, flush_type, signaled);
if (ret) {
*signaled = fence->signaled_type;
goto out0;
}
if ((fence->signaled_type | *signaled) == fence->signaled_type)
goto out0;
_glthread_LOCK_MUTEX(mgr->mutex);
_glthread_UNLOCK_MUTEX(fence->mutex);
driSignalPreviousFencesLocked(mgr, &fence->head, fence->fence_class,
*signaled);
_glthread_UNLOCK_MUTEX(mgr->mutex);
return 0;
out0:
_glthread_UNLOCK_MUTEX(fence->mutex);
return ret;
}
struct _DriFenceObject *
driFenceReference(struct _DriFenceObject *fence)
{
_glthread_LOCK_MUTEX(fence->mgr->mutex);
++fence->refCount;
_glthread_UNLOCK_MUTEX(fence->mgr->mutex);
return fence;
}
void
driFenceUnReference(struct _DriFenceObject **pFence)
{
struct _DriFenceMgr *mgr;
if (*pFence == NULL)
return;
mgr = (*pFence)->mgr;
_glthread_LOCK_MUTEX(mgr->mutex);
++mgr->refCount;
driFenceUnReferenceLocked(pFence);
driFenceMgrUnrefUnlock(&mgr);
}
struct _DriFenceObject
*driFenceCreate(struct _DriFenceMgr *mgr, uint32_t fence_class,
uint32_t fence_type, void *private, size_t private_size)
{
struct _DriFenceObject *fence;
size_t fence_size = sizeof(*fence);
if (private_size)
fence_size = ((fence_size + 15) & ~15);
fence = calloc(1, fence_size + private_size);
if (!fence) {
int ret = mgr->info.finish(mgr, private, fence_type, 0);
if (ret)
usleep(10000000);
return NULL;
}
_glthread_INIT_MUTEX(fence->mutex);
_glthread_LOCK_MUTEX(fence->mutex);
_glthread_LOCK_MUTEX(mgr->mutex);
fence->refCount = 1;
DRMLISTADDTAIL(&fence->head, &mgr->heads[fence_class]);
fence->mgr = mgr;
++mgr->refCount;
_glthread_UNLOCK_MUTEX(mgr->mutex);
fence->fence_class = fence_class;
fence->fence_type = fence_type;
fence->signaled_type = 0;
fence->private = private;
if (private_size) {
fence->private = (void *)(((uint8_t *) fence) + fence_size);
memcpy(fence->private, private, private_size);
}
_glthread_UNLOCK_MUTEX(fence->mutex);
return fence;
}
static int
tSignaled(struct _DriFenceMgr *mgr, void *private, uint32_t flush_type,
uint32_t *signaled_type)
{
long fd = (long) mgr->private;
int dummy;
drmFence *fence = (drmFence *) private;
int ret;
*signaled_type = 0;
ret = drmFenceSignaled((int) fd, fence, flush_type, &dummy);
if (ret)
return ret;
*signaled_type = fence->signaled;
return 0;
}
static int
tFinish(struct _DriFenceMgr *mgr, void *private, uint32_t fence_type,
int lazy_hint)
{
long fd = (long) mgr->private;
unsigned flags = lazy_hint ? DRM_FENCE_FLAG_WAIT_LAZY : 0;
return drmFenceWait((int)fd, flags, (drmFence *) private, fence_type);
}
static int
tUnref(struct _DriFenceMgr *mgr, void **private)
{
long fd = (long) mgr->private;
drmFence *fence = (drmFence *) *private;
*private = NULL;
return drmFenceUnreference(fd, fence);
}
struct _DriFenceMgr *driFenceMgrTTMInit(int fd)
{
struct _DriFenceMgrCreateInfo info;
struct _DriFenceMgr *mgr;
info.flags = DRI_FENCE_CLASS_ORDERED;
info.num_classes = 4;
info.signaled = tSignaled;
info.finish = tFinish;
info.unreference = tUnref;
mgr = driFenceMgrCreate(&info);
if (mgr == NULL)
return NULL;
mgr->private = (void *) (long) fd;
return mgr;
}
|