summaryrefslogtreecommitdiff
path: root/eagle.c
blob: 5874e970ca20e53b5ba5420ef4a0cbece601738a (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
 * Copyright © 2008 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 <fcntl.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <sys/time.h>
#include <GL/gl.h>
#include <xf86drm.h>
#include <drm.h>
#include <xf86drmMode.h>
#include "eagle.h"
#include "eagle-internal.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof ((a)[0]))

/* FIXME: Use TLS. */
static EGLint		lastError;
static EGLContext	currentContext;

static void
eglSetError(EGLint error)
{
	lastError = error;
}

EGLint
eglGetError()
{
	return lastError;
}

static __DRIbuffer *
dri2GetBuffers(__DRIdrawable *driDrawable,
	       int *width, int *height,
	       unsigned int *attachments, int count,
	       int *out_count, void *loaderPrivate)
{
	EGLSurface surface = loaderPrivate;
	EGLDisplay display = surface->display;

	display->backend->getBuffers(surface, attachments, count);
	
	*width = surface->width;
	*height = surface->height;
	*out_count = surface->count;

	return surface->buffers;
}

const static __DRIdri2LoaderExtension dri2LoaderExtension = {
	{ __DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION },
	dri2GetBuffers
};

static int
getUST(int64_t *ust)
{
	struct timeval tv;
    
	if (ust == NULL)
		return -EFAULT;

	if (gettimeofday(&tv, NULL) == 0) {
		ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
		return 0;
	} else {
		return -errno;
	}
}

const __DRIsystemTimeExtension systemTimeExtension = {
    { __DRI_SYSTEM_TIME, __DRI_SYSTEM_TIME_VERSION },
    getUST,
    NULL,
};

static const __DRIextension *eglLoaderExtensions[] = {
	&dri2LoaderExtension.base,
	&systemTimeExtension.base,
	NULL
};

static const char driDriverPath[] = "/usr/lib/dri";

static int
eglLoadDriver(EGLDisplay display, const char *driverName)
{
	char filename[PATH_MAX];
	const __DRIextension **extensions;
	const char *path;
	int i;

	/* Don't allow setuid applications to override the driver path. */
	if (geteuid() == getuid()) {
		path = getenv("EAGLE_DRIVER_PATH");
		if (path == NULL)
			getenv("LIBGL_DRIVERS_PATH");
	}
	if (path == NULL)
		path = driDriverPath;

	snprintf(filename, sizeof filename, "%s/%s_dri.so",
		 path, driverName);

	display->driver = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
	if (display->driver == NULL) {
		fprintf(stderr, "dlopen: %s\n", dlerror());
		return -1;
	}

	extensions = dlsym(display->driver, __DRI_DRIVER_EXTENSIONS);
	if (extensions == NULL) {
		fprintf(stderr, "dlsym: %s\n", dlerror());
		dlclose(display->driver);
		return -1;
	}

	for (i = 0; extensions[i]; i++) {
		if (strcmp(extensions[i]->name, __DRI_CORE) == 0 &&
		    extensions[i]->version >= __DRI_CORE_VERSION) {
			display->core = (__DRIcoreExtension *) extensions[i];
		}

		if (strcmp(extensions[i]->name, __DRI_DRI2) == 0 &&
		    extensions[i]->version >= __DRI_DRI2_VERSION) {
			display->dri2 = (__DRIdri2Extension *) extensions[i];
		}
	}

	if (display->core == NULL || display->dri2 == NULL) {
		dlclose(display->driver);
		return -1;
	}

	return 0;
}

/* Should be eagleCreateDisplay to avoid invading egl namespace?/ */

int
eglInitDisplay(EGLDisplay display,
	       struct udev_device *device, const char *driver)
{
	const __DRIconfig **configs;
	const __DRIextension **extensions;
	const char *path;
	int i;

	memset(display, 0, sizeof *display);
	display->initialized = EGL_FALSE;
	display->next_surface_id = 1;

	path = udev_device_get_devnode(device);
	display->fd = open(path, O_RDWR);
	if (display->fd < 0) {
		fprintf(stderr,
			"failed to open drm device %s: %m\n", path);
		return -1;
	}

	if (eglLoadDriver(display, driver) < 0)
		goto fail;
	
	display->driScreen =
		display->dri2->createNewScreen(0, display->fd,
					       eglLoaderExtensions,
					       &configs,
					       display);

	if (display->driScreen == NULL)
		goto fail;

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

	for (i = 0; extensions[i]; i++) {
		if (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0 &&
		    extensions[i]->version >= __DRI_TEX_BUFFER_VERSION) {
			display->texBuffer =
				(__DRItexBufferExtension *) extensions[i];
		}

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

	for (i = 0; configs[i]; i++)
		;
	display->numConfigs = i;
	display->configs =
		malloc(display->numConfigs * sizeof *display->configs);
	for (i = 0; configs[i]; i++) {
		display->configs[i].driConfig = configs[i];
		display->configs[i].id = i;
	}

	return 0;

 fail:
	close(display->fd);

	return -1;
}

struct dri_driver_entry {
	uint32_t vendor_id;
	uint32_t chip_id;
	EGLDisplay (*createDisplay)(struct udev_device *device);
};

static const struct dri_driver_entry driver_map[] = {
	/* FIXME: We need to extract this table from the dri drivers
	 * and store it on disk.  For now, map my i965 to i965,
	 * anything else intel to i915 and that's that. */

	{ 0x8086, 0x2a02, i965CreateDisplay },
	{ 0x8086,     ~0, i915CreateDisplay },
	{ 0, }
};

EGLDisplay
eglCreateDisplayNative(struct udev_device *device)
{
	struct udev_device *parent;
	const char *pci_id;
	uint32_t vendor_id, chip_id;
	int i;

	parent = udev_device_get_parent(device);
	pci_id = udev_device_get_property_value(parent, "PCI_ID");
	if (sscanf(pci_id, "%x:%x", &vendor_id, &chip_id) != 2)
		return NULL;

	for (i = 0; i < ARRAY_SIZE(driver_map); i++) {
		if (driver_map[i].vendor_id == vendor_id &&
		    (driver_map[i].chip_id == ~0 || driver_map[i].chip_id == chip_id)) 
			return driver_map[i].createDisplay(device);
	}

	return NULL;
}


int
eglGetDisplayFD(EGLDisplay display)
{
	return display->fd;
}

EGLBoolean
eglInitialize(EGLDisplay display, EGLint *major, EGLint *minor)
{
	if (major != NULL)
		*major = 1;
	if (minor != NULL)
		*minor = 1;

	display->initialized = EGL_TRUE;

	return EGL_TRUE;
}

EGLBoolean
eglTerminate(EGLDisplay display)
{
	return EGL_TRUE;
}

const char *
eglQueryString(EGLDisplay display, EGLint name)
{
	if (!display->initialized) {
		eglSetError(EGL_NOT_INITIALIZED);
		return NULL;
	}

	switch (name) {
	case EGL_CLIENT_APIS:
	case EGL_EXTENSIONS:
	case EGL_VENDOR:
	case EGL_VERSION:
	default:
		eglSetError(EGL_BAD_PARAMETER);
		return NULL;
	}
}

EGLBoolean
eglGetConfigs(EGLDisplay display, EGLConfig *configs,
	      EGLint configSize, EGLint *numConfigs)
{
	int num, i;

	if (display->numConfigs > configSize)
		num = configSize;
	else
		num = display->numConfigs;

	if (numConfigs != NULL)
		*numConfigs = display->numConfigs;

	if (configs != NULL)
		for (i = 0; i < num; i++)
			configs[i] = &display->configs[i];
	
	return EGL_TRUE;
}

static struct { int egl_attrib, dri_attrib; } attrib_map[] = {
	{ EGL_BUFFER_SIZE,		__DRI_ATTRIB_BUFFER_SIZE },
	{ EGL_RED_SIZE,			__DRI_ATTRIB_RED_SIZE },
	{ EGL_GREEN_SIZE,		__DRI_ATTRIB_GREEN_SIZE },
	{ EGL_BLUE_SIZE,		__DRI_ATTRIB_BLUE_SIZE },
	{ EGL_LUMINANCE_SIZE,		__DRI_ATTRIB_LUMINANCE_SIZE },
	{ EGL_ALPHA_SIZE,		__DRI_ATTRIB_ALPHA_SIZE },
	{ EGL_ALPHA_MASK_SIZE,		__DRI_ATTRIB_ALPHA_MASK_SIZE },
	{ EGL_BIND_TO_TEXTURE_RGB,	__DRI_ATTRIB_BIND_TO_TEXTURE_RGB },
	{ EGL_BIND_TO_TEXTURE_RGBA,	__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA },
	{ EGL_CONFIG_CAVEAT,		__DRI_ATTRIB_CONFIG_CAVEAT },
	{ EGL_DEPTH_SIZE,		__DRI_ATTRIB_DEPTH_SIZE },
	{ EGL_LEVEL,			__DRI_ATTRIB_LEVEL },
	{ EGL_MAX_PBUFFER_WIDTH,	__DRI_ATTRIB_MAX_PBUFFER_WIDTH },
	{ EGL_MAX_PBUFFER_HEIGHT,	__DRI_ATTRIB_MAX_PBUFFER_HEIGHT },
	{ EGL_MAX_PBUFFER_PIXELS,	__DRI_ATTRIB_MAX_PBUFFER_PIXELS },
	{ EGL_MAX_SWAP_INTERVAL,	__DRI_ATTRIB_MAX_SWAP_INTERVAL },
	{ EGL_MIN_SWAP_INTERVAL,	__DRI_ATTRIB_MIN_SWAP_INTERVAL },
	{ EGL_SAMPLE_BUFFERS,		__DRI_ATTRIB_SAMPLE_BUFFERS },
	{ EGL_SAMPLES,			__DRI_ATTRIB_SAMPLES },
	{ EGL_STENCIL_SIZE,		__DRI_ATTRIB_STENCIL_SIZE },
	{ EGL_TRANSPARENT_TYPE,		__DRI_ATTRIB_TRANSPARENT_TYPE },
	{ EGL_TRANSPARENT_RED_VALUE,	__DRI_ATTRIB_TRANSPARENT_RED_VALUE },
	{ EGL_TRANSPARENT_GREEN_VALUE,	__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE },
	{ EGL_TRANSPARENT_BLUE_VALUE,	__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE },
};

static EGLBoolean
attributeMatches(EGLDisplay display,
		 EGLConfig config, EGLint attribute, EGLint value)
{
	EGLint config_value;

	/* FIXME: Handle non-exact matches. */

	if (!eglGetConfigAttrib(display, config, attribute, &config_value))
		return EGL_FALSE;

	return value == config_value;
}

static EGLBoolean
configMatches(EGLDisplay display,
	      EGLConfig config, const EGLint *attribList)
{
	int i;

	for (i = 0; attribList[i] != EGL_NONE; i += 2)
		if (!attributeMatches(display, config,
				      attribList[i], attribList[i + 1]))
			return EGL_FALSE;

	return EGL_TRUE;
}

EGLBoolean
eglChooseConfig(EGLDisplay display, const EGLint *attribList,
		EGLConfig *configs, EGLint configSize, EGLint *numConfigs)
{
	int i, j;

	for (i = 0, j = 0; i < display->numConfigs; i++) {
		if (configMatches(display, &display->configs[i], attribList) && j < configSize)
			configs[j++] = &display->configs[i];
	}

	if (numConfigs != NULL)
		*numConfigs = j;

	return j > 0;
}

EGLBoolean
eglGetConfigAttrib(EGLDisplay display, EGLConfig config,
		   EGLint attribute, EGLint *value)
{
	unsigned int dri_attrib, dri_value, i;
	
        switch (attribute) {
	case EGL_CONFIG_ID:
		*value = config->id;
		return EGL_TRUE;
	case EGL_SURFACE_TYPE:
		*value = EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT;
		return EGL_TRUE;
	case EGL_RENDERABLE_TYPE:
		*value = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
		return EGL_TRUE;
	case EGL_NATIVE_RENDERABLE:
		*value = EGL_TRUE;
		return EGL_TRUE;
	case EGL_NATIVE_VISUAL_ID:
		*value = 1000; /* What's a native visual anyway? */
		return EGL_TRUE;
	case EGL_CONFORMANT:
		/* FIXME: Look up __DRI_ATTRIB_CONFIG_CAVEAT instead. */
		eglSetError(EGL_BAD_ATTRIBUTE);
		return EGL_FALSE;
	case EGL_COLOR_BUFFER_TYPE:
		return EGL_FALSE;
	}

	for (i = 0; i < ARRAY_SIZE(attrib_map); i++)
		if (attrib_map[i].egl_attrib == attribute) {
			dri_attrib = attrib_map[i].dri_attrib;
			break;
		}

	if (i == ARRAY_SIZE(attrib_map))
		return EGL_FALSE;

	/* FIXME: Signedness of attribs and tokens? */
	if (!display->core->getConfigAttrib(config->driConfig,
					    dri_attrib, &dri_value)) {
		eglSetError(EGL_BAD_ATTRIBUTE);
		return EGL_FALSE;
	}

	switch (attribute) {
	case EGL_COLOR_BUFFER_TYPE:
		*value = EGL_RGB_BUFFER;
		*value = EGL_LUMINANCE_BUFFER;
		break;
	case EGL_CONFIG_CAVEAT:
		if (dri_value & __DRI_ATTRIB_SLOW_BIT)
			*value = EGL_SLOW_CONFIG;
		else if  (dri_value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
			*value = EGL_NON_CONFORMANT_CONFIG;
		else
			*value = EGL_NONE;
		break;
	case EGL_TRANSPARENT_TYPE:
		*value = EGL_NONE;
		*value = EGL_TRANSPARENT_RGB;
		break;
	default:
		*value = dri_value;
		break;
	}

	return EGL_TRUE;
}

void
eglInitSurface(EGLSurface surface,
	       EGLDisplay display, EGLConfig fbconfig, int width, int height)
{
	surface->display = display;
	surface->id = display->next_surface_id++;
	surface->width = width;
	surface->height = height;
	surface->count = 0;

	surface->driDrawable =
		display->dri2->createNewDrawable(display->driScreen,
						 fbconfig->driConfig,
						 surface);
}

EGLSurface
eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
		       EGLNativeWindowType win, const EGLint *attribList)
{
	return EGL_NO_SURFACE;
}

EGLSurface
eglCreatePbufferSurface(EGLDisplay display,
			EGLConfig config, const EGLint *attribList)
{
	int i, width, height;

	for (i = 0; attribList[i]; i += 2) {
		switch (attribList[i]) {
		case EGL_WIDTH:
			width = attribList[i + 1];
			break;
		case EGL_HEIGHT:
			height = attribList[i + 1];
			break;
		case EGL_LARGEST_PBUFFER:
		case EGL_TEXTURE_FORMAT:
		case EGL_TEXTURE_TARGET:
		case EGL_MIPMAP_TEXTURE:
		case EGL_COLORSPACE:
		case EGL_ALPHA_FORMAT:
			break;
		}
	}

	return EGL_NO_SURFACE;
}

EGLSurface
eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
		       EGLNativePixmapType pixmap, const EGLint *attribList)
{
	return EGL_NO_SURFACE;
}

EGLSurface
eglCreateSurfaceForName(EGLDisplay display, EGLConfig config,
			uint32_t name, uint32_t width,
			uint32_t height, uint32_t stride, const EGLint *attribList)
{
	return display->backend->createSurfaceForName(display, config, name,
						      width, height, stride, attribList);
}

EGLBoolean
eglDestroySurface(EGLDisplay display, EGLSurface surface)
{
	display->core->destroyDrawable(surface->driDrawable);

	return display->backend->destroySurface(display, surface);
}

EGLBoolean
eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
		 EGLint attribute, EGLint value)
{
	return EGL_TRUE;
}

EGLBoolean
eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
		EGLint attribute, EGLint *value)
{
	return EGL_TRUE;
}

EGLBoolean
eglBindTexImage(EGLDisplay display, EGLSurface surface, EGLint buffer)
{
	EGLContext context;

	context = eglGetCurrentContext();

	display->texBuffer->setTexBuffer(context->driContext,
					 buffer, surface->driDrawable);

	return EGL_TRUE;
}

EGLBoolean eglReleaseTexImage(EGLDisplay display,
			      EGLSurface surface, EGLint buffer)
{
	return EGL_TRUE;
}

EGLContext
eglCreateContext(EGLDisplay display, EGLConfig config,
		 EGLContext shared, const EGLint *attribList)
{
	EGLContext context;
	__DRIcontext *driShared;

	context = malloc(sizeof *context);

	if (shared)
		driShared = shared->driContext;
	else
		driShared = NULL;

	context->driContext =
		display->dri2->createNewContext(display->driScreen,
						config->driConfig,
						driShared,
						context);
	if (context->driContext == NULL) {
		free(context);
		return EGL_NO_CONTEXT;
	}

	return context;
}

void
eglDestroyContext(EGLDisplay display, EGLContext context)
{
	if (context == currentContext)
		currentContext = EGL_NO_CONTEXT;

	display->core->destroyContext(context->driContext);
	free(context);
}

EGLBoolean
eglMakeCurrent(EGLDisplay display,
	       EGLSurface draw, EGLSurface read, EGLContext context)
{
	__DRIdrawable *driDraw, *driRead;
	EGLBoolean status;

	context->drawSurface = draw;
	context->readSurface = read;

	if (draw != NULL)
		driDraw = draw->driDrawable;
	if (read != NULL)
		driRead = read->driDrawable;

	status = display->core->bindContext(context->driContext,
					    driDraw, driRead);
	if (status)
		currentContext = context;

	return status;
}

EGLContext
eglGetCurrentContext()
{
	return currentContext;
}

EGLSurface
eglGetCurrentSurface(EGLint readdraw)
{
	switch (readdraw) {
	case EGL_READ:
		return currentContext->readSurface;
	case EGL_DRAW:
		return currentContext->drawSurface;
	default:
		eglSetError(EGL_BAD_SURFACE); /* FIXME: what's this error */
		return EGL_NO_SURFACE;
	}
}

EGLDisplay
eglGetCurrentDisplay(void)
{
	return currentContext->display;
}

EGLBoolean
eglQueryContext(EGLDisplay display, EGLContext context,
		EGLint attribute, EGLint *value)
{
	switch (attribute) {
	case EGL_CONFIG_ID:
		*value = context->config->id;
		return EGL_TRUE;
	case EGL_CONTEXT_CLIENT_TYPE:
		*value = EGL_OPENGL_ES_BIT;
		return EGL_TRUE;
	case EGL_CONTEXT_CLIENT_VERSION:
		/* FIXME: What? */
		return EGL_FALSE;
	case EGL_RENDER_BUFFER:
		if (context->drawSurface == NULL)
			return EGL_NONE;
		*value = EGL_SINGLE_BUFFER; /* pixmap */
		*value = EGL_BACK_BUFFER; /* pbuffer */
		/* window: either, depending on surface EGL_RENDER_BUFFER */
		return EGL_TRUE;
	default:
		eglSetError(EGL_BAD_ATTRIBUTE);
		return EGL_FALSE;
	}
}

EGLBoolean
eglBindAPI(EGLenum api)
{
	return EGL_TRUE;
}

EGLenum
eglQueryAPI(void)
{
	return EGL_TRUE;
}

EGLBoolean
eglWaitClient(void)
{
	return EGL_TRUE;
}

EGLBoolean
eglWaitGL(void)
{
	return EGL_TRUE;
}

EGLBoolean
eglWaitNative(EGLint engine)
{
	return EGL_TRUE;
}

EGLBoolean
eglSwapBuffers(EGLDisplay display, EGLSurface surface)
{
	return display->backend->swapBuffers(display, surface);
}

EGLBoolean
eglCopyBuffers(EGLDisplay dpy,
	       EGLSurface surface, EGLNativePixmapType target)
{
	return EGL_TRUE;
}

EGLBoolean
eglSwapInterval(EGLDisplay display, EGLint interval)
{
	__DRIdrawable *drawable;

	if (display->swapControl) {
		drawable = currentContext->readSurface->driDrawable;
		display->swapControl->setSwapInterval(drawable, interval);
		return EGL_TRUE;
	} else {
		return EGL_FALSE;
	}
}

void (*eglGetProcAddress(const char *procname))(void)
{
	return NULL;
}

EGLBoolean
eglCopyNativeBuffers(EGLDisplay display,
		     EGLSurface dst, GLenum dstBuffer, int32_t dst_x, int32_t dst_y,
		     EGLSurface src, GLenum srcBuffer, int32_t x, int32_t y, int32_t width, int32_t height)
{
	EGLContext context;
	__DRIbuffer *srcDRIBuffer, *dstDRIBuffer;
	unsigned int attachments[1];

	context = eglGetCurrentContext();

	/* FIXME: glCopyPixels should work, but then we'll have to
	 * call eglMakeCurrent to set up the src and dest surfaces
	 * first.  This seems cheaper, but maybe there's a better way
	 * to accomplish this. */

	attachments[0] = __DRI_BUFFER_FRONT_LEFT;
	display->backend->getBuffers(src, attachments, 1);

	/* FIXME: Actually use dstBuffer and srcBuffer (eg
	 * GL_FRONT_LEFT) to look up the buffers to use. */

	dstDRIBuffer = &dst->buffers[0];
	srcDRIBuffer = &src->buffers[0];

	return display->copyBuffer->copyBuffer(context->driContext,
					       dstDRIBuffer,
					       dst_x, dst_y,
					       srcDRIBuffer,
					       x, y, width, height);
}