summaryrefslogtreecommitdiff
path: root/src/i965_output_wayland.c
blob: 5a75397c9dfc847a60d068391a4700c9b54b7ee5 (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
/*
 * Copyright (C) 2012 Intel Corporation. All Rights Reserved.
 *
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS 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.
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <va/va_backend.h>
#include <va/va_backend_wayland.h>
#include <wayland-client.h>
#include <wayland-drm-client-protocol.h>
#include "intel_driver.h"
#include "i965_output_wayland.h"
#include "i965_drv_video.h"
#include "i965_defines.h"
#include "dso_utils.h"

#define LIBEGL_NAME             "libEGL.so.1"
#define LIBWAYLAND_CLIENT_NAME  "libwayland-client.so.0"

typedef uint32_t (*wl_display_get_global_func)(struct wl_display *display,
    const char *interface, uint32_t version);
typedef void (*wl_display_roundtrip_func)(struct wl_display *display);

typedef struct wl_proxy *(*wl_proxy_create_func)(struct wl_proxy *factory,
    const struct wl_interface *interface);
typedef void (*wl_proxy_destroy_func)(struct wl_proxy *proxy);
typedef void (*wl_proxy_marshal_func)(struct wl_proxy *p, uint32_t opcode, ...);
typedef int (*wl_proxy_add_listener_func) (struct wl_proxy *proxy,
    void (**implementation)(void), void *data);

struct wl_vtable {
    const struct wl_interface  *buffer_interface;
    const struct wl_interface  *drm_interface;
    const struct wl_interface  *registry_interface;
    wl_display_roundtrip_func   display_roundtrip;
    wl_proxy_create_func        proxy_create;
    wl_proxy_destroy_func       proxy_destroy;
    wl_proxy_marshal_func       proxy_marshal;
    wl_proxy_add_listener_func  proxy_add_listener;
};

struct va_wl_output {
    struct dso_handle  *libegl_handle;
    struct dso_handle  *libwl_client_handle;
    struct wl_vtable    vtable;
    struct wl_drm      *wl_drm;
    struct wl_registry *wl_registry;
};

/* These function are copied and adapted from the version inside
 * wayland-client-protocol.h
 */
static void *
registry_bind(
    struct wl_vtable          *wl_vtable,
    struct wl_registry        *wl_registry,
    uint32_t                   name,
    const struct wl_interface *interface,
    uint32_t                   version
)
{
    struct wl_proxy *id;

    id = wl_vtable->proxy_create((struct wl_proxy *) wl_registry,
                                 interface);
    if (!id)
      return NULL;

    wl_vtable->proxy_marshal((struct wl_proxy *) wl_registry,
                             WL_REGISTRY_BIND, name, interface->name,
                             version, id);

    return (void *) id;
}

static struct wl_registry *
display_get_registry(
    struct wl_vtable  *wl_vtable,
    struct wl_display *wl_display
)
{
    struct wl_proxy *callback;

    callback = wl_vtable->proxy_create((struct wl_proxy *) wl_display,
                                       wl_vtable->registry_interface);
    if (!callback)
      return NULL;

    wl_vtable->proxy_marshal((struct wl_proxy *) wl_display,
                             WL_DISPLAY_GET_REGISTRY, callback);

    return (struct wl_registry *) callback;
}

static int
registry_add_listener(
    struct wl_vtable                  *wl_vtable,
    struct wl_registry                *wl_registry,
    const struct wl_registry_listener *listener,
    void                              *data
)
{
    return wl_vtable->proxy_add_listener((struct wl_proxy *) wl_registry,
                                         (void (**)(void)) listener, data);
}

static void
registry_handle_global(
    void               *data,
    struct wl_registry *registry,
    uint32_t            id,
    const char         *interface,
    uint32_t            version
)
{
    VADriverContextP ctx = data;
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    struct va_wl_output * const wl_output = i965->wl_output;
    struct wl_vtable * const wl_vtable = &wl_output->vtable;

    if (strcmp(interface, "wl_drm") == 0) {
        wl_output->wl_drm = registry_bind(wl_vtable, wl_output->wl_registry,
                                          id, wl_vtable->drm_interface, 1);
    }
}

static const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    NULL
};

/* Ensure wl_drm instance is created */
static bool
ensure_wl_output(VADriverContextP ctx)
{
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    struct va_wl_output * const wl_output = i965->wl_output;
    struct wl_vtable * const wl_vtable = &wl_output->vtable;

    if (wl_output->wl_drm)
        return true;

    wl_output->wl_registry = display_get_registry(wl_vtable, ctx->native_dpy);
    registry_add_listener(wl_vtable, wl_output->wl_registry,
                          &registry_listener, ctx);
    wl_vtable->display_roundtrip(ctx->native_dpy);
    if (!wl_output->wl_drm)
        return false;
    return true;
}

/* Create planar YUV buffer */
static struct wl_buffer *
create_planar_buffer(
    struct va_wl_output *wl_output,
    uint32_t             name,
    int32_t              width,
    int32_t              height,
    uint32_t             format,
    int32_t              offsets[3],
    int32_t              pitches[3]
)
{
    struct wl_vtable * const wl_vtable = &wl_output->vtable;
    struct wl_proxy *id;

    id = wl_vtable->proxy_create(
        (struct wl_proxy *)wl_output->wl_drm,
        wl_vtable->buffer_interface
    );
    if (!id)
        return NULL;

    wl_vtable->proxy_marshal(
        (struct wl_proxy *)wl_output->wl_drm,
        WL_DRM_CREATE_PLANAR_BUFFER,
        id,
        name,
        width, height, format,
        offsets[0], pitches[0],
        offsets[1], pitches[1],
        offsets[2], pitches[2]
    );
    return (struct wl_buffer *)id;
}

/* Hook to return Wayland buffer associated with the VA surface */
static VAStatus
va_GetSurfaceBufferWl(
    struct VADriverContext *ctx,
    VASurfaceID             surface,
    unsigned int            flags,
    struct wl_buffer      **out_buffer
)
{
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    struct object_surface *obj_surface;
    struct wl_buffer *buffer;
    uint32_t name, drm_format;
    int offsets[3], pitches[3];

    obj_surface = SURFACE(surface);
    if (!obj_surface)
        return VA_STATUS_ERROR_INVALID_SURFACE;

    if (flags != VA_FRAME_PICTURE)
        return VA_STATUS_ERROR_FLAG_NOT_SUPPORTED;

    if (!out_buffer)
        return VA_STATUS_ERROR_INVALID_PARAMETER;

    if (!ensure_wl_output(ctx))
        return VA_STATUS_ERROR_INVALID_DISPLAY;

    if (drm_intel_bo_flink(obj_surface->bo, &name) != 0)
        return VA_STATUS_ERROR_INVALID_SURFACE;

    switch (obj_surface->fourcc) {
    case VA_FOURCC_NV12:
        drm_format = WL_DRM_FORMAT_NV12;
        offsets[0] = 0;
        pitches[0] = obj_surface->width;
        offsets[1] = obj_surface->width * obj_surface->y_cb_offset;
        pitches[1] = obj_surface->cb_cr_pitch;
        offsets[2] = 0;
        pitches[2] = 0;
        break;
    case VA_FOURCC_YV12:
    case VA_FOURCC_I420:
    case VA_FOURCC_IMC1:
    case VA_FOURCC_IMC3:
    case VA_FOURCC_422H:
    case VA_FOURCC_422V:
    case VA_FOURCC_411P:
    case VA_FOURCC_444P:
        switch (obj_surface->subsampling) {
        case SUBSAMPLE_YUV411:
            drm_format = WL_DRM_FORMAT_YUV411;
            break;
        case SUBSAMPLE_YUV420:
            drm_format = WL_DRM_FORMAT_YUV420;
            break;
        case SUBSAMPLE_YUV422H:
        case SUBSAMPLE_YUV422V:
            drm_format = WL_DRM_FORMAT_YUV422;
            break;
        case SUBSAMPLE_YUV444:
            drm_format = WL_DRM_FORMAT_YUV444;
            break;
        default:
            assert(0 && "unsupported subsampling");
            return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
        }
        offsets[0] = 0;
        pitches[0] = obj_surface->width;
        offsets[1] = obj_surface->width * obj_surface->y_cb_offset;
        pitches[1] = obj_surface->cb_cr_pitch;
        offsets[2] = obj_surface->width * obj_surface->y_cr_offset;
        pitches[2] = obj_surface->cb_cr_pitch;
        break;
    default:
        assert(0 && "unsupported format");
        return VA_STATUS_ERROR_INVALID_IMAGE_FORMAT;
    }

    buffer = create_planar_buffer(
        i965->wl_output,
        name,
        obj_surface->orig_width,
        obj_surface->orig_height,
        drm_format,
        offsets,
        pitches
    );
    if (!buffer)
        return VA_STATUS_ERROR_ALLOCATION_FAILED;

    *out_buffer = buffer;
    return VA_STATUS_SUCCESS;
}

/* Hook to return Wayland buffer associated with the VA image */
static VAStatus
va_GetImageBufferWl(
    struct VADriverContext *ctx,
    VAImageID               image,
    unsigned int            flags,
    struct wl_buffer      **out_buffer
)
{
    return VA_STATUS_ERROR_UNIMPLEMENTED;
}

bool
ensure_driver_vtable(VADriverContextP ctx)
{
    struct VADriverVTableWayland * const vtable = ctx->vtable_wayland;

    if (!vtable)
        return false;

    vtable->vaGetSurfaceBufferWl = va_GetSurfaceBufferWl;
    vtable->vaGetImageBufferWl   = va_GetImageBufferWl;
    return true;
}

bool
i965_output_wayland_init(VADriverContextP ctx)
{
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    struct dso_handle *dso_handle;
    struct wl_vtable *wl_vtable;

    static const struct dso_symbol libegl_symbols[] = {
        { "wl_drm_interface",
          offsetof(struct wl_vtable, drm_interface) },
        { NULL, }
    };

    static const struct dso_symbol libwl_client_symbols[] = {
        { "wl_buffer_interface",
          offsetof(struct wl_vtable, buffer_interface) },
        { "wl_registry_interface",
          offsetof(struct wl_vtable, registry_interface) },
        { "wl_display_roundtrip",
          offsetof(struct wl_vtable, display_roundtrip) },
        { "wl_proxy_create",
          offsetof(struct wl_vtable, proxy_create) },
        { "wl_proxy_destroy",
          offsetof(struct wl_vtable, proxy_destroy) },
        { "wl_proxy_marshal",
          offsetof(struct wl_vtable, proxy_marshal) },
        { "wl_proxy_add_listener",
          offsetof(struct wl_vtable, proxy_add_listener) },
        { NULL, }
    };

    if (ctx->display_type != VA_DISPLAY_WAYLAND)
        return false;

    i965->wl_output = calloc(1, sizeof(struct va_wl_output));
    if (!i965->wl_output)
        goto error;

    i965->wl_output->libegl_handle = dso_open(LIBEGL_NAME);
    if (!i965->wl_output->libegl_handle)
        goto error;

    dso_handle = i965->wl_output->libegl_handle;
    wl_vtable  = &i965->wl_output->vtable;
    if (!dso_get_symbols(dso_handle, wl_vtable, sizeof(*wl_vtable),
                         libegl_symbols))
        goto error;

    i965->wl_output->libwl_client_handle = dso_open(LIBWAYLAND_CLIENT_NAME);
    if (!i965->wl_output->libwl_client_handle)
        goto error;

    dso_handle = i965->wl_output->libwl_client_handle;
    wl_vtable  = &i965->wl_output->vtable;
    if (!dso_get_symbols(dso_handle, wl_vtable, sizeof(*wl_vtable),
                         libwl_client_symbols))
        goto error;

    if (!ensure_driver_vtable(ctx))
        goto error;
    return true;

error:
    i965_output_wayland_terminate(ctx);
    return false;
}

void
i965_output_wayland_terminate(VADriverContextP ctx)
{
    struct i965_driver_data * const i965 = i965_driver_data(ctx);
    struct va_wl_output *wl_output;

    if (ctx->display_type != VA_DISPLAY_WAYLAND)
        return;

    wl_output = i965->wl_output;
    if (!wl_output)
        return;

    if (wl_output->wl_drm) {
        wl_output->vtable.proxy_destroy((struct wl_proxy *)wl_output->wl_drm);
        wl_output->wl_drm = NULL;
    }

    if (wl_output->libegl_handle) {
        dso_close(wl_output->libegl_handle);
        wl_output->libegl_handle = NULL;
    }

    if (wl_output->libwl_client_handle) {
        dso_close(wl_output->libwl_client_handle);
        wl_output->libwl_client_handle = NULL;
    }
    free(wl_output);
    i965->wl_output = NULL;
}