summaryrefslogtreecommitdiff
path: root/native.c
blob: 40b3dfc5777f30593d0ff48c26749fed8af5b1b0 (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
/*
 * 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 <radeon_drm.h>
#include <GL/gl.h> /* dri_interface.h uses some GL integer types... */
#include "eagle-internal.h"

#define INTEL_STRIDE_ALIGNMENT 64

struct EagleBackendNative {
	struct EagleBackend	base;
	int (*createBuffer)(int fd,
			    GLint width, GLint height, __DRIbuffer *buffer);
};

typedef struct EGLSurfaceNative *EGLSurfaceNative;
struct EGLSurfaceNative {
	struct EGLSurface	base;
	__DRIbuffer		*current;
	__DRIbuffer		colorBuffers[2];
	int			colorBufferCount;
	uint32_t		colorBufferHandles[2];
	uint32_t		handles[10];
};

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 EagleBackendNative *backend =
		(struct EagleBackendNative *) surface->display->backend;
	__DRIbuffer *buffer;
	int i;

	buffer = &surface->buffers[0];
	buffer->pitch = nativeSurface->current->pitch;
	buffer->name = nativeSurface->current->name;
	buffer->cpp = nativeSurface->current->cpp;

	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) {
			buffer->pitch = nativeSurface->current->pitch;
			buffer->name = nativeSurface->current->name;
			buffer->cpp = nativeSurface->current->cpp;
			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] =
			backend->createBuffer(surface->display->fd,
					      surface->width,
					      surface->height,
					      buffer);
	}

	surface->count = count;
}

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

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

	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 EGLDisplay
nativeCreateDisplay(struct udev_device *device,
		    const char *driver, const struct EagleBackend *backend)
{
	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 = backend;

	return display;
}

/* 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 const struct EagleBackendNative intelBackend = {
	{
		nativeGetBuffers,
		nativeDestroySurface,
	},
	intelCreateBuffer
};

EGLDisplay
i915CreateDisplay(struct udev_device *device)
{
	return nativeCreateDisplay(device, "i915", &intelBackend.base);
}

EGLDisplay
i965CreateDisplay(struct udev_device *device)
{
	return nativeCreateDisplay(device, "i965", &intelBackend.base);
}

#define RADEON_ALIGNMENT 64

static int
radeonCreateBuffer(int fd, GLint width, GLint height, __DRIbuffer *buffer)
{
#ifdef DRM_IOCTL_RADEON_GEM_CREATE
	struct drm_radeon_gem_create create;
	struct drm_gem_flink flink;

	buffer->pitch = align_to(width * 4, RADEON_ALIGNMENT);
	buffer->cpp = 4;

	create.size = buffer->pitch * height;
	create.alignment = 4096;
	create.initial_domain = RADEON_GEM_DOMAIN_VRAM;
	create.flags = RADEON_GEM_NO_BACKING_STORE;

	if (ioctl(fd, DRM_IOCTL_RADEON_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;

	return create.handle;
#else
	return -1;
#endif
}

static const struct EagleBackendNative radeonBackend = {
	{
		nativeGetBuffers,
		nativeDestroySurface,
	},

	radeonCreateBuffer
};

EGLDisplay
r300CreateDisplay(struct udev_device *device)
{
	return nativeCreateDisplay(device, "r300", &radeonBackend.base);
}