summaryrefslogtreecommitdiff
path: root/EGL/yagl_display.c
blob: 4aec2823d0e52a6ca5f94a960f00ac7747bb975f (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 * YaGL
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact :
 * Stanislav Vorobiov <s.vorobiov@samsung.com>
 * Jinhyung Jo <jinhyung.jo@samsung.com>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#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 "yagl_malloc.h"
#include "yagl_fence.h"
#include "yagl_native_display.h"
#include "yagl_native_platform.h"
#include "yagl_client_image.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define YAGL_EGL_BASE_EXTENSIONS "EGL_KHR_image_base " \
                                 "EGL_KHR_lock_surface " \
                                 "EGL_KHR_surfaceless_context "

#define YAGL_EGL_PIXMAPS_EXTENSIONS "EGL_KHR_image " \
                                    "EGL_KHR_image_pixmap " \
                                    "EGL_NOK_texture_from_pixmap "

#define YAGL_EGL_WL_BIND_WAYLAND_DISPLAY_EXTENSIONS "EGL_WL_bind_wayland_display "

#define YAGL_EGL_GL_TEXTURE_EXTENSIONS "EGL_KHR_gl_texture_2D_image "

#define YAGL_EGL_TIZEN_EXTENSIONS "EGL_TIZEN_image_native_surface "

#define YAGL_EGL_BUFFER_AGE_EXTENSIONS "EGL_EXT_buffer_age "

#define YAGL_EGL_FENCE_EXTENSIONS "EGL_KHR_fence_sync "

void yagl_set_fence_display(struct yagl_display *fence_dpy);

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,
                       yagl_os_display display_id,
                       struct yagl_native_display *native_dpy,
                       yagl_host_handle host_dpy)
{
    yagl_list_init(&dpy->list);
    dpy->display_id = display_id;
    dpy->native_dpy = native_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;

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

void yagl_display_atfork(void)
{
    /*
     * Don't actually destroy displays, just
     * reset the list. See yagl_state.c:yagl_state_atfork.
     */

    yagl_list_init(&g_displays);
}

struct yagl_display *yagl_display_get(EGLDisplay handle)
{
    yagl_host_handle host_dpy = (yagl_host_handle)VOIDP2INT(handle);
    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);

            /*
             * We need to make sure fence_dpy is setup properly for current
             * thread state. Otherwise we may not be able to create fences in
             * transport layer and eventually will face a race condition between
             * host and target.
             *
             * E.g. the app may initialize all EGL stuff (i.e. get display,
             * choose config, etc.) and save it to some global state var. If
             * then it tries to create new context in some other thread,
             * fence_dpy will be NULL and as a result we may not get proper
             * values returned from the host.
             */
            yagl_set_fence_display(dpy);

            return dpy;
        }
    }

    pthread_mutex_unlock(&g_displays_mutex);

    return NULL;
}

struct yagl_display *yagl_display_get_os(yagl_os_display os_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->native_dpy->os_dpy == os_dpy) {
            pthread_mutex_unlock(&g_displays_mutex);

            return dpy;
        }
    }

    pthread_mutex_unlock(&g_displays_mutex);

    return NULL;
}

struct yagl_display *yagl_display_add(struct yagl_native_platform *platform,
                                      yagl_os_display display_id)
{
    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->display_id == display_id) {
            pthread_mutex_unlock(&g_displays_mutex);
            return dpy;
        }
    }

    dpy = yagl_get_backend()->create_display(platform, display_id);

    if (!dpy) {
        pthread_mutex_unlock(&g_displays_mutex);
        return NULL;
    }

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

    pthread_mutex_unlock(&g_displays_mutex);

    yagl_set_fence_display(dpy);

    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);
    }

    yagl_list_for_each_safe(struct yagl_resource,
                            res,
                            next,
                            &dpy->fences,
                            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));
    assert(yagl_list_empty(&dpy->fences));

    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));
}

const char *yagl_display_get_extensions(struct yagl_display *dpy)
{
    if (!dpy) {
        return YAGL_EGL_BASE_EXTENSIONS;
    }

    pthread_mutex_lock(&dpy->mutex);

    if (!dpy->extensions) {
        uint32_t len = strlen(YAGL_EGL_BASE_EXTENSIONS);

        if (dpy->native_dpy->platform->pixmaps_supported) {
            len += strlen(YAGL_EGL_PIXMAPS_EXTENSIONS);
        }

        if (dpy->native_dpy->WL_bind_wayland_display_supported) {
            len += strlen(YAGL_EGL_WL_BIND_WAYLAND_DISPLAY_EXTENSIONS);
        }

        if (dpy->native_dpy->platform->buffer_age_supported) {
            len += strlen(YAGL_EGL_BUFFER_AGE_EXTENSIONS);
        }

        if (yagl_egl_fence_supported()) {
            len += strlen(YAGL_EGL_FENCE_EXTENSIONS);
        }

        len += strlen(YAGL_EGL_GL_TEXTURE_EXTENSIONS);

        len += strlen(YAGL_EGL_TIZEN_EXTENSIONS);

        dpy->extensions = yagl_malloc(len + 1);

        strcpy(dpy->extensions, YAGL_EGL_BASE_EXTENSIONS);

        if (dpy->native_dpy->platform->pixmaps_supported) {
            strcat(dpy->extensions, YAGL_EGL_PIXMAPS_EXTENSIONS);
        }

        if (dpy->native_dpy->WL_bind_wayland_display_supported) {
            strcat(dpy->extensions, YAGL_EGL_WL_BIND_WAYLAND_DISPLAY_EXTENSIONS);
        }

        if (dpy->native_dpy->platform->buffer_age_supported) {
            strcat(dpy->extensions, YAGL_EGL_BUFFER_AGE_EXTENSIONS);
        }

        if (yagl_egl_fence_supported()) {
            strcat(dpy->extensions, YAGL_EGL_FENCE_EXTENSIONS);
        }

        strcat(dpy->extensions, YAGL_EGL_GL_TEXTURE_EXTENSIONS);

        strcat(dpy->extensions, YAGL_EGL_TIZEN_EXTENSIONS);
    }

    pthread_mutex_unlock(&dpy->mutex);

    return dpy->extensions;
}

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)VOIDP2INT(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)VOIDP2INT(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;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->images, list) {
        if (((struct yagl_image*)res)->client_handle == image->client_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 (((struct yagl_image*)res)->client_handle == 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 (((struct yagl_image*)res)->client_handle == 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_finalize_images(struct yagl_display *dpy)
{
    struct yagl_resource *res;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->images, list) {
        struct yagl_image *image = (struct yagl_image *)res;
        int need_finalize;

        yagl_image_acquire(image);

        yagl_client_image_acquire(image->client_image);
        need_finalize = image->client_image->need_finalize;
        image->client_image->need_finalize = 0;
        yagl_client_image_release(image->client_image);

        if (need_finalize && image->finalize) {
            image->finalize(image);
        }

        yagl_image_release(image);
    }

    pthread_mutex_unlock(&dpy->mutex);
}

void yagl_display_fence_add(struct yagl_display *dpy,
                            struct yagl_fence *fence)
{
    pthread_mutex_lock(&dpy->mutex);

    yagl_resource_acquire(&fence->base.res);
    yagl_list_add_tail(&dpy->fences, &fence->base.res.list);

    pthread_mutex_unlock(&dpy->mutex);
}

struct yagl_fence *yagl_display_fence_acquire(struct yagl_display *dpy,
                                              EGLSyncKHR handle)
{
    struct yagl_resource *res = NULL;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->fences, list) {
        if ((EGLSyncKHR)res == handle) {
            yagl_resource_acquire(res);
            pthread_mutex_unlock(&dpy->mutex);
            return (struct yagl_fence*)res;
        }
    }

    pthread_mutex_unlock(&dpy->mutex);

    return NULL;
}

int yagl_display_fence_remove(struct yagl_display *dpy,
                              EGLSyncKHR handle)
{
    struct yagl_resource *res;

    pthread_mutex_lock(&dpy->mutex);

    yagl_list_for_each(struct yagl_resource, res, &dpy->fences, list) {
        if ((EGLSyncKHR)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;
}