summaryrefslogtreecommitdiff
path: root/egl.c
blob: 5acaa245cada9aecd98084a1db0a2a31401f6658 (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
/*
 * Derived from GStreamer 0.10 EGL Library in gst-omx
 * Copyright (C) 2014 Fluendo S.A.
 *   @author: Josep Torra <josep@fluendo.com>
 * *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


#define EGL_EGLEXT_PROTOTYPES
#include "egl.h"

#define DEBUG_MAGIC_CHECK 0

#if DEBUG_MAGIC_CHECK
#define EGLMEMORY_MAGIC 0x5fce5fce
#define EGLMEMORY_POOL_MAGIC 0x5fce9001

#define EGLMEMORY_POOL_MAGIC_ASSERT(pool)  G_STMT_START {  \
  g_assert (pool->magic == EGLMEMORY_POOL_MAGIC);          \
} G_STMT_END
#define EGLMEMORY_MAGIC_ASSERT(mem)  G_STMT_START {        \
  g_assert (mem->magic == EGLMEMORY_MAGIC);                \
} G_STMT_END
#else
#define EGLMEMORY_POOL_MAGIC_ASSERT(pool)  G_STMT_START{ }G_STMT_END
#define EGLMEMORY_MAGIC_ASSERT(mem)  G_STMT_START{ }G_STMT_END
#endif

struct _EGLImageMemory
{
  volatile gint refcount;

#if DEBUG_MAGIC_CHECK
  guint32 magic;
#endif

  EGLClientBuffer client_buffer;
  EGLImageKHR image;

  gpointer user_data;
  GDestroyNotify destroy_data;

  EGLImageMemoryPool *pool;
};

#define EGL_IMAGE_MEMORY(mem) ((EGLImageMemory *)(mem))

struct _EGLImageMemoryPool
{
  volatile gint refcount;

#if DEBUG_MAGIC_CHECK
  guint32 magic;
#endif

  GMutex *lock;
  GCond *cond;

  EGLDisplayWrapper *display;
  EGLImageMemory *memory;

  gint size;
  volatile gint unused;
  volatile gint active;

  gpointer user_data;
  DestroyNotifyEGLImageMemoryPool destroy_data;
};

#define EGL_IMAGE_MEMORY_POOL_LOCK(pool) G_STMT_START {           \
  g_mutex_lock (pool->lock);                                          \
} G_STMT_END

#define EGL_IMAGE_MEMORY_POOL_UNLOCK(pool) G_STMT_START {         \
  g_mutex_unlock (pool->lock);                                        \
} G_STMT_END

#define EGL_IMAGE_MEMORY_POOL_WAIT_RELEASED(pool) G_STMT_START {  \
  g_cond_wait (pool->cond, pool->lock);                               \
} G_STMT_END

#define EGL_IMAGE_MEMORY_POOL_SIGNAL_UNUSED(pool) G_STMT_START {  \
  g_cond_signal (pool->cond);                                         \
} G_STMT_END

#define EGL_IMAGE_MEMORY_POOL_IS_FULL(pool) (g_atomic_int_get (&pool->unused) == 0)
#define EGL_IMAGE_MEMORY_POOL_IS_EMPTY(pool) (g_atomic_int_get (&pool->unused) == pool->size)

#define EGL_IMAGE_MEMORY_POOL_SET_ACTIVE(pool,active) \
  g_atomic_int_set (&pool->active, active)
#define EGL_IMAGE_MEMORY_POOL_IS_ACTIVE(pool) (g_atomic_int_get (&pool->active) == TRUE)

struct _EGLDisplayWrapper
{
  volatile gint refcount;

  EGLDisplay display;

  gpointer user_data;
  GDestroyNotify destroy_data;
};

/**
 * egl_image_memory_ref
 * @mem: a #EGLImageMemory
 *
 * Increase the refcount of @mem.
 *
 */
EGLImageMemory *
egl_image_memory_ref (EGLImageMemory * mem)
{
  g_return_val_if_fail (mem != NULL, NULL);

  EGLMEMORY_MAGIC_ASSERT (mem);

  g_atomic_int_inc (&mem->refcount);

  return mem;
}

static void
egl_image_memory_free (EGLImageMemory * mem)
{
  EGLImageMemoryPool *pool = mem->pool;

  /* We grab the pool lock as we will make changes to number of unused
   * memories. */
  EGL_IMAGE_MEMORY_POOL_LOCK (pool);

  /* We have one more unused memory */
  g_atomic_int_inc (&pool->unused);

  /* Wake up potential waiters */
  EGL_IMAGE_MEMORY_POOL_SIGNAL_UNUSED (pool);

  EGL_IMAGE_MEMORY_POOL_UNLOCK (pool);

  if (mem->destroy_data) {
    mem->destroy_data (mem->user_data);
  }

  /* Remove our ref to the pool. That could destroy the pool so it is
   * important to do that when we released the lock */
  mem->pool = egl_image_memory_pool_unref (pool);
}

/**
 * egl_image_memory_unref:
 * @mem: a #EGLImageMemory
 *
 * Unref @mem and when the refcount reaches 0 frees the resources
 * and return NULL.
 *
 */
EGLImageMemory *
egl_image_memory_unref (EGLImageMemory * mem)
{
  g_return_val_if_fail (mem != NULL, NULL);

  EGLMEMORY_MAGIC_ASSERT (mem);

  if (g_atomic_int_dec_and_test (&mem->refcount)) {
    egl_image_memory_free (mem);
    mem = NULL;
  }

  return mem;
}

/**
 * egl_image_memory_get_image:
 * @mem: a #EGLImageMemory
 *
 * Gives the #EGLImageKHR used by the #EGLImageMemory.
 *
 */
EGLImageKHR
egl_image_memory_get_image (EGLImageMemory * mem)
{
  return mem->image;
}

/**
 * egl_image_memory_pool_new:
 * @size: a number of memories managed by the pool
 * @display: a #EGLDisplayWrapper display
 * @user_data: user data passed to the callback
 * @destroy_data: #DestroyNotifyEGLImageMemoryPool for user_data
 *
 * Create a new EGLImageMemoryPool instance with the provided images.
 *
 * Returns: a new #EGLImageMemoryPool
 *
 */
EGLImageMemoryPool *
egl_image_memory_pool_new (gint size, EGLDisplayWrapper * display,
    gpointer user_data, DestroyNotifyEGLImageMemoryPool destroy_data)
{
  EGLImageMemoryPool *pool;

  pool = g_new0 (EGLImageMemoryPool, 1);

  pool->refcount = 1;
  pool->lock = g_mutex_new ();
  pool->cond = g_cond_new ();
  pool->display = egl_display_ref (display);
  pool->memory = (EGLImageMemory *) g_new0 (EGLImageMemory, size);
  pool->size = pool->unused = size;
  pool->user_data = user_data;
  pool->destroy_data = destroy_data;

#if DEBUG_MAGIC_CHECK
  {
    gint i;
    pool->magic = EGLMEMORY_POOL_MAGIC;
    for (i = 0; i < size; i++) {
      EGLImageMemory *mem = &pool->memory[i];
      mem->magic = EGLMEMORY_MAGIC;
    }
  }
#endif

  return pool;
}

/**
 * egl_image_memory_pool_ref:
 * @pool: a #EGLImageMemoryPool
 *
 * Increase the refcount of @pool.
 *
 */
EGLImageMemoryPool *
egl_image_memory_pool_ref (EGLImageMemoryPool * pool)
{
  g_return_val_if_fail (pool != NULL, NULL);

  EGLMEMORY_POOL_MAGIC_ASSERT (pool);

  g_atomic_int_inc (&pool->refcount);

  return pool;
}

static void
egl_image_memory_pool_free (EGLImageMemoryPool * pool)
{
  if (g_atomic_int_get (&pool->unused) != pool->size) {
    g_critical ("refcounting problem detected, some memory is still in use");
  }

  if (pool->destroy_data) {
    pool->destroy_data (pool, pool->user_data);
  }

  egl_display_unref (pool->display);
  g_mutex_free (pool->lock);
  g_cond_free (pool->cond);

  g_free (pool->memory);
  g_free (pool);
}

/**
 * egl_image_memory_pool_unref:
 * @pool: a #EGLImageMemoryPool
 *
 * Unref @pool and when the refcount reaches 0 frees the resources
 * and return NULL.
 *
 */
EGLImageMemoryPool *
egl_image_memory_pool_unref (EGLImageMemoryPool * pool)
{
  g_return_val_if_fail (pool != NULL, NULL);

  EGLMEMORY_POOL_MAGIC_ASSERT (pool);

  if (g_atomic_int_dec_and_test (&pool->refcount)) {
    egl_image_memory_pool_free (pool);
    pool = NULL;
  }
  return pool;
}

/**
 * egl_image_memory_pool_get_size:
 * @pool: a #EGLImageMemoryPool
 *
 * Gives the number of memories that are managed by the pool.
 *
 */
gint
egl_image_memory_pool_get_size (EGLImageMemoryPool * pool)
{
  g_return_val_if_fail (pool != NULL, 0);

  return pool->size;
}

/**
 * egl_image_memory_pool_set_resources:
 * @pool: a #EGLImageMemoryPool
 * @idx: memory index
 * @client_buffer: an #EGLClientBuffer to store in the pool
 * @image: an #EGLImageKHR to store in the pool.
 *
 * Stores @client_buffer and @image at @idx memory slot.
 *
 */
gboolean
egl_image_memory_pool_set_resources (EGLImageMemoryPool * pool, gint idx,
    EGLClientBuffer client_buffer, EGLImageKHR image)
{
  EGLImageMemory *mem;

  g_return_val_if_fail (pool != NULL, FALSE);
  g_return_val_if_fail (idx >= 0 && idx < pool->size, FALSE);

  mem = &pool->memory[idx];
  mem->client_buffer = client_buffer;
  mem->image = image;

  return TRUE;
}

/**
 * egl_image_memory_pool_get_resources:
 * @pool: a #EGLImageMemoryPool
 * @idx: memory index
 * @client_buffer: (out) (allow-none): the #EGLClientBuffer at @idx 
 * @image: (out) (allow-none): the #EGLImageKHR at @idx
 *
 * Retrieves @client_buffer and @image at @idx memory slot.
 *
 */
gboolean
egl_image_memory_pool_get_resources (EGLImageMemoryPool * pool, gint idx,
    EGLClientBuffer * client_buffer, EGLImageKHR * image)
{
  EGLImageMemory *mem;

  g_return_val_if_fail (pool != NULL, FALSE);
  g_return_val_if_fail (idx >= 0 && idx < pool->size, FALSE);

  mem = &pool->memory[idx];

  if (client_buffer)
    *client_buffer = mem->client_buffer;

  if (image)
    *image = mem->image;

  return TRUE;
}

/**
 * egl_image_memory_pool_get_display:
 * @pool: a #EGLImageMemoryPool
 *
 * Provides a reference to the #EGLDisplayWrapper used by the pool
 */
EGLDisplayWrapper *
egl_image_memory_pool_get_display (EGLImageMemoryPool * pool)
{
  g_return_val_if_fail (pool != NULL, NULL);

  EGLMEMORY_POOL_MAGIC_ASSERT (pool);

  g_return_val_if_fail (pool->display != NULL, NULL);

  return egl_display_ref (pool->display);
}

/**
 * egl_image_memory_pool_get_images:
 * @pool: a #EGLImageMemoryPool
 *
 * Provides a #GList of EGL images
 */
GList *
egl_image_memory_pool_get_images (EGLImageMemoryPool * pool)
{
  gint i;
  GList *images = NULL;

  g_return_val_if_fail (pool != NULL, NULL);

  for (i = 0; i < pool->size; i++) {
    EGLImageMemory *mem = &pool->memory[i];
    images = g_list_append (images, mem->image);
  }

  return images;
}

/**
 * egl_image_memory_pool_set_active:
 * @pool: a #EGLImageMemoryPool
 * @active: a #gboolean that indicates the pool is active
 *
 * Change @pool active state and unblocks any wait condition if needed.
 *
 */
void
egl_image_memory_pool_set_active (EGLImageMemoryPool * pool,
    gboolean active)
{
  g_return_if_fail (pool != NULL);

  EGLMEMORY_POOL_MAGIC_ASSERT (pool);

  EGL_IMAGE_MEMORY_POOL_SET_ACTIVE (pool, active);

  if (!active) {
    EGL_IMAGE_MEMORY_POOL_SIGNAL_UNUSED (pool);
  }
}

/**
 * egl_image_memory_pool_wait_released:
 * @pool: a #EGLImageMemoryPool
 *
 * Waits until none of the memory is in use.
 *
 */
void
egl_image_memory_pool_wait_released (EGLImageMemoryPool * pool)
{
  g_return_if_fail (pool != NULL);

  EGLMEMORY_POOL_MAGIC_ASSERT (pool);

  EGL_IMAGE_MEMORY_POOL_LOCK (pool);
  while (!EGL_IMAGE_MEMORY_POOL_IS_EMPTY (pool)) {
    EGL_IMAGE_MEMORY_POOL_WAIT_RELEASED (pool);
  }
  EGL_IMAGE_MEMORY_POOL_UNLOCK (pool);
}

/**
 * egl_image_memory_pool_acquire_memory:
 * @pool: a #EGLImageMemoryPool
 * @idx: ordinal that specifies a #EGLImageMemory in the pool
 * @user_data: user data passed to the callback
 * @destroy_data: #GDestroyNotify for user_data
 * 
 * Provides an specified #EGLImageMemory.
 *
 */
EGLImageMemory *
egl_image_memory_pool_acquire_memory (EGLImageMemoryPool * pool,
    gint idx, gpointer user_data, GDestroyNotify destroy_data)
{
  EGLImageMemory *mem;

  g_return_val_if_fail (pool != NULL, NULL);
  g_return_val_if_fail (idx >= 0 && idx < pool->size, NULL);

  EGL_IMAGE_MEMORY_POOL_LOCK (pool);
  if (!EGL_IMAGE_MEMORY_POOL_IS_ACTIVE (pool)) {
    EGL_IMAGE_MEMORY_POOL_UNLOCK (pool);
    return NULL;
  }

  mem = &pool->memory[idx];

  g_atomic_int_add (&pool->unused, -1);
  g_atomic_int_set (&mem->refcount, 1);
  mem->pool = egl_image_memory_pool_ref (pool);
  mem->user_data = user_data;
  mem->destroy_data = destroy_data;

  EGL_IMAGE_MEMORY_POOL_UNLOCK (pool);

  return mem;
}

G_DEFINE_BOXED_TYPE (EGLImageMemoryPool, egl_image_memory_pool,
    (GBoxedCopyFunc) egl_image_memory_pool_ref,
    (GBoxedFreeFunc) egl_image_memory_pool_unref);

/**
 * egl_display_new:
 * @display: a #EGLDisplayWrapper display
 * @user_data: user data passed to the callback
 * @destroy_data: #GDestroyNotify for user_data
 *
 * Create a new #EGLDisplayWrapper that wraps and refcount @display.
 *
 * Returns: a new #EGLDisplayWrapper
 *
 */
EGLDisplayWrapper *
egl_display_new (EGLDisplay display, gpointer user_data,
    GDestroyNotify destroy_data)
{
  EGLDisplayWrapper *gdisplay;

  gdisplay = g_slice_new (EGLDisplayWrapper);
  gdisplay->refcount = 1;
  gdisplay->display = display;
  gdisplay->user_data = user_data;
  gdisplay->destroy_data = destroy_data;

  return gdisplay;
}

/**
 * egl_display_ref
 * @display: a #EGLDisplayWrapper
 *
 * Increase the refcount of @display.
 *
 */
EGLDisplayWrapper *
egl_display_ref (EGLDisplayWrapper * display)
{
  g_return_val_if_fail (display != NULL, NULL);

  g_atomic_int_inc (&display->refcount);

  return display;
}

/**
 * egl_display_unref:
 * @display: a #EGLDisplayWrapper
 *
 * Decrease the refcount of @display and calls provided destroy function on
 * last reference.
 *
 */
void
egl_display_unref (EGLDisplayWrapper * display)
{
  g_return_if_fail (display != NULL);

  if (g_atomic_int_dec_and_test (&display->refcount)) {
    if (display->destroy_data) {
      display->destroy_data (display->user_data);
    }
    g_slice_free (EGLDisplayWrapper, display);
  }
}

/**
 * egl_display_get
 * @display: a #EGLDisplayWrapper
 *
 * Gives the #EGLDisplay wrapped.
 *
 */
EGLDisplay
egl_display_get (EGLDisplayWrapper * display)
{
  g_return_val_if_fail (display != NULL, EGL_NO_DISPLAY);

  return display->display;
}