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
|
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <dlfcn.h>
#include "va_egl_private.h"
#include "va_egl_impl.h"
// Returns the OpenGL ES VTable
static inline VAOpenGLESVTableP
gles_get_vtable(VADriverContextP ctx)
{
return &VA_DRIVER_CONTEXT_EGL(ctx)->gles_vtable;
}
// Lookup for a EGL function
typedef void (*EGLFuncPtr)(void);
typedef EGLFuncPtr (*EGLGetProcAddressProc)(const char *);
static EGLFuncPtr
get_proc_address_default(const char *name)
{
return NULL;
}
static EGLGetProcAddressProc
get_proc_address_func(void)
{
EGLGetProcAddressProc get_proc_func;
dlerror();
get_proc_func = (EGLGetProcAddressProc)
dlsym(RTLD_DEFAULT, "eglGetProcAddress");
if (!dlerror())
return get_proc_func;
return get_proc_address_default;
}
static inline EGLFuncPtr
get_proc_address(const char *name)
{
static EGLGetProcAddressProc get_proc_func = NULL;
get_proc_func = get_proc_address_func();
return get_proc_func(name);
}
// Check for GLES extensions (TFP, FBO)
static int
check_extension(const char *name, const char *exts)
{
const char *end;
int name_len, n;
if (!name || !exts)
return 0;
end = exts + strlen(exts);
name_len = strlen(name);
while (exts < end) {
n = strcspn(exts, " ");
if (n == name_len && strncmp(name, exts, n) == 0)
return 1;
exts += (n + 1);
}
return 0;
}
static int
check_extensions(VADriverContextP ctx, EGLDisplay egl_display)
{
const char *exts;
exts = (const char *)eglQueryString(egl_display, EGL_EXTENSIONS);
if (!check_extension("EGL_KHR_image_pixmap", exts))
return 0;
exts = (const char *)glGetString(GL_EXTENSIONS);
if (!check_extension("GL_OES_EGL_image", exts))
return 0;
#if 0
if (!check_extension("GL_OES_texture_npot", exts))
return 0;
#endif
return 1;
}
static int
init_extensions(VADriverContextP ctx)
{
VAOpenGLESVTableP pOpenGLESVTable = gles_get_vtable(ctx);
/* EGL_KHR_image_pixmap */
pOpenGLESVTable->egl_create_image_khr =
(PFNEGLCREATEIMAGEKHRPROC)get_proc_address("eglCreateImageKHR");
pOpenGLESVTable->egl_destroy_image_khr =
(PFNEGLDESTROYIMAGEKHRPROC)get_proc_address("eglDestroyImageKHR");
/* GL_OES_EGL_image */
pOpenGLESVTable->gles_egl_image_target_texture_2d =
(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)get_proc_address("glEGLImageTargetTexture2DOES");
return 1;
}
/* ========================================================================= */
/* === VA/EGL implementation from the driver (fordward calls) === */
/* ========================================================================= */
#ifdef INVOKE
#undef INVOKE
#endif
#define INVOKE(ctx, func, args) do { \
VADriverVTableEGLP vtable = (ctx)->vtable_egl; \
if (!vtable->va##func##EGL) \
return VA_STATUS_ERROR_UNIMPLEMENTED; \
\
VAStatus status = vtable->va##func##EGL args; \
if (status != VA_STATUS_SUCCESS) \
return status; \
} while (0)
static VAStatus
vaCreateSurfaceEGL_impl_driver(VADisplay dpy,
GLenum target,
GLuint texture,
unsigned int width,
unsigned int height,
VASurfaceEGL *egl_surface)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
INVOKE(ctx, CreateSurface, (ctx, target, texture, width, height, egl_surface));
return VA_STATUS_SUCCESS;
}
static VAStatus
vaDestroySurfaceEGL_impl_driver(VADisplay dpy, VASurfaceEGL egl_surface)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
INVOKE(ctx, DestroySurface, (ctx, egl_surface));
return VA_STATUS_SUCCESS;
}
static VAStatus
vaAssociateSurfaceEGL_impl_driver(VADisplay dpy,
VASurfaceEGL egl_surface,
VASurfaceID surface,
unsigned int flags)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
INVOKE(ctx, AssociateSurface, (ctx, egl_surface, surface, flags));
return VA_STATUS_SUCCESS;
}
static VAStatus
vaUpdateAssociatedSurfaceEGL_impl_driver(VADisplay dpy,
VASurfaceEGL egl_surface)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
INVOKE(ctx, UpdateAssociatedSurface, (ctx, egl_surface));
return VA_STATUS_SUCCESS;
}
static VAStatus
vaDeassociateSurfaceEGL_impl_driver(VADisplay dpy,
VASurfaceEGL egl_surface)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
INVOKE(ctx, DeassociateSurface, (ctx, egl_surface));
return VA_STATUS_SUCCESS;
}
#undef INVOKE
/* ========================================================================= */
/* === VA/EGL helpers === */
/* ========================================================================= */
/** Unique VASurfaceImplEGL identifier */
#define VA_SURFACE_IMPL_EGL_MAGIC VA_FOURCC('V','I','E','L')
struct VASurfaceImplEGL {
uint32_t magic; ///< Magic number identifying a VASurfaceImplEGL
VASurfaceID surface; ///< Associated VA surface
GLenum target; ///< GL target to which the texture is bound
GLuint texture; ///< GL texture
unsigned int width;
unsigned int height;
void *pixmap;
EGLImageKHR img_khr;
unsigned int flags;
};
static void *
create_native_pixmap(VADisplay dpy, unsigned int width, unsigned int height)
{
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
VAStatus status;
void *native_pixmap = NULL;
status = pDisplayContext->vaCreateNativePixmap(pDisplayContext, width, height, &native_pixmap);
if (status != VA_STATUS_SUCCESS)
native_pixmap = NULL;
return native_pixmap;
}
static void
destroy_native_pixmap(VADisplay dpy, void *native_pixmap)
{
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
pDisplayContext->vaFreeNativePixmap(pDisplayContext, native_pixmap);
}
// Destroy VA/EGL surface
static void
destroy_surface(VADisplay dpy, VASurfaceImplEGLP pSurfaceImplEGL)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
VADriverContextEGLP egl_ctx = VA_DRIVER_CONTEXT_EGL(ctx);
VAOpenGLESVTableP const pOpenGLESVTable = gles_get_vtable(ctx);
if (pSurfaceImplEGL->img_khr) {
pOpenGLESVTable->egl_destroy_image_khr(egl_ctx->egl_display,
pSurfaceImplEGL->img_khr);
pSurfaceImplEGL->img_khr = EGL_NO_IMAGE_KHR;
}
if (pSurfaceImplEGL->pixmap) {
destroy_native_pixmap(dpy, pSurfaceImplEGL->pixmap);
pSurfaceImplEGL->pixmap = 0;
}
free(pSurfaceImplEGL);
}
// Create VA/EGL surface
static VASurfaceImplEGLP
create_surface(VADisplay dpy,
GLenum target,
GLuint texture,
unsigned int width,
unsigned int height)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
VADriverContextEGLP egl_ctx = VA_DRIVER_CONTEXT_EGL(ctx);
VAOpenGLESVTableP const pOpenGLESVTable = gles_get_vtable(ctx);
VASurfaceImplEGLP pSurfaceImplEGL = NULL;
int is_error = 1;
const EGLint img_attribs[] = {
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
EGL_NONE
};
pSurfaceImplEGL = calloc(1, sizeof(*pSurfaceImplEGL));
if (!pSurfaceImplEGL)
goto end;
pSurfaceImplEGL->magic = VA_SURFACE_IMPL_EGL_MAGIC;
pSurfaceImplEGL->surface = VA_INVALID_SURFACE;
pSurfaceImplEGL->target = target;
pSurfaceImplEGL->texture = texture;
pSurfaceImplEGL->width = width;
pSurfaceImplEGL->height = height;
pSurfaceImplEGL->pixmap = create_native_pixmap(dpy, width, height);
if (!pSurfaceImplEGL->pixmap)
goto end;
pSurfaceImplEGL->img_khr = pOpenGLESVTable->egl_create_image_khr(egl_ctx->egl_display,
EGL_NO_CONTEXT,
EGL_NATIVE_PIXMAP_KHR,
(EGLClientBuffer)pSurfaceImplEGL->pixmap,
img_attribs);
if (!pSurfaceImplEGL->img_khr)
goto end;
is_error = 0;
end:
if (is_error && pSurfaceImplEGL) {
destroy_surface(dpy, pSurfaceImplEGL);
pSurfaceImplEGL = NULL;
}
return pSurfaceImplEGL;
}
// Check VASurfaceImplEGL is valid
static inline int check_surface(VASurfaceImplEGLP pSurfaceImplEGL)
{
return pSurfaceImplEGL && pSurfaceImplEGL->magic == VA_SURFACE_IMPL_EGL_MAGIC;
}
static inline VAStatus
deassociate_surface(VADriverContextP ctx, VASurfaceImplEGLP pSurfaceImplEGL)
{
pSurfaceImplEGL->surface = VA_INVALID_SURFACE;
return VA_STATUS_SUCCESS;
}
static VAStatus
associate_surface(VADriverContextP ctx,
VASurfaceImplEGLP pSurfaceImplEGL,
VASurfaceID surface,
unsigned int flags)
{
VAStatus status;
status = deassociate_surface(ctx, pSurfaceImplEGL);
if (status != VA_STATUS_SUCCESS)
return status;
pSurfaceImplEGL->surface = surface;
pSurfaceImplEGL->flags = flags;
return VA_STATUS_SUCCESS;
}
static inline VAStatus
sync_surface(VADriverContextP ctx, VASurfaceImplEGLP pSurfaceImplEGL)
{
if (pSurfaceImplEGL->surface == VA_INVALID_SURFACE)
return VA_STATUS_ERROR_INVALID_SURFACE;
return ctx->vtable->vaSyncSurface(ctx, pSurfaceImplEGL->surface);
}
static VAStatus
update_accociated_surface(VADriverContextP ctx, VASurfaceImplEGLP pSurfaceImplEGL)
{
VAOpenGLESVTableP pOpenGLESVTable = gles_get_vtable(ctx);
VAStatus status;
status = sync_surface(ctx, pSurfaceImplEGL);
if (status != VA_STATUS_SUCCESS)
return status;
status = ctx->vtable->vaPutSurface(
ctx,
pSurfaceImplEGL->surface,
(void *)pSurfaceImplEGL->pixmap,
0, 0, pSurfaceImplEGL->width, pSurfaceImplEGL->height,
0, 0, pSurfaceImplEGL->width, pSurfaceImplEGL->height,
NULL, 0,
pSurfaceImplEGL->flags
);
if (status == VA_STATUS_SUCCESS) {
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
pOpenGLESVTable->gles_egl_image_target_texture_2d(pSurfaceImplEGL->target,
(GLeglImageOES)pSurfaceImplEGL->img_khr);
}
return status;
}
/* ========================================================================= */
/* === VA/EGL implementation from libVA (generic and suboptimal path) === */
/* ========================================================================= */
#ifdef INIT_SURFACE
#undef INIT_SURFACE
#endif
#define INIT_SURFACE(surface, egl_surface) do { \
surface = (VASurfaceImplEGLP)(egl_surface); \
if (!check_surface(surface)) \
return VA_STATUS_ERROR_INVALID_SURFACE; \
} while (0)
static VAStatus
vaCreateSurfaceEGL_impl_libva(VADisplay dpy,
GLenum target,
GLuint texture,
unsigned int width,
unsigned int height,
VASurfaceEGL *egl_surface)
{
VASurfaceEGL pSurfaceEGL;
pSurfaceEGL = create_surface(dpy, target, texture, width, height);
if (!pSurfaceEGL)
return VA_STATUS_ERROR_ALLOCATION_FAILED;
*egl_surface = pSurfaceEGL;
return VA_STATUS_SUCCESS;
}
static VAStatus
vaDestroySurfaceEGL_impl_libva(VADisplay dpy, VASurfaceEGL egl_surface)
{
VASurfaceImplEGLP pSurfaceImplEGL;
INIT_SURFACE(pSurfaceImplEGL, egl_surface);
destroy_surface(dpy, pSurfaceImplEGL);
return VA_STATUS_SUCCESS;
}
static VAStatus
vaAssociateSurfaceEGL_impl_libva(
VADisplay dpy,
VASurfaceEGL egl_surface,
VASurfaceID surface,
unsigned int flags
)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
VASurfaceImplEGLP pSurfaceImplEGL;
VAStatus status;
INIT_SURFACE(pSurfaceImplEGL, egl_surface);
status = associate_surface(ctx, egl_surface, surface, flags);
return status;
}
static VAStatus
vaUpdateAssociatedSurfaceEGL_impl_libva(
VADisplay dpy,
VASurfaceEGL egl_surface
)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
VASurfaceImplEGLP pSurfaceImplEGL;
VAStatus status;
INIT_SURFACE(pSurfaceImplEGL, egl_surface);
status = update_accociated_surface(ctx, egl_surface);
return status;
}
static VAStatus
vaDeassociateSurfaceEGL_impl_libva(
VADisplay dpy,
VASurfaceEGL egl_surface
)
{
VADriverContextP ctx = ((VADisplayContextP)(dpy))->pDriverContext;
VASurfaceImplEGLP pSurfaceImplEGL;
VAStatus status;
INIT_SURFACE(pSurfaceImplEGL, egl_surface);
status = deassociate_surface(ctx, egl_surface);
return status;
}
#undef INIT_SURFACE
/* ========================================================================= */
/* === Private VA/EGL vtable initialization === */
/* ========================================================================= */
// Initialize EGL driver context
VAStatus va_egl_init_context(VADisplay dpy)
{
VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
VADriverContextP ctx = pDisplayContext->pDriverContext;
VADriverContextEGLP egl_ctx = VA_DRIVER_CONTEXT_EGL(ctx);
VADriverVTablePrivEGLP vtable = &egl_ctx->vtable;
if (egl_ctx->is_initialized)
return VA_STATUS_SUCCESS;
if (ctx->vtable_egl && ctx->vtable_egl->vaCreateSurfaceEGL) {
vtable->vaCreateSurfaceEGL = vaCreateSurfaceEGL_impl_driver;
vtable->vaDestroySurfaceEGL = vaDestroySurfaceEGL_impl_driver;
vtable->vaAssociateSurfaceEGL = vaAssociateSurfaceEGL_impl_driver;
vtable->vaUpdateAssociatedSurfaceEGL = vaUpdateAssociatedSurfaceEGL_impl_driver;
vtable->vaDeassociateSurfaceEGL = vaDeassociateSurfaceEGL_impl_driver;
}
else {
if (pDisplayContext->vaCreateNativePixmap == NULL ||
pDisplayContext->vaFreeNativePixmap == NULL)
return VA_STATUS_ERROR_UNIMPLEMENTED;
vtable->vaCreateSurfaceEGL = vaCreateSurfaceEGL_impl_libva;
vtable->vaDestroySurfaceEGL = vaDestroySurfaceEGL_impl_libva;
vtable->vaAssociateSurfaceEGL = vaAssociateSurfaceEGL_impl_libva;
vtable->vaUpdateAssociatedSurfaceEGL = vaUpdateAssociatedSurfaceEGL_impl_libva;
vtable->vaDeassociateSurfaceEGL = vaDeassociateSurfaceEGL_impl_libva;
if (!check_extensions(ctx, egl_ctx->egl_display) || !init_extensions(ctx))
return VA_STATUS_ERROR_UNIMPLEMENTED;
}
egl_ctx->is_initialized = 1;
return VA_STATUS_SUCCESS;
}
|