aboutsummaryrefslogtreecommitdiff
path: root/src/egl
diff options
context:
space:
mode:
Diffstat (limited to 'src/egl')
-rw-r--r--src/egl/Makefile24
-rw-r--r--src/egl/drivers/demo/Makefile31
-rw-r--r--src/egl/drivers/demo/demo.c321
-rw-r--r--src/egl/drivers/dri/Makefile64
-rw-r--r--src/egl/drivers/dri/egldri.c1001
-rw-r--r--src/egl/drivers/dri/egldri.h113
-rw-r--r--src/egl/main/Makefile64
-rw-r--r--src/egl/main/eglapi.c497
-rw-r--r--src/egl/main/eglconfig.c577
-rw-r--r--src/egl/main/eglconfig.h75
-rw-r--r--src/egl/main/eglcontext.c240
-rw-r--r--src/egl/main/eglcontext.h67
-rw-r--r--src/egl/main/egldisplay.c71
-rw-r--r--src/egl/main/egldisplay.h44
-rw-r--r--src/egl/main/egldriver.c251
-rw-r--r--src/egl/main/egldriver.h164
-rw-r--r--src/egl/main/eglglobals.c65
-rw-r--r--src/egl/main/eglglobals.h44
-rw-r--r--src/egl/main/eglhash.c347
-rw-r--r--src/egl/main/eglhash.h39
-rw-r--r--src/egl/main/eglmode.c199
-rw-r--r--src/egl/main/eglmode.h53
-rw-r--r--src/egl/main/eglscreen.c334
-rw-r--r--src/egl/main/eglscreen.h83
-rw-r--r--src/egl/main/eglsurface.c323
-rw-r--r--src/egl/main/eglsurface.h100
-rw-r--r--src/egl/main/egltypedefs.h28
27 files changed, 5219 insertions, 0 deletions
diff --git a/src/egl/Makefile b/src/egl/Makefile
new file mode 100644
index 000000000..931e9d0cb
--- /dev/null
+++ b/src/egl/Makefile
@@ -0,0 +1,24 @@
+# src/egl/Makefile
+
+TOP = ../..
+
+SUBDIRS = main drivers/demo drivers/dri
+
+
+default: subdirs
+
+
+subdirs:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir ; $(MAKE)) || exit 1 ; \
+ fi \
+ done
+
+
+clean:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir ; $(MAKE) clean) ; \
+ fi \
+ done
diff --git a/src/egl/drivers/demo/Makefile b/src/egl/drivers/demo/Makefile
new file mode 100644
index 000000000..fd587b79e
--- /dev/null
+++ b/src/egl/drivers/demo/Makefile
@@ -0,0 +1,31 @@
+# src/egl/drivers/demo/Makefile
+
+TOP = ../../../..
+include $(TOP)/configs/current
+
+
+INCLUDE_DIRS = -I$(TOP)/include -I$(TOP)/src/egl/main
+
+
+SOURCES = demo.c
+
+OBJECTS = $(SOURCES:.c=.o)
+
+
+.c.o:
+ $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
+
+
+
+default: $(LIB_DIR)/demodriver.so
+
+
+$(LIB_DIR)/demodriver.so: $(OBJECTS)
+ $(TOP)/bin/mklib -o demodriver.so -noprefix \
+ -install $(LIB_DIR) $(OBJECTS)
+
+
+
+clean:
+ rm -f *.o
+ rm -f *.so
diff --git a/src/egl/drivers/demo/demo.c b/src/egl/drivers/demo/demo.c
new file mode 100644
index 000000000..4c08ac569
--- /dev/null
+++ b/src/egl/drivers/demo/demo.c
@@ -0,0 +1,321 @@
+/*
+ * Sample driver: Demo
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+#include "eglmode.h"
+#include "eglscreen.h"
+#include "eglsurface.h"
+
+
+/**
+ * Demo driver-specific driver class derived from _EGLDriver
+ */
+typedef struct demo_driver
+{
+ _EGLDriver Base; /* base class/object */
+ GLuint DemoStuff;
+} DemoDriver;
+
+#define DEMO_DRIVER(D) ((DemoDriver *) (D))
+
+
+/**
+ * Demo driver-specific surface class derived from _EGLSurface
+ */
+typedef struct demo_surface
+{
+ _EGLSurface Base; /* base class/object */
+ GLuint DemoStuff;
+} DemoSurface;
+
+
+/**
+ * Demo driver-specific context class derived from _EGLContext
+ */
+typedef struct demo_context
+{
+ _EGLContext Base; /* base class/object */
+ GLuint DemoStuff;
+} DemoContext;
+
+
+
+static EGLBoolean
+demoInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ _EGLScreen *scrn;
+ EGLint i;
+
+ /* Create a screen */
+ scrn = calloc(1, sizeof(*scrn));
+ _eglAddScreen(disp, scrn);
+
+ /* Create the screen's modes - silly example */
+ _eglAddMode(scrn, 1600, 1200, 72 * 1000, "1600x1200-72");
+ _eglAddMode(scrn, 1280, 1024, 72 * 1000, "1280x1024-70");
+ _eglAddMode(scrn, 1280, 1024, 70 * 1000, "1280x1024-70");
+ _eglAddMode(scrn, 1024, 768, 72 * 1000, "1024x768-72");
+
+ /* Create the display's visual configs - silly example */
+ for (i = 0; i < 4; i++) {
+ _EGLConfig config;
+ _eglInitConfig(&config, i + 1);
+ _eglSetConfigAttrib(&config, EGL_RED_SIZE, 8);
+ _eglSetConfigAttrib(&config, EGL_GREEN_SIZE, 8);
+ _eglSetConfigAttrib(&config, EGL_BLUE_SIZE, 8);
+ _eglSetConfigAttrib(&config, EGL_ALPHA_SIZE, 8);
+ _eglSetConfigAttrib(&config, EGL_BUFFER_SIZE, 32);
+ if (i & 1) {
+ _eglSetConfigAttrib(&config, EGL_DEPTH_SIZE, 32);
+ }
+ if (i & 2) {
+ _eglSetConfigAttrib(&config, EGL_STENCIL_SIZE, 8);
+ }
+ _eglSetConfigAttrib(&config, EGL_SURFACE_TYPE,
+ (EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT));
+ _eglAddConfig(disp, &config);
+ }
+
+ drv->Initialized = EGL_TRUE;
+
+ *major = 1;
+ *minor = 0;
+
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+demoTerminate(_EGLDriver *drv, EGLDisplay dpy)
+{
+ /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
+ free(drv);
+ return EGL_TRUE;
+}
+
+
+static DemoContext *
+LookupDemoContext(EGLContext ctx)
+{
+ _EGLContext *c = _eglLookupContext(ctx);
+ return (DemoContext *) c;
+}
+
+
+static DemoSurface *
+LookupDemoSurface(EGLSurface surf)
+{
+ _EGLSurface *s = _eglLookupSurface(surf);
+ return (DemoSurface *) s;
+}
+
+
+
+static EGLContext
+demoCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
+{
+ _EGLConfig *conf;
+ DemoContext *c;
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ int i;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreateContext");
+ return EGL_NO_CONTEXT;
+ }
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs defined for now */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
+ return EGL_NO_CONTEXT;
+ }
+ }
+
+ c = (DemoContext *) calloc(1, sizeof(DemoContext));
+ if (!c)
+ return EGL_NO_CONTEXT;
+
+ _eglInitContext(&c->Base);
+ c->Base.Display = disp;
+ c->Base.Config = conf;
+ c->Base.DrawSurface = EGL_NO_SURFACE;
+ c->Base.ReadSurface = EGL_NO_SURFACE;
+ c->DemoStuff = 1;
+ printf("demoCreateContext\n");
+
+ /* generate handle and insert into hash table */
+ _eglSaveContext(&c->Base);
+ assert(c->Base.Handle);
+
+ return c->Base.Handle;
+}
+
+
+static EGLSurface
+demoCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
+{
+ int i;
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs at this time */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreateWindowSurface");
+ return EGL_NO_SURFACE;
+ }
+ }
+ printf("eglCreateWindowSurface()\n");
+ /* XXX unfinished */
+
+ return EGL_NO_SURFACE;
+}
+
+
+static EGLSurface
+demoCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
+{
+ _EGLConfig *conf;
+ EGLint i;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs at this time */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+ }
+
+ if (conf->Attrib[EGL_SURFACE_TYPE - FIRST_ATTRIB] == 0) {
+ _eglError(EGL_BAD_MATCH, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+
+ printf("eglCreatePixmapSurface()\n");
+ return EGL_NO_SURFACE;
+}
+
+
+static EGLSurface
+demoCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ DemoSurface *surf = (DemoSurface *) calloc(1, sizeof(DemoSurface));
+ if (!surf)
+ return EGL_NO_SURFACE;
+
+ if (_eglInitPbufferSurface(&surf->Base, drv, dpy, config, attrib_list)
+ == EGL_NO_SURFACE) {
+ free(surf);
+ return EGL_NO_SURFACE;
+ }
+
+ /* a real driver would allocate the pbuffer memory here */
+
+ return surf->Base.Handle;
+}
+
+
+static EGLBoolean
+demoDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
+{
+ DemoSurface *fs = LookupDemoSurface(surface);
+ _eglRemoveSurface(&fs->Base);
+ if (fs->Base.IsBound) {
+ fs->Base.DeletePending = EGL_TRUE;
+ }
+ else {
+ free(fs);
+ }
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+demoDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
+{
+ DemoContext *fc = LookupDemoContext(context);
+ _eglRemoveContext(&fc->Base);
+ if (fc->Base.IsBound) {
+ fc->Base.DeletePending = EGL_TRUE;
+ }
+ else {
+ free(fc);
+ }
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+demoMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext context)
+{
+ /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
+ DemoSurface *readSurf = LookupDemoSurface(read);
+ DemoSurface *drawSurf = LookupDemoSurface(draw);
+ DemoContext *ctx = LookupDemoContext(context);
+ EGLBoolean b;
+
+ b = _eglMakeCurrent(drv, dpy, draw, read, context);
+ if (!b)
+ return EGL_FALSE;
+
+ /* XXX this is where we'd do the hardware context switch */
+ (void) drawSurf;
+ (void) readSurf;
+ (void) ctx;
+
+ printf("eglMakeCurrent()\n");
+ return EGL_TRUE;
+}
+
+
+/**
+ * The bootstrap function. Return a new DemoDriver object and
+ * plug in API functions.
+ */
+_EGLDriver *
+_eglMain(_EGLDisplay *dpy)
+{
+ DemoDriver *demo;
+
+ demo = (DemoDriver *) calloc(1, sizeof(DemoDriver));
+ if (!demo) {
+ return NULL;
+ }
+
+ /* First fill in the dispatch table with defaults */
+ _eglInitDriverFallbacks(&demo->Base);
+ /* then plug in our Demo-specific functions */
+ demo->Base.Initialize = demoInitialize;
+ demo->Base.Terminate = demoTerminate;
+ demo->Base.CreateContext = demoCreateContext;
+ demo->Base.MakeCurrent = demoMakeCurrent;
+ demo->Base.CreateWindowSurface = demoCreateWindowSurface;
+ demo->Base.CreatePixmapSurface = demoCreatePixmapSurface;
+ demo->Base.CreatePbufferSurface = demoCreatePbufferSurface;
+ demo->Base.DestroySurface = demoDestroySurface;
+ demo->Base.DestroyContext = demoDestroyContext;
+
+ /* enable supported extensions */
+ demo->Base.MESA_screen_surface = EGL_TRUE;
+ demo->Base.MESA_copy_context = EGL_TRUE;
+
+ return &demo->Base;
+}
diff --git a/src/egl/drivers/dri/Makefile b/src/egl/drivers/dri/Makefile
new file mode 100644
index 000000000..6b50959a5
--- /dev/null
+++ b/src/egl/drivers/dri/Makefile
@@ -0,0 +1,64 @@
+# src/egl/drivers/dri/Makefile
+
+TOP = ../../../..
+include $(TOP)/configs/current
+
+
+### Include directories
+INCLUDE_DIRS = \
+ -I. \
+ -I$(DRM_SOURCE_PATH)/shared-core \
+ -I$(DRM_SOURCE_PATH)/libdrm \
+ -I$(TOP)/include \
+ -I$(TOP)/include/GL/internal \
+ -I$(TOP)/src/mesa \
+ -I$(TOP)/src/mesa/main \
+ -I$(TOP)/src/mesa/glapi \
+ -I$(TOP)/src/mesa/math \
+ -I$(TOP)/src/mesa/transform \
+ -I$(TOP)/src/mesa/shader \
+ -I$(TOP)/src/mesa/swrast \
+ -I$(TOP)/src/mesa/swrast_setup \
+ -I$(TOP)/src/egl/main \
+ -I$(TOP)/src/mesa/drivers/dri/common
+
+
+HEADERS = egldri.h
+
+SOURCES = egldri.c \
+ $(DRM_SOURCE_PATH)/libdrm/xf86drm.c \
+ $(DRM_SOURCE_PATH)/libdrm/xf86drmHash.c \
+ $(DRM_SOURCE_PATH)/libdrm/xf86drmRandom.c
+
+OBJECTS = $(SOURCES:.c=.o)
+
+
+.c.o:
+ $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
+
+
+
+default: depend library Makefile
+
+
+# EGLdri Library
+library: $(LIB_DIR)/libEGLdri.so
+
+$(LIB_DIR)/libEGLdri.so: $(OBJECTS)
+ $(TOP)/bin/mklib -o EGLdri -major 1 -minor 0 \
+ -install $(LIB_DIR) -ldl $(OBJECTS)
+
+
+clean:
+ rm -f *.o
+ rm -f *.so
+
+depend: $(SOURCES) $(HEADERS)
+ @ echo "running $(MKDEP)"
+ @ touch depend
+ $(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
+ $(SOURCES) $(HEADERS) > /dev/null
+
+include depend
+# DO NOT DELETE
+
diff --git a/src/egl/drivers/dri/egldri.c b/src/egl/drivers/dri/egldri.c
new file mode 100644
index 000000000..2d4512776
--- /dev/null
+++ b/src/egl/drivers/dri/egldri.c
@@ -0,0 +1,1001 @@
+/*
+ * Generic EGL driver for DRI
+ */
+#include <dirent.h>
+#include <stdio.h>
+#include <string.h>
+#include <linux/fb.h>
+#include <assert.h>
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/time.h>
+
+#include "egldriver.h"
+#include "egldisplay.h"
+#include "eglcontext.h"
+#include "eglconfig.h"
+#include "eglsurface.h"
+#include "eglscreen.h"
+#include "eglglobals.h"
+#include "eglmode.h"
+
+#include "egldri.h"
+
+const char *sysfs = "/sys/class";
+#define None 0
+static const int empty_attribute_list[1] = { None };
+
+/**
+ * The bootstrap function. Return a new driDriver object and
+ * plug in API functions.
+ */
+_EGLDriver *
+_eglMain(_EGLDisplay *dpy)
+{
+ int length;
+ char path[NAME_MAX];
+ struct dirent *dirent;
+ FILE *file;
+ DIR *dir;
+ _EGLDriver *driver = NULL;;
+
+ snprintf(path, sizeof(path), "%s/drm", sysfs);
+ if (!(dir = opendir(path))) {
+ printf("EGL - %s DRM devices not found.", path);
+ return EGL_FALSE;
+ }
+ while ((dirent = readdir(dir))) {
+
+ if (strncmp(&dirent->d_name[0], "card", 4) != 0)
+ continue;
+ if (strcmp(&dirent->d_name[4], &dpy->Name[1]) != 0)
+ continue;
+
+ snprintf(path, sizeof(path), "%s/drm/card%s/dri_library_name", sysfs, &dpy->Name[1]);
+ file = fopen(path, "r");
+ fgets(path, sizeof(path), file);
+ fclose(file);
+
+ if ((length = strlen(path)) > 0)
+ path[length - 1] = '\0'; /* remove the trailing newline from sysfs */
+ strncat(path, "_dri", sizeof(path));
+
+ driver = _eglOpenDriver(dpy, path);
+
+ break;
+ }
+ closedir(dir);
+
+ return driver;
+}
+
+
+static EGLContext
+_eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
+{
+ _EGLConfig *conf;
+ driContext *c;
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *share = Lookup_driContext(share_list);
+ void *sharePriv;
+ __GLcontextModes mode;
+ int i;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreateContext");
+ return EGL_NO_CONTEXT;
+ }
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs defined for now */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
+ return EGL_NO_CONTEXT;
+ }
+ }
+
+ c = (driContext *) calloc(1, sizeof(*c));
+ if (!c)
+ return EGL_NO_CONTEXT;
+
+ _eglInitContext(&c->Base);
+ c->Base.Display = &disp->Base;
+ c->Base.Config = conf;
+ c->Base.DrawSurface = EGL_NO_SURFACE;
+ c->Base.ReadSurface = EGL_NO_SURFACE;
+
+ _eglConfigToContextModesRec(conf, &mode);
+
+ if (share)
+ sharePriv = share->driContext.private;
+ else
+ sharePriv = NULL;
+
+ c->driContext.private = disp->driScreen.createNewContext(disp, &mode,
+ GLX_WINDOW_BIT, sharePriv, &c->driContext);
+
+ if (!c->driContext.private) {
+ free(c);
+ return EGL_FALSE;
+ }
+
+ /* generate handle and insert into hash table */
+ _eglSaveContext(&c->Base);
+ assert(c->Base.Handle);
+
+ return c->Base.Handle;
+}
+
+
+static EGLBoolean
+_eglDRIMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext context)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *ctx = Lookup_driContext(context);
+ EGLBoolean b;
+
+ b = _eglMakeCurrent(drv, dpy, draw, read, context);
+ if (!b)
+ return EGL_FALSE;
+
+ if (ctx) {
+ ctx->driContext.bindContext(disp, 0, read, draw, &ctx->driContext);
+ } else {
+// _mesa_make_current( NULL, NULL, NULL );
+ }
+ return EGL_TRUE;
+}
+
+
+static EGLSurface
+_eglDRICreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
+{
+ int i;
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs at this time */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreateWindowSurface");
+ return EGL_NO_SURFACE;
+ }
+ }
+ printf("eglCreateWindowSurface()\n");
+ /* XXX unfinished */
+
+ return EGL_NO_SURFACE;
+}
+
+
+static EGLSurface
+_eglDRICreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
+{
+ _EGLConfig *conf;
+ EGLint i;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ /* no attribs at this time */
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+ }
+
+ if (conf->Attrib[EGL_SURFACE_TYPE - FIRST_ATTRIB] == 0) {
+ _eglError(EGL_BAD_MATCH, "eglCreatePixmapSurface");
+ return EGL_NO_SURFACE;
+ }
+
+ printf("eglCreatePixmapSurface()\n");
+ return EGL_NO_SURFACE;
+}
+
+
+static EGLSurface
+_eglDRICreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
+{
+ driSurface *surf;
+
+ surf = (driSurface *) calloc(1, sizeof(*surf));
+ if (!surf) {
+ return EGL_NO_SURFACE;
+ }
+
+ if (_eglInitPbufferSurface(&surf->Base, drv, dpy, config, attrib_list) == EGL_NO_SURFACE) {
+ free(surf);
+ return EGL_NO_SURFACE;
+ }
+
+ /* create software-based pbuffer */
+ {
+// GLcontext *ctx = NULL; /* this _should_ be OK */
+ GLvisual vis;
+ _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
+ assert(conf); /* bad config should be caught earlier */
+ _eglConfigToContextModesRec(conf, &vis);
+
+#if 0
+ surf->mesa_framebuffer = _mesa_create_framebuffer(&vis);
+ _mesa_add_soft_renderbuffers(surf->mesa_framebuffer,
+ GL_TRUE, /* color bufs */
+ vis.haveDepthBuffer,
+ vis.haveStencilBuffer,
+ vis.haveAccumBuffer,
+ GL_FALSE, /* alpha */
+ GL_FALSE /* aux */ );
+
+ /* set pbuffer/framebuffer size */
+ _mesa_resize_framebuffer(ctx, surf->mesa_framebuffer,
+ surf->Base.Width, surf->Base.Height);
+#endif
+ }
+
+ return surf->Base.Handle;
+}
+
+
+static EGLBoolean
+_eglDRIDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driSurface *fs = Lookup_driSurface(surface);
+ _eglRemoveSurface(&fs->Base);
+
+ fs->drawable.destroyDrawable(disp, fs->drawable.private);
+
+ if (fs->Base.IsBound) {
+ fs->Base.DeletePending = EGL_TRUE;
+ }
+ else {
+ free(fs);
+ }
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+_eglDRIDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *fc = Lookup_driContext(context);
+
+ _eglRemoveContext(&fc->Base);
+
+ fc->driContext.destroyContext(disp, 0, fc->driContext.private);
+
+ if (fc->Base.IsBound) {
+ fc->Base.DeletePending = EGL_TRUE;
+ } else {
+ free(fc);
+ }
+ return EGL_TRUE;
+}
+
+
+/**
+ * Create a drawing surface which can be directly displayed on a screen.
+ */
+static EGLSurface
+_eglDRICreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
+ const EGLint *attrib_list)
+{
+ _EGLConfig *config = _eglLookupConfig(drv, dpy, cfg);
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driSurface *surface;
+ EGLSurface surf;
+ GLvisual vis;
+
+ surface = (driSurface *) malloc(sizeof(*surface));
+ if (!surface) {
+ return EGL_NO_SURFACE;
+ }
+
+ /* init base class, error check, etc. */
+ surf = _eglInitScreenSurface(&surface->Base, drv, dpy, cfg, attrib_list);
+ if (surf == EGL_NO_SURFACE) {
+ free(surface);
+ return EGL_NO_SURFACE;
+ }
+
+ /* convert EGLConfig to GLvisual */
+ _eglConfigToContextModesRec(config, &vis);
+
+ /* Create a new drawable */
+ if (!disp->driScreen.createNewDrawable(disp, &vis, surf, &surface->drawable,
+ GLX_WINDOW_BIT, empty_attribute_list)) {
+ free(surface);
+ _eglRemoveSurface(&surface->Base);
+ return EGL_NO_SURFACE;
+ }
+ return surf;
+}
+
+
+/**
+ * Show the given surface on the named screen.
+ * If surface is EGL_NO_SURFACE, disable the screen's output.
+ */
+EGLBoolean
+_eglDRIShowSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface surface, EGLModeMESA m)
+{
+ driDisplay *display = Lookup_driDisplay(dpy);
+ driScreen *scrn = Lookup_driScreen(dpy, screen);
+ driSurface *surf = Lookup_driSurface(surface);
+ FILE *file;
+ char buffer[NAME_MAX];
+ _EGLMode *mode = _eglLookupMode(dpy, m);
+ int width, height, temp;
+
+ _eglQuerySurface(drv, dpy, surface, EGL_WIDTH, &width);
+ _eglQuerySurface(drv, dpy, surface, EGL_HEIGHT, &height);
+
+ if (!_eglShowSurfaceMESA(drv, dpy, screen, surface, m))
+ return EGL_FALSE;
+
+ snprintf(buffer, sizeof(buffer), "%s/graphics/%s/blank", sysfs, scrn->fb);
+
+ file = fopen(buffer, "r+");
+ if (!file) {
+err:
+ printf("kernel patch?? chown all fb sysfs attrib to allow write - %s\n", buffer);
+ _eglError(EGL_BAD_SURFACE, "eglShowSurfaceMESA");
+ return EGL_FALSE;
+ }
+ snprintf(buffer, sizeof(buffer), "%d", (m == EGL_NO_MODE_MESA ? VESA_POWERDOWN : VESA_VSYNC_SUSPEND));
+ fputs(buffer, file);
+ fclose(file);
+
+ if (m == EGL_NO_MODE_MESA)
+ return EGL_TRUE;
+
+ snprintf(buffer, sizeof(buffer), "%s/graphics/%s/mode", sysfs, scrn->fb);
+
+ file = fopen(buffer, "r+");
+ if (!file)
+ goto err;
+ fputs(mode->Name, file);
+ fclose(file);
+
+ snprintf(buffer, sizeof(buffer), "%s/graphics/%s/bits_per_pixel", sysfs, scrn->fb);
+
+ file = fopen(buffer, "r+");
+ if (!file)
+ goto err;
+ display->bpp = GET_CONFIG_ATTRIB(surf->Base.Config, EGL_BUFFER_SIZE);
+ display->cpp = display->bpp / 8;
+ snprintf(buffer, sizeof(buffer), "%d", display->bpp);
+ fputs(buffer, file);
+ fclose(file);
+
+ snprintf(buffer, sizeof(buffer), "%s/graphics/%s/blank", sysfs, scrn->fb);
+
+ file = fopen(buffer, "r+");
+ if (!file)
+ goto err;
+
+ snprintf(buffer, sizeof(buffer), "%d", VESA_NO_BLANKING);
+ fputs(buffer, file);
+ fclose(file);
+
+ snprintf(buffer, sizeof(buffer), "%s/graphics/%s/virtual_size", sysfs, scrn->fb);
+ file = fopen(buffer, "r+");
+ snprintf(buffer, sizeof(buffer), "%d,%d", width, height);
+ fputs(buffer, file);
+ rewind(file);
+ fgets(buffer, sizeof(buffer), file);
+ sscanf(buffer, "%d,%d", &display->virtualWidth, &display->virtualHeight);
+ fclose(file);
+
+ temp = display->virtualWidth;
+ switch (display->bpp / 8) {
+ case 1: temp = (display->virtualWidth + 127) & ~127; break;
+ case 2: temp = (display->virtualWidth + 31) & ~31; break;
+ case 3:
+ case 4: temp = (display->virtualWidth + 15) & ~15; break;
+ }
+ display->virtualWidth = temp;
+
+ if ((width != display->virtualWidth) || (height != display->virtualHeight))
+ goto err;
+
+ return EGL_TRUE;
+}
+
+
+/* If the backbuffer is on a videocard, this is extraordinarily slow!
+ */
+static EGLBoolean
+_eglDRISwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
+{
+ driSurface *drawable = Lookup_driSurface(draw);
+
+ if (!_eglSwapBuffers(drv, dpy, draw))
+ return EGL_FALSE;
+
+ drawable->drawable.swapBuffers(NULL, drawable->drawable.private);
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglDRIGetDisplayInfo( driDisplay *dpy) {
+ char path[ NAME_MAX ];
+ FILE *file;
+ int rc, mtrr;
+ unsigned int i;
+ drmMapType type;
+ drmMapFlags flags;
+ drm_handle_t handle, offset;
+ drmSize size;
+ drmSetVersion sv;
+ drm_magic_t magic;
+
+ snprintf( path, sizeof( path ), "%s/graphics/fb%d/device/device", sysfs, dpy->minor );
+ file = fopen( path, "r" );
+ fgets( path, sizeof( path ), file );
+ sscanf( path, "%x", &dpy->chipset );
+ fclose( file );
+
+ sprintf(path, DRM_DEV_NAME, DRM_DIR_NAME, dpy->minor);
+ if ( ( dpy->drmFD = open(path, O_RDWR, 0) ) < 0 ) {
+ fprintf( stderr, "[drm] drmOpen failed\n" );
+ return EGL_FALSE;
+ }
+
+ /* Set the interface version, asking for 1.2 */
+ sv.drm_di_major = 1;
+ sv.drm_di_minor = 2;
+ sv.drm_dd_major = -1;
+ if ((rc = drmSetInterfaceVersion(dpy->drmFD, &sv)))
+ return EGL_FALSE;
+
+ /* self authorize */
+ if (drmGetMagic(dpy->drmFD, &magic))
+ return EGL_FALSE;
+ if (drmAuthMagic(dpy->drmFD, magic))
+ return EGL_FALSE;
+
+ for ( i = 0;; i++ ) {
+ if ( ( rc = drmGetMap( dpy->drmFD, i, &offset, &size, &type, &flags, &handle, &mtrr ) ) != 0 )
+ break;
+ if ( type == DRM_FRAME_BUFFER ) {
+ if ( ( rc = drmMap( dpy->drmFD, offset, size, ( drmAddressPtr ) & dpy->pFB ) ) < 0 )
+ return EGL_FALSE;
+ dpy->fbSize = size;
+ break;
+ }
+ }
+ if ( !dpy->pFB )
+ return EGL_FALSE;
+
+ dpy->SAREASize = SAREA_MAX;
+
+ for ( i = 0;; i++ ) {
+ if ( drmGetMap( dpy->drmFD, i, &offset, &size, &type, &flags, &handle, &mtrr ) != 0 )
+ break;
+ if ( type == DRM_SHM ) {
+ if ( drmMap( dpy->drmFD, offset, size, ( drmAddressPtr ) ( &dpy->pSAREA ) ) < 0 ) {
+ fprintf( stderr, "[drm] drmMap failed\n" );
+ return 0;
+ }
+ break;
+ }
+ }
+ if ( !dpy->pSAREA )
+ return 0;
+
+ memset( dpy->pSAREA, 0, dpy->SAREASize );
+ fprintf( stderr, "[drm] mapped SAREA 0x%08lx to %p, size %d\n",
+ offset, dpy->pSAREA, dpy->SAREASize );
+ return EGL_TRUE;
+}
+
+
+ /* Return the DRI per screen structure */
+static __DRIscreen *__eglFindDRIScreen(__DRInativeDisplay *ndpy, int scrn)
+{
+ driDisplay *disp = (driDisplay *)ndpy;
+ return &disp->driScreen;
+}
+
+static GLboolean __eglCreateContextWithConfig(__DRInativeDisplay* ndpy, int screen, int configID, void* context, drm_context_t * hHWContext)
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreenPrivate *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreenPrivate *) pDRIScreen->private;
+ if (psp->fd) {
+ if (drmCreateContext(psp->fd, hHWContext)) {
+ fprintf(stderr, ">>> drmCreateContext failed\n");
+ return GL_FALSE;
+ }
+ *(void**)context = (void*) *hHWContext;
+ }
+#if 0
+ __DRIscreen *pDRIScreen;
+ __DRIscreenPrivate *psp;
+
+ pDRIScreen = __glXFindDRIScreen(dpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+
+ psp = (__DRIscreenPrivate *) pDRIScreen->private;
+
+ if (psp->fd) {
+ if (drmCreateContext(psp->fd, hHWContext)) {
+ fprintf(stderr, ">>> drmCreateContext failed\n");
+ return GL_FALSE;
+ }
+ *(void**)contextID = (void*) *hHWContext;
+ }
+#endif
+ return GL_TRUE;
+}
+
+static GLboolean __eglDestroyContext( __DRInativeDisplay * ndpy, int screen, __DRIid context )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreenPrivate *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreenPrivate *) pDRIScreen->private;
+ if (psp->fd)
+ drmDestroyContext(psp->fd, context);
+
+ return GL_TRUE;
+}
+
+static GLboolean __eglCreateDrawable( __DRInativeDisplay * ndpy, int screen, __DRIid drawable, drm_drawable_t * hHWDrawable )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreenPrivate *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreenPrivate *) pDRIScreen->private;
+ if (psp->fd) {
+ if (drmCreateDrawable(psp->fd, hHWDrawable)) {
+ fprintf(stderr, ">>> drmCreateDrawable failed\n");
+ return GL_FALSE;
+ }
+ }
+ return GL_TRUE;
+}
+
+static GLboolean __eglDestroyDrawable( __DRInativeDisplay * ndpy, int screen, __DRIid drawable )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreenPrivate *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreenPrivate *) pDRIScreen->private;
+ if (psp->fd)
+ drmDestroyDrawable(psp->fd, drawable);
+
+ return GL_TRUE;
+}
+
+static GLboolean __eglGetDrawableInfo(__DRInativeDisplay * ndpy, int screen, __DRIid drawable,
+ unsigned int* index, unsigned int* stamp,
+ int* X, int* Y, int* W, int* H,
+ int* numClipRects, drm_clip_rect_t ** pClipRects,
+ int* backX, int* backY,
+ int* numBackClipRects, drm_clip_rect_t ** pBackClipRects )
+{
+ driSurface *surf = Lookup_driSurface(drawable);
+
+ *X = 0;
+ *Y = 0;
+ *W = surf->Base.Width;
+ *H = surf->Base.Height;
+
+ *numClipRects = 1;
+ *pClipRects = malloc(sizeof(**pClipRects));
+ **pClipRects = (drm_clip_rect_t){0, 0, surf->Base.Width, surf->Base.Height};
+
+#if 0
+ GLXDrawable drawable = (GLXDrawable) draw;
+ drm_clip_rect_t * cliprect;
+ Display* display = (Display*)dpy;
+ __DRIcontextPrivate *pcp = (__DRIcontextPrivate *)CurrentContext->driContext.private;
+ if (drawable == 0) {
+ return GL_FALSE;
+ }
+
+ cliprect = (drm_clip_rect_t*) _mesa_malloc(sizeof(drm_clip_rect_t));
+ cliprect->x1 = drawable->x;
+ cliprect->y1 = drawable->y;
+ cliprect->x2 = drawable->x + drawable->w;
+ cliprect->y2 = drawable->y + drawable->h;
+
+ /* the drawable index is by client id */
+ *index = display->clientID;
+
+ *stamp = pcp->driScreenPriv->pSAREA->drawableTable[display->clientID].stamp;
+ *x = drawable->x;
+ *y = drawable->y;
+ *width = drawable->w;
+ *height = drawable->h;
+ *numClipRects = 1;
+ *pClipRects = cliprect;
+
+ *backX = drawable->x;
+ *backY = drawable->y;
+ *numBackClipRects = 0;
+ *pBackClipRects = 0;
+#endif
+ return GL_TRUE;
+}
+
+/**
+ * Implement \c __DRIinterfaceMethods::getProcAddress.
+ */
+static __DRIfuncPtr get_proc_address( const char * proc_name )
+{
+#if 0
+ if (strcmp( proc_name, "glxEnableExtension" ) == 0) {
+ return (__DRIfuncPtr) __glXScrEnableExtension;
+ }
+#endif
+ return NULL;
+}
+
+
+/**
+ * Destroy a linked list of \c __GLcontextModes structures created by
+ * \c _gl_context_modes_create.
+ *
+ * \param modes Linked list of structures to be destroyed. All structres
+ * in the list will be freed.
+ */
+void
+__egl_context_modes_destroy( __GLcontextModes * modes )
+{
+ while ( modes != NULL ) {
+ __GLcontextModes * const next = modes->next;
+
+ free( modes );
+ modes = next;
+ }
+}
+
+
+/**
+ * Allocate a linked list of \c __GLcontextModes structures. The fields of
+ * each structure will be initialized to "reasonable" default values. In
+ * most cases this is the default value defined by table 3.4 of the GLX
+ * 1.3 specification. This means that most values are either initialized to
+ * zero or \c GLX_DONT_CARE (which is -1). As support for additional
+ * extensions is added, the new values will be initialized to appropriate
+ * values from the extension specification.
+ *
+ * \param count Number of structures to allocate.
+ * \param minimum_size Minimum size of a structure to allocate. This allows
+ * for differences in the version of the
+ * \c __GLcontextModes stucture used in libGL and in a
+ * DRI-based driver.
+ * \returns A pointer to the first element in a linked list of \c count
+ * stuctures on success, or \c NULL on failure.
+ *
+ * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
+ * The fundamental assumption is that if the \c minimum_size
+ * specified by the driver and the size of the \c __GLcontextModes
+ * structure in libGL is the same, then the meaning of each byte in
+ * the structure is the same in both places. \b Be \b careful!
+ * Basically this means that fields have to be added in libGL and
+ * then propagated to drivers. Drivers should \b never arbitrarilly
+ * extend the \c __GLcontextModes data-structure.
+ */
+__GLcontextModes *
+__egl_context_modes_create( unsigned count, size_t minimum_size )
+{
+ const size_t size = (minimum_size > sizeof( __GLcontextModes ))
+ ? minimum_size : sizeof( __GLcontextModes );
+ __GLcontextModes * base = NULL;
+ __GLcontextModes ** next;
+ unsigned i;
+
+ next = & base;
+ for ( i = 0 ; i < count ; i++ ) {
+ *next = (__GLcontextModes *) malloc( size );
+ if ( *next == NULL ) {
+ __egl_context_modes_destroy( base );
+ base = NULL;
+ break;
+ }
+
+ (void) memset( *next, 0, size );
+ (*next)->visualID = GLX_DONT_CARE;
+ (*next)->visualType = GLX_DONT_CARE;
+ (*next)->visualRating = GLX_NONE;
+ (*next)->transparentPixel = GLX_NONE;
+ (*next)->transparentRed = GLX_DONT_CARE;
+ (*next)->transparentGreen = GLX_DONT_CARE;
+ (*next)->transparentBlue = GLX_DONT_CARE;
+ (*next)->transparentAlpha = GLX_DONT_CARE;
+ (*next)->transparentIndex = GLX_DONT_CARE;
+ (*next)->xRenderable = GLX_DONT_CARE;
+ (*next)->fbconfigID = GLX_DONT_CARE;
+ (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
+
+ next = & ((*next)->next);
+ }
+
+ return base;
+}
+
+
+GLboolean __eglWindowExists(__DRInativeDisplay *dpy, __DRIid draw)
+{
+ return EGL_TRUE;
+}
+
+
+/**
+ * Get the unadjusted system time (UST). Currently, the UST is measured in
+ * microseconds since Epoc. The actual resolution of the UST may vary from
+ * system to system, and the units may vary from release to release.
+ * Drivers should not call this function directly. They should instead use
+ * \c glXGetProcAddress to obtain a pointer to the function.
+ *
+ * \param ust Location to store the 64-bit UST
+ * \returns Zero on success or a negative errno value on failure.
+ *
+ * \sa glXGetProcAddress, PFNGLXGETUSTPROC
+ *
+ * \since Internal API version 20030317.
+ */
+int __eglGetUST( 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;
+ }
+}
+
+/**
+ * Determine the refresh rate of the specified drawable and display.
+ *
+ * \param dpy Display whose refresh rate is to be determined.
+ * \param drawable Drawable whose refresh rate is to be determined.
+ * \param numerator Numerator of the refresh rate.
+ * \param demoninator Denominator of the refresh rate.
+ * \return If the refresh rate for the specified display and drawable could
+ * be calculated, True is returned. Otherwise False is returned.
+ *
+ * \note This function is implemented entirely client-side. A lot of other
+ * functionality is required to export GLX_OML_sync_control, so on
+ * XFree86 this function can be called for direct-rendering contexts
+ * when GLX_OML_sync_control appears in the client extension string.
+ */
+GLboolean __eglGetMSCRate(__DRInativeDisplay * dpy, __DRIid drawable, int32_t * numerator, int32_t * denominator)
+{
+ return EGL_TRUE;
+}
+
+/**
+ * Table of functions exported by the loader to the driver.
+ */
+static const __DRIinterfaceMethods interface_methods = {
+ get_proc_address,
+
+ __egl_context_modes_create,
+ __egl_context_modes_destroy,
+
+ __eglFindDRIScreen,
+ __eglWindowExists,
+
+ __eglCreateContextWithConfig,
+ __eglDestroyContext,
+
+ __eglCreateDrawable,
+ __eglDestroyDrawable,
+ __eglGetDrawableInfo,
+
+ __eglGetUST,
+ __eglGetMSCRate,
+};
+
+
+int __glXGetInternalVersion(void)
+{
+ return 20050725;
+}
+
+static const char createNewScreenName[] = "__driCreateNewScreen_20050727";
+
+EGLBoolean
+_eglDRICreateDisplay( driDisplay *dpy, __DRIframebuffer *framebuffer) {
+ PFNCREATENEWSCREENFUNC createNewScreen;
+ int api_ver = __glXGetInternalVersion();
+ __DRIversion ddx_version;
+ __DRIversion dri_version;
+ __DRIversion drm_version;
+ drmVersionPtr version;
+
+ version = drmGetVersion( dpy->drmFD );
+ if ( version ) {
+ drm_version.major = version->version_major;
+ drm_version.minor = version->version_minor;
+ drm_version.patch = version->version_patchlevel;
+ drmFreeVersion( version );
+ } else {
+ drm_version.major = -1;
+ drm_version.minor = -1;
+ drm_version.patch = -1;
+ }
+
+ /*
+ * Get device name (like "tdfx") and the ddx version numbers.
+ * We'll check the version in each DRI driver's "createScreen"
+ * function.
+ */
+ ddx_version.major = 4;
+ ddx_version.minor = 0;
+ ddx_version.patch = 0;
+
+ /*
+ * Get the DRI X extension version.
+ */
+ dri_version.major = 4;
+ dri_version.minor = 0;
+ dri_version.patch = 0;
+
+ createNewScreen = ( PFNCREATENEWSCREENFUNC ) dlsym( dpy->Base.Driver->LibHandle, createNewScreenName );
+ if ( !createNewScreen ) {
+ fprintf( stderr, "Couldn't find %s in CallCreateNewScreen\n", createNewScreenName );
+ return EGL_FALSE;
+ }
+
+ dpy->driScreen.private = createNewScreen( dpy, 0, &dpy->driScreen, NULL,
+ &ddx_version, &dri_version,
+ &drm_version, framebuffer,
+ dpy->pSAREA, dpy->drmFD,
+ api_ver,
+ & interface_methods,
+ ( __GLcontextModes ** ) & dpy->driver_modes );
+ if (!dpy->driScreen.private)
+ return EGL_FALSE;
+
+ DRM_UNLOCK( dpy->drmFD, dpy->pSAREA, dpy->serverContext );
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglDRICreateScreen( driDisplay *dpy) {
+ char c, *buffer, path[ NAME_MAX ];
+ unsigned int i, x, y, r;
+ int fd;
+ FILE *file;
+ driScreen *s;
+ _EGLScreen *scrn;
+
+ /* Create a screen */
+ if ( !( s = ( driScreen * ) calloc( 1, sizeof( *s ) ) ) )
+ return EGL_FALSE;
+
+ snprintf( s->fb, NAME_MAX, "fb%d", dpy->minor );
+ scrn = &s->Base;
+ _eglInitScreen( scrn );
+ _eglAddScreen( &dpy->Base, scrn );
+
+ snprintf( path, sizeof( path ), "%s/graphics/%s/modes", sysfs, s->fb );
+ file = fopen( path, "r" );
+ while ( fgets( path, sizeof( path ), file ) ) {
+ path[ strlen( path ) - 1 ] = '\0'; /* strip off \n from sysfs */
+ sscanf( path, "%c:%ux%u-%u", &c, &x, &y, &r );
+ _eglAddMode( scrn, x, y, r * 1000, path );
+ }
+ fclose( file );
+
+ /* cmap attribute uses 256 lines of 16 bytes */
+ if ( !( buffer = malloc( 256 * 16 ) ) )
+ return EGL_FALSE;
+
+ /* cmap attribute uses 256 lines of 16 bytes */
+ for ( i = 0; i < 256; i++ )
+ sprintf( &buffer[ i * 16 ], "%02x%c%4x%4x%4x\n",
+ i, ' ', 256 * i, 256 * i, 256 * i );
+
+ snprintf( path, sizeof( path ), "%s/graphics/%s/color_map", sysfs, s->fb );
+ if ( !( fd = open( path, O_RDWR ) ) )
+ return EGL_FALSE;
+ write( fd, buffer, 256 * 16 );
+ close( fd );
+
+ free( buffer );
+
+ return EGL_TRUE;
+}
+
+EGLBoolean
+_eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ driDisplay *display;
+
+ /* Switch display structure to one with our private fields */
+ display = calloc(1, sizeof(*display));
+ display->Base = *disp;
+ _eglHashInsert(_eglGlobal.Displays, disp->Handle, display);
+ free(disp);
+
+ *major = 1;
+ *minor = 0;
+
+ sscanf(&disp->Name[1], "%d", &display->minor);
+
+ drv->Initialized = EGL_TRUE;
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+_eglDRITerminate(_EGLDriver *drv, EGLDisplay dpy)
+{
+ driDisplay *display = Lookup_driDisplay(dpy);
+ _eglCleanupDisplay(&display->Base);
+ free(display);
+ free(drv);
+ return EGL_TRUE;
+}
+
+
+void
+_eglDRIInitDriverFallbacks(_EGLDriver *drv)
+{
+ _eglInitDriverFallbacks(drv);
+
+ drv->Initialize = _eglDRIInitialize;
+ drv->Terminate = _eglDRITerminate;
+ drv->CreateContext = _eglDRICreateContext;
+ drv->MakeCurrent = _eglDRIMakeCurrent;
+ drv->CreateWindowSurface = _eglDRICreateWindowSurface;
+ drv->CreatePixmapSurface = _eglDRICreatePixmapSurface;
+ drv->CreatePbufferSurface = _eglDRICreatePbufferSurface;
+ drv->DestroySurface = _eglDRIDestroySurface;
+ drv->DestroyContext = _eglDRIDestroyContext;
+ drv->CreateScreenSurfaceMESA = _eglDRICreateScreenSurfaceMESA;
+ drv->ShowSurfaceMESA = _eglDRIShowSurfaceMESA;
+ drv->SwapBuffers = _eglDRISwapBuffers;
+
+ /* enable supported extensions */
+ drv->MESA_screen_surface = EGL_TRUE;
+ drv->MESA_copy_context = EGL_TRUE;
+
+}
diff --git a/src/egl/drivers/dri/egldri.h b/src/egl/drivers/dri/egldri.h
new file mode 100644
index 000000000..4573c50ea
--- /dev/null
+++ b/src/egl/drivers/dri/egldri.h
@@ -0,0 +1,113 @@
+#ifndef EGLDRI_INCLUDED
+#define EGLDRI_INCLUDED
+
+#include "egldisplay.h"
+#include "eglscreen.h"
+#include "eglsurface.h"
+#include "eglcontext.h"
+#include "mtypes.h"
+#include "dri_util.h"
+#include "drm_sarea.h"
+
+/**
+ * dri display-specific driver class derived from _EGLDisplay
+ */
+typedef struct dri_display
+{
+ _EGLDisplay Base; /* base class/object */
+ void *pFB;
+ int drmFD; /**< \brief DRM device file descriptor */
+ int minor;
+ unsigned long hFrameBuffer;
+
+ int virtualWidth;
+ int virtualHeight;
+ int fbSize;
+ int bpp;
+ int cpp;
+ int isPCI;
+ int SAREASize;
+ drm_sarea_t *pSAREA;
+ unsigned int serverContext; /**< \brief DRM context only active on server */
+ unsigned long FBStart; /**< \brief physical address of the framebuffer */
+ void *driverClientMsg;
+ int driverClientMsgSize;
+ int chipset;
+ void *driverPrivate;
+ drm_magic_t magic;
+
+ __GLcontextModes *driver_modes;
+ __DRIscreen driScreen;
+
+} driDisplay;
+
+/**
+ * dri driver-specific screen class derived from _EGLScreen
+ */
+typedef struct dri_screen
+{
+ _EGLScreen Base;
+ char fb[NAME_MAX];
+} driScreen;
+
+
+/**
+ * dri driver-specific surface class derived from _EGLSurface
+ */
+typedef struct dri_surface
+{
+ _EGLSurface Base; /* base class/object */
+ __DRIdrawable drawable;
+} driSurface;
+
+
+/**
+ * dri driver-specific context class derived from _EGLContext
+ */
+typedef struct dri_context
+{
+ _EGLContext Base; /* base class/object */
+ __DRIcontext driContext; /**< \brief context dependent methods */
+} driContext;
+
+
+
+static inline driDisplay *
+Lookup_driDisplay(EGLDisplay dpy)
+{
+ _EGLDisplay *d = _eglLookupDisplay(dpy);
+ return (driDisplay *) d;
+}
+
+
+static inline driScreen *
+Lookup_driScreen(EGLDisplay dpy, EGLScreenMESA screen)
+{
+ _EGLScreen *s = _eglLookupScreen(dpy, screen);
+ return (driScreen *) s;
+}
+
+
+static inline driContext *
+Lookup_driContext(EGLContext ctx)
+{
+ _EGLContext *c = _eglLookupContext(ctx);
+ return (driContext *) c;
+}
+
+
+static inline driSurface *
+Lookup_driSurface(EGLSurface surf)
+{
+ _EGLSurface *s = _eglLookupSurface(surf);
+ return (driSurface *) s;
+}
+
+extern void _eglDRIInitDriverFallbacks(_EGLDriver *drv);
+extern EGLBoolean _eglDRIShowSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLSurface surface, EGLModeMESA m);
+extern EGLBoolean _eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor);
+extern EGLBoolean _eglDRIGetDisplayInfo(driDisplay *dpy);
+extern EGLBoolean _eglDRICreateDisplay(driDisplay *dpy, __DRIframebuffer *framebuffer);
+extern EGLBoolean _eglDRICreateScreen(driDisplay *dpy);
+
+#endif /* EGLDRI_INCLUDED */
diff --git a/src/egl/main/Makefile b/src/egl/main/Makefile
new file mode 100644
index 000000000..4bb1ffc9e
--- /dev/null
+++ b/src/egl/main/Makefile
@@ -0,0 +1,64 @@
+# src/egl/main/Makefile
+
+TOP = ../../..
+include $(TOP)/configs/current
+
+
+INCLUDE_DIRS = -I$(TOP)/include -I$(TOP)/src/mesa/glapi
+
+HEADERS = \
+ eglconfig.h \
+ eglcontext.h \
+ egldisplay.h \
+ egldriver.h \
+ eglglobals.h \
+ eglhash.h \
+ eglmode.h \
+ eglscreen.h \
+ eglsurface.h
+
+SOURCES = \
+ eglapi.c \
+ eglconfig.c \
+ eglcontext.c \
+ egldisplay.c \
+ egldriver.c \
+ eglglobals.c \
+ eglhash.c \
+ eglmode.c \
+ eglscreen.c \
+ eglsurface.c
+
+OBJECTS = $(SOURCES:.c=.o)
+
+
+.c.o:
+ $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
+
+
+
+default: depend library
+
+
+# EGL Library
+library: $(LIB_DIR)/libEGL.so
+
+$(LIB_DIR)/libEGL.so: $(OBJECTS)
+ $(TOP)/bin/mklib -o EGL -major 1 -minor 0 \
+ -install $(LIB_DIR) -ldl $(OBJECTS)
+
+
+
+clean:
+ rm -f *.o *.so*
+ rm -f core.*
+
+
+depend: $(SOURCES) $(HEADERS)
+ @ echo "running $(MKDEP)"
+ @ touch depend
+ $(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
+ $(SOURCES) $(HEADERS) > /dev/null
+
+include depend
+# DO NOT DELETE
diff --git a/src/egl/main/eglapi.c b/src/egl/main/eglapi.c
new file mode 100644
index 000000000..87160c7f4
--- /dev/null
+++ b/src/egl/main/eglapi.c
@@ -0,0 +1,497 @@
+/**
+ * Public EGL API entrypoints
+ *
+ * Generally, we use the EGLDisplay parameter as a key to lookup the
+ * appropriate device driver handle, then jump though the driver's
+ * dispatch table to handle the function.
+ *
+ * That allows us the option of supporting multiple, simultaneous,
+ * heterogeneous hardware devices in the future.
+ *
+ * The EGLDisplay, EGLConfig, EGLContext and EGLSurface types are
+ * opaque handles implemented with 32-bit unsigned integers.
+ * It's up to the driver function or fallback function to look up the
+ * handle and get an object.
+ * By using opaque handles, we leave open the possibility of having
+ * indirect rendering in the future, like GLX.
+ *
+ *
+ * Notes on naming conventions:
+ *
+ * eglFooBar - public EGL function
+ * EGL_FOO_BAR - public EGL token
+ * EGLDatatype - public EGL datatype
+ *
+ * _eglFooBar - private EGL function
+ * _EGLDatatype - private EGL datatype, typedef'd struct
+ * _egl_struct - private EGL struct, non-typedef'd
+ *
+ */
+
+
+
+#include <stdio.h>
+#include <string.h>
+/**#include "glapi.h"**/
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "egltypedefs.h"
+#include "eglglobals.h"
+#include "egldriver.h"
+#include "eglsurface.h"
+
+
+
+/**
+ * NOTE: displayName is treated as a string in _eglChooseDriver()!!!
+ * This will probably change!
+ * See _eglChooseDriver() for details!
+ */
+EGLDisplay APIENTRY
+eglGetDisplay(NativeDisplayType displayName)
+{
+ _EGLDisplay *dpy;
+ _eglInitGlobals();
+ dpy = _eglNewDisplay(displayName);
+ if (dpy)
+ return dpy->Handle;
+ else
+ return EGL_NO_DISPLAY;
+}
+
+
+EGLBoolean APIENTRY
+eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ if (dpy) {
+ _EGLDriver *drv = _eglChooseDriver(dpy);
+ if (drv)
+ return drv->Initialize(drv, dpy, major, minor);
+ }
+ return EGL_FALSE;
+}
+
+
+EGLBoolean APIENTRY
+eglTerminate(EGLDisplay dpy)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return _eglCloseDriver(drv, dpy);
+ else
+ return EGL_FALSE;
+}
+
+
+const char * APIENTRY
+eglQueryString(EGLDisplay dpy, EGLint name)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->QueryString(drv, dpy, name);
+ else
+ return NULL;
+}
+
+
+EGLBoolean APIENTRY
+eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ /* XXX check drv for null in remaining functions */
+ return drv->GetConfigs(drv, dpy, configs, config_size, num_config);
+}
+
+
+EGLBoolean APIENTRY
+eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->ChooseConfig(drv, dpy, attrib_list, configs, config_size, num_config);
+}
+
+
+EGLBoolean APIENTRY
+eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->GetConfigAttrib(drv, dpy, config, attribute, value);
+}
+
+
+EGLContext APIENTRY
+eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CreateContext(drv, dpy, config, share_list, attrib_list);
+}
+
+
+EGLBoolean APIENTRY
+eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->DestroyContext(drv, dpy, ctx);
+}
+
+
+EGLBoolean APIENTRY
+eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->MakeCurrent(drv, dpy, draw, read, ctx);
+}
+
+
+EGLBoolean APIENTRY
+eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QueryContext(drv, dpy, ctx, attribute, value);
+}
+
+
+EGLSurface APIENTRY
+eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CreateWindowSurface(drv, dpy, config, window, attrib_list);
+}
+
+
+EGLSurface APIENTRY
+eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CreatePixmapSurface(drv, dpy, config, pixmap, attrib_list);
+}
+
+
+EGLSurface APIENTRY
+eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CreatePbufferSurface(drv, dpy, config, attrib_list);
+}
+
+
+EGLBoolean APIENTRY
+eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->DestroySurface(drv, dpy, surface);
+}
+
+
+EGLBoolean APIENTRY
+eglQuerySurface(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QuerySurface(drv, dpy, surface, attribute, value);
+}
+
+
+EGLBoolean APIENTRY
+eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->SurfaceAttrib(drv, dpy, surface, attribute, value);
+}
+
+
+EGLBoolean APIENTRY
+eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->BindTexImage(drv, dpy, surface, buffer);
+}
+
+
+EGLBoolean APIENTRY
+eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->ReleaseTexImage(drv, dpy, surface, buffer);
+}
+
+
+EGLBoolean APIENTRY
+eglSwapInterval(EGLDisplay dpy, EGLint interval)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->SwapInterval(drv, dpy, interval);
+}
+
+
+EGLBoolean APIENTRY
+eglSwapBuffers(EGLDisplay dpy, EGLSurface draw)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->SwapBuffers(drv, dpy, draw);
+}
+
+
+EGLBoolean APIENTRY
+eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, NativePixmapType target)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CopyBuffers(drv, dpy, surface, target);
+}
+
+
+EGLBoolean APIENTRY
+eglWaitGL(void)
+{
+ EGLDisplay dpy = eglGetCurrentDisplay();
+ if (dpy != EGL_NO_DISPLAY) {
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->WaitGL(drv, dpy);
+ }
+ else
+ return EGL_FALSE;
+}
+
+
+EGLBoolean APIENTRY
+eglWaitNative(EGLint engine)
+{
+ EGLDisplay dpy = eglGetCurrentDisplay();
+ if (dpy != EGL_NO_DISPLAY) {
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->WaitNative(drv, dpy, engine);
+ }
+ else
+ return EGL_FALSE;
+}
+
+
+EGLDisplay APIENTRY
+eglGetCurrentDisplay(void)
+{
+ _EGLDisplay *dpy = _eglGetCurrentDisplay();
+ if (dpy)
+ return dpy->Handle;
+ else
+ return EGL_NO_DISPLAY;
+}
+
+
+EGLContext APIENTRY
+eglGetCurrentContext(void)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ if (ctx)
+ return ctx->Handle;
+ else
+ return EGL_NO_CONTEXT;
+}
+
+
+EGLSurface APIENTRY
+eglGetCurrentSurface(EGLint readdraw)
+{
+ _EGLSurface *s = _eglGetCurrentSurface(readdraw);
+ if (s)
+ return s->Handle;
+ else
+ return EGL_NO_SURFACE;
+}
+
+
+EGLint APIENTRY
+eglGetError(void)
+{
+ EGLint e = _eglGlobal.LastError;
+ _eglGlobal.LastError = EGL_SUCCESS;
+ return e;
+}
+
+
+void (* APIENTRY eglGetProcAddress(const char *procname))()
+{
+ typedef void (*genericFunc)();
+ struct name_function {
+ const char *name;
+ _EGLProc function;
+ };
+ static struct name_function egl_functions[] = {
+ /* alphabetical order */
+ { "eglBindTexImage", (_EGLProc) eglBindTexImage },
+ { "eglChooseConfig", (_EGLProc) eglChooseConfig },
+ { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
+ { "eglCreateContext", (_EGLProc) eglCreateContext },
+ { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
+ { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
+ { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
+ { "eglDestroyContext", (_EGLProc) eglDestroyContext },
+ { "eglDestroySurface", (_EGLProc) eglDestroySurface },
+ { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
+ { "eglGetConfigs", (_EGLProc) eglGetConfigs },
+ { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
+ { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
+ { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
+ { "eglGetDisplay", (_EGLProc) eglGetDisplay },
+ { "eglGetError", (_EGLProc) eglGetError },
+ { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
+ { "eglInitialize", (_EGLProc) eglInitialize },
+ { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
+ { "eglQueryContext", (_EGLProc) eglQueryContext },
+ { "eglQueryString", (_EGLProc) eglQueryString },
+ { "eglQuerySurface", (_EGLProc) eglQuerySurface },
+ { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
+ { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
+ { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
+ { "eglSwapInterval", (_EGLProc) eglSwapInterval },
+ { "eglTerminate", (_EGLProc) eglTerminate },
+ { "eglWaitGL", (_EGLProc) eglWaitGL },
+ { "eglWaitNative", (_EGLProc) eglWaitNative },
+ /* Extensions */
+ { "eglChooseModeMESA", (_EGLProc) eglChooseModeMESA },
+ { "eglGetModesMESA", (_EGLProc) eglGetModesMESA },
+ { "eglGetModeAttribMESA", (_EGLProc) eglGetModeAttribMESA },
+ { "eglCopyContextMESA", (_EGLProc) eglCopyContextMESA },
+ { "eglGetScreensMESA", (_EGLProc) eglGetScreensMESA },
+ { "eglCreateScreenSurfaceMESA", (_EGLProc) eglCreateScreenSurfaceMESA },
+ { "eglShowSurfaceMESA", (_EGLProc) eglShowSurfaceMESA },
+ { "eglScreenPositionMESA", (_EGLProc) eglScreenPositionMESA },
+ { "eglQueryScreenMESA", (_EGLProc) eglQueryScreenMESA },
+ { "eglQueryScreenSurfaceMESA", (_EGLProc) eglQueryScreenSurfaceMESA },
+ { "eglQueryScreenModeMESA", (_EGLProc) eglQueryScreenModeMESA },
+ { "eglQueryModeStringMESA", (_EGLProc) eglQueryModeStringMESA },
+ { NULL, NULL }
+ };
+ EGLint i;
+ for (i = 0; egl_functions[i].name; i++) {
+ if (strcmp(egl_functions[i].name, procname) == 0) {
+ return (genericFunc) egl_functions[i].function;
+ }
+ }
+#if 0
+ /* XXX enable this code someday */
+ return (genericFunc) _glapi_get_proc_address(procname);
+#else
+ return NULL;
+#endif
+}
+
+
+/*
+ * EGL_MESA_screen extension
+ */
+
+EGLBoolean APIENTRY
+eglChooseModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->ChooseModeMESA(drv, dpy, screen, attrib_list, modes, modes_size, num_modes);
+ else
+ return EGL_FALSE;
+}
+
+
+EGLBoolean APIENTRY
+eglGetModesMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *modes, EGLint mode_size, EGLint *num_mode)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->GetModesMESA(drv, dpy, screen, modes, mode_size, num_mode);
+ else
+ return EGL_FALSE;
+}
+
+
+EGLBoolean APIENTRY
+eglGetModeAttribMESA(EGLDisplay dpy, EGLModeMESA mode, EGLint attribute, EGLint *value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->GetModeAttribMESA(drv, dpy, mode, attribute, value);
+ else
+ return EGL_FALSE;
+}
+
+
+EGLBoolean APIENTRY
+eglCopyContextMESA(EGLDisplay dpy, EGLContext source, EGLContext dest, EGLint mask)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->CopyContextMESA(drv, dpy, source, dest, mask);
+ else
+ return EGL_FALSE;
+}
+
+
+EGLBoolean
+eglGetScreensMESA(EGLDisplay dpy, EGLScreenMESA *screens, EGLint max_screens, EGLint *num_screens)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ if (drv)
+ return drv->GetScreensMESA(drv, dpy, screens, max_screens, num_screens);
+ else
+ return EGL_FALSE;
+}
+
+
+EGLSurface
+eglCreateScreenSurfaceMESA(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->CreateScreenSurfaceMESA(drv, dpy, config, attrib_list);
+}
+
+
+EGLBoolean
+eglShowSurfaceMESA(EGLDisplay dpy, EGLint screen, EGLSurface surface, EGLModeMESA mode)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->ShowSurfaceMESA(drv, dpy, screen, surface, mode);
+}
+
+
+EGLBoolean
+eglScreenPositionMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLint x, EGLint y)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->ScreenPositionMESA(drv, dpy, screen, x, y);
+}
+
+
+EGLBoolean
+eglQueryScreenMESA( EGLDisplay dpy, EGLScreenMESA screen, EGLint attribute, EGLint *value)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QueryScreenMESA(drv, dpy, screen, attribute, value);
+}
+
+
+EGLBoolean
+eglQueryScreenSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLSurface *surface)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QueryScreenSurfaceMESA(drv, dpy, screen, surface);
+}
+
+
+EGLBoolean
+eglQueryScreenModeMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *mode)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QueryScreenModeMESA(drv, dpy, screen, mode);
+}
+
+
+const char *
+eglQueryModeStringMESA(EGLDisplay dpy, EGLModeMESA mode)
+{
+ _EGLDriver *drv = _eglLookupDriver(dpy);
+ return drv->QueryModeStringMESA(drv, dpy, mode);
+}
+
+
+
+
+
diff --git a/src/egl/main/eglconfig.c b/src/egl/main/eglconfig.c
new file mode 100644
index 000000000..9f3d3ad27
--- /dev/null
+++ b/src/egl/main/eglconfig.c
@@ -0,0 +1,577 @@
+/**
+ * EGL Configuration (pixel format) functions.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "eglconfig.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+
+
+#define MIN2(A, B) (((A) < (B)) ? (A) : (B))
+
+
+/**
+ * Convert an _EGLConfig to a __GLcontextModes object.
+ * NOTE: This routine may be incomplete - we're only making sure that
+ * the fields needed by Mesa (for _mesa_create_context/framebuffer) are
+ * set correctly.
+ */
+void
+_eglConfigToContextModesRec(const _EGLConfig *config, __GLcontextModes *mode)
+{
+ memset(mode, 0, sizeof(*mode));
+
+ mode->rgbMode = GL_TRUE; /* no color index */
+ mode->colorIndexMode = GL_FALSE;
+ mode->doubleBufferMode = GL_TRUE; /* always DB for now */
+ mode->stereoMode = GL_FALSE;
+
+ mode->redBits = GET_CONFIG_ATTRIB(config, EGL_RED_SIZE);
+ mode->greenBits = GET_CONFIG_ATTRIB(config, EGL_GREEN_SIZE);
+ mode->blueBits = GET_CONFIG_ATTRIB(config, EGL_BLUE_SIZE);
+ mode->alphaBits = GET_CONFIG_ATTRIB(config, EGL_ALPHA_SIZE);
+ mode->rgbBits = GET_CONFIG_ATTRIB(config, EGL_BUFFER_SIZE);
+
+ /* no rgba masks - fix? */
+
+ mode->depthBits = GET_CONFIG_ATTRIB(config, EGL_DEPTH_SIZE);
+ mode->haveDepthBuffer = mode->depthBits > 0;
+
+ mode->stencilBits = GET_CONFIG_ATTRIB(config, EGL_STENCIL_SIZE);
+ mode->haveStencilBuffer = mode->stencilBits > 0;
+
+ /* no accum */
+
+ mode->level = GET_CONFIG_ATTRIB(config, EGL_LEVEL);
+ mode->samples = GET_CONFIG_ATTRIB(config, EGL_SAMPLES);
+ mode->sampleBuffers = GET_CONFIG_ATTRIB(config, EGL_SAMPLE_BUFFERS);
+
+ /* surface type - not really needed */
+ mode->visualType = GLX_TRUE_COLOR;
+ mode->renderType = GLX_RGBA_BIT;
+}
+
+
+void
+_eglSetConfigAttrib(_EGLConfig *config, EGLint attr, EGLint val)
+{
+ assert(attr >= FIRST_ATTRIB);
+ assert(attr < FIRST_ATTRIB + MAX_ATTRIBS);
+ config->Attrib[attr - FIRST_ATTRIB] = val;
+}
+
+
+/**
+ * Init the given _EGLconfig to default values.
+ * \param id the configuration's ID.
+ */
+void
+_eglInitConfig(_EGLConfig *config, EGLint id)
+{
+ memset(config, 0, sizeof(*config));
+ config->Handle = id;
+ _eglSetConfigAttrib(config, EGL_CONFIG_ID, id);
+ _eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGB, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGBA, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_CONFIG_CAVEAT, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_NATIVE_RENDERABLE, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_NATIVE_VISUAL_TYPE, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_MIN_SWAP_INTERVAL, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_MAX_SWAP_INTERVAL, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_SURFACE_TYPE,
+ EGL_SCREEN_BIT_MESA | EGL_PBUFFER_BIT |
+ EGL_PIXMAP_BIT | EGL_WINDOW_BIT);
+ _eglSetConfigAttrib(config, EGL_TRANSPARENT_TYPE, EGL_NONE);
+ _eglSetConfigAttrib(config, EGL_TRANSPARENT_RED_VALUE, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_TRANSPARENT_GREEN_VALUE, EGL_DONT_CARE);
+ _eglSetConfigAttrib(config, EGL_TRANSPARENT_BLUE_VALUE, EGL_DONT_CARE);
+}
+
+
+/**
+ * Given an EGLConfig handle, return the corresponding _EGLConfig object.
+ */
+_EGLConfig *
+_eglLookupConfig(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config)
+{
+ EGLint i;
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ for (i = 0; i < disp->NumConfigs; i++) {
+ if (disp->Configs[i].Handle == config) {
+ return disp->Configs + i;
+ }
+ }
+ return NULL;
+}
+
+
+/**
+ * Add the given _EGLConifg to the given display.
+ */
+_EGLConfig *
+_eglAddConfig(_EGLDisplay *display, const _EGLConfig *config)
+{
+ _EGLConfig *newConfigs;
+ EGLint n;
+
+ n = display->NumConfigs;
+
+ newConfigs = (_EGLConfig *) realloc(display->Configs,
+ (n + 1) * sizeof(_EGLConfig));
+ if (newConfigs) {
+ display->Configs = newConfigs;
+ display->Configs[n] = *config; /* copy struct */
+ display->Configs[n].Handle = n;
+ display->NumConfigs++;
+ return display->Configs + n;
+ }
+ else {
+ return NULL;
+ }
+}
+
+
+/**
+ * Parse the attrib_list to fill in the fields of the given _egl_config
+ * Return EGL_FALSE if any errors, EGL_TRUE otherwise.
+ */
+EGLBoolean
+_eglParseConfigAttribs(_EGLConfig *config, const EGLint *attrib_list)
+{
+ EGLint i;
+
+ /* XXX set all config attribs to EGL_DONT_CARE */
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ EGLint k = attrib_list[i] - FIRST_ATTRIB;
+ if (k >= 0 && k < MAX_ATTRIBS) {
+ config->Attrib[k] = attrib_list[++i];
+ }
+ else {
+ _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
+ return EGL_FALSE;
+ }
+ }
+ return EGL_TRUE;
+}
+
+
+#define EXACT 1
+#define ATLEAST 2
+#define MASK 3
+#define SMALLER 4
+#define SPECIAL 5
+#define NONE 6
+
+struct sort_info {
+ EGLint Attribute;
+ EGLint MatchCriteria;
+ EGLint SortOrder;
+};
+
+/* This encodes the info from Table 3.5 of the EGL spec, ordered by
+ * Sort Priority.
+ */
+static struct sort_info SortInfo[] = {
+ { EGL_CONFIG_CAVEAT, EXACT, SPECIAL },
+ { EGL_RED_SIZE, ATLEAST, SPECIAL },
+ { EGL_GREEN_SIZE, ATLEAST, SPECIAL },
+ { EGL_BLUE_SIZE, ATLEAST, SPECIAL },
+ { EGL_ALPHA_SIZE, ATLEAST, SPECIAL },
+ { EGL_BUFFER_SIZE, ATLEAST, SMALLER },
+ { EGL_SAMPLE_BUFFERS, ATLEAST, SMALLER },
+ { EGL_SAMPLES, ATLEAST, SMALLER },
+ { EGL_DEPTH_SIZE, ATLEAST, SMALLER },
+ { EGL_STENCIL_SIZE, ATLEAST, SMALLER },
+ { EGL_NATIVE_VISUAL_TYPE, EXACT, SPECIAL },
+ { EGL_CONFIG_ID, EXACT, SMALLER },
+ { EGL_BIND_TO_TEXTURE_RGB, EXACT, NONE },
+ { EGL_BIND_TO_TEXTURE_RGBA, EXACT, NONE },
+ { EGL_LEVEL, EXACT, NONE },
+ { EGL_NATIVE_RENDERABLE, EXACT, NONE },
+ { EGL_MAX_SWAP_INTERVAL, EXACT, NONE },
+ { EGL_MIN_SWAP_INTERVAL, EXACT, NONE },
+ { EGL_SURFACE_TYPE, MASK, NONE },
+ { EGL_TRANSPARENT_TYPE, EXACT, NONE },
+ { EGL_TRANSPARENT_RED_VALUE, EXACT, NONE },
+ { EGL_TRANSPARENT_GREEN_VALUE, EXACT, NONE },
+ { EGL_TRANSPARENT_BLUE_VALUE, EXACT, NONE },
+ { 0, 0, 0 }
+};
+
+
+/**
+ * Return EGL_TRUE if the attributes of c meet or exceed the minimums
+ * specified by min.
+ */
+EGLBoolean
+_eglConfigQualifies(const _EGLConfig *c, const _EGLConfig *min)
+{
+ EGLint i;
+ for (i = 0; SortInfo[i].Attribute != 0; i++) {
+ const EGLint mv = GET_CONFIG_ATTRIB(min, SortInfo[i].Attribute);
+ if (mv != EGL_DONT_CARE) {
+ const EGLint cv = GET_CONFIG_ATTRIB(c, SortInfo[i].Attribute);
+ if (SortInfo[i].MatchCriteria == EXACT) {
+ if (cv != mv) {
+ return EGL_FALSE;
+ }
+ }
+ else if (SortInfo[i].MatchCriteria == ATLEAST) {
+ if (cv < mv) {
+ return EGL_FALSE;
+ }
+ }
+ else {
+ assert(SortInfo[i].MatchCriteria == MASK);
+ if ((mv & cv) != mv) {
+ return EGL_FALSE;
+ }
+ }
+ }
+ }
+ return EGL_TRUE;
+}
+
+
+/**
+ * Compare configs 'a' and 'b' and return -1 if a belongs before b,
+ * 1 if a belongs after b, or 0 if they're equal.
+ */
+EGLint
+_eglCompareConfigs(const _EGLConfig *a, const _EGLConfig *b)
+{
+ EGLint i;
+ for (i = 0; SortInfo[i].Attribute != 0; i++) {
+ const EGLint av = GET_CONFIG_ATTRIB(a, SortInfo[i].Attribute);
+ const EGLint bv = GET_CONFIG_ATTRIB(b, SortInfo[i].Attribute);
+ if (SortInfo[i].SortOrder == SMALLER) {
+ if (av < bv)
+ return -1;
+ else if (av > bv)
+ return 1;
+ /* else, continue examining attribute values */
+ }
+ else if (SortInfo[i].SortOrder == SPECIAL) {
+ if (SortInfo[i].Attribute == EGL_CONFIG_CAVEAT) {
+ /* values are EGL_NONE, SLOW_CONFIG, or NON_CONFORMANT_CONFIG */
+ if (av < bv)
+ return -1;
+ else if (av > bv)
+ return 1;
+ }
+ else if (SortInfo[i].Attribute == EGL_RED_SIZE ||
+ SortInfo[i].Attribute == EGL_GREEN_SIZE ||
+ SortInfo[i].Attribute == EGL_BLUE_SIZE ||
+ SortInfo[i].Attribute == EGL_ALPHA_SIZE) {
+ if (av > bv)
+ return -1;
+ else if (av < bv)
+ return 1;
+ }
+ else {
+ assert(SortInfo[i].Attribute == EGL_NATIVE_VISUAL_TYPE);
+ if (av < bv)
+ return -1;
+ else if (av > bv)
+ return 1;
+ }
+ }
+ else {
+ assert(SortInfo[i].SortOrder == NONE);
+ /* continue examining attribute values */
+ }
+ }
+ return 0;
+}
+
+
+/**
+ * Typical fallback routine for eglChooseConfig
+ */
+EGLBoolean
+_eglChooseConfig(_EGLDriver *drv, EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
+{
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ _EGLConfig criteria;
+ EGLint i;
+
+ /* parse the attrib_list to initialize criteria */
+ if (!_eglParseConfigAttribs(&criteria, attrib_list)) {
+ return EGL_FALSE;
+ }
+
+ *num_config = 0;
+ for (i = 0; i < disp->NumConfigs; i++) {
+ const _EGLConfig *conf = disp->Configs + i;
+ if (_eglConfigQualifies(conf, &criteria)) {
+ if (*num_config < config_size) {
+ /* save */
+ configs[*num_config] = conf->Handle;
+ (*num_config)++;
+ }
+ else {
+ break;
+ }
+ }
+ }
+
+ /* XXX sort the list here */
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Fallback for eglGetConfigAttrib.
+ */
+EGLBoolean
+_eglGetConfigAttrib(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
+{
+ const _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
+ const EGLint k = attribute - FIRST_ATTRIB;
+ if (k >= 0 && k < MAX_ATTRIBS) {
+ *value = conf->Attrib[k];
+ return EGL_TRUE;
+ }
+ else {
+ _eglError(EGL_BAD_ATTRIBUTE, "eglGetConfigAttrib");
+ return EGL_FALSE;
+ }
+}
+
+
+/**
+ * Fallback for eglGetConfigs.
+ */
+EGLBoolean
+_eglGetConfigs(_EGLDriver *drv, EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
+{
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+
+ if (!drv->Initialized) {
+ _eglError(EGL_NOT_INITIALIZED, "eglGetConfigs");
+ return EGL_FALSE;
+ }
+
+ if (configs) {
+ EGLint i;
+ *num_config = MIN2(disp->NumConfigs, config_size);
+ for (i = 0; i < *num_config; i++) {
+ configs[i] = disp->Configs[i].Handle;
+ }
+ } else
+ *num_config = disp->NumConfigs;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Creates a set of \c __GLcontextModes that a driver will expose.
+ *
+ * A set of \c __GLcontextModes will be created based on the supplied
+ * parameters. The number of modes processed will be 2 *
+ * \c num_depth_stencil_bits * \c num_db_modes.
+ *
+ * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
+ * \c db_modes, and \c visType into each \c __GLcontextModes element.
+ * However, the meanings of \c fb_format and \c fb_type require further
+ * explanation. The \c fb_format specifies which color components are in
+ * each pixel and what the default order is. For example, \c GL_RGB specifies
+ * that red, green, blue are available and red is in the "most significant"
+ * position and blue is in the "least significant". The \c fb_type specifies
+ * the bit sizes of each component and the actual ordering. For example, if
+ * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
+ * are the blue value, bits [10:5] are the green value, and bits [4:0] are
+ * the red value.
+ *
+ * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
+ * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
+ * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
+ * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
+ * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
+ * still uses 32-bits.
+ *
+ * If in doubt, look at the tables used in the function.
+ *
+ * \param ptr_to_modes Pointer to a pointer to a linked list of
+ * \c __GLcontextModes. Upon completion, a pointer to
+ * the next element to be process will be stored here.
+ * If the function fails and returns \c GL_FALSE, this
+ * value will be unmodified, but some elements in the
+ * linked list may be modified.
+ * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
+ * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
+ * \param fb_type Type of the pixels in the framebuffer. Currently only
+ * \c GL_UNSIGNED_SHORT_5_6_5,
+ * \c GL_UNSIGNED_SHORT_5_6_5_REV,
+ * \c GL_UNSIGNED_INT_8_8_8_8, and
+ * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
+ * \param depth_bits Array of depth buffer sizes to be exposed.
+ * \param stencil_bits Array of stencil buffer sizes to be exposed.
+ * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
+ * \c stencil_bits.
+ * \param db_modes Array of buffer swap modes. If an element has a
+ * value of \c GLX_NONE, then it represents a
+ * single-buffered mode. Other valid values are
+ * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
+ * \c GLX_SWAP_UNDEFINED_OML. See the
+ * GLX_OML_swap_method extension spec for more details.
+ * \param num_db_modes Number of entries in \c db_modes.
+ * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
+ * \c GLX_DIRECT_COLOR.
+ *
+ * \returns
+ * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
+ * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
+ * \c fb_type).
+ *
+ * \todo
+ * There is currently no way to support packed RGB modes (i.e., modes with
+ * exactly 3 bytes per pixel) or floating-point modes. This could probably
+ * be done by creating some new, private enums with clever names likes
+ * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
+ * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
+ */
+GLboolean
+_eglFillInConfigs(_EGLConfig * configs,
+ GLenum fb_format, GLenum fb_type,
+ const u_int8_t * depth_bits, const u_int8_t * stencil_bits,
+ unsigned num_depth_stencil_bits,
+ const GLenum * db_modes, unsigned num_db_modes,
+ int visType) {
+ static const u_int8_t bits_table[3][4] = {
+ /* R G B A */
+ { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
+ { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
+ { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
+ };
+
+ /* The following arrays are all indexed by the fb_type masked with 0x07.
+ * Given the four supported fb_type values, this results in valid array
+ * indices of 3, 4, 5, and 7.
+ */
+ static const u_int32_t masks_table_rgb[8][4] = {
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
+ {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
+ {0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000}, /* 8_8_8_8 */
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000} /* 8_8_8_8_REV */
+ };
+
+ static const u_int32_t masks_table_rgba[8][4] = {
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
+ {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
+ {0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF}, /* 8_8_8_8 */
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}, /* 8_8_8_8_REV */
+ };
+
+ static const u_int32_t masks_table_bgr[8][4] = {
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
+ {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
+ {0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000}, /* 8_8_8_8 */
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000}, /* 8_8_8_8_REV */
+ };
+
+ static const u_int32_t masks_table_bgra[8][4] = {
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
+ {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
+ {0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF}, /* 8_8_8_8 */
+ {0x00000000, 0x00000000, 0x00000000, 0x00000000},
+ {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000}, /* 8_8_8_8_REV */
+ };
+
+ static const u_int8_t bytes_per_pixel[8] = {
+ 0, 0, 0, 2, 2, 4, 0, 4
+ };
+
+ const u_int8_t * bits;
+ const u_int32_t * masks;
+ const int index = fb_type & 0x07;
+ _EGLConfig *config;
+ unsigned i;
+ unsigned j;
+ unsigned k;
+
+ if ( bytes_per_pixel[index] == 0 ) {
+ fprintf(stderr, "[%s:%u] Framebuffer type 0x%04x has 0 bytes per pixel.\n",
+ __FUNCTION__, __LINE__, fb_type);
+ return GL_FALSE;
+ }
+
+ /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
+ * the _REV versions.
+ *
+ * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
+ */
+ switch ( fb_format ) {
+ case GL_RGB:
+ bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[1];
+ masks = masks_table_rgb[index];
+ break;
+
+ case GL_RGBA:
+ bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[2];
+ masks = masks_table_rgba[index];
+ break;
+
+ case GL_BGR:
+ bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[1];
+ masks = masks_table_bgr[index];
+ break;
+
+ case GL_BGRA:
+ bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[2];
+ masks = masks_table_bgra[index];
+ break;
+
+ default:
+ fprintf(stderr, "[%s:%u] Framebuffer format 0x%04x is not GL_RGB, GL_RGBA, GL_BGR, or GL_BGRA.\n",
+ __FUNCTION__, __LINE__, fb_format);
+ return GL_FALSE;
+ }
+
+ config = configs;
+ for (k = 0; k < num_depth_stencil_bits; k++) {
+ for (i = 0; i < num_db_modes; i++) {
+ for (j = 0; j < 2; j++) {
+ _eglSetConfigAttrib(config, EGL_RED_SIZE, bits[0]);
+ _eglSetConfigAttrib(config, EGL_GREEN_SIZE, bits[1]);
+ _eglSetConfigAttrib(config, EGL_BLUE_SIZE, bits[2]);
+ _eglSetConfigAttrib(config, EGL_ALPHA_SIZE, bits[3]);
+ _eglSetConfigAttrib(config, EGL_BUFFER_SIZE,
+ bits[0] + bits[1] + bits[2] + bits[3]);
+
+ _eglSetConfigAttrib(config, EGL_STENCIL_SIZE, stencil_bits[k]);
+ _eglSetConfigAttrib(config, EGL_DEPTH_SIZE, depth_bits[i]);
+
+ _eglSetConfigAttrib(config, EGL_SURFACE_TYPE, EGL_SCREEN_BIT_MESA |
+ EGL_PBUFFER_BIT | EGL_PIXMAP_BIT | EGL_WINDOW_BIT);
+
+ config++;
+ }
+ }
+ }
+ return GL_TRUE;
+}
diff --git a/src/egl/main/eglconfig.h b/src/egl/main/eglconfig.h
new file mode 100644
index 000000000..65ce0f3ed
--- /dev/null
+++ b/src/egl/main/eglconfig.h
@@ -0,0 +1,75 @@
+#ifndef EGLCONFIG_INCLUDED
+#define EGLCONFIG_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "GL/internal/glcore.h"
+
+
+#define MAX_ATTRIBS 100
+#define FIRST_ATTRIB EGL_BUFFER_SIZE
+
+
+struct _egl_config
+{
+ EGLConfig Handle; /* the public/opaque handle which names this config */
+ EGLint Attrib[MAX_ATTRIBS];
+};
+
+
+#define SET_CONFIG_ATTRIB(CONF, ATTR, VAL) ((CONF)->Attrib[(ATTR) - FIRST_ATTRIB] = VAL)
+#define GET_CONFIG_ATTRIB(CONF, ATTR) ((CONF)->Attrib[(ATTR) - FIRST_ATTRIB])
+
+
+extern void
+_eglInitConfig(_EGLConfig *config, EGLint id);
+
+
+extern _EGLConfig *
+_eglLookupConfig(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config);
+
+
+extern _EGLConfig *
+_eglAddConfig(_EGLDisplay *display, const _EGLConfig *config);
+
+
+extern EGLBoolean
+_eglParseConfigAttribs(_EGLConfig *config, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglConfigQualifies(const _EGLConfig *c, const _EGLConfig *min);
+
+
+extern EGLint
+_eglCompareConfigs(const _EGLConfig *a, const _EGLConfig *b);
+
+
+extern EGLBoolean
+_eglChooseConfig(_EGLDriver *drv, EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+
+
+extern EGLBoolean
+_eglGetConfigAttrib(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
+
+
+extern EGLBoolean
+_eglGetConfigs(_EGLDriver *drv, EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+
+
+extern void
+_eglSetConfigAttrib(_EGLConfig *config, EGLint attr, EGLint val);
+
+extern GLboolean
+_eglFillInConfigs( _EGLConfig *configs,
+ GLenum fb_format, GLenum fb_type,
+ const u_int8_t * depth_bits, const u_int8_t * stencil_bits,
+ unsigned num_depth_stencil_bits,
+ const GLenum * db_modes, unsigned num_db_modes,
+ int visType );
+
+extern void
+_eglConfigToContextModesRec(const _EGLConfig *config, __GLcontextModes *mode);
+
+
+#endif /* EGLCONFIG_INCLUDED */
diff --git a/src/egl/main/eglcontext.c b/src/egl/main/eglcontext.c
new file mode 100644
index 000000000..283dd3f1a
--- /dev/null
+++ b/src/egl/main/eglcontext.c
@@ -0,0 +1,240 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+#include "eglhash.h"
+#include "eglsurface.h"
+
+
+/**
+ * Initialize the given _EGLContext object to defaults.
+ */
+void
+_eglInitContext(_EGLContext *ctx)
+{
+ /* just init to zero for now */
+ memset(ctx, 0, sizeof(_EGLContext));
+}
+
+
+/*
+ * Assign an EGLContext handle to the _EGLContext object then put it into
+ * the hash table.
+ */
+void
+_eglSaveContext(_EGLContext *ctx)
+{
+ assert(ctx);
+ ctx->Handle = _eglHashGenKey(_eglGlobal.Contexts);
+ _eglHashInsert(_eglGlobal.Contexts, ctx->Handle, ctx);
+}
+
+
+/**
+ * Remove the given _EGLContext object from the hash table.
+ */
+void
+_eglRemoveContext(_EGLContext *ctx)
+{
+ _eglHashRemove(_eglGlobal.Contexts, ctx->Handle);
+}
+
+
+/**
+ * Return the _EGLContext object that corresponds to the given
+ * EGLContext handle.
+ */
+_EGLContext *
+_eglLookupContext(EGLContext ctx)
+{
+ _EGLContext *c = (_EGLContext *) _eglHashLookup(_eglGlobal.Contexts, ctx);
+ return c;
+}
+
+
+/**
+ * Return the currently bound _EGLContext object, or NULL.
+ */
+_EGLContext *
+_eglGetCurrentContext(void)
+{
+ /* XXX this should be per-thread someday */
+ return _eglGlobal.CurrentContext;
+}
+
+
+/**
+ * Just a placeholder/demo function. Real driver will never use this!
+ */
+EGLContext
+_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
+{
+ _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreateContext");
+ return EGL_NO_CONTEXT;
+ }
+
+ if (share_list != EGL_NO_CONTEXT) {
+ _EGLContext *shareCtx = _eglLookupContext(share_list);
+ if (!shareCtx) {
+ _eglError(EGL_BAD_CONTEXT, "eglCreateContext(share_list)");
+ return EGL_NO_CONTEXT;
+ }
+ }
+
+ return EGL_NO_CONTEXT;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLBoolean
+_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
+{
+ _EGLContext *context = _eglLookupContext(ctx);
+ if (context) {
+ _eglHashRemove(_eglGlobal.Contexts, ctx);
+ if (context->IsBound) {
+ context->DeletePending = EGL_TRUE;
+ }
+ else {
+ free(context);
+ }
+ return EGL_TRUE;
+ }
+ else {
+ _eglError(EGL_BAD_CONTEXT, "eglDestroyContext");
+ return EGL_TRUE;
+ }
+}
+
+
+EGLBoolean
+_eglQueryContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value)
+{
+ _EGLContext *c = _eglLookupContext(ctx);
+
+ (void) drv;
+ (void) dpy;
+
+ if (!c) {
+ _eglError(EGL_BAD_CONTEXT, "eglQueryContext");
+ return EGL_FALSE;
+ }
+
+ switch (attribute) {
+ case EGL_CONFIG_ID:
+ *value = GET_CONFIG_ATTRIB(c->Config, EGL_CONFIG_ID);
+ return EGL_TRUE;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
+ return EGL_FALSE;
+ }
+}
+
+
+/**
+ * Drivers will typically call this to do the error checking and
+ * update the various IsBound and DeletePending flags.
+ * Then, the driver will do its device-dependent Make-Current stuff.
+ */
+EGLBoolean
+_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d, EGLSurface r, EGLContext context)
+{
+ _EGLContext *ctx = _eglLookupContext(context);
+ _EGLSurface *draw = _eglLookupSurface(d);
+ _EGLSurface *read = _eglLookupSurface(r);
+
+ _EGLContext *oldContext = _eglGetCurrentContext();
+ _EGLSurface *oldDrawSurface = _eglGetCurrentSurface(EGL_DRAW);
+ _EGLSurface *oldReadSurface = _eglGetCurrentSurface(EGL_READ);
+
+ /* error checking */
+ if (ctx) {
+ if (draw == NULL || read == NULL) {
+ _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ return EGL_FALSE;
+ }
+ if (draw->Config != ctx->Config) {
+ _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ return EGL_FALSE;
+ }
+ if (read->Config != ctx->Config) {
+ _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ return EGL_FALSE;
+ }
+ }
+
+ /*
+ * check if the old context or surfaces need to be deleted
+ */
+ if (oldDrawSurface != NULL) {
+ oldDrawSurface->IsBound = EGL_FALSE;
+ if (oldDrawSurface->DeletePending) {
+ /* make sure we don't try to rebind a deleted surface */
+ if (draw == oldDrawSurface || draw == oldReadSurface) {
+ draw = NULL;
+ }
+ /* really delete surface now */
+ drv->DestroySurface(drv, dpy, oldDrawSurface->Handle);
+ }
+ }
+ if (oldReadSurface != NULL && oldReadSurface != oldDrawSurface) {
+ oldReadSurface->IsBound = EGL_FALSE;
+ if (oldReadSurface->DeletePending) {
+ /* make sure we don't try to rebind a deleted surface */
+ if (read == oldDrawSurface || read == oldReadSurface) {
+ read = NULL;
+ }
+ /* really delete surface now */
+ drv->DestroySurface(drv, dpy, oldReadSurface->Handle);
+ }
+ }
+ if (oldContext != NULL) {
+ oldContext->IsBound = EGL_FALSE;
+ if (oldContext->DeletePending) {
+ /* make sure we don't try to rebind a deleted context */
+ if (ctx == oldContext) {
+ ctx = NULL;
+ }
+ /* really delete context now */
+ drv->DestroyContext(drv, dpy, oldContext->Handle);
+ }
+ }
+
+ if (ctx) {
+ /* check read/draw again, in case we deleted them above */
+ if (draw == NULL || read == NULL) {
+ _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ return EGL_FALSE;
+ }
+ ctx->DrawSurface = draw;
+ ctx->ReadSurface = read;
+ ctx->IsBound = EGL_TRUE;
+ draw->IsBound = EGL_TRUE;
+ read->IsBound = EGL_TRUE;
+ }
+
+ _eglGlobal.CurrentContext = ctx;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * This is defined by the EGL_MESA_copy_context extension.
+ */
+EGLBoolean
+_eglCopyContextMESA(_EGLDriver *drv, EGLDisplay dpy, EGLContext source,
+ EGLContext dest, EGLint mask)
+{
+ /* This function will always have to be overridden/implemented in the
+ * device driver. If the driver is based on Mesa, use _mesa_copy_context().
+ */
+ return EGL_FALSE;
+}
diff --git a/src/egl/main/eglcontext.h b/src/egl/main/eglcontext.h
new file mode 100644
index 000000000..d74d6e325
--- /dev/null
+++ b/src/egl/main/eglcontext.h
@@ -0,0 +1,67 @@
+
+#ifndef EGLCONTEXT_INCLUDED
+#define EGLCONTEXT_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+/**
+ * "Base" class for device driver contexts.
+ */
+struct _egl_context
+{
+ EGLContext Handle; /* The public/opaque handle which names this object */
+
+ _EGLDisplay *Display; /* who do I belong to? */
+
+ _EGLConfig *Config;
+
+ _EGLSurface *DrawSurface;
+ _EGLSurface *ReadSurface;
+
+ EGLBoolean IsBound;
+ EGLBoolean DeletePending;
+};
+
+
+extern void
+_eglInitContext(_EGLContext *ctx);
+
+
+extern void
+_eglSaveContext(_EGLContext *ctx);
+
+
+extern void
+_eglRemoveContext(_EGLContext *ctx);
+
+
+extern _EGLContext *
+_eglLookupContext(EGLContext ctx);
+
+
+extern _EGLContext *
+_eglGetCurrentContext(void);
+
+
+extern EGLContext
+_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx);
+
+
+extern EGLBoolean
+_eglQueryContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value);
+
+
+extern EGLBoolean
+_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
+
+
+extern EGLBoolean
+_eglCopyContextMESA(_EGLDriver *drv, EGLDisplay dpy, EGLContext source, EGLContext dest, EGLint mask);
+
+#endif /* EGLCONTEXT_INCLUDED */
diff --git a/src/egl/main/egldisplay.c b/src/egl/main/egldisplay.c
new file mode 100644
index 000000000..669a4d508
--- /dev/null
+++ b/src/egl/main/egldisplay.c
@@ -0,0 +1,71 @@
+#include <stdlib.h>
+#include <string.h>
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "eglglobals.h"
+#include "eglhash.h"
+
+
+static char *
+my_strdup(const char *s)
+{
+ int l = strlen(s);
+ char *s2 = malloc(l + 1);
+ strcpy(s2, s);
+ return s2;
+}
+
+
+/**
+ * We're assuming that the NativeDisplayType parameter is actually
+ * a string.
+ * Return a new _EGLDisplay object for the given displayName
+ */
+_EGLDisplay *
+_eglNewDisplay(NativeDisplayType displayName)
+{
+ _EGLDisplay *dpy = (_EGLDisplay *) malloc(sizeof(_EGLDisplay));
+ if (dpy) {
+ dpy->Handle = _eglHashGenKey(_eglGlobal.Displays);
+ _eglHashInsert(_eglGlobal.Displays, dpy->Handle, dpy);
+ if (displayName)
+ dpy->Name = my_strdup(displayName);
+ else
+ dpy->Name = NULL;
+ dpy->Driver = NULL; /* this gets set later */
+ }
+ return dpy;
+}
+
+
+/**
+ * Return the _EGLDisplay object that corresponds to the given public/
+ * opaque display handle.
+ */
+_EGLDisplay *
+_eglLookupDisplay(EGLDisplay dpy)
+{
+ _EGLDisplay *d = (_EGLDisplay *) _eglHashLookup(_eglGlobal.Displays, dpy);
+ return d;
+}
+
+
+_EGLDisplay *
+_eglGetCurrentDisplay(void)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ if (ctx)
+ return ctx->Display;
+ else
+ return NULL;
+}
+
+
+void
+_eglCleanupDisplay(_EGLDisplay *disp)
+{
+ /* XXX incomplete */
+ free(disp->Configs);
+ free(disp->Name);
+ /* driver deletes _EGLDisplay */
+}
diff --git a/src/egl/main/egldisplay.h b/src/egl/main/egldisplay.h
new file mode 100644
index 000000000..1a03fdd4a
--- /dev/null
+++ b/src/egl/main/egldisplay.h
@@ -0,0 +1,44 @@
+#ifndef EGLDISPLAY_INCLUDED
+#define EGLDISPLAY_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+struct _egl_display
+{
+ EGLDisplay Handle;
+
+ char *Name;
+ _EGLDriver *Driver;
+
+ EGLint NumScreens;
+ _EGLScreen **Screens; /* array [NumScreens] */
+
+ EGLint NumConfigs;
+ _EGLConfig *Configs; /* array [NumConfigs] */
+};
+
+
+extern _EGLDisplay *
+_eglNewDisplay(NativeDisplayType displayName);
+
+
+extern _EGLDisplay *
+_eglLookupDisplay(EGLDisplay dpy);
+
+
+extern _EGLDisplay *
+_eglGetCurrentDisplay(void);
+
+
+extern void
+_eglCleanupDisplay(_EGLDisplay *disp);
+
+
+extern EGLBoolean
+_eglQueryDisplayMESA(_EGLDriver *drv, EGLDisplay dpy, EGLint attrib, EGLint *value);
+
+
+
+#endif /* EGLDISPLAY_INCLUDED */
diff --git a/src/egl/main/egldriver.c b/src/egl/main/egldriver.c
new file mode 100644
index 000000000..6e8c9b3b4
--- /dev/null
+++ b/src/egl/main/egldriver.c
@@ -0,0 +1,251 @@
+#include <assert.h>
+#include <dlfcn.h>
+#include <stdio.h>
+#include <string.h>
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+#include "eglmode.h"
+#include "eglscreen.h"
+#include "eglsurface.h"
+
+
+const char *DefaultDriverName = "demodriver";
+
+
+/**
+ * Choose and open/init the hardware driver for the given EGLDisplay.
+ * Previously, the EGLDisplay was created with _eglNewDisplay() where
+ * we recorded the user's NativeDisplayType parameter.
+ *
+ * Now we'll use the NativeDisplayType value.
+ *
+ * Currently, the native display value is treated as a string.
+ * If the first character is ':' we interpret it as a screen or card index
+ * number (i.e. ":0" or ":1", etc)
+ * Else if the first character is '!' we interpret it as specific driver name
+ * (i.e. "!r200" or "!i830".
+ */
+_EGLDriver *
+_eglChooseDriver(EGLDisplay display)
+{
+ _EGLDisplay *dpy = _eglLookupDisplay(display);
+ _EGLDriver *drv;
+ const char *driverName = DefaultDriverName;
+ const char *name;
+
+ assert(dpy);
+
+ name = dpy->Name;
+ if (!name) {
+ /* use default */
+ }
+ else if (name[0] == ':' && (name[1] >= '0' && name[1] <= '9') && !name[2]) {
+ printf("EGL: Use driver for screen: %s\n", name);
+ /* XXX probe hardware here to determine which driver to open */
+ driverName = "libEGLdri";
+ }
+ else if (name[0] == '!') {
+ /* use specified driver name */
+ driverName = name + 1;
+ printf("EGL: Use driver named %s\n", driverName);
+ }
+ else {
+ /* Maybe display was returned by XOpenDisplay? */
+ printf("EGL: can't parse display pointer\n");
+ }
+
+ drv = _eglOpenDriver(dpy, driverName);
+ dpy->Driver = drv;
+
+ return drv;
+}
+
+
+/**
+ * Open/load the named driver and call its bootstrap function: _eglMain().
+ * \return new _EGLDriver object.
+ */
+_EGLDriver *
+_eglOpenDriver(_EGLDisplay *dpy, const char *driverName)
+{
+ _EGLDriver *drv;
+ _EGLMain_t mainFunc;
+ void *lib;
+ char driverFilename[1000];
+
+ /* XXX also prepend a directory path??? */
+ sprintf(driverFilename, "%s.so", driverName);
+
+ lib = dlopen(driverFilename, RTLD_NOW);
+ if (!lib) {
+ fprintf(stderr, "EGLdebug: Error opening %s: %s\n", driverFilename, dlerror());
+ return NULL;
+ }
+
+ mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
+ if (!mainFunc) {
+ fprintf(stderr, "_eglMain not found in %s", (char *) driverFilename);
+ dlclose(lib);
+ return NULL;
+ }
+
+ drv = mainFunc(dpy);
+ if (!drv) {
+ dlclose(lib);
+ return NULL;
+ }
+ /* with a recurvise open you want the inner most handle */
+ if (!drv->LibHandle)
+ drv->LibHandle = lib;
+ else
+ dlclose(lib);
+
+ drv->Display = dpy;
+ return drv;
+}
+
+
+EGLBoolean
+_eglCloseDriver(_EGLDriver *drv, EGLDisplay dpy)
+{
+ void *handle = drv->LibHandle;
+ EGLBoolean b;
+ fprintf(stderr, "EGL debug: Closing driver\n");
+
+ /*
+ * XXX check for currently bound context/surfaces and delete them?
+ */
+
+ b = drv->Terminate(drv, dpy);
+ dlclose(handle);
+ return b;
+}
+
+
+/**
+ * Given a display handle, return the _EGLDriver for that display.
+ */
+_EGLDriver *
+_eglLookupDriver(EGLDisplay dpy)
+{
+ _EGLDisplay *d = _eglLookupDisplay(dpy);
+ if (d)
+ return d->Driver;
+ else
+ return NULL;
+}
+
+
+/**
+ * Plug all the available fallback routines into the given driver's
+ * dispatch table.
+ */
+void
+_eglInitDriverFallbacks(_EGLDriver *drv)
+{
+ /* If a pointer is set to NULL, then the device driver _really_ has
+ * to implement it.
+ */
+ drv->Initialize = NULL;
+ drv->Terminate = NULL;
+
+ drv->GetConfigs = _eglGetConfigs;
+ drv->ChooseConfig = _eglChooseConfig;
+ drv->GetConfigAttrib = _eglGetConfigAttrib;
+
+ drv->CreateContext = _eglCreateContext;
+ drv->DestroyContext = _eglDestroyContext;
+ drv->MakeCurrent = _eglMakeCurrent;
+ drv->QueryContext = _eglQueryContext;
+
+ drv->CreateWindowSurface = _eglCreateWindowSurface;
+ drv->CreatePixmapSurface = _eglCreatePixmapSurface;
+ drv->CreatePbufferSurface = _eglCreatePbufferSurface;
+ drv->DestroySurface = _eglDestroySurface;
+ drv->QuerySurface = _eglQuerySurface;
+ drv->SurfaceAttrib = _eglSurfaceAttrib;
+ drv->BindTexImage = _eglBindTexImage;
+ drv->ReleaseTexImage = _eglReleaseTexImage;
+ drv->SwapInterval = _eglSwapInterval;
+ drv->SwapBuffers = _eglSwapBuffers;
+ drv->CopyBuffers = _eglCopyBuffers;
+
+ drv->QueryString = _eglQueryString;
+ drv->WaitGL = _eglWaitGL;
+ drv->WaitNative = _eglWaitNative;
+
+ /* EGL_MESA_screen */
+ drv->ChooseModeMESA = _eglChooseModeMESA;
+ drv->GetModesMESA = _eglGetModesMESA;
+ drv->GetModeAttribMESA = _eglGetModeAttribMESA;
+ drv->GetScreensMESA = _eglGetScreensMESA;
+ drv->CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
+ drv->ShowSurfaceMESA = _eglShowSurfaceMESA;
+ drv->ScreenPositionMESA = _eglScreenPositionMESA;
+ drv->QueryScreenMESA = _eglQueryScreenMESA;
+ drv->QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
+ drv->QueryScreenModeMESA = _eglQueryScreenModeMESA;
+ drv->QueryModeStringMESA = _eglQueryModeStringMESA;
+}
+
+
+/**
+ * Examine the individual extension enable/disable flags and recompute
+ * the driver's Extensions string.
+ */
+static void
+UpdateExtensionsString(_EGLDriver *drv)
+{
+ drv->Extensions[0] = 0;
+
+ if (drv->MESA_screen_surface)
+ strcat(drv->Extensions, "EGL_MESA_screen_surface ");
+ if (drv->MESA_copy_context)
+ strcat(drv->Extensions, "EGL_MESA_copy_context ");
+ assert(strlen(drv->Extensions) < MAX_EXTENSIONS_LEN);
+}
+
+
+
+const char *
+_eglQueryString(_EGLDriver *drv, EGLDisplay dpy, EGLint name)
+{
+ (void) drv;
+ (void) dpy;
+ switch (name) {
+ case EGL_VENDOR:
+ return "Mesa Project";
+ case EGL_VERSION:
+ return "1.0";
+ case EGL_EXTENSIONS:
+ UpdateExtensionsString(drv);
+ return drv->Extensions;
+ default:
+ _eglError(EGL_BAD_PARAMETER, "eglQueryString");
+ return NULL;
+ }
+}
+
+
+EGLBoolean
+_eglWaitGL(_EGLDriver *drv, EGLDisplay dpy)
+{
+ /* just a placeholder */
+ (void) drv;
+ (void) dpy;
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglWaitNative(_EGLDriver *drv, EGLDisplay dpy, EGLint engine)
+{
+ /* just a placeholder */
+ (void) drv;
+ (void) dpy;
+ (void) engine;
+ return EGL_TRUE;
+}
diff --git a/src/egl/main/egldriver.h b/src/egl/main/egldriver.h
new file mode 100644
index 000000000..7ab62f182
--- /dev/null
+++ b/src/egl/main/egldriver.h
@@ -0,0 +1,164 @@
+#ifndef EGLDRIVER_INCLUDED
+#define EGLDRIVER_INCLUDED
+
+
+#include "egltypedefs.h"
+
+/* should probably use a dynamic-lengh string, but this will do */
+#define MAX_EXTENSIONS_LEN 1000
+
+
+/* driver funcs */
+typedef EGLBoolean (*Initialize_t)(_EGLDriver *, EGLDisplay dpy, EGLint *major, EGLint *minor);
+typedef EGLBoolean (*Terminate_t)(_EGLDriver *, EGLDisplay dpy);
+
+/* config funcs */
+typedef EGLBoolean (*GetConfigs_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+typedef EGLBoolean (*ChooseConfig_t)(_EGLDriver *drv, EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+typedef EGLBoolean (*GetConfigAttrib_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value);
+
+/* context funcs */
+typedef EGLContext (*CreateContext_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list);
+typedef EGLBoolean (*DestroyContext_t)(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx);
+typedef EGLBoolean (*MakeCurrent_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
+typedef EGLBoolean (*QueryContext_t)(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value);
+
+/* surface funcs */
+typedef EGLSurface (*CreateWindowSurface_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list);
+typedef EGLSurface (*CreatePixmapSurface_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list);
+typedef EGLSurface (*CreatePbufferSurface_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
+typedef EGLBoolean (*DestroySurface_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface);
+typedef EGLBoolean (*QuerySurface_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*SurfaceAttrib_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value);
+typedef EGLBoolean (*BindTexImage_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer);
+typedef EGLBoolean (*ReleaseTexImage_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer);
+typedef EGLBoolean (*SwapInterval_t)(_EGLDriver *drv, EGLDisplay dpy, EGLint interval);
+typedef EGLBoolean (*SwapBuffers_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw);
+typedef EGLBoolean (*CopyBuffers_t)(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, NativePixmapType target);
+
+/* misc funcs */
+typedef const char *(*QueryString_t)(_EGLDriver *drv, EGLDisplay dpy, EGLint name);
+typedef EGLBoolean (*WaitGL_t)(_EGLDriver *drv, EGLDisplay dpy);
+typedef EGLBoolean (*WaitNative_t)(_EGLDriver *drv, EGLDisplay dpy, EGLint engine);
+
+
+/* EGL_MESA_screen extension */
+typedef EGLBoolean (*ChooseModeMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, const EGLint *attrib_list, EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes);
+typedef EGLBoolean (*GetModesMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *modes, EGLint mode_size, EGLint *num_mode);
+typedef EGLBoolean (*GetModeAttribMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLModeMESA mode, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*CopyContextMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLContext source, EGLContext dest, EGLint mask);
+typedef EGLBoolean (*GetScreensMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA *screens, EGLint max_screens, EGLint *num_screens);
+typedef EGLSurface (*CreateScreenSurfaceMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
+typedef EGLBoolean (*ShowSurfaceMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLSurface surface, EGLModeMESA mode);
+typedef EGLBoolean (*ScreenPositionMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLint x, EGLint y);
+typedef EGLBoolean (*QueryScreenMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*QueryScreenSurfaceMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLSurface *surface);
+typedef EGLBoolean (*QueryScreenModeMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *mode);
+typedef const char * (*QueryModeStringMESA_t)(_EGLDriver *drv, EGLDisplay dpy, EGLModeMESA mode);
+
+
+/**
+ * Base class for device drivers.
+ */
+struct _egl_driver
+{
+ EGLBoolean Initialized; /* set by driver after initialized */
+
+ void *LibHandle; /* dlopen handle */
+
+ _EGLDisplay *Display;
+
+ int ABIversion;
+ int APImajor, APIminor; /* returned through eglInitialize */
+
+ /*
+ * The API dispatcher jumps through these functions
+ */
+ Initialize_t Initialize;
+ Terminate_t Terminate;
+
+ GetConfigs_t GetConfigs;
+ ChooseConfig_t ChooseConfig;
+ GetConfigAttrib_t GetConfigAttrib;
+
+ CreateContext_t CreateContext;
+ DestroyContext_t DestroyContext;
+ MakeCurrent_t MakeCurrent;
+ QueryContext_t QueryContext;
+
+ CreateWindowSurface_t CreateWindowSurface;
+ CreatePixmapSurface_t CreatePixmapSurface;
+ CreatePbufferSurface_t CreatePbufferSurface;
+ DestroySurface_t DestroySurface;
+ QuerySurface_t QuerySurface;
+ SurfaceAttrib_t SurfaceAttrib;
+ BindTexImage_t BindTexImage;
+ ReleaseTexImage_t ReleaseTexImage;
+ SwapInterval_t SwapInterval;
+ SwapBuffers_t SwapBuffers;
+ CopyBuffers_t CopyBuffers;
+
+ QueryString_t QueryString;
+ WaitGL_t WaitGL;
+ WaitNative_t WaitNative;
+
+ /* EGL_MESA_screen extension */
+ ChooseModeMESA_t ChooseModeMESA;
+ GetModesMESA_t GetModesMESA;
+ GetModeAttribMESA_t GetModeAttribMESA;
+ CopyContextMESA_t CopyContextMESA;
+ GetScreensMESA_t GetScreensMESA;
+ CreateScreenSurfaceMESA_t CreateScreenSurfaceMESA;
+ ShowSurfaceMESA_t ShowSurfaceMESA;
+ ScreenPositionMESA_t ScreenPositionMESA;
+ QueryScreenMESA_t QueryScreenMESA;
+ QueryScreenSurfaceMESA_t QueryScreenSurfaceMESA;
+ QueryScreenModeMESA_t QueryScreenModeMESA;
+ QueryModeStringMESA_t QueryModeStringMESA;
+
+ /* Extension enable flags */
+ EGLBoolean MESA_screen_surface;
+ EGLBoolean MESA_copy_context;
+
+ /* Extensions string */
+ char Extensions[MAX_EXTENSIONS_LEN];
+};
+
+
+extern _EGLDriver *_eglMain(_EGLDisplay *dpy);
+
+
+extern _EGLDriver *
+_eglChooseDriver(EGLDisplay dpy);
+
+
+extern _EGLDriver *
+_eglOpenDriver(_EGLDisplay *dpy, const char *driverName);
+
+
+extern EGLBoolean
+_eglCloseDriver(_EGLDriver *drv, EGLDisplay dpy);
+
+
+extern _EGLDriver *
+_eglLookupDriver(EGLDisplay d);
+
+
+extern void
+_eglInitDriverFallbacks(_EGLDriver *drv);
+
+
+extern const char *
+_eglQueryString(_EGLDriver *drv, EGLDisplay dpy, EGLint name);
+
+
+extern EGLBoolean
+_eglWaitGL(_EGLDriver *drv, EGLDisplay dpy);
+
+
+extern EGLBoolean
+_eglWaitNative(_EGLDriver *drv, EGLDisplay dpy, EGLint engine);
+
+
+
+#endif /* EGLDRIVER_INCLUDED */
diff --git a/src/egl/main/eglglobals.c b/src/egl/main/eglglobals.c
new file mode 100644
index 000000000..6265b0752
--- /dev/null
+++ b/src/egl/main/eglglobals.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include "eglglobals.h"
+
+
+struct _egl_global _eglGlobal = { EGL_FALSE };
+
+
+/**
+ * Init the fields in the _eglGlobal struct
+ * May be safely called more than once.
+ */
+void
+_eglInitGlobals(void)
+{
+ if (!_eglGlobal.Initialized) {
+ _eglGlobal.Displays = _eglNewHashTable();
+ _eglGlobal.Contexts = _eglNewHashTable();
+ _eglGlobal.Surfaces = _eglNewHashTable();
+ _eglGlobal.FreeScreenHandle = 1;
+ _eglGlobal.CurrentContext = EGL_NO_CONTEXT;
+ _eglGlobal.LastError = EGL_SUCCESS;
+ _eglGlobal.Initialized = EGL_TRUE;
+ }
+}
+
+
+/**
+ * Should call this via an atexit handler.
+ */
+void
+_eglDestroyGlobals(void)
+{
+ /* XXX TODO walk over table entries, deleting each */
+ _eglDeleteHashTable(_eglGlobal.Displays);
+ _eglDeleteHashTable(_eglGlobal.Contexts);
+ _eglDeleteHashTable(_eglGlobal.Surfaces);
+}
+
+
+
+/**
+ * Record EGL error code.
+ */
+void
+_eglError(EGLint errCode, const char *msg)
+{
+ if (_eglGlobal.LastError == EGL_SUCCESS) {
+ _eglGlobal.LastError = errCode;
+ /* XXX temporary */
+ fprintf(stderr, "EGL Error 0x%x in %s\n", errCode, msg);
+ }
+}
+
+
+/**
+ * Return a new screen handle/ID.
+ * NOTE: we never reuse these!
+ */
+EGLScreenMESA
+_eglAllocScreenHandle(void)
+{
+ EGLScreenMESA s = _eglGlobal.FreeScreenHandle;
+ _eglGlobal.FreeScreenHandle++;
+ return s;
+}
diff --git a/src/egl/main/eglglobals.h b/src/egl/main/eglglobals.h
new file mode 100644
index 000000000..1a6f12d17
--- /dev/null
+++ b/src/egl/main/eglglobals.h
@@ -0,0 +1,44 @@
+#ifndef EGLGLOBALS_INCLUDED
+#define EGLGLOBALS_INCLUDED
+
+#include "egltypedefs.h"
+#include "eglhash.h"
+
+
+struct _egl_global
+{
+ EGLBoolean Initialized;
+
+ _EGLHashtable *Displays;
+ _EGLHashtable *Contexts;
+ _EGLHashtable *Surfaces;
+
+ EGLScreenMESA FreeScreenHandle;
+
+ EGLint LastError;
+
+ /* XXX this should be per-thread someday */
+ _EGLContext *CurrentContext;
+};
+
+
+extern struct _egl_global _eglGlobal;
+
+
+extern void
+_eglInitGlobals(void);
+
+
+extern void
+_eglDestroyGlobals(void);
+
+
+extern void
+_eglError(EGLint errCode, const char *msg);
+
+
+extern EGLScreenMESA
+_eglAllocScreenHandle(void);
+
+
+#endif /* EGLGLOBALS_INCLUDED */
diff --git a/src/egl/main/eglhash.c b/src/egl/main/eglhash.c
new file mode 100644
index 000000000..8e3da2e90
--- /dev/null
+++ b/src/egl/main/eglhash.c
@@ -0,0 +1,347 @@
+/**
+ * \file hash.c
+ * Generic hash table.
+ *
+ * This code taken from Mesa and adapted.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "eglhash.h"
+
+
+#define TABLE_SIZE 1023 /**< Size of lookup table/array */
+
+#define HASH_FUNC(K) ((K) % TABLE_SIZE)
+
+
+/*
+ * Unfinished mutex stuff
+ */
+
+typedef int _EGLMutex;
+
+static void
+_eglInitMutex(_EGLMutex m)
+{
+}
+
+static void
+_eglDestroyMutex(_EGLMutex m)
+{
+}
+
+static void
+_eglLockMutex(_EGLMutex m)
+{
+}
+
+static void
+_eglUnlockMutex(_EGLMutex m)
+{
+}
+
+
+
+typedef struct _egl_hashentry _EGLHashentry;
+
+struct _egl_hashentry
+{
+ EGLuint Key; /**< the entry's key */
+ void *Data; /**< the entry's data */
+ _EGLHashentry *Next; /**< pointer to next entry */
+};
+
+
+struct _egl_hashtable
+{
+ _EGLHashentry *Table[TABLE_SIZE]; /**< the lookup table */
+ EGLuint MaxKey; /**< highest key inserted so far */
+ _EGLMutex Mutex; /**< mutual exclusion lock */
+};
+
+
+/**
+ * Create a new hash table.
+ *
+ * \return pointer to a new, empty hash table.
+ */
+_EGLHashtable *
+_eglNewHashTable(void)
+{
+ _EGLHashtable *table = (_EGLHashtable *) calloc(1, sizeof(_EGLHashtable));
+ if (table) {
+ _eglInitMutex(table->Mutex);
+ table->MaxKey = 1;
+ }
+ return table;
+}
+
+
+
+/**
+ * Delete a hash table.
+ * Frees each entry on the hash table and then the hash table structure itself.
+ * Note that the caller should have already traversed the table and deleted
+ * the objects in the table (i.e. We don't free the entries' data pointer).
+ *
+ * \param table the hash table to delete.
+ */
+void
+_eglDeleteHashTable(_EGLHashtable *table)
+{
+ EGLuint i;
+ assert(table);
+ for (i = 0; i < TABLE_SIZE; i++) {
+ _EGLHashentry *entry = table->Table[i];
+ while (entry) {
+ _EGLHashentry *next = entry->Next;
+ free(entry);
+ entry = next;
+ }
+ }
+ _eglDestroyMutex(table->Mutex);
+ free(table);
+}
+
+
+
+/**
+ * Lookup an entry in the hash table.
+ *
+ * \param table the hash table.
+ * \param key the key.
+ *
+ * \return pointer to user's data or NULL if key not in table
+ */
+void *
+_eglHashLookup(const _EGLHashtable *table, EGLuint key)
+{
+ EGLuint pos;
+ const _EGLHashentry *entry;
+
+ assert(table);
+
+ if (!key)
+ return NULL;
+
+ pos = HASH_FUNC(key);
+ entry = table->Table[pos];
+ while (entry) {
+ if (entry->Key == key) {
+ return entry->Data;
+ }
+ entry = entry->Next;
+ }
+ return NULL;
+}
+
+
+
+/**
+ * Insert a key/pointer pair into the hash table.
+ * If an entry with this key already exists we'll replace the existing entry.
+ *
+ * \param table the hash table.
+ * \param key the key (not zero).
+ * \param data pointer to user data.
+ */
+void
+_eglHashInsert(_EGLHashtable *table, EGLuint key, void *data)
+{
+ /* search for existing entry with this key */
+ EGLuint pos;
+ _EGLHashentry *entry;
+
+ assert(table);
+ assert(key);
+
+ _eglLockMutex(table->Mutex);
+
+ if (key > table->MaxKey)
+ table->MaxKey = key;
+
+ pos = HASH_FUNC(key);
+ entry = table->Table[pos];
+ while (entry) {
+ if (entry->Key == key) {
+ /* replace entry's data */
+ entry->Data = data;
+ _eglUnlockMutex(table->Mutex);
+ return;
+ }
+ entry = entry->Next;
+ }
+
+ /* alloc and insert new table entry */
+ entry = (_EGLHashentry *) malloc(sizeof(_EGLHashentry));
+ entry->Key = key;
+ entry->Data = data;
+ entry->Next = table->Table[pos];
+ table->Table[pos] = entry;
+
+ _eglUnlockMutex(table->Mutex);
+}
+
+
+
+/**
+ * Remove an entry from the hash table.
+ *
+ * \param table the hash table.
+ * \param key key of entry to remove.
+ *
+ * While holding the hash table's lock, searches the entry with the matching
+ * key and unlinks it.
+ */
+void
+_eglHashRemove(_EGLHashtable *table, EGLuint key)
+{
+ EGLuint pos;
+ _EGLHashentry *entry, *prev;
+
+ assert(table);
+ assert(key);
+
+ _eglLockMutex(table->Mutex);
+
+ pos = HASH_FUNC(key);
+ prev = NULL;
+ entry = table->Table[pos];
+ while (entry) {
+ if (entry->Key == key) {
+ /* found it! */
+ if (prev) {
+ prev->Next = entry->Next;
+ }
+ else {
+ table->Table[pos] = entry->Next;
+ }
+ free(entry);
+ _eglUnlockMutex(table->Mutex);
+ return;
+ }
+ prev = entry;
+ entry = entry->Next;
+ }
+
+ _eglUnlockMutex(table->Mutex);
+}
+
+
+
+/**
+ * Get the key of the "first" entry in the hash table.
+ *
+ * This is used in the course of deleting all display lists when
+ * a context is destroyed.
+ *
+ * \param table the hash table
+ *
+ * \return key for the "first" entry in the hash table.
+ *
+ * While holding the lock, walks through all table positions until finding
+ * the first entry of the first non-empty one.
+ */
+EGLuint
+_eglHashFirstEntry(_EGLHashtable *table)
+{
+ EGLuint pos;
+ assert(table);
+ _eglLockMutex(table->Mutex);
+ for (pos = 0; pos < TABLE_SIZE; pos++) {
+ if (table->Table[pos]) {
+ _eglUnlockMutex(table->Mutex);
+ return table->Table[pos]->Key;
+ }
+ }
+ _eglUnlockMutex(table->Mutex);
+ return 0;
+}
+
+
+/**
+ * Given a hash table key, return the next key. This is used to walk
+ * over all entries in the table. Note that the keys returned during
+ * walking won't be in any particular order.
+ * \return next hash key or 0 if end of table.
+ */
+EGLuint
+_eglHashNextEntry(const _EGLHashtable *table, EGLuint key)
+{
+ const _EGLHashentry *entry;
+ EGLuint pos;
+
+ assert(table);
+ assert(key);
+
+ /* Find the entry with given key */
+ pos = HASH_FUNC(key);
+ entry = table->Table[pos];
+ while (entry) {
+ if (entry->Key == key) {
+ break;
+ }
+ entry = entry->Next;
+ }
+
+ if (!entry) {
+ /* the key was not found, we can't find next entry */
+ return 0;
+ }
+
+ if (entry->Next) {
+ /* return next in linked list */
+ return entry->Next->Key;
+ }
+ else {
+ /* look for next non-empty table slot */
+ pos++;
+ while (pos < TABLE_SIZE) {
+ if (table->Table[pos]) {
+ return table->Table[pos]->Key;
+ }
+ pos++;
+ }
+ return 0;
+ }
+}
+
+
+/**
+ * Dump contents of hash table for debugging.
+ *
+ * \param table the hash table.
+ */
+void
+_eglHashPrint(const _EGLHashtable *table)
+{
+ EGLuint i;
+ assert(table);
+ for (i = 0; i < TABLE_SIZE; i++) {
+ const _EGLHashentry *entry = table->Table[i];
+ while (entry) {
+ printf("%u %p\n", entry->Key, entry->Data);
+ entry = entry->Next;
+ }
+ }
+}
+
+
+
+/**
+ * Return a new, unused hash key.
+ */
+EGLuint
+_eglHashGenKey(_EGLHashtable *table)
+{
+ EGLuint k;
+
+ _eglLockMutex(table->Mutex);
+ k = table->MaxKey;
+ table->MaxKey++;
+ _eglUnlockMutex(table->Mutex);
+ return k;
+}
+
diff --git a/src/egl/main/eglhash.h b/src/egl/main/eglhash.h
new file mode 100644
index 000000000..1d6db9598
--- /dev/null
+++ b/src/egl/main/eglhash.h
@@ -0,0 +1,39 @@
+/**
+ * \file eglhash.h
+ * Generic hash table.
+ */
+
+
+#ifndef EGLHASH_INCLUDED
+#define EGLHASH_INCLUDED
+
+
+/* XXX move this? */
+typedef unsigned int EGLuint;
+
+
+typedef struct _egl_hashtable _EGLHashtable;
+
+
+extern _EGLHashtable *_eglNewHashTable(void);
+
+extern void _eglDeleteHashTable(_EGLHashtable *table);
+
+extern void *_eglHashLookup(const _EGLHashtable *table, EGLuint key);
+
+extern void _eglHashInsert(_EGLHashtable *table, EGLuint key, void *data);
+
+extern void _eglHashRemove(_EGLHashtable *table, EGLuint key);
+
+extern EGLuint _eglHashFirstEntry(_EGLHashtable *table);
+
+extern EGLuint _eglHashNextEntry(const _EGLHashtable *table, EGLuint key);
+
+extern void _eglHashPrint(const _EGLHashtable *table);
+
+extern EGLuint _eglHashGenKey(_EGLHashtable *table);
+
+extern void _egltest_hash_functions(void);
+
+
+#endif /* EGLHASH_INCLUDED */
diff --git a/src/egl/main/eglmode.c b/src/egl/main/eglmode.c
new file mode 100644
index 000000000..f2b17a6d2
--- /dev/null
+++ b/src/egl/main/eglmode.c
@@ -0,0 +1,199 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglmode.h"
+#include "eglglobals.h"
+#include "eglscreen.h"
+
+
+#define MIN2(A, B) (((A) < (B)) ? (A) : (B))
+
+
+static char *
+my_strdup(const char *s)
+{
+ int l = strlen(s);
+ char *s2 = malloc(l + 1);
+ strcpy(s2, s);
+ return s2;
+}
+
+
+/**
+ * Given an EGLModeMESA handle, return the corresponding _EGLMode object
+ * or null if non-existant.
+ */
+_EGLMode *
+_eglLookupMode(EGLDisplay dpy, EGLModeMESA mode)
+{
+ const _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ EGLint scrnum;
+
+ /* loop over all screens on the display */
+ for (scrnum = 0; scrnum < disp->NumScreens; scrnum++) {
+ const _EGLScreen *scrn = disp->Screens[scrnum];
+ EGLint i;
+ /* search list of modes for handle */
+ for (i = 0; i < scrn->NumModes; i++) {
+ if (scrn->Modes[i].Handle == mode) {
+ return scrn->Modes + i;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+
+/**
+ * Add a new mode with the given attributes (width, height, depth, refreshRate)
+ * to the given screen.
+ * Assign a new mode ID/handle to the mode as well.
+ * \return pointer to the new _EGLMode
+ */
+_EGLMode *
+_eglAddMode(_EGLScreen *screen, EGLint width, EGLint height,
+ EGLint refreshRate, const char *name)
+{
+ EGLint n;
+ _EGLMode *newModes;
+
+ assert(screen);
+ assert(width > 0);
+ assert(height > 0);
+ assert(refreshRate > 0);
+
+ n = screen->NumModes;
+ newModes = (_EGLMode *) realloc(screen->Modes, (n+1) * sizeof(_EGLMode));
+ if (newModes) {
+ screen->Modes = newModes;
+ screen->Modes[n].Handle = n + 1;
+ screen->Modes[n].Width = width;
+ screen->Modes[n].Height = height;
+ screen->Modes[n].RefreshRate = refreshRate;
+ screen->Modes[n].Stereo = EGL_FALSE;
+ screen->Modes[n].Name = my_strdup(name);
+ screen->NumModes++;
+ return screen->Modes + n;
+ }
+ else {
+ return NULL;
+ }
+}
+
+
+
+/**
+ * Search for the EGLMode that best matches the given attribute list.
+ */
+EGLBoolean
+_eglChooseModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes)
+{
+ EGLint i;
+
+ /* XXX incomplete */
+
+ for (i = 0; attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ case EGL_WIDTH:
+ i++;
+ break;
+ case EGL_HEIGHT:
+ i++;
+ break;
+ case EGL_REFRESH_RATE_MESA:
+ i++;
+ break;
+#if 0
+ case EGL_STEREO_MESA:
+ i++;
+ break;
+#endif
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglChooseMode");
+ return EGL_FALSE;
+ }
+ }
+
+ return EGL_TRUE;
+}
+
+
+
+/**
+ * Return all possible modes for the given screen
+ */
+EGLBoolean
+_eglGetModesMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes)
+{
+ _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+ EGLint i;
+
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, "eglGetModes");
+ return EGL_FALSE;
+ }
+
+ *num_modes = MIN2(modes_size, scrn->NumModes);
+ for (i = 0; i < *num_modes; i++) {
+ modes[i] = scrn->Modes[i].Handle;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query an attribute of a mode.
+ */
+EGLBoolean
+_eglGetModeAttribMESA(_EGLDriver *drv, EGLDisplay dpy,
+ EGLModeMESA mode, EGLint attribute, EGLint *value)
+{
+ _EGLMode *m = _eglLookupMode(dpy, mode);
+
+ switch (attribute) {
+ case EGL_MODE_ID_MESA:
+ *value = m->Handle;
+ break;
+ case EGL_WIDTH:
+ *value = m->Width;
+ break;
+ case EGL_HEIGHT:
+ *value = m->Height;
+ break;
+#if 0
+ case EGL_DEPTH_MESA:
+ *value = m->Depth;
+ break;
+#endif
+ case EGL_REFRESH_RATE_MESA:
+ *value = m->RefreshRate;
+ break;
+#if 0
+ case EGL_STEREO_MESA:
+ *value = m->Stereo;
+ break;
+#endif
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglGetModeAttrib");
+ return EGL_FALSE;
+ }
+ return EGL_TRUE;
+}
+
+
+const char *
+_eglQueryModeStringMESA(_EGLDriver *drv, EGLDisplay dpy, EGLModeMESA mode)
+{
+ _EGLMode *m = _eglLookupMode(dpy, mode);
+ return m->Name;
+}
+
+
diff --git a/src/egl/main/eglmode.h b/src/egl/main/eglmode.h
new file mode 100644
index 000000000..ed2f022d0
--- /dev/null
+++ b/src/egl/main/eglmode.h
@@ -0,0 +1,53 @@
+#ifndef EGLMODE_INCLUDED
+#define EGLMODE_INCLUDED
+
+#include "egltypedefs.h"
+
+
+/**
+ * Data structure which corresponds to an EGLModeMESA.
+ */
+struct _egl_mode
+{
+ EGLModeMESA Handle; /* the public/opaque handle which names this mode */
+ EGLint Width, Height; /* size in pixels */
+ EGLint RefreshRate; /* rate * 1000.0 */
+ EGLBoolean Stereo;
+ const char *Name;
+
+ /* Other possible attributes */
+ /* interlaced */
+ /* external sync */
+};
+
+
+extern _EGLMode *
+_eglLookupMode(EGLDisplay dpy, EGLModeMESA mode);
+
+
+extern _EGLMode *
+_eglAddMode(_EGLScreen *screen, EGLint width, EGLint height,
+ EGLint refreshRate, const char *name);
+
+
+extern EGLBoolean
+_eglChooseModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes);
+
+
+extern EGLBoolean
+_eglGetModesMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes);
+
+
+extern EGLBoolean
+_eglGetModeAttribMESA(_EGLDriver *drv, EGLDisplay dpy, EGLModeMESA mode,
+ EGLint attribute, EGLint *value);
+
+
+extern const char *
+_eglQueryModeStringMESA(_EGLDriver *drv, EGLDisplay dpy, EGLModeMESA mode);
+
+
+#endif /* EGLMODE_INCLUDED */
diff --git a/src/egl/main/eglscreen.c b/src/egl/main/eglscreen.c
new file mode 100644
index 000000000..e213e9a84
--- /dev/null
+++ b/src/egl/main/eglscreen.c
@@ -0,0 +1,334 @@
+/*
+ * Ideas for screen management extension to EGL.
+ *
+ * Each EGLDisplay has one or more screens (CRTs, Flat Panels, etc).
+ * The screens' handles can be obtained with eglGetScreensMESA().
+ *
+ * A new kind of EGLSurface is possible- one which can be directly scanned
+ * out on a screen. Such a surface is created with eglCreateScreenSurface().
+ *
+ * To actually display a screen surface on a screen, the eglShowSurface()
+ * function is called.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "egldisplay.h"
+#include "eglglobals.h"
+#include "eglmode.h"
+#include "eglconfig.h"
+#include "eglsurface.h"
+#include "eglscreen.h"
+
+
+/**
+ * Initialize an _EGLScreen object to default values.
+ */
+void
+_eglInitScreen(_EGLScreen *screen)
+{
+ /* just init to zero for now */
+ memset(screen, 0, sizeof(_EGLScreen));
+}
+
+
+/**
+ * Given a public screen handle, return the internal _EGLScreen object.
+ */
+_EGLScreen *
+_eglLookupScreen(EGLDisplay dpy, EGLScreenMESA screen)
+{
+ EGLint i;
+ _EGLDisplay *display = _eglLookupDisplay(dpy);
+
+ if (!display)
+ return NULL;
+
+ for (i = 0; i < display->NumScreens; i++) {
+ if (display->Screens[i]->Handle == screen)
+ return display->Screens[i];
+ }
+ return NULL;
+}
+
+
+/**
+ * Add the given _EGLScreen to the display's list of screens.
+ */
+void
+_eglAddScreen(_EGLDisplay *display, _EGLScreen *screen)
+{
+ EGLint n;
+
+ assert(display);
+ assert(screen);
+
+ screen->Handle = _eglAllocScreenHandle();
+ n = display->NumScreens;
+ display->Screens = realloc(display->Screens, (n+1) * sizeof(_EGLScreen *));
+ display->Screens[n] = screen;
+ display->NumScreens++;
+}
+
+
+
+EGLBoolean
+_eglGetScreensMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA *screens,
+ EGLint max_screens, EGLint *num_screens)
+{
+ _EGLDisplay *display = _eglLookupDisplay(dpy);
+ EGLint n;
+
+ if (!display) {
+ _eglError(EGL_BAD_DISPLAY, "eglGetScreensMESA");
+ return EGL_FALSE;
+ }
+
+ if (display->NumScreens > max_screens) {
+ n = max_screens;
+ }
+ else {
+ n = display->NumScreens;
+ }
+
+ if (screens) {
+ EGLint i;
+ for (i = 0; i < n; i++)
+ screens[i] = display->Screens[i]->Handle;
+ }
+ if (num_screens)
+ *num_screens = n;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Initialize the given _EGLSurface object. Do error checking.
+ * Assign it an EGLSurface handle and insert into hash table.
+ * \return EGLSurface handle or EGL_NO_SURFACE if error.
+ */
+EGLSurface
+_eglInitScreenSurface(_EGLSurface *surf, _EGLDriver *drv, EGLDisplay dpy,
+ EGLConfig config, const EGLint *attrib_list)
+{
+ EGLint width = 0, height = 0;
+ EGLint i;
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ case EGL_WIDTH:
+ width = attrib_list[++i];
+ break;
+ case EGL_HEIGHT:
+ height = attrib_list[++i];
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreateScreenSurfaceMESA");
+ return EGL_NO_SURFACE;
+ }
+ }
+
+ if (width <= 0 || height <= 0) {
+ _eglError(EGL_BAD_ATTRIBUTE,
+ "eglCreateScreenSurfaceMESA(width or height)");
+ return EGL_NO_SURFACE;
+ }
+
+ _eglInitSurface(surf);
+ surf->Width = width;
+ surf->Height = height;
+ surf->Type = EGL_SCREEN_BIT_MESA;
+ surf->Config = _eglLookupConfig(drv, dpy, config);
+
+ /* insert into hash table */
+ _eglSaveSurface(surf);
+ assert(surf->Handle);
+
+ return surf->Handle;
+}
+
+
+/**
+ * Create a drawing surface which can be directly displayed on a screen.
+ */
+EGLSurface
+_eglCreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ _EGLSurface *surf;
+ EGLSurface surface;
+
+ surf = (_EGLSurface *) malloc(sizeof(_EGLSurface));
+ surface = _eglInitScreenSurface(surf, drv, dpy, config, attrib_list);
+ if (surface == EGL_NO_SURFACE)
+ free(surf);
+
+ return surface;
+}
+
+
+/**
+ * Show the given surface on the named screen.
+ * If surface is EGL_NO_SURFACE, disable the screen's output.
+ *
+ * This is just a placeholder function; drivers will always override
+ * this with code that _really_ shows the surface.
+ */
+EGLBoolean
+_eglShowSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface surface, EGLModeMESA m)
+{
+ _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+ _EGLMode *mode = _eglLookupMode(dpy, m);
+
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, "eglShowSurfaceMESA");
+ return EGL_FALSE;
+ }
+ if (!mode && (m != EGL_NO_MODE_MESA )) {
+ _eglError(EGL_BAD_MODE_MESA, "eglShowSurfaceMESA");
+ return EGL_FALSE;
+ }
+
+ if (surface == EGL_NO_SURFACE) {
+ scrn->CurrentSurface = NULL;
+ } else {
+ _EGLSurface *surf = _eglLookupSurface(surface);
+ if (!surf || surf->Type != EGL_SCREEN_BIT_MESA) {
+ _eglError(EGL_BAD_SURFACE, "eglShowSurfaceMESA");
+ return EGL_FALSE;
+ }
+ if (surf->Width < mode->Width || surf->Height < mode->Height) {
+ _eglError(EGL_BAD_SURFACE,
+ "eglShowSurfaceMESA(surface smaller than screen size)");
+ return EGL_FALSE;
+ }
+
+ scrn->CurrentSurface = surf;
+ scrn->CurrentMode = mode;
+ }
+ return EGL_TRUE;
+}
+
+
+/**
+ * Set a screen's current display mode.
+ * Note: mode = EGL_NO_MODE is valid (turns off the screen)
+ *
+ * This is just a placeholder function; drivers will always override
+ * this with code that _really_ sets the mode.
+ */
+EGLBoolean
+_eglScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA mode)
+{
+ _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, "eglScreenModeMESA");
+ return EGL_FALSE;
+ }
+
+ scrn->CurrentMode = _eglLookupMode(dpy, mode);
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Set a screen's surface origin.
+ */
+EGLBoolean
+_eglScreenPositionMESA(_EGLDriver *drv, EGLDisplay dpy,
+ EGLScreenMESA screen, EGLint x, EGLint y)
+{
+ _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, "eglScreenPositionMESA");
+ return EGL_FALSE;
+ }
+
+ scrn->OriginX = x;
+ scrn->OriginY = y;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query a screen's current surface.
+ */
+EGLBoolean
+_eglQueryScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
+ EGLScreenMESA screen, EGLSurface *surface)
+{
+ const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+ if (scrn->CurrentSurface)
+ *surface = scrn->CurrentSurface->Handle;
+ else
+ *surface = EGL_NO_SURFACE;
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query a screen's current mode.
+ */
+EGLBoolean
+_eglQueryScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *mode)
+{
+ const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+ if (scrn->CurrentMode)
+ *mode = scrn->CurrentMode->Handle;
+ else
+ *mode = EGL_NO_MODE_MESA;
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglQueryScreenMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint attribute, EGLint *value)
+{
+ const _EGLScreen *scrn = _eglLookupScreen(dpy, screen);
+
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, "eglQueryScreenMESA");
+ return EGL_FALSE;
+ }
+
+ switch (attribute) {
+ case EGL_SCREEN_POSITION_MESA:
+ value[0] = scrn->OriginX;
+ value[1] = scrn->OriginY;
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglQueryScreenMESA");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+
+void
+_eglDestroyScreenModes(_EGLScreen *scrn)
+{
+ free(scrn->Modes);
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+void
+_eglDestroyScreen(_EGLScreen *scrn)
+{
+ _eglDestroyScreenModes(scrn);
+ free(scrn);
+}
+
diff --git a/src/egl/main/eglscreen.h b/src/egl/main/eglscreen.h
new file mode 100644
index 000000000..afc52a5ec
--- /dev/null
+++ b/src/egl/main/eglscreen.h
@@ -0,0 +1,83 @@
+#ifndef EGLSCREEN_INCLUDED
+#define EGLSCREEN_INCLUDED
+
+
+/* NOTE: there is no public EGLScreen type, we refers to screens with
+ * an integer.
+ */
+
+struct _egl_screen
+{
+ EGLScreenMESA Handle; /* The public/opaque handle which names this object */
+
+ _EGLMode *CurrentMode;
+ _EGLSurface *CurrentSurface;
+ EGLint OriginX, OriginY;
+
+ EGLint NumModes;
+ _EGLMode *Modes; /* array [NumModes] */
+};
+
+
+extern void
+_eglInitScreen(_EGLScreen *screen);
+
+
+extern _EGLScreen *
+_eglLookupScreen(EGLDisplay dpy, EGLScreenMESA screen);
+
+
+extern void
+_eglAddScreen(_EGLDisplay *display, _EGLScreen *screen);
+
+
+extern EGLBoolean
+_eglGetScreensMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA *screens, EGLint max_screens, EGLint *num_screens);
+
+
+extern EGLSurface
+_eglInitScreenSurface(_EGLSurface *surf, _EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
+
+
+extern EGLSurface
+_eglCreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglShowSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLSurface surface, EGLModeMESA mode);
+
+
+extern EGLBoolean
+_eglScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA mode);
+
+
+extern EGLBoolean
+_eglScreenPositionMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLint x, EGLint y);
+
+
+extern EGLBoolean
+_eglQueryDisplayMESA(_EGLDriver *drv, EGLDisplay dpy, EGLint attribute, EGLint *value);
+
+
+extern EGLBoolean
+_eglQueryScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
+ EGLScreenMESA screen, EGLSurface *surface);
+
+
+extern EGLBoolean
+_eglQueryScreenModeMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *mode);
+
+
+extern EGLBoolean
+_eglQueryScreenMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLint attribute, EGLint *value);
+
+
+extern void
+_eglDestroyScreenModes(_EGLScreen *scrn);
+
+
+extern void
+_eglDestroyScreen(_EGLScreen *scrn);
+
+
+#endif /* EGLSCREEN_INCLUDED */
diff --git a/src/egl/main/eglsurface.c b/src/egl/main/eglsurface.c
new file mode 100644
index 000000000..94b86fa98
--- /dev/null
+++ b/src/egl/main/eglsurface.c
@@ -0,0 +1,323 @@
+/**
+ * Surface-related functions.
+ *
+ * See the eglcontext.c file for comments that also apply here.
+ */
+
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "eglcontext.h"
+#include "eglconfig.h"
+#include "eglsurface.h"
+#include "eglglobals.h"
+#include "eglhash.h"
+
+
+void
+_eglInitSurface(_EGLSurface *surf)
+{
+ /* XXX fix this up */
+ memset(surf, 0, sizeof(_EGLSurface));
+}
+
+
+void
+_eglSaveSurface(_EGLSurface *surf)
+{
+ assert(surf);
+ surf->Handle = _eglHashGenKey(_eglGlobal.Contexts);
+ _eglHashInsert(_eglGlobal.Surfaces, surf->Handle, surf);
+}
+
+
+void
+_eglRemoveSurface(_EGLSurface *surf)
+{
+ _eglHashRemove(_eglGlobal.Surfaces, surf->Handle);
+}
+
+
+_EGLSurface *
+_eglLookupSurface(EGLSurface surf)
+{
+ _EGLSurface *c = (_EGLSurface *) _eglHashLookup(_eglGlobal.Surfaces, surf);
+ return c;
+}
+
+
+_EGLSurface *
+_eglGetCurrentSurface(EGLint readdraw)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ if (ctx) {
+ switch (readdraw) {
+ case EGL_DRAW:
+ return ctx->DrawSurface;
+ case EGL_READ:
+ return ctx->ReadSurface;
+ default:
+ return NULL;
+ }
+ }
+ return NULL;
+}
+
+
+EGLBoolean
+_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
+{
+ /* Basically just do error checking */
+ _EGLContext *context = _eglGetCurrentContext();
+ _EGLSurface *surface = _eglLookupSurface(draw);
+ if (context && context->DrawSurface != surface) {
+ _eglError(EGL_BAD_SURFACE, "eglSwapBuffers");
+ return EGL_FALSE;
+ }
+ if (surface == NULL) {
+ _eglError(EGL_BAD_SURFACE, "eglSwapBuffers");
+ return EGL_FALSE;
+ }
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglCopyBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, NativePixmapType target)
+{
+ /* XXX unfinished */
+ return EGL_FALSE;
+}
+
+
+EGLBoolean
+_eglQuerySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surf, EGLint attribute, EGLint *value)
+{
+ _EGLSurface *surface = _eglLookupSurface(surf);
+ if (surface == NULL) {
+ _eglError(EGL_BAD_SURFACE, "eglQuerySurface");
+ return EGL_FALSE;
+ }
+ switch (attribute) {
+ case EGL_WIDTH:
+ *value = surface->Width;
+ return EGL_TRUE;
+ case EGL_HEIGHT:
+ *value = surface->Height;
+ return EGL_TRUE;
+ case EGL_CONFIG_ID:
+ *value = GET_CONFIG_ATTRIB(surface->Config, EGL_CONFIG_ID);
+ return EGL_TRUE;
+ case EGL_TEXTURE_FORMAT:
+ /* texture attributes: only for pbuffers, no error otherwise */
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->TextureFormat;
+ return EGL_TRUE;
+ case EGL_TEXTURE_TARGET:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->TextureTarget;
+ return EGL_TRUE;
+ case EGL_MIPMAP_TEXTURE:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->MipmapTexture;
+ return EGL_TRUE;
+ case EGL_MIPMAP_LEVEL:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->MipmapLevel;
+ return EGL_TRUE;
+ case EGL_SURFACE_TYPE:
+ *value = surface->Type;
+ return EGL_TRUE;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglQuerySurface");
+ return EGL_FALSE;
+ }
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLSurface
+_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
+{
+ /* nothing - just a placeholder */
+ return EGL_NO_SURFACE;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLSurface
+_eglCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
+{
+ /* nothing - just a placeholder */
+ return EGL_NO_SURFACE;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLSurface
+_eglCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
+{
+ /* nothing - just a placeholder */
+ return EGL_NO_SURFACE;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLBoolean
+_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
+{
+ _EGLSurface *surf = _eglLookupSurface(surface);
+ if (surf) {
+ _eglHashRemove(_eglGlobal.Surfaces, surface);
+ if (surf->IsBound) {
+ surf->DeletePending = EGL_TRUE;
+ }
+ else {
+ free(surf);
+ }
+ return EGL_TRUE;
+ }
+ else {
+ _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
+ return EGL_FALSE;
+ }
+}
+
+
+/**
+ * Default fallback routine - drivers might override this.
+ */
+EGLBoolean
+_eglSurfaceAttrib(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surf, EGLint attribute, EGLint value)
+{
+ _EGLSurface *surface = _eglLookupSurface(surf);
+
+ if (surface == NULL) {
+ _eglError(EGL_BAD_SURFACE, "eglSurfaceAttrib");
+ return EGL_FALSE;
+ }
+
+ switch (attribute) {
+ case EGL_MIPMAP_LEVEL:
+ surface->MipmapLevel = value;
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglSurfaceAttrib");
+ return EGL_FALSE;
+ }
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglBindTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ /* XXX unfinished */
+ return EGL_FALSE;
+}
+
+
+EGLBoolean
+_eglReleaseTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ /* XXX unfinished */
+ return EGL_FALSE;
+}
+
+
+EGLBoolean
+_eglSwapInterval(_EGLDriver *drv, EGLDisplay dpy, EGLint interval)
+{
+ _EGLSurface *surf = _eglGetCurrentSurface(EGL_DRAW);
+ if (surf == NULL) {
+ _eglError(EGL_BAD_SURFACE, "eglSwapInterval");
+ return EGL_FALSE;
+ }
+ surf->SwapInterval = interval;
+ return EGL_TRUE;
+}
+
+
+
+/**
+ ** EGL Surface Utility Functions. This could be handy for device drivers.
+ **/
+
+
+/**
+ * Initialize the fields of the given _EGLSurface object from the other
+ * parameters. Do error checking too. Allocate EGLSurface handle and
+ * insert into hash table.
+ * \return EGLSurface handle or EGL_NO_SURFACE if any error
+ */
+EGLSurface
+_eglInitPbufferSurface(_EGLSurface *surface, _EGLDriver *drv, EGLDisplay dpy,
+ EGLConfig config, const EGLint *attrib_list)
+{
+ _EGLConfig *conf;
+ EGLint width = 0, height = 0, largest = 0;
+ EGLint texFormat = 0, texTarget = 0, mipmapTex = 0;
+ EGLint i;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, "eglCreatePbufferSurface");
+ return EGL_NO_SURFACE;
+ }
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ case EGL_WIDTH:
+ width = attrib_list[++i];
+ break;
+ case EGL_HEIGHT:
+ height = attrib_list[++i];
+ break;
+ case EGL_LARGEST_PBUFFER:
+ largest = attrib_list[++i];
+ break;
+ case EGL_TEXTURE_FORMAT:
+ texFormat = attrib_list[++i];
+ break;
+ case EGL_TEXTURE_TARGET:
+ texTarget = attrib_list[++i];
+ break;
+ case EGL_MIPMAP_TEXTURE:
+ mipmapTex = attrib_list[++i];
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
+ return EGL_NO_SURFACE;
+ }
+ }
+
+ if (width <= 0 || height <= 0) {
+ _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface(width or height)");
+ return EGL_NO_SURFACE;
+ }
+
+ surface->Config = conf;
+ surface->Type = EGL_PBUFFER_BIT;
+ surface->Width = width;
+ surface->Height = height;
+ surface->TextureFormat = texFormat;
+ surface->TextureTarget = texTarget;
+ surface->MipmapTexture = mipmapTex;
+ surface->MipmapLevel = 0;
+ surface->SwapInterval = 0;
+
+ /* insert into hash table */
+ _eglSaveSurface(surface);
+ assert(surface->Handle);
+
+ return surface->Handle;
+}
diff --git a/src/egl/main/eglsurface.h b/src/egl/main/eglsurface.h
new file mode 100644
index 000000000..4853edfcd
--- /dev/null
+++ b/src/egl/main/eglsurface.h
@@ -0,0 +1,100 @@
+#ifndef EGLSURFACE_INCLUDED
+#define EGLSURFACE_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+/**
+ * "Base" class for device driver surfaces.
+ */
+struct _egl_surface
+{
+ EGLSurface Handle; /* The public/opaque handle which names this object */
+ _EGLConfig *Config;
+
+ /* May need reference counting here */
+ EGLBoolean IsBound;
+ EGLBoolean DeletePending;
+
+ EGLint Type; /* one of EGL_WINDOW_BIT, EGL_PIXMAP_BIT or EGL_PBUFFER_BIT */
+ EGLint Width, Height;
+ EGLint TextureFormat, TextureTarget;
+ EGLint MipmapTexture, MipmapLevel;
+ EGLint SwapInterval;
+
+ /* If type == EGL_SCREEN_BIT: */
+ EGLint VisibleRefCount; /* number of screens I'm displayed on */
+};
+
+
+extern void
+_eglInitSurface(_EGLSurface *surf);
+
+
+extern void
+_eglSaveSurface(_EGLSurface *surf);
+
+
+extern void
+_eglRemoveSurface(_EGLSurface *surf);
+
+
+extern _EGLSurface *
+_eglLookupSurface(EGLSurface surf);
+
+
+extern _EGLSurface *
+_eglGetCurrentSurface(EGLint readdraw);
+
+
+extern EGLBoolean
+_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw);
+
+
+extern EGLBoolean
+_eglCopyBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, NativePixmapType target);
+
+
+extern EGLBoolean
+_eglQuerySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint *value);
+
+
+extern EGLSurface
+_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list);
+
+
+extern EGLSurface
+_eglCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list);
+
+
+extern EGLSurface
+_eglCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface);
+
+
+extern EGLBoolean
+_eglSurfaceAttrib(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value);
+
+
+extern EGLBoolean
+_eglBindTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer);
+
+
+extern EGLBoolean
+_eglReleaseTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface, EGLint buffer);
+
+
+extern EGLBoolean
+_eglSwapInterval(_EGLDriver *drv, EGLDisplay dpy, EGLint interval);
+
+
+
+extern EGLSurface
+_eglInitPbufferSurface(_EGLSurface *surface, _EGLDriver *drv, EGLDisplay dpy,
+ EGLConfig config, const EGLint *attrib_list);
+
+#endif /* EGLSURFACE_INCLUDED */
diff --git a/src/egl/main/egltypedefs.h b/src/egl/main/egltypedefs.h
new file mode 100644
index 000000000..a66fbed73
--- /dev/null
+++ b/src/egl/main/egltypedefs.h
@@ -0,0 +1,28 @@
+#ifndef EGLTYPEDEFS_INCLUDED
+#define EGLTYPEDEFS_INCLUDED
+
+
+#include <GLES/egl.h>
+
+
+typedef struct _egl_config _EGLConfig;
+
+typedef struct _egl_context _EGLContext;
+
+typedef struct _egl_display _EGLDisplay;
+
+typedef struct _egl_driver _EGLDriver;
+
+typedef struct _egl_mode _EGLMode;
+
+typedef struct _egl_screen _EGLScreen;
+
+typedef struct _egl_surface _EGLSurface;
+
+
+typedef void (*_EGLProc)();
+
+typedef _EGLDriver *(*_EGLMain_t)(_EGLDisplay *dpy);
+
+
+#endif /* EGLTYPEDEFS_INCLUDED */