summaryrefslogtreecommitdiff
path: root/intel.c
blob: 20fb5039e5ba2875708027b691a0f099f56f4d70 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <i915_drm.h>
#include <xf86drmMode.h>
#include <linux/fb.h>
#include <GL/gl.h> /* dri_interface.h uses some GL integer types... */
#include "eagle-internal.h"

#define INTEL_STRIDE_ALIGNMENT 64

typedef struct EGLDisplayNative *EGLDisplayNative;
struct EGLDisplayNative {
	struct EGLDisplay		 base;

#ifdef __DRI_COPY_BUFFER
	__DRIbuffer			 front;
	__DRIcopyBufferExtension	*copyBuffer;
#endif

	unsigned int	 stride;
	unsigned int	 width;
	unsigned int	 height;
	void		*fb;
};

typedef struct EGLSurfaceNative *EGLSurfaceNative;
struct EGLSurfaceNative {
	struct EGLSurface	base;
	uint32_t		handles[10];
	int			x, y, width, height;
	uint32_t		name, stride;
};

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

static void
nativeGetBuffers(EGLSurface surface, unsigned int *attachments, int count)
{
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;
	struct drm_i915_gem_create create;
	struct drm_gem_flink flink;
	struct drm_gem_open open_arg;
	__DRIbuffer *buffer;
	uint32_t size;
	int fd, i, ret;

	if (nativeSurface->width == surface->width &&
	    nativeSurface->height == surface->height)
		return;

	for (i = 0; i < count; i++) {
		if (attachments[i] == __DRI_BUFFER_DEPTH)
			surface->depth = i;

		buffer = &surface->buffers[i];
		buffer->attachment = attachments[i];
		buffer->pitch = align_to(nativeSurface->width * 4,
					 INTEL_STRIDE_ALIGNMENT);
		buffer->cpp = 4;

		if (buffer->attachment == __DRI_BUFFER_FRONT_LEFT &&
		    nativeSurface->name != ~0) {
			buffer->name = nativeSurface->name;
			buffer->pitch = nativeSurface->stride;
			open_arg.name = buffer->name;
			ret = ioctl(surface->display->fd,
				    DRM_IOCTL_GEM_OPEN, &open_arg);
			nativeSurface->handles[i] = open_arg.handle;
			continue;
		}

		if (attachments[i] == __DRI_BUFFER_STENCIL) {
			buffer->name = surface->buffers[surface->depth].name;
			nativeSurface->handles[i] =
				nativeSurface->handles[surface->depth];
			continue;
		}

		size = buffer->pitch * nativeSurface->height;
		fd = surface->display->fd;
		create.size = size;
		if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create)) {
			fprintf(stderr, "failed to create buffer\n");
			return;
		}
		nativeSurface->handles[i] = create.handle;

		flink.handle = create.handle;
		if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) < 0) {
			fprintf(stderr, "failed to create buffer\n");
			return;
		}

		buffer->name = flink.name;
	}

	surface->width = nativeSurface->width;
	surface->height = nativeSurface->height;
	surface->count = count;
}

static EGLBoolean
nativeDestroySurface(EGLDisplay display, EGLSurface surface)
{
	free(surface);

	return EGL_TRUE;
}

#ifdef __DRI_COPY_BUFFER

static EGLBoolean
nativeDRISwapBuffers(EGLDisplay display, EGLSurface surface)
{
	EGLDisplayNative nativeDisplay = (EGLDisplayNative) display;
	EGLContext context;

	context = eglGetCurrentContext();

	return nativeDisplay->copyBuffer->copyBuffer(context->driContext,
						     surface->driDrawable,
						     __DRI_BUFFER_FRONT_LEFT,
						     &nativeDisplay->front);
}

static const struct EagleBackend nativeDRIBackend = {
	nativeGetBuffers,
	nativeDRISwapBuffers,
	nativeDestroySurface,
};

static void
nativeInitDRICopyBuffer(EGLDisplay display)
{
	EGLDisplayNative nativeDisplay = (EGLDisplayNative) display;
	const __DRIextension **extensions;
	drmModeResPtr mode_res;
	drmModeCrtcPtr crtc;
	drmModeFBPtr fb;
	struct drm_gem_flink flink;
	struct driPrivate *private;
	int i;

	extensions = display->core->getExtensions(display->driScreen);
	if (extensions == NULL)
		return;

	for (i = 0; extensions[i]; i++) {
		if (strcmp(extensions[i]->name, __DRI_COPY_BUFFER) == 0 &&
		    extensions[i]->version >= __DRI_COPY_BUFFER_VERSION) {
			nativeDisplay->copyBuffer =
				(__DRIcopyBufferExtension *) extensions[i];
		}
	}

	if (nativeDisplay->copyBuffer == NULL)
		goto fail;

	mode_res = drmModeGetResources(display->fd);
	if (mode_res == NULL) {
		fprintf(stderr, "drmModeGetResources returns NULL\n");
		goto fail;
	}

	crtc = drmModeGetCrtc(display->fd, mode_res->crtcs[1]);
	if (crtc == NULL) {
		fprintf(stderr, "drmModeGetCrtc returns NULL\n");
		goto fail;
	}

	fb = drmModeGetFB(display->fd, crtc->buffer_id);
	if (fb == NULL) {
		fprintf(stderr, "drmModeGetFB returns NULL\n");
		goto fail;
	}

	flink.handle = fb->handle;
	if (ioctl(display->fd, DRM_IOCTL_GEM_FLINK, &flink) < 0) {
		fprintf(stderr, "failed to create buffer\n");
		goto fail;
	}

	nativeDisplay->front.attachment = __DRI_BUFFER_FRONT_LEFT;
	nativeDisplay->front.name = flink.name;
	nativeDisplay->front.cpp = fb->bpp / 8;
	nativeDisplay->front.pitch = fb->pitch;
	nativeDisplay->front.flags = 0;

	display->width = fb->width;
	display->height = fb->height;
	display->backend = &nativeDRIBackend;

	printf("Using DRI CopyBuffer swapbuffer\n");

	return;

 fail:
	free(private);
}

#endif

static EGLBoolean
nativeMemcpySwapBuffers(EGLDisplay display, EGLSurface surface)
{
	EGLDisplayNative nativeDisplay = (EGLDisplayNative) display;
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;
	struct drm_i915_gem_pread pread;
	char *data, *dst;
	int i, stride, x, y;

	stride = surface->buffers[0].pitch;

	pread.handle = nativeSurface->handles[0];
	pread.pad = 0;
	pread.offset = 0;
	pread.size = stride * nativeSurface->height;

	data = malloc(pread.size);
	if (data == NULL) {
		fprintf(stderr, "swap buffers malloc failed\n");
		return EGL_FALSE;
	}

	pread.data_ptr = (long) data;

	if (ioctl(display->fd, DRM_IOCTL_I915_GEM_PREAD, &pread)) {
		fprintf(stderr, "gem pread failed\n");
		return EGL_FALSE;
	}

	x = nativeSurface->x;
	y = nativeSurface->y;
	dst = nativeDisplay->fb + nativeDisplay->stride * y + x * 4;
	for (i = 0; i < nativeSurface->height; i++)
		memcpy(dst + nativeDisplay->stride * i,
		       data + stride * i, nativeSurface->width * 4);

	free(data);

	return EGL_TRUE;
}

static const struct EagleBackend intelMemcpyBackend = {
	nativeGetBuffers,
	nativeMemcpySwapBuffers,
	nativeDestroySurface,
};

static const char fb_device[] = "/dev/fb";

static void
nativeInitMemcpy(EGLDisplay display)
{
	EGLDisplayNative nativeDisplay = (EGLDisplayNative) display;
	struct fb_fix_screeninfo fix;
	struct fb_var_screeninfo var;
	int fd;

	fd = open(fb_device, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "failed to open %s\n", fb_device);
		return;
	}

	if (ioctl(fd, FBIOGET_FSCREENINFO, &fix) < 0) {
		fprintf(stderr, "fb get fixed failed\n");
		return;
	}

	if (ioctl(fd, FBIOGET_VSCREENINFO, &var) < 0) {
		fprintf(stderr, "fb get fixed failed\n");
		return;
	}

	nativeDisplay->stride = fix.line_length;
	nativeDisplay->width = var.xres;
	nativeDisplay->height = var.yres;
	nativeDisplay->fb = mmap(NULL, nativeDisplay->stride * nativeDisplay->height,
				 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	close(fd);

	if (nativeDisplay->fb == MAP_FAILED) {
		fprintf(stderr, "fb map failed\n");
		return;
	}

	display->backend = &intelMemcpyBackend;
	display->width = nativeDisplay->width;
	display->height = nativeDisplay->height;

	printf("Using memcpy swapbuffer\n");
}

EGLDisplay
eglCreateDisplayNative(const char *device, const char *driver)
{
	EGLDisplayNative nativeDisplay;

	nativeDisplay = malloc(sizeof *nativeDisplay);
	if (nativeDisplay == NULL)
		return NULL;
	
	if (eglInitDisplay(&nativeDisplay->base, device, driver) < 0) {
		free(nativeDisplay);
		return NULL;
	}

#ifdef __DRI_COPY_BUFFER
	nativeInitDRICopyBuffer(&nativeDisplay->base);
#endif

	if (nativeDisplay->base.backend == NULL)
		nativeInitMemcpy(&nativeDisplay->base);

	return &nativeDisplay->base;
}

EGLSurface
eglCreateSurfaceNative(EGLDisplay display, EGLConfig config,
		       int x, int y, int width, int height)
{
	EGLSurfaceNative nativeSurface;

	nativeSurface = malloc(sizeof *nativeSurface);
	if (nativeSurface == NULL)
		return NULL;

	nativeSurface->x = x;
	nativeSurface->y = y;
	nativeSurface->width = width;
	nativeSurface->height = height;
	nativeSurface->name = ~0;
	nativeSurface->stride = 0;

	eglInitSurface(&nativeSurface->base, display, config);

	return &nativeSurface->base;
}

EGLSurface
eglCreatePixmapForName(EGLDisplay display, EGLConfig config,
		       uint32_t name, uint32_t width,
		       uint32_t height, uint32_t stride, const EGLint *attribList)
{
	EGLSurfaceNative nativeSurface;

	nativeSurface = malloc(sizeof *nativeSurface);
	if (nativeSurface == NULL)
		return NULL;

	nativeSurface->x = 0;
	nativeSurface->y = 0;
	nativeSurface->width = width;
	nativeSurface->height = height;
	nativeSurface->name = name;
	nativeSurface->stride = stride;

	eglInitSurface(&nativeSurface->base, display, config);

	return &nativeSurface->base;
}