summaryrefslogtreecommitdiff
path: root/EGL/yagl_display.c
blob: 543c13bac55cab83816266943ec6f5dc26efaebe (plain)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "yagl_display.h"
#include "yagl_utils.h"
#include "yagl_backend.h"
#include "yagl_log.h"
#include "yagl_surface.h"
#include "yagl_context.h"
#include "yagl_image.h"
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

static pthread_once_t g_displays_init = PTHREAD_ONCE_INIT;
static pthread_mutex_t g_displays_mutex;
static YAGL_DECLARE_LIST(g_displays);

static void yagl_displays_init_once(void)
{
    /*
     * We need a recursive mutex here because in DRI2 an event can come in
     * during any X.Org function call and we need to access displays in
     * DRI2 event handler.
     */
    yagl_recursive_mutex_init(&g_displays_mutex);
}

static void yagl_displays_init(void)
{
    pthread_once(&g_displays_init, yagl_displays_init_once);
}

void yagl_display_init(struct yagl_display *dpy,
                       EGLNativeDisplayType display_id,
                       Display *x_dpy,
                       yagl_host_handle host_dpy)
{
    int xmajor;
    int xminor;
    Bool pixmaps;

    YAGL_LOG_FUNC_SET(eglGetDisplay);

    yagl_list_init(&dpy->list);
    dpy->display_id = display_id;
    dpy->x_dpy = x_dpy;
    dpy->host_dpy = host_dpy;
    /*
     * Need recursive mutex here,
     * see 'g_displays_mutex' initialization for reasons.
     */
    yagl_recursive_mutex_init(&dpy->mutex);
    dpy->prepared = 0;
    dpy->xshm_images_supported = XShmQueryVersion(dpy->x_dpy,
                                                  &xmajor,
                                                  &xminor,
                                                  &pixmaps);
    dpy->xshm_pixmaps_supported = pixmaps;

    YAGL_LOG_DEBUG("XShm images are%s supported, version %d, %d (pixmaps = %d)",
                   (dpy->xshm_images_supported ? "" : " NOT"),
                   xmajor,
                   xminor,
                   pixmaps);

    yagl_list_init(&dpy->surfaces);
    yagl_list_init(&dpy->contexts);
    yagl_list_init(&dpy->images);
}

void yagl_display_cleanup(struct yagl_display *dpy)
{
    assert(yagl_list_empty(&dpy->images));
    assert(yagl_list_empty(&dpy->contexts));
    assert(yagl_list_empty(&dpy->surfaces));
    pthread_mutex_destroy(&dpy->mutex);
    assert(yagl_list_empty(&dpy->list));
}

struct yagl_display *yagl_display_get(EGLDisplay native_dpy)
{
    yagl_host_handle host_dpy = (yagl_host_handle)native_dpy;
    struct yagl_display *dpy;

    yagl_displays_init();

    pthread_mutex_lock(&g_displays_mutex);

    yagl_list_for_each(struct yagl_display, dpy, &g_displays, list) {
        if (dpy->host_dpy == host_dpy) {
            pthread_mutex_unlock(&g_displays_mutex);

            return dpy;
        }
    }

    pthread_mutex_unlock(&g_displays_mutex);

    return NULL;
}

struct yagl_display *yagl_display_get_x(Display *x_dpy)
{
    struct yagl_display *dpy;

    yagl_displays_init();

    pthread_mutex_lock(&g_displays_mutex);

    yagl_list_for_each(struct yagl_display, dpy, &g_displays, list) {
        if (dpy->x_dpy == x_dpy) {
            pthread_mutex_unlock(&g_displays_mutex);

            return dpy;
        }
    }

    pthread_mutex_unlock(&g_displays_mutex);

    return NULL;
}

struct yagl_display *yagl_display_add(EGLNativeDisplayType display_id,
                                      yagl_host_handle host_dpy)
{
    struct yagl_display *dpy;
    Display *x_dpy;

    YAGL_LOG_FUNC_SET(eglGetDisplay);

    yagl_displays_init();

    pthread_mutex_lock(&g_displays_mutex);

    yagl_list_for_each(struct yagl_display, dpy, &g_displays, list) {
        if (dpy->host_dpy == host_dpy) {
            pthread_mutex_unlock(&g_displays_mutex);

            if (dpy->display_id != display_id) {
                YAGL_LOG_ERROR(
                    "display id mismatch, host handle is %u, corresponding display id is %p, passed display id is %p",
                     dpy->host_dpy,
                     dpy->display_id,
                     display_id );

                return NULL;
            } else {
                return dpy;
            }
        }
    }

    if (display_id == EGL_DEFAULT_DISPLAY) {
        x_dpy = XOpenDisplay(0);

        if (!x_dpy) {
            pthread_mutex_unlock(&g_displays_mutex);

            YAGL_LOG_ERROR("unable to open display 0");

            return NULL;
        }
    } else {
        x_dpy = display_id;
    }

    dpy = yagl_get_backend()->create_display(display_id,
                                             x_dpy,
                                             host_dpy);

    if (!dpy) {
        if (display_id == EGL_DEFAULT_DISPLAY) {
            XCloseDisplay(x_dpy);
        }

        pthread_mutex_unlock(&g_displays_mutex);

        return NULL;
    }

    yagl_list_add(&g_displays, &dpy->list);

    pthread_mutex_unlock(&g_displays_mutex);

    return dpy;
}

void yagl_display_prepare(struct yagl_display *dpy)
{
    pthread_mutex_lock(&dpy->mutex);
    dpy->prepared = 1;
    pthread_mutex_unlock(&dpy->mutex);
}

int yagl_display_is_prepared(struct yagl_display *dpy)
{
    int ret;

    pthread_mutex_lock(&dpy->mutex);
    ret = dpy->prepared;
    pthread_mutex_unlock(&dpy->mutex);

    return ret;
}

void yagl_display_terminate(struct yagl_display *dpy)
{
    struct yagl_list tmp_list;
    struct yagl_resource *res, *next;

    /*
     * First, move all surfaces, contexts and images to a
     * temporary list to release later.
     */

    yagl_list_init(&tmp_list);

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each_safe(struct yagl_resource,
                            res,
                            next,
                            &dpy->surfaces,
                            list) {
        yagl_list_remove(&res->list);
        yagl_list_add_tail(&tmp_list, &res->list);
    }

    yagl_list_for_each_safe(struct yagl_resource,
                            res,
                            next,
                            &dpy->contexts,
                            list) {
        yagl_list_remove(&res->list);
        yagl_list_add_tail(&tmp_list, &res->list);
    }

    yagl_list_for_each_safe(struct yagl_resource,
                            res,
                            next,
                            &dpy->images,
                            list) {
        yagl_list_remove(&res->list);
        yagl_list_add_tail(&tmp_list, &res->list);
    }

    assert(yagl_list_empty(&dpy->surfaces));
    assert(yagl_list_empty(&dpy->contexts));
    assert(yagl_list_empty(&dpy->images));

    dpy->prepared = 0;

    pthread_mutex_unlock(&dpy->mutex);

    /*
     * We release here because we don't want the resources to be released
     * when display mutex is held.
     */

    yagl_list_for_each_safe(struct yagl_resource, res, next, &tmp_list, list) {
        yagl_list_remove(&res->list);
        yagl_resource_release(res);
    }

    assert(yagl_list_empty(&tmp_list));
}

int yagl_display_surface_add(struct yagl_display *dpy,
                              struct yagl_surface *sfc)
{
    struct yagl_resource *res = NULL;
    EGLSurface handle = yagl_surface_get_handle(sfc);

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->surfaces, list) {
        if (yagl_surface_get_handle((struct yagl_surface*)res) == handle) {
            pthread_mutex_unlock(&dpy->mutex);
            return 0;
        }
    }

    yagl_resource_acquire(&sfc->res);
    yagl_list_add_tail(&dpy->surfaces, &sfc->res.list);

    pthread_mutex_unlock(&dpy->mutex);

    return 1;
}

struct yagl_surface *yagl_display_surface_acquire(struct yagl_display *dpy,
                                                  EGLSurface handle)
{
    struct yagl_resource *res = NULL;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->surfaces, list) {
        if (yagl_surface_get_handle((struct yagl_surface*)res) == handle) {
            yagl_resource_acquire(res);
            pthread_mutex_unlock(&dpy->mutex);
            return (struct yagl_surface*)res;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return NULL;
}

int yagl_display_surface_remove(struct yagl_display *dpy,
                                EGLSurface handle)
{
    struct yagl_resource *res;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->surfaces, list) {
        if (yagl_surface_get_handle((struct yagl_surface*)res) == handle) {
            yagl_list_remove(&res->list);
            yagl_resource_release(res);
            pthread_mutex_unlock(&dpy->mutex);
            return 1;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return 0;
}

void yagl_display_context_add(struct yagl_display *dpy,
                              struct yagl_context *ctx)
{
    pthread_mutex_lock(&dpy->mutex);

    yagl_resource_acquire(&ctx->res);
    yagl_list_add_tail(&dpy->contexts, &ctx->res.list);

    pthread_mutex_unlock(&dpy->mutex);
}

struct yagl_context *yagl_display_context_acquire(struct yagl_display *dpy,
                                                  EGLContext handle)
{
    struct yagl_resource *res = NULL;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->contexts, list) {
        if (res->handle == (yagl_host_handle)handle) {
            yagl_resource_acquire(res);
            pthread_mutex_unlock(&dpy->mutex);
            return (struct yagl_context*)res;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return NULL;
}

void yagl_display_context_remove(struct yagl_display *dpy,
                                 EGLContext handle)
{
    struct yagl_resource *res;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->contexts, list) {
        if (res->handle == (yagl_host_handle)handle) {
            yagl_list_remove(&res->list);
            yagl_resource_release(res);
            break;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);
}

int yagl_display_image_add(struct yagl_display *dpy,
                           struct yagl_image *image)
{
    struct yagl_resource *res = NULL;
    EGLImageKHR handle = yagl_image_get_handle(image);

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->images, list) {
        if (yagl_image_get_handle((struct yagl_image*)res) == handle) {
            pthread_mutex_unlock(&dpy->mutex);
            return 0;
        }
    }

    yagl_resource_acquire(&image->res);
    yagl_list_add_tail(&dpy->images, &image->res.list);

    pthread_mutex_unlock(&dpy->mutex);

    return 1;
}

struct yagl_image *yagl_display_image_acquire(struct yagl_display *dpy,
                                              EGLImageKHR handle)
{
    struct yagl_resource *res = NULL;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->images, list) {
        if (yagl_image_get_handle((struct yagl_image*)res) == handle) {
            yagl_resource_acquire(res);
            pthread_mutex_unlock(&dpy->mutex);
            return (struct yagl_image*)res;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return NULL;
}

int yagl_display_image_remove(struct yagl_display *dpy,
                              EGLImageKHR handle)
{
    struct yagl_resource *res;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->images, list) {
        if (yagl_image_get_handle((struct yagl_image*)res) == handle) {
            yagl_list_remove(&res->list);
            yagl_resource_release(res);
            pthread_mutex_unlock(&dpy->mutex);
            return 1;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return 0;
}