summaryrefslogtreecommitdiff
path: root/client.c
blob: be7368aad484e02d6428e35925c279353741213e (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
#define EGL_EGLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <error.h>
#include <X11/Xlib.h>
#include "fdpass.h"
#include "common.h"

#include <EGL/egl.h>
#include <EGL/eglext.h>

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

const char *vertex_src =
	"attribute vec4        position;		 \
   attribute vec2        texcoords;		 \
   varying vec2          tcoords;		 \
           					 \
   void main()					 \
   {						 \
      tcoords = texcoords;				 \
      gl_Position = position;            \
   }							 \
";


const char *fragment_src =
	"#extension GL_OES_EGL_image_external : require   \n                                                   \
   varying highp vec2    tcoords;					       \
   uniform samplerExternalOES samp;				       \
          						       \
   void  main()						       \
   {							       \
      gl_FragColor  = texture2D(samp, tcoords);		       \
   }							       \
";

struct x11_window {
	Window win;
	EGLSurface egl_surface;
};

struct window {
	struct display *display;
	int width, height;
	struct x11_window x11;
};

struct x11_display {
	Display *dpy;
	EGLDisplay egl_display;
	EGLConfig egl_conf;
	EGLContext egl_ctx;
};
struct display {
	struct x11_display x11;
};

struct texture {
	int rem_id;
	GLuint local_id;
	EGLImageKHR image;
	struct texture *next;
};

struct client {
	struct display *d;
	struct window *w;
	int sock_fd;
	EGLImageKHR image;
	GLuint tex_id;
	GLuint tex_loc;
	GLuint attr_pos, attr_tex;
	struct texture *texhead;
};

static void x11_window_create(struct window *w)
{
	Window root;
	EGLBoolean b;
	struct display *d = w->display;
	XSetWindowAttributes  swa;
	root = DefaultRootWindow(d->x11.dpy);

	memset(&swa, 0, sizeof(XSetWindowAttributes));
	w->x11.win = XCreateWindow(d->x11.dpy, root, 0, 0,
				   w->width, w->height, 0,
				   CopyFromParent, InputOutput,
				   CopyFromParent, CWEventMask,
				   &swa);

	XMapWindow(d->x11.dpy, w->x11.win);

	
	w->x11.egl_surface = eglCreateWindowSurface( d->x11.egl_display,
						     d->x11.egl_conf,
						     (EGLNativeWindowType)w->x11.win, NULL);

	if (!w->x11.egl_surface)
		error(1, errno, "failed to init egl surface");

	b = eglMakeCurrent(d->x11.egl_display,
			   w->x11.egl_surface,
			   w->x11.egl_surface,
			   d->x11.egl_ctx);
	if (!b)
		error(1, errno, "Cannot activate EGL context");
}

static void init_fns(void)
{
	
}

static struct window *window_create(struct display *d)
{

	struct window *w;

	w = calloc(1, sizeof(struct window));
	if (!w)
		error(1, errno, "can't allocate window");

	w->display = d;
	w->width = 250;
	w->height = 250;

	x11_window_create(w);
	return w;
}

static void x11_window_destroy(struct window *w)
{
	struct display *d = w->display;

	eglMakeCurrent(d->x11.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
		       EGL_NO_CONTEXT);
	eglDestroySurface(d->x11.egl_display, w->x11.egl_surface);
	XDestroyWindow(d->x11.dpy, w->x11.win);
}
static void window_destroy(struct window *window)
{
	x11_window_destroy(window);
	free(window);
}

static void x11_display_init(struct display *d)
{
	static const EGLint conf_att[] = {
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
		EGL_RED_SIZE, 1,
		EGL_GREEN_SIZE, 1,
		EGL_BLUE_SIZE, 1,
		EGL_ALPHA_SIZE, 0,
		EGL_NONE,
	};
	static const EGLint ctx_att[] = {
		EGL_CONTEXT_CLIENT_VERSION, 2,
		EGL_NONE
	};
	EGLBoolean b;
	EGLenum api;
	EGLint major, minor, n;
	d->x11.dpy = XOpenDisplay(NULL);
	if (!d->x11.dpy)
		error(1, errno, "Unable to open X server display");

	d->x11.egl_display = eglGetDisplay((EGLNativeDisplayType)d->x11.dpy);
	if (d->x11.egl_display == EGL_NO_DISPLAY)
		error(1, errno, "Failed to get EGL display");

	if (!eglInitialize(d->x11.egl_display, &major, &minor))
		error(1, errno, "Failed to init EGL display");
	fprintf(stderr, "EGL major/minor: %d.%d\n", major, minor);
	fprintf(stderr, "EGL version: %s\n",
		eglQueryString(d->x11.egl_display, EGL_VERSION));
	fprintf(stderr, "EGL vendor: %s\n",
		eglQueryString(d->x11.egl_display, EGL_VENDOR));
	fprintf(stderr, "EGL extensions: %s\n",
		eglQueryString(d->x11.egl_display, EGL_EXTENSIONS));

	api = EGL_OPENGL_ES_API;
	b = eglBindAPI(api);
	if (!b)
		error(1, errno, "cannot bind OpenGLES API");

	b = eglChooseConfig(d->x11.egl_display, conf_att, &d->x11.egl_conf,
			    1, &n);

	if (!b || n != 1)
		error(1, errno, "cannot find suitable EGL config");

	d->x11.egl_ctx = eglCreateContext(d->x11.egl_display,
					  d->x11.egl_conf,
					  EGL_NO_CONTEXT,
					  ctx_att);
	if (!d->x11.egl_ctx)
		error(1, errno, "cannot create EGL context");
	
	eglMakeCurrent(d->x11.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
		       EGL_NO_CONTEXT);
}

static struct display *display_create(void)
{
	struct display *d;

	d = calloc(1, sizeof(*d));
	if (!d)
		error(1, errno, "cannot allocate memory");

	x11_display_init(d);
	return d;
}

static void display_destroy(struct display *d)
{
	XCloseDisplay(d->x11.dpy);
	free(d);
}

static void
draw_rect_from_arrays(struct client *c, const void *verts, const void *tex)
{
	GLuint buf = 0;

	glGenBuffers(1, &buf);
	glBindBuffer(GL_ARRAY_BUFFER, buf);
	glBufferData(GL_ARRAY_BUFFER,
		     (sizeof(GLfloat) * 4 * 4) +
		     (sizeof(GLfloat) * 4 * 2),
		     NULL,
		     GL_STATIC_DRAW);

	if (verts) {
		glBufferSubData(GL_ARRAY_BUFFER,
				0,
				sizeof(GLfloat) * 4 * 4,
				verts);
		glVertexAttribPointer(c->attr_pos, 4, GL_FLOAT,
				      GL_FALSE, 0, 0);
		glEnableVertexAttribArray(c->attr_pos);
	}
	if (tex) {
		glBufferSubData(GL_ARRAY_BUFFER,
				sizeof(GLfloat) * 4 * 4,
				sizeof(GLfloat) * 4 * 2,
				tex);
		glVertexAttribPointer(c->attr_tex, 2, GL_FLOAT,
				      GL_FALSE, 0,
				      (void *)(sizeof(GLfloat) * 4 * 4));
		glEnableVertexAttribArray(c->attr_tex);
	}
			
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	if (verts)
		glDisableVertexAttribArray(c->attr_pos);
	if (tex)
		glDisableVertexAttribArray(c->attr_tex);

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glDeleteBuffers(1, &buf);
}

static GLvoid
client_draw_rect_tex(struct client *c,
		     float x, float y, float w, float h,
                     float tx, float ty, float tw, float th)
{
        float verts[4][4];
        float tex[4][2];

        verts[0][0] = x;
        verts[0][1] = y;
        verts[0][2] = 0.0;
        verts[0][3] = 1.0;
        tex[0][0] = tx;
        tex[0][1] = ty;
        verts[1][0] = x + w;
        verts[1][1] = y;
        verts[1][2] = 0.0;
        verts[1][3] = 1.0;
        tex[1][0] = tx + tw;
        tex[1][1] = ty;
        verts[2][0] = x;
        verts[2][1] = y + h;
        verts[2][2] = 0.0;
        verts[2][3] = 1.0;
        tex[2][0] = tx;
        tex[2][1] = ty + th;
        verts[3][0] = x + w;
        verts[3][1] = y + h;
        verts[3][2] = 0.0;
        verts[3][3] = 1.0;
        tex[3][0] = tx + tw;
        tex[3][1] = ty + th;

        draw_rect_from_arrays(c, verts, tex);
}

static void draw_screen(struct client *c)
{
	client_draw_rect_tex(c, -1, -1, 2, 2,
			     0, 0, 1, 1);
}

static void init_shaders(struct client *c)
{
	GLuint fs, vs, prog;
	GLint stat;

	fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, (const char **)&fragment_src, NULL);
	glCompileShader(fs);
	glGetShaderiv(fs, GL_COMPILE_STATUS, &stat);
	if (!stat)
		error(1, errno, "Failed to compile FS");

	vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, (const char **)&vertex_src, NULL);
	glCompileShader(vs);
	glGetShaderiv(vs, GL_COMPILE_STATUS, &stat);
	if (!stat)
		error(1, errno, "failed to compile VS");

	prog = glCreateProgram();
	glAttachShader(prog, fs);
	glAttachShader(prog, vs);
	glLinkProgram(prog);

	glGetProgramiv(prog, GL_LINK_STATUS, &stat);
	if (!stat) {
		char log[1000];
		GLsizei len;
		glGetProgramInfoLog(prog, 1000, &len, log);
		printf("Error linking: %s\n", log);
		exit(1);
	}

	glUseProgram(prog);

	c->attr_pos = glGetAttribLocation(prog, "position");
	c->attr_tex = glGetAttribLocation(prog, "texcoords");
	c->tex_loc = glGetUniformLocation(prog, "samp");
}

void add_texture(struct client *client, struct cmd_buf *cbuf, int fd)
{
	struct texture *texture;
	EGLint attrs[13];

	texture = calloc(1, sizeof(*texture));
	if (!texture)
		error(1, errno, "failed to create texture storage\n");
	
	texture->rem_id = cbuf->u.buf.id;
			
	attrs[0] = EGL_DMA_BUF_PLANE0_FD_EXT;
	attrs[1] = fd;
	attrs[2] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
	attrs[3] = cbuf->u.buf.stride;
	attrs[4] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
	attrs[5] = 0;
	attrs[6] = EGL_WIDTH;
	attrs[7] = cbuf->u.buf.width;
	attrs[8] = EGL_HEIGHT;
	attrs[9] = cbuf->u.buf.height;
	attrs[10] = EGL_LINUX_DRM_FOURCC_EXT;
	attrs[11] = cbuf->u.buf.format;
	attrs[12] = EGL_NONE;
	texture->image = eglCreateImageKHR(client->d->x11.egl_display,
					  EGL_NO_CONTEXT,
					  EGL_LINUX_DMA_BUF_EXT,
					  (EGLClientBuffer)NULL,
					  attrs);

	if (!texture->image)
		error(1, errno, "failed to import image dma-buf");

	glGenTextures(1, &texture->local_id);
	glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture->local_id);
	//glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	//glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

//	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0,
//		     GL_RGBA, GL_UNSIGNED_BYTE, NULL);
	glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)texture->image);

	if (!client->texhead)
		client->texhead = texture;
	else {
		struct texture *iter = client->texhead;
		while (iter->next)
			iter = iter->next;
		iter->next = texture;
	}
}

struct client *client_create(int sock_fd)
{
	struct client *client;
	ssize_t size;
	client = calloc(1, sizeof(struct client));
	if (!client)
		error(1, errno, "cannot allocate memory");

	client->d = display_create();
	client->w = window_create(client->d);
	client->sock_fd = sock_fd;

	init_shaders(client);

	glViewport(0, 0, client->w->width, client->w->height);
	glClearColor(0.0, 1.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	eglSwapBuffers(client->d->x11.egl_display, client->w->x11.egl_surface);

	glUniform1i(client->tex_loc, 0);
	sleep(1);
	for (;;) {

		struct cmd_buf buf;
		int myfd;
		size = sock_fd_read(client->sock_fd, &buf, sizeof(buf), &myfd);
		if (size <= 0)
			break;

		if (buf.type == CMD_TYPE_BUF) {
			add_texture(client, &buf, myfd);
		} else if (buf.type == CMD_TYPE_DIRT) {
			draw_screen(client);

			eglSwapBuffers(client->d->x11.egl_display, client->w->x11.egl_surface);
		}


	}
	return client;
}

void client_destroy(struct client *client)
{
	window_destroy(client->w);
	display_destroy(client->d);
	close(client->sock_fd);
	free(client);

}