summaryrefslogtreecommitdiff
path: root/va/wayland/va_wayland.c
blob: 73f80662c2cc3c3416fd1214cec9cae54cfd10eb (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
/*
 * Copyright (c) 2011 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 "va.h"
#include "va_backend.h"
#include "va_wayland.h"
#include "egl/va_backend_egl.h"
#include "va_drmcommon.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <wayland-client.h>
#include <wayland-util.h>
#include "wayland-drm-client-protocol.h"
#include "wayland-egl-priv.h"

#include <intel_bufmgr.h>

struct wldrm_ctx {
	struct VADriverContext base;

	struct wl_drm *drm;
	int authenticated;

	drm_intel_bufmgr *bufmgr;
};

struct wldrm_buffer {
	union drm_buffer base;
	drm_intel_bo *bo;
	struct wl_buffer *buf;

};

enum wldrm_buffer_type {
	WLDRM_BUFFER_FRONT,
	WLDRM_BUFFER_BACK,
	WLDRM_BUFFER_COUNT
};

#define DRAWABLE_HASH_SZ 32
struct wldrm_drawable {
	struct drm_drawable base;
	int is_window;
	struct wl_egl_window *win;

	int block_swap_buffers;

	struct wldrm_buffer *buffer[WLDRM_BUFFER_COUNT];

	struct wldrm_drawable *next;
};

struct wldrm_state {
	struct drm_state base;
	struct wldrm_drawable *drawable_hash[DRAWABLE_HASH_SZ];
};

enum {
	__DRI_BUFFER_FRONT_LEFT = 0,
	__DRI_BUFFER_BACK_LEFT,
	__DRI_BUFFER_FRONT_RIGHT,
	__DRI_BUFFER_BACK_RIGHT,
	__DRI_BUFFER_DEPTH,
	__DRI_BUFFER_STENCIL,
	__DRI_BUFFER_ACCUM,
	__DRI_BUFFER_FAKE_FRONT_LEFT, 
	__DRI_BUFFER_FAKE_FRONT_RIGHT
};

/* Cast helpers */

static inline struct wldrm_ctx *
wldrm_ctx(struct VADriverContext *driver_ctx)
{
	return (struct wldrm_ctx *) driver_ctx;
}

static inline struct wldrm_drawable *
wldrm_drawable(struct drm_drawable *drawable)
{
	return (struct wldrm_drawable *) drawable;
}

static inline struct VADriverContext *
va_driver_ctx(struct VADisplayContext *display_ctx)
{
	return display_ctx->pDriverContext;
}

/* Wayland utility */

static void
sync_callback(void *data)
{
	int *done = data;

	*done = 1;
}

static void
force_roundtrip(struct wl_display *display)
{
	int done = 0;

	wl_display_sync_callback(display, sync_callback, &done);
	wl_display_iterate(display, WL_DISPLAY_WRITABLE);
	while (!done)
		wl_display_iterate(display, WL_DISPLAY_READABLE);
}

static inline uint32_t
surface_get_id(struct wl_surface *surface)
{
	struct wl_object *object = (struct wl_object *) surface;

	return object->id;
}

static inline struct wl_drm *
wl_drm_create_without_bind(struct wl_display *display, uint32_t id)
{
	return (struct wl_drm *)
		wl_proxy_create_for_id(display, &wl_drm_interface, id);
}

static int
wayland_drawable_is_window(void *draw)
{
	struct wl_egl_window *win = draw;
	struct wl_object *obj = (struct wl_object *) win->surface;
	
	/* wl_egl_windows first element is a wl_surface */
	return (obj->interface == &wl_surface_interface);
}

static inline int
align(int value, int alignment)
{
	return (value + alignment - 1) & ~(alignment - 1);
}

static drm_intel_bo *
bo_alloc(struct wldrm_ctx *wctx,
	 int width, int height, int cpp,
	 int *name, int *pitch)
{
	drm_intel_bo *bo;
	int size;

	*pitch = align(width, 64) * cpp;
	size = *pitch * align(height, 4);

	bo = drm_intel_bo_alloc_for_render(wctx->bufmgr,
					   "render buffer", size, 0);
	if (drm_intel_bo_flink(bo, name))
		fprintf(stderr, "flink failed for bo: %p\n", bo);

	return bo;
}

static union drm_buffer *
wldrm_get_render_buffer(struct VADriverContext *ctx,
			struct drm_drawable *drawable)
{
	struct wldrm_ctx *wctx = wldrm_ctx(ctx);
	struct wldrm_drawable *wdrawable = wldrm_drawable(drawable);
	struct wl_egl_window *win = wdrawable->win;
	struct wldrm_buffer *wbuffer;

	if (!wdrawable->buffer[WLDRM_BUFFER_BACK]) {
		wbuffer = calloc(1, sizeof *wbuffer);
		if (!wbuffer)
			return NULL;

		wbuffer->bo = bo_alloc(wctx,
				       win->width, win->height, 4,
				       &wbuffer->base.dri2.name,
				       &wbuffer->base.dri2.pitch);

		wbuffer->base.dri2.attachment = __DRI_BUFFER_BACK_LEFT;
		wbuffer->base.dri2.cpp = 4;
		wbuffer->base.dri2.flags = 0;

		wdrawable->buffer[WLDRM_BUFFER_BACK] = wbuffer;
	}

	return &wdrawable->buffer[WLDRM_BUFFER_BACK]->base;
}

static void
wldrm_frame_callback(struct wl_surface *surface, void *data, uint32_t time)
{
	struct wldrm_drawable *wdrawable = data;

	wdrawable->block_swap_buffers = 0;
}

static void
wldrm_swap_buffer(struct VADriverContext *ctx,
		  struct drm_drawable *drawable)
{
	struct wldrm_ctx *wctx = wldrm_ctx(ctx);
	struct wldrm_drawable *wdrawable = wldrm_drawable(drawable);
	struct wl_display *display = wctx->base.native_dpy;
	struct wl_egl_window *win = wdrawable->win;
	struct wldrm_buffer *wbuf;

	if (!wdrawable->is_window)
		return;

	while (wdrawable->block_swap_buffers)
		wl_display_iterate(display, WL_DISPLAY_READABLE);

	wdrawable->block_swap_buffers = 1;
	wl_display_frame_callback(display, win->surface,
				  wldrm_frame_callback, wdrawable);

	wbuf = wdrawable->buffer[WLDRM_BUFFER_BACK];
 	wdrawable->buffer[WLDRM_BUFFER_BACK] =
		wdrawable->buffer[WLDRM_BUFFER_FRONT];
	wdrawable->buffer[WLDRM_BUFFER_FRONT] = wbuf;
	assert(wbuf);

	if (!wbuf->buf) {
		if (!wctx->drm) {
			int id = wl_display_get_global(display, "wl_drm", 1);
			assert(id != 0);
			wctx->drm = wl_drm_create_without_bind(display, id);
		}
		wbuf->buf =
			wl_drm_create_buffer(wctx->drm, wbuf->base.dri2.name,
					     win->width, win->height,
					     wbuf->base.dri2.pitch, win->visual);
		wl_drm_destroy(wctx->drm);
		wctx->drm = NULL;
	}

	wl_buffer_damage(wbuf->buf, 0, 0, win->width, win->height);
	wl_surface_attach(win->surface, wbuf->buf, 0, 0);
	wl_surface_damage(win->surface, 0, 0, win->width, win->height);

	wl_display_flush(display);
}

static struct drm_drawable *
wldrm_get_drawable(struct VADriverContext *ctx, void *native_drawable)
{
	struct wldrm_state *drm_state = ctx->drm_state;
	struct wldrm_drawable *wdrawable;

	if (wayland_drawable_is_window(native_drawable)) {
		struct wl_egl_window *win = native_drawable;
		int index = surface_get_id(win->surface) % DRAWABLE_HASH_SZ;

		for (wdrawable = drm_state->drawable_hash[index];
		     wdrawable != NULL; wdrawable = wdrawable->next) {
			if (wdrawable->win == win)
				return &wdrawable->base;
		}

		wdrawable = calloc(1, sizeof *wdrawable);
		if (!wdrawable)
			return NULL;

		wdrawable->next = drm_state->drawable_hash[index];
		drm_state->drawable_hash[index] = wdrawable;

		wdrawable->is_window = 1;
		wdrawable->base.x = 0;
		wdrawable->base.y = 0;
		wdrawable->base.width = win->width;
		wdrawable->base.height = win->height;

		wdrawable->win = win;
		wdrawable->buffer[0] = wdrawable->buffer[1] = NULL;
		wdrawable->block_swap_buffers = 0;
	} else {
		struct wl_egl_pixmap *pixmap = native_drawable;

		wdrawable = calloc(1, sizeof *wdrawable);
		if (!wdrawable)
			return NULL;

		wdrawable->is_window = 0;
		wdrawable->base.x = 0;
		wdrawable->base.y = 0;
		wdrawable->base.width = pixmap->width;
		wdrawable->base.height = pixmap->height;

		wdrawable->buffer[WLDRM_BUFFER_FRONT] = NULL;
		/* XXX: ugly and unsafe, since this implies the
		 * egl driver stores a pointer to a DRIbuffer in the first
		 * element of the (for us) unknown driver_private structure
		 */
		wdrawable->buffer[WLDRM_BUFFER_BACK] = (struct wldrm_buffer *)
			*((union dri_buffer **) pixmap->driver_private);
		wdrawable->block_swap_buffers = 0;
	}

	return &wdrawable->base;
}

static void
drm_handle_device(void *data, struct wl_drm *drm, const char *device)
{
	struct wldrm_ctx *wctx = data;
	struct drm_state *drm_state = wctx->base.drm_state;
	drm_magic_t magic;
	struct stat st;

	if (stat(device, &st) == -1) {
		fprintf(stderr, "Cannot identify '%s': %d, %s\n",
			device, errno, strerror(errno));
		return;
	}

	if (!S_ISCHR(st.st_mode)) {
		fprintf(stderr, "%s is no device\n", device);
		return;
	}

	drm_state->fd = open(device, O_RDWR);
	if (drm_state->fd == -1) {
		fprintf(stderr, "Cannot open '%s': %d, %s\n",
			device, errno, strerror(errno));
		return;
	}

	drmGetMagic(drm_state->fd, &magic);
	wl_drm_authenticate(wctx->drm, magic);
}

static void
drm_handle_authenticated(void *data, struct wl_drm *drm)
{
	struct wldrm_ctx *wctx = data;

	wctx->authenticated = 1;
}

static const struct wl_drm_listener drm_listener = {
	drm_handle_device,
	drm_handle_authenticated
};

static VAStatus
wldrm_display_ctx_get_driver_name(struct VADisplayContext *display_ctx,
				  char **driver_name)
{
	struct wldrm_ctx *wctx = wldrm_ctx(va_driver_ctx(display_ctx));
	struct drm_state *drm_state = wctx->base.drm_state;
	struct wl_display *display = wctx->base.native_dpy;
	uint32_t id;

	memset(drm_state, 0, sizeof(*drm_state));
	id = wl_display_get_global(display, "wl_drm", 1);
	if (id == 0)
		force_roundtrip(display);
	id = wl_display_get_global(display, "wl_drm", 1);
	if (id == 0)
		goto error_unknown;
	wctx->drm = wl_drm_create(display, id, 1);
	if (!wctx->drm)
		goto error_unknown;
	wl_drm_add_listener(wctx->drm, &drm_listener, wctx);
	force_roundtrip(display);
	if (drm_state->fd == -1) {
		fprintf(stderr, "can't open DRM devices\n");
		goto cleanup_drm;
	}

	force_roundtrip(display);
	if (!wctx->authenticated)
		goto cleanup_fd;

	wctx->bufmgr = intel_bufmgr_gem_init(dup(drm_state->fd), 0x80000);
	//intel_bufmgr_gem_enable_reuse(wctx->bufmgr);

	*driver_name = strdup("i965");
	drm_state->driConnectedFlag = VA_DRI2;

	drm_state->getRenderingBuffer = wldrm_get_render_buffer;
	drm_state->swapBuffer = wldrm_swap_buffer;
	drm_state->getDrawable = wldrm_get_drawable;

	wl_drm_destroy(wctx->drm);
	wctx->drm = NULL;

	return VA_STATUS_SUCCESS;

cleanup_fd:
	close(drm_state->fd);
	drm_state->fd = -1;
cleanup_drm:
	wl_drm_destroy(wctx->drm);
error_unknown:
	return VA_STATUS_ERROR_UNKNOWN;
}

static int
wldrm_display_ctx_is_valid(struct VADisplayContext *display_ctx)
{
	return (display_ctx != NULL && va_driver_ctx(display_ctx) != NULL);
}

static void 
wldrm_display_ctx_destroy(struct VADisplayContext *display_ctx)
{
	struct wldrm_ctx *wctx;
	struct drm_state *drm_state;

	if (!wldrm_display_ctx_is_valid(display_ctx))
		return;

	wctx = wldrm_ctx(va_driver_ctx(display_ctx));
	drm_state = wctx->base.drm_state;

	drm_intel_bufmgr_destroy(wctx->bufmgr);
	close(drm_state->fd);

	free(drm_state);
	free(wctx);
	free(display_ctx);
}

VADisplay
vaGetDisplay(struct wl_display *native_dpy)
{
	VADisplay dpy = NULL;
	struct VADisplayContext *display_ctx;

	if (!native_dpy)
		return NULL;

	if (!dpy) {
		/* create new entry */
		struct wldrm_ctx *wctx;
		struct wldrm_state *drm_state;

		display_ctx = calloc(1, sizeof *display_ctx);
		wctx        = calloc(1, sizeof *wctx);
		drm_state   = calloc(1, sizeof *drm_state);

		if (display_ctx && wctx && drm_state) {
			display_ctx->vadpy_magic = VA_DISPLAY_MAGIC;          
			display_ctx->pDriverContext = &wctx->base;
			display_ctx->vaGetDriverName =
				wldrm_display_ctx_get_driver_name;
			display_ctx->vaIsValid  = wldrm_display_ctx_is_valid;
			display_ctx->vaDestroy = wldrm_display_ctx_destroy;

			wctx->base.native_dpy = native_dpy;
			wctx->base.drm_state  = drm_state;

			dpy = (VADisplay) display_ctx;
		} else {
			if (display_ctx)
				free(display_ctx);
			if (wctx)
				free(wctx);
			if (drm_state)
				free(drm_state);
		}
	}

	return dpy;
}

#define CTX(dpy) (va_driver_ctx((struct VADisplayContext *)dpy))
#define CHECK_DISPLAY(dpy) \
	if( !wldrm_display_ctx_is_valid(dpy) ) { \
	return VA_STATUS_ERROR_INVALID_DISPLAY; }

extern int fool_postp; /* do nothing for vaPutSurface if set */
extern int trace_flag; /* trace vaPutSurface parameters */

void 
va_TracePutSurface(VADisplay dpy,
		   VASurfaceID surface,
		   void *draw,
		   short srcx, short srcy,
		   unsigned short srcw, unsigned short srch,
		   short destx, short desty,
		   unsigned short destw, unsigned short desth,
		   VARectangle *cliprects,
		   unsigned int number_cliprects,
		   unsigned int flags);

#define VA_TRACE(trace_func,...)		\
	if (trace_flag) {			\
		trace_func(__VA_ARGS__);	\
	}

VAStatus
vaPutSurface(VADisplay dpy,
	     VASurfaceID surface,
	     void *draw,
	     short srcx, short srcy,
	     unsigned short srcw, unsigned short srch,
	     short destx, short desty,
	     unsigned short destw, unsigned short desth,
	     VARectangle *cliprects, /* client supplied clip list */
	     /* number of clip rects in the clip list */
	     unsigned int number_cliprects,
	     unsigned int flags /* de-interlacing flags */)
{
	struct VADriverContext *ctx;

	if (fool_postp)
		return VA_STATUS_SUCCESS;

	if (draw == NULL)
		return VA_STATUS_ERROR_UNKNOWN;

	CHECK_DISPLAY(dpy);
	ctx = CTX(dpy);

	VA_TRACE(va_TracePutSurface, dpy, surface, draw,
		 srcx, srcy, srcw, srch,
		 destx, desty, destw, desth,
		 cliprects, number_cliprects, flags);

	return ctx->vtable->vaPutSurface(ctx, surface, draw,
					 srcx, srcy, srcw, srch,
					 destx, desty, destw, desth,
					 cliprects, number_cliprects, flags);
}

VAStatus
vaAttachSurface(VADisplay dpy, VASurfaceID surface,
		struct wl_egl_window *window, struct wl_visual *visual)
{
	struct VADriverContext *ctx;
	struct wldrm_ctx *wctx;
	struct wl_display *display;
	struct wldrm_state *drm_state;
	struct wl_buffer *buf;
	VAStatus status;
	VAImage image;
	uint32_t name, pitch;

	if (fool_postp)
		return VA_STATUS_SUCCESS;

	if (window == NULL)
		return VA_STATUS_ERROR_UNKNOWN;

	CHECK_DISPLAY(dpy);
	ctx = CTX(dpy);
	wctx = wldrm_ctx(ctx);
	display = wctx->base.native_dpy;
	drm_state = ctx->drm_state;

	status = ctx->vtable->vaDeriveImage(ctx, surface, &image);
	if (status != VA_STATUS_SUCCESS)
		return status;

	status = drm_state->base.image_export(ctx, image.image_id,
					      &name, &pitch);
	if (status != VA_STATUS_SUCCESS) {
		/* destroy image */
		return status;
	}

	if (!wctx->drm) {
		int id = wl_display_get_global(display, "wl_drm", 1);
		assert(id != 0);
		wctx->drm = wl_drm_create_without_bind(display, id);
	}
	buf = wl_drm_create_buffer(wctx->drm, name,
				   image.width, image.height,
				   pitch, visual);
	wl_drm_destroy(wctx->drm);
	wctx->drm = NULL;

	wl_buffer_damage(buf, 0, 0, image.width, image.height);
	wl_surface_attach(window->surface, buf, 0, 0);
	wl_surface_damage(window->surface, 0, 0, image.width, image.height);

	/* wl_buffer_destroy(buf); */

	/* destroy image in sync callback */
	
	return VA_STATUS_SUCCESS;
}