summaryrefslogtreecommitdiff
path: root/test/egl/va_egl_x11.c
blob: 7d7521d3c9b0ad770a492294d79534aefdb183d9 (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
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <va/va_x11.h>
#include <va/va_egl.h>

struct va_egl_context
{
    Display *x11_dpy;
    Window win;

    EGLDisplay egl_dpy;
    EGLContext egl_ctx;
    EGLSurface egl_surf;
    unsigned int egl_target;
    EGLClientBuffer egl_buffer;
    EGLImageKHR egl_image;
    PFNEGLCREATEIMAGEKHRPROC egl_create_image_khr;
    PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image_hkr;
    PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glegl_image_target_texture2d_oes;

    VADisplay va_dpy;
    VASurfaceID va_surface;
    VASurfaceEGL va_egl_surface;

    int x, y;
    unsigned int width, height;
    GLuint texture;
    GLfloat ar;
    unsigned int box_width;
    unsigned char ydata;
};

static void
va_egl_fini_egl(struct va_egl_context *ctx)
{
    eglMakeCurrent(ctx->egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglTerminate(ctx->egl_dpy);
}

static int
va_egl_init_egl(struct va_egl_context *ctx)
{
    EGLint egl_major, egl_minor;
    const char *s;

    ctx->egl_dpy = eglGetDisplay(ctx->x11_dpy);

    if (!ctx->egl_dpy) {
        printf("Error: eglGetDisplay() failed\n");
        return -1;
    }

    if (!eglInitialize(ctx->egl_dpy, &egl_major, &egl_minor)) {
        printf("Error: eglInitialize() failed\n");
        return -1;
    }

    s = eglQueryString(ctx->egl_dpy, EGL_VERSION);
    printf("EGL_VERSION = %s\n", s);

    return 0;
}

static int 
yuvgen_planar(int width, int height,
              unsigned char *Y_start, int Y_pitch,
              unsigned char *U_start, int U_pitch,
              unsigned char *V_start, int V_pitch,
              int UV_interleave, int box_width, unsigned char ydata)
{
    int row;

    /* copy Y plane */
    for (row = 0; row < height; row++) {
        unsigned char *Y_row = Y_start + row * Y_pitch;
        int jj, xpos, ypos;

        ypos = (row / box_width) & 0x1;

        for (jj = 0; jj < width; jj++) {
            xpos = ((jj) / box_width) & 0x1;
                        
            if ((xpos == 0) && (ypos == 0))
                Y_row[jj] = ydata;
            if ((xpos == 1) && (ypos == 1))
                Y_row[jj] = ydata;
                        
            if ((xpos == 1) && (ypos == 0))
                Y_row[jj] = 0xff - ydata;
            if ((xpos == 0) && (ypos == 1))
                Y_row[jj] = 0xff - ydata;
        }
    }
  
    /* copy UV data */
    for( row = 0; row < height/2; row++) {
        unsigned short value = 0x80;

        if (UV_interleave) {
            unsigned short *UV_row = (unsigned short *)(U_start + row * U_pitch);

            memset(UV_row, value, width);
        } else {
            unsigned char *U_row = U_start + row * U_pitch;
            unsigned char *V_row = V_start + row * V_pitch;
            
            memset(U_row, value, width / 2);
            memset(V_row, value, width / 2);
        }
    }

    return 0;
}

static int 
va_egl_upload_surface(struct va_egl_context *ctx)
{
    VAImage surface_image;
    void *surface_p = NULL, *U_start, *V_start;
    
    vaDeriveImage(ctx->va_dpy, ctx->va_surface, &surface_image);

    vaMapBuffer(ctx->va_dpy, surface_image.buf, &surface_p);
        
    U_start = (char *)surface_p + surface_image.offsets[1];
    V_start = (char *)surface_p + surface_image.offsets[2];

    /* assume surface is planar format */
    yuvgen_planar(surface_image.width, surface_image.height,
                  (unsigned char *)surface_p, surface_image.pitches[0],
                  (unsigned char *)U_start, surface_image.pitches[1],
                  (unsigned char *)V_start, surface_image.pitches[2],
                  (surface_image.format.fourcc==VA_FOURCC_NV12),
                  ctx->box_width, ctx->ydata);
        
    vaUnmapBuffer(ctx->va_dpy,surface_image.buf);

    vaDestroyImage(ctx->va_dpy,surface_image.image_id);

    return 0;
}

static void
va_egl_fini_va(struct va_egl_context *ctx)
{
    vaDestroySurfaces(ctx->va_dpy, &ctx->va_surface, 1);    
    vaTerminate(ctx->va_dpy);
}

static int
va_egl_init_va(struct va_egl_context *ctx)
{
    VAStatus va_status;
    int major_ver, minor_ver;

    ctx->va_dpy = vaGetDisplayEGL(ctx->x11_dpy, ctx->egl_dpy);

    if (!ctx->va_dpy) {
        printf("Error: vaGetDisplayEGL() failed\n");
        return -1;
    }

    va_status = vaInitialize(ctx->va_dpy, &major_ver, &minor_ver);

    if (va_status != VA_STATUS_SUCCESS) {
        printf("Error: vaInitialize() failed\n");
        return -1;
    }

    va_status = vaCreateSurfaces(ctx->va_dpy,
                                 ctx->width, ctx->height,
                                 VA_RT_FORMAT_YUV420, 
                                 1, &ctx->va_surface);

    if (va_status != VA_STATUS_SUCCESS) {
        printf("Error: vaCreateSurfaces() failed\n");
        return -1;
    }

    va_egl_upload_surface(ctx);

    return 0;
}

static void
va_egl_make_window(struct va_egl_context *ctx, const char *title)
{
    int scrnum;
    XSetWindowAttributes attr;
    unsigned long mask;
    Window root;
    XVisualInfo *visInfo, visTemplate;
    int num_visuals;
    EGLConfig config;
    EGLint num_configs, vid;
    const EGLint attribs[] = {
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
        EGL_NONE
    };

    scrnum = DefaultScreen(ctx->x11_dpy);
    root = RootWindow(ctx->x11_dpy, scrnum);

    if (!eglChooseConfig(ctx->egl_dpy, attribs, &config, 1, &num_configs) ||
        !num_configs) {
        printf("Error: couldn't get an EGL visual config\n");

        return;
    }

    if (!eglGetConfigAttrib(ctx->egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
        printf("Error: eglGetConfigAttrib() failed\n");

        return;
    }

    /* The X window visual must match the EGL config */
    visTemplate.visualid = vid;
    visInfo = XGetVisualInfo(ctx->x11_dpy, VisualIDMask, &visTemplate, &num_visuals);

    if (!visInfo) {
        printf("Error: couldn't get X visual\n");

        return;
    }

    /* window attributes */
    attr.background_pixel = 0;
    attr.border_pixel = 0;
    attr.colormap = XCreateColormap(ctx->x11_dpy, root, visInfo->visual, AllocNone);
    attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
    attr.override_redirect = 0;
    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;

    ctx->win = XCreateWindow(ctx->x11_dpy,
                             root,
                             ctx->x, ctx->y,
                             ctx->width, ctx->height,
                             0, visInfo->depth, InputOutput,
                             visInfo->visual, mask, &attr);

    /* set hints and properties */
    {
        XSizeHints sizehints;
        sizehints.x = ctx->x;
        sizehints.y = ctx->y;
        sizehints.width  = ctx->width;
        sizehints.height = ctx->height;
        sizehints.flags = USSize | USPosition;
        XSetNormalHints(ctx->x11_dpy, ctx->win, &sizehints);
        XSetStandardProperties(ctx->x11_dpy, ctx->win, title, title,
                               None, (char **)NULL, 0, &sizehints);
    }

    eglBindAPI(EGL_OPENGL_ES_API);

    ctx->egl_ctx = eglCreateContext(ctx->egl_dpy, config, EGL_NO_CONTEXT, NULL);

    if (!ctx->egl_ctx) {
        printf("Error: eglCreateContext() failed\n");
        
        return;
    }

    ctx->egl_surf = eglCreateWindowSurface(ctx->egl_dpy, config, ctx->win, NULL);
    eglMakeCurrent(ctx->egl_dpy, ctx->egl_surf, ctx->egl_surf, ctx->egl_ctx);
    XFree(visInfo);
}

static int
va_egl_init_extension(struct va_egl_context *ctx)
{
   const char *exts;

   exts = eglQueryString(ctx->egl_dpy, EGL_EXTENSIONS);
   ctx->egl_create_image_khr =
       (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
   ctx->egl_destroy_image_hkr =
       (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");

   if (!exts ||
       !strstr(exts, "EGL_KHR_image_base") ||
       !ctx->egl_create_image_khr ||
       !ctx->egl_destroy_image_hkr) {
       printf("EGL does not support EGL_KHR_image_base\n");
       return -1;
   }

   exts = (const char *)glGetString(GL_EXTENSIONS);
   ctx->glegl_image_target_texture2d_oes =
       (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");

   if (!exts ||
       !strstr(exts, "GL_OES_EGL_image") ||
       !ctx->glegl_image_target_texture2d_oes) {
       printf("OpenGL ES does not support GL_OES_EGL_image\n");
       return -1;
   }

   return 0;
}

static void
va_egl_fini_gles(struct va_egl_context *ctx)
{
    glDeleteTextures(1, &ctx->texture);
}

static int
va_egl_init_gles(struct va_egl_context *ctx)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor4f(1.0, 1.0, 1.0, 1.0);

    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);

    glGenTextures(1, &ctx->texture);
    glBindTexture(GL_TEXTURE_2D, ctx->texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glEnable(GL_TEXTURE_2D);

    return 0;
}

static void
va_egl_fini_va_egl(struct va_egl_context *ctx)
{
    if (ctx->egl_image)
        ctx->egl_destroy_image_hkr(ctx->egl_dpy, ctx->egl_image);

    vaDeassociateSurfaceEGL(ctx->va_dpy, ctx->va_egl_surface);
    vaDestroySurfaceEGL(ctx->va_dpy, ctx->va_egl_surface);
}

static int
va_egl_init_va_egl(struct va_egl_context *ctx)
{
    VAStatus va_status;
    int num_max_targets = 0, num_targets = 0;
    int num_max_attributes = 0, num_attribs = 0;
    unsigned int *target_list = NULL;
    EGLint *img_attribs = NULL;

    num_targets = num_max_targets = vaMaxNumSurfaceTargetsEGL(ctx->va_dpy);
    
    if (num_max_targets < 1) {
        printf("Error: vaMaxNumSurfaceTargetsEGL() returns %d\n", num_max_targets);
        return -1;
    }

    num_attribs = num_max_attributes = vaMaxNumSurfaceAttributesEGL(ctx->va_dpy);

    if (num_max_attributes < 1) {
        printf("Error: vaMaxNumSurfaceAttributesEGL() returns %d\n", num_max_attributes);
        return -1;
    }

    target_list = malloc(num_max_targets * sizeof(unsigned int));
    va_status = vaQuerySurfaceTargetsEGL(ctx->va_dpy,
                                         target_list,
                                         &num_targets);
    
    if (va_status != VA_STATUS_SUCCESS || num_targets < 1) {
        printf("Error: vaQuerySurfaceTargetsEGL() failed\n");
        return -1;
    }
    
    va_status = vaCreateSurfaceEGL(ctx->va_dpy,
                                   target_list[0],
                                   ctx->width, ctx->height,
                                   &ctx->va_egl_surface);

    if (va_status != VA_STATUS_SUCCESS) {
        printf("Error: vaCreateSurfaceEGL() failed\n");
        return -1;
    }

    va_status = vaAssociateSurfaceEGL(ctx->va_dpy,
                                      ctx->va_egl_surface,
                                      ctx->va_surface,
                                      0);
 
    if (va_status != VA_STATUS_SUCCESS) {
        printf("Error: vaAssociateSurfaceEGL() failed\n");
        return -1;
    }

    img_attribs = malloc(2 * num_max_attributes * sizeof(EGLint));
    va_status = vaGetSurfaceInfoEGL(ctx->va_dpy,
                                    ctx->va_egl_surface,
                                    &ctx->egl_target,
                                    &ctx->egl_buffer,
                                    img_attribs,
                                    &num_attribs);

    if (va_status != VA_STATUS_SUCCESS) {
        printf("Error: vaGetSurfaceInfoEGL() failed\n");
        return -1;
    }

    ctx->egl_image = ctx->egl_create_image_khr(ctx->egl_dpy,
                                               EGL_NO_CONTEXT,
                                               ctx->egl_target,
                                               ctx->egl_buffer,
                                               img_attribs);

    vaSyncSurfaceEGL(ctx->va_dpy, ctx->va_egl_surface);
    ctx->glegl_image_target_texture2d_oes(GL_TEXTURE_2D,
                                          (GLeglImageOES)ctx->egl_image);

    return 0;
}

static void
va_egl_fini(struct va_egl_context *ctx)
{
    va_egl_fini_gles(ctx);
    va_egl_fini_va(ctx);
    va_egl_fini_egl(ctx);
    va_egl_fini_gles(ctx);
    va_egl_fini_va_egl(ctx);

    // XDestroyWindow(ctx->x11_dpy, ctx->win);
    XCloseDisplay(ctx->x11_dpy);
}

static int
va_egl_init(struct va_egl_context *ctx, int argc, char **argv)
{
    memset(ctx, 0, sizeof(*ctx));
    ctx->x11_dpy = XOpenDisplay(NULL);
    ctx->width = 320;
    ctx->height = 320;
    ctx->ar = 1.0;
    ctx->box_width = 16;
    ctx->ydata = 0xff;

    if (!ctx->x11_dpy) {
        printf("Error: couldn't open display %s\n", getenv("DISPLAY"));
        return -1;
    }

    if (va_egl_init_egl(ctx) != 0)
        return -1;

    if (va_egl_init_va(ctx) != 0)
        return -1;

    va_egl_make_window(ctx, "VA/EGL");
    va_egl_init_extension(ctx);
    va_egl_init_gles(ctx);
    va_egl_init_va_egl(ctx);

    return 0;
}

static void
va_egl_reshape(struct va_egl_context *ctx, int width, int height)
{
    GLfloat ar = (GLfloat) width / (GLfloat) height;

    ctx->width = width;
    ctx->height = height;
    ctx->ar = ar;

    glViewport(0, 0, (GLint) width, (GLint) height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(-ar, ar, -ar, ar, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

static void
va_egl_draw(struct va_egl_context *ctx)
{
    const GLfloat verts[][3] = {
        { -ctx->ar, -ctx->ar, 0 },
        {  ctx->ar, -ctx->ar, 0 },
        {  ctx->ar,  ctx->ar, 0 },
        { -ctx->ar,  ctx->ar, 0 }
    };
    const GLfloat texs[][2] = {
        { 0, 0 },
        { 1, 0 },
        { 1, 1 },
        { 0, 1 }
    };

    glClear(GL_COLOR_BUFFER_BIT);

    glVertexPointer(3, GL_FLOAT, 0, verts);
    glTexCoordPointer(2, GL_FLOAT, 0, texs);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

static void
va_egl_event_loop(struct va_egl_context *ctx)
{
    while (1) {
        int redraw = 0;

        if (XPending(ctx->x11_dpy) > 0) {
            XEvent event;
            XNextEvent(ctx->x11_dpy, &event);

            switch (event.type) {
            case Expose:
                redraw = 1;
                break;

            case ConfigureNotify:
                va_egl_reshape(ctx, event.xconfigure.width, event.xconfigure.height);
                redraw = 1;
                break;

            case KeyPress:
            {
                char buffer[10];
                int code;
                code = XLookupKeysym(&event.xkey, 0);

                if (code == XK_y) {
                    ctx->ydata += 0x10;
                    va_egl_upload_surface(ctx);
                    vaSyncSurfaceEGL(ctx->va_dpy, ctx->va_egl_surface);
                    ctx->glegl_image_target_texture2d_oes(GL_TEXTURE_2D,
                                                          (GLeglImageOES)ctx->egl_image);
                    redraw = 1;
                } else {
                    XLookupString(&event.xkey, buffer, sizeof(buffer),
                                  NULL, NULL);
                    
                    if (buffer[0] == 27) {
                        /* escape */
                        return;
                    }
                }
            }
                
            break;

            default:
                ; /*no-op*/
            }
        }

        if (redraw) {
            va_egl_draw(ctx);
            eglSwapBuffers(ctx->egl_dpy, ctx->egl_surf);
        }
    }
}

static void
va_egl_run(struct va_egl_context *ctx)
{
    XMapWindow(ctx->x11_dpy, ctx->win);
    va_egl_reshape(ctx, ctx->width, ctx->height);
    va_egl_event_loop(ctx);
}

int
main(int argc, char *argv[])
{
    struct va_egl_context ctx;

    printf("Usage: press 'y' to change Y plane \n\n");

    if (va_egl_init(&ctx, argc, argv) == 0) {
        va_egl_run(&ctx);
        va_egl_fini(&ctx);
    }

    return 0;
}