summaryrefslogtreecommitdiff
path: root/va-attach.c
blob: 2b74f981c263ec3fe53cd5243b813a02e492cf2b (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
/*
 * Copyright © 2011 Benjamin Franzke
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

/*
 * compile using:
 * gcc -o va-attach va-attach.c -I /path/to/libva/test \
 * `pkg-config --libs --cflags wayland-{client,egl} libva{,-wayland}`
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>

#include <wayland-client.h>
#include <wayland-egl.h>

#include <va/va.h>
#include <va/va_wayland.h>

#define CHECK_VASTATUS(va_status,func)                                  \
if (va_status != VA_STATUS_SUCCESS) {                                   \
    fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \
    exit(1);                                                            \
}
#include "loadsurface.h"

struct display {
	struct wl_display *display;
	struct wl_visual *premultiplied_argb_visual;
	struct wl_visual *yv12_visual;
	struct wl_compositor *compositor;
	struct wl_shell *shell;
	struct {
		VADisplay dpy;
	} va;
	uint32_t mask;
};

struct window {
	struct display *display;
	struct {
		int width, height;
	} geometry;
	struct {
#define SURFACE_NUM 16
		VASurfaceID surface_id[SURFACE_NUM];
		int width, height;
		int index;
	} va;

	struct wl_egl_window *native;
	struct wl_surface *surface;
};

static int
init_va(struct display *display)
{
	int major, minor; 

	display->va.dpy = vaGetDisplay(display->display);
	assert(display->va.dpy);

	vaInitialize(display->va.dpy, &major, &minor);
}

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

	*done = 1;
}

static void
create_surface(struct window *window)
{
	struct display *display = window->display;
	struct wl_visual *visual;
	int done = 0;
	
	if (!display->premultiplied_argb_visual) {
		wl_display_sync_callback(display->display, sync_callback, &done);
		while (!done)
			wl_display_iterate(display->display, display->mask);
		if (!display->premultiplied_argb_visual) {
			fprintf(stderr, "premultiplied argb visual missing\n");
			exit(1);
		}
	}

	window->surface = wl_compositor_create_surface(display->compositor);
	visual = display->premultiplied_argb_visual;
	window->native =
		wl_egl_window_create(window->surface,
				     window->geometry.width,
				     window->geometry.height,
				     visual);

	wl_shell_set_toplevel(display->shell, window->surface);
}


static VASurfaceID
get_next_free_surface(struct window *window)
{
	struct display *display = window->display;
	int i;

	i = window->va.index;
	i++;
	if (i == SURFACE_NUM)
		i = 0;
	window->va.index = i;

	return window->va.surface_id[i];
}

static void
redraw_va_surf(struct window *window, uint32_t time)
{
	struct display *display = window->display;
	VAStatus vaStatus;
	VASurfaceID surface_id = VA_INVALID_SURFACE;

	while (surface_id == VA_INVALID_SURFACE)
		surface_id = get_next_free_surface(window);

	vaStatus = vaAttachSurface(display->va.dpy, surface_id, window->native,
				   display->yv12_visual);
	CHECK_VASTATUS(vaStatus, "vaAttachSurface");
	usleep(300000);
}

static void
redraw(struct wl_surface *surface, void *data, uint32_t time)
{
	struct window *window = data;

	redraw_va_surf(window, time);

	wl_display_frame_callback(window->display->display,
				  window->surface,
				  redraw, window);
}

static void
compositor_handle_visual(void *data,
			 struct wl_compositor *compositor,
			 uint32_t id, uint32_t token)
{
	struct display *d = data;

	switch (token) {
	case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32:
		d->premultiplied_argb_visual =
			wl_visual_create(d->display, id, 1);
		break;
	case WL_COMPOSITOR_VISUAL_YV12:
		d->yv12_visual = wl_visual_create(d->display, id, 1);
		break;
	}
}

static const struct wl_compositor_listener compositor_listener = {
	compositor_handle_visual,
};

static void
display_handle_global(struct wl_display *display, uint32_t id,
		      const char *interface, uint32_t version, void *data)
{
	struct display *d = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		d->compositor = wl_compositor_create(display, id, 1);
		wl_compositor_add_listener(d->compositor,
					   &compositor_listener, d);
	} else if (strcmp(interface, "wl_shell") == 0) {
		d->shell = wl_shell_create(display, id, 1);
	}
}

static int
event_mask_update(uint32_t mask, void *data)
{
	struct display *d = data;

	d->mask = mask;

	return 0;
}

static void
upload_source_YUV_once_for_all(struct window *window)
{
	int box_width_loc = 8, row_shift_loc = 0;
	int i;

	for (i = 0; i < SURFACE_NUM; i++) {
		upload_surface(window->display->va.dpy,
			       window->va.surface_id[i],
			       box_width_loc, row_shift_loc, 0);

		row_shift_loc++;
		if (row_shift_loc == (2*box_width_loc))
			row_shift_loc= 0;
	}
}

static int
create_va_surfaces(struct window *window)
{
	struct display *d = window->display;
	VAStatus status;

	window->va.width = 352;
	window->va.height = 288;
	status = vaCreateSurfaces(d->va.dpy,
				  window->va.width, window->va.height,
				  VA_RT_FORMAT_YUV420,
				  SURFACE_NUM, window->va.surface_id);
	upload_source_YUV_once_for_all(window);
}

int
main(int argc, char **argv)
{
	struct display display = { 0 };
	struct window  window  = { 0 };

	memset(&display, 0, sizeof display);
	memset(&window,  0, sizeof window);

	window.display = &display;
	window.geometry.width  = 352;
	window.geometry.height = 288;

	display.display = wl_display_connect(NULL);
	assert(display.display);

	wl_display_add_global_listener(display.display,
				       display_handle_global, &display);

	wl_display_get_fd(display.display, event_mask_update, &display);

	init_va(&display);

	create_surface(&window);
	create_va_surfaces(&window);

	redraw(window.surface, &window, 1);

	while (true)
		wl_display_iterate(display.display, display.mask);

	return 0;
}