summaryrefslogtreecommitdiff
path: root/intel.c
blob: 74cb80fa2503d291d8e88cbfcfaeb296179a21d0 (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
/*
 * Copyright © 2008, 2009 Kristian Høgsberg
 *
 * 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.
 */

#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 <GL/gl.h> /* dri_interface.h uses some GL integer types... */
#include "eagle-internal.h"

#define INTEL_STRIDE_ALIGNMENT 64

typedef struct EGLSurfaceNative *EGLSurfaceNative;
struct EGLSurfaceNative {
	struct EGLSurface	base;
	uint32_t		handles[10];
	EGLBoolean		backBuffer;
	__DRIbuffer		front;
	uint32_t		frontHandle;
};

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

/* Look at xf86-video-intel/src/common.h for the full horror of device
 * identification.
 */
enum {
	GEN_1  = 0x10,
	GEN_2  = 0x20,
	GEN_3  = 0x40,
	GEN_31 = 0x41,
	GEN_4  = 0x80,

	GEN_MAJOR_MASK = 0xf0,
	GEN_MINOR_MASK = 0x0f,
};

#define IS_I965(x) ((x) & GEN_4)
#define IS_I915(x) ((x) & GEN_3)
#define IS_I9xx(x) ((x) & (GEN_3 | GEN_4))
#define IS_I8xx(x) ((x) & (GEN_1 | GEN_2))

#define HAS_128_BYTE_Y_TILING(x) (((x) & (GEN_3 | GEN_4 | GEN_MINOR_MASK)) > GEN_3)

static uint32_t
tiling_stride (int dev, int tiling_mode, uint32_t pitch)
{
	uint32_t tile_width;

	if (tiling_mode == I915_TILING_NONE)
		return pitch;

	if (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING (dev))
		tile_width = 128;
	else
		tile_width = 512;

	/* 965+ just needs multiples of tile width */
	if (IS_I965 (dev))
		return align_to (pitch,  tile_width);

	/* Pre-965 needs power of two tile widths */
	while (tile_width < pitch)
		tile_width <<= 1;

	return tile_width;
}

static uint32_t
tiling_size (int dev, uint32_t tiling, uint32_t size)
{
	uint32_t fence;

	if (tiling == I915_TILING_NONE)
		return size;

	/* The 965 can have fences at any page boundary. */
	if (IS_I965 (dev))
		return align_to (size, 4096);

	/* Align the size to a power of two greater than the smallest fence. */
	if (IS_I9xx (dev))
		fence = 1024 * 1024; /* 1 MiB */
	else
		fence = 512 * 1024; /* 512 KiB */
	while (fence < size)
		fence <<= 1;

	return fence;
}

static int
intelCreateBuffer(int fd, GLint width, GLint height, __DRIbuffer *buffer)
{
	struct drm_i915_gem_create create;
	struct drm_gem_flink flink;
	uint32_t size;
	int tiling;
	int dev = GEN_4; /* XXX query using I915_GETPARAM + PARAM_CHIPSET_ID */

	tiling = I915_TILING_X;
	buffer->pitch = align_to(width * 4, INTEL_STRIDE_ALIGNMENT);
	if (tiling != I915_TILING_NONE) {
		buffer->pitch = tiling_stride (dev, tiling, buffer->pitch);
		size = buffer->pitch * height;
		size = tiling_size (dev, tiling, size);
	} else {
		size = buffer->pitch * height;
	}

	create.size = size;
	if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create)) {
		fprintf(stderr, "failed to create buffer\n");
		return -1;
	}

	if (tiling != I915_TILING_NONE) {
		struct drm_i915_gem_set_tiling set_tiling;

		memset (&set_tiling, 0, sizeof (set_tiling));
		set_tiling.handle = create.handle;
		set_tiling.tiling_mode = tiling;
		set_tiling.stride = buffer->pitch;

		if (ioctl (fd,
			   DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling)) {
			fprintf(stderr, "failed to enable tiling\n");
		}
	}

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

	buffer->name = flink.name;
	buffer->cpp = 4;

	return create.handle;
}

static void
intelGetBuffers(EGLSurface surface, unsigned int *attachments, int count)
{
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;
	__DRIbuffer *buffer;
	struct drm_gem_open open_arg;
	int i, ret;

	if (count == surface->count)
		return;

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

		buffer = &surface->buffers[i];
		buffer->attachment = attachments[i];

		if (buffer->attachment == __DRI_BUFFER_FRONT_LEFT &&
		    nativeSurface->front.name != ~0 &&
		    !nativeSurface->backBuffer) {
			buffer->name = nativeSurface->front.name;
			buffer->pitch = nativeSurface->front.pitch;
			buffer->cpp = 4;
			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;
		}

		nativeSurface->handles[i] =
			intelCreateBuffer(surface->display->fd,
					  surface->width,
					  surface->height,
					  buffer);
	}

	surface->count = count;
}

static EGLBoolean
intelDestroySurface(EGLDisplay display, EGLSurface surface)
{
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;
	struct drm_gem_close close;
	int i;

	for (i = 0; i < surface->count; i++) {
		close.handle = nativeSurface->handles[i];
		if (ioctl(surface->display->fd, DRM_IOCTL_GEM_CLOSE, &close) < 0)
			fprintf(stderr, "close of bo %d failed\n", close.handle);
	}
		
	free(surface);

	return EGL_TRUE;
}

static EGLBoolean
intelSwapBuffers(EGLDisplay display, EGLSurface surface)
{
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;
	EGLContext context;

	if (!nativeSurface->backBuffer)
		return EGL_TRUE;

	context = eglGetCurrentContext();

	display->copyBuffer->copyBuffer(context->driContext,
					&nativeSurface->front,
					0, 0,
					surface->driDrawable, __DRI_BUFFER_FRONT_LEFT,
					0, 0,
					surface->width,
					surface->height);

	glFlush();

	return EGL_TRUE;
}

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

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

	nativeSurface->backBuffer = EGL_FALSE;

	nativeSurface->front.attachment = __DRI_BUFFER_FRONT_LEFT;
	if (name == 0) {
		nativeSurface->frontHandle =
			intelCreateBuffer(display->fd,
					  width, height,
					  &nativeSurface->front);
	} else {
		nativeSurface->front.name = name;
		nativeSurface->front.pitch = stride;
		nativeSurface->front.cpp = 4;
		nativeSurface->front.flags = 0;
		nativeSurface->frontHandle = 0;
	}

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

	for (i = 0; attribList && attribList[i] != EGL_NONE; i += 2) {
		if (attribList[i] == EGL_RENDER_BUFFER &&
		    attribList[i + 1] == EGL_BACK_BUFFER)
			nativeSurface->backBuffer = EGL_TRUE;
	}

	return &nativeSurface->base;
}

static const struct EagleBackend intelBackend = {
	intelGetBuffers,
	intelSwapBuffers,
	intelDestroySurface,
	intelCreateSurfaceForName
};

static EGLDisplay
intelCreateDisplay(struct udev_device *device, const char *driver)
{
	EGLDisplay display;
	const char *path;

	display = malloc(sizeof *display);
	if (display == NULL)
		return NULL;
	
	path = udev_device_get_devnode(device);
	if (eglInitDisplay(display, path, driver) < 0) {
		free(display);
		return NULL;
	}

	display->backend = &intelBackend;

	return display;
}

EAGLE_EXPORT EGLBoolean
eglGetNativeBuffer(EGLSurface surface, GLenum buffer,
		   uint32_t *name, uint32_t *handle, uint32_t *stride)
{
	EGLSurfaceNative nativeSurface = (EGLSurfaceNative) surface;

	switch (buffer) {
	case GL_FRONT_LEFT:
		*name = nativeSurface->front.name;
		*handle = nativeSurface->frontHandle;
		*stride = nativeSurface->front.pitch;
		return EGL_TRUE;
	case GL_BACK_LEFT:
		*name = surface->buffers[0].name;
		*handle = nativeSurface->handles[0];
		*stride = surface->buffers[0].pitch;
		return EGL_TRUE;
	default:
		return EGL_FALSE;
	}
}

EGLDisplay
i915CreateDisplay(struct udev_device *device)
{
	return intelCreateDisplay(device, "i915");
}

EGLDisplay
i965CreateDisplay(struct udev_device *device)
{
	return intelCreateDisplay(device, "i965");
}