diff options
author | Dave Airlie <airlied@redhat.com> | 2013-12-17 13:33:35 +1000 |
---|---|---|
committer | Dave Airlie <airlied@redhat.com> | 2013-12-17 13:33:35 +1000 |
commit | f19256be28dc1d008f6b84e7343398fdcf319350 (patch) | |
tree | 6785e36dc47f2a58e70f38f49741c4a4acd09b71 | |
parent | 829e3cc6da0ecc9ff48cf6c876166c4591b118fa (diff) |
add more bits
-rw-r--r-- | client.c | 199 | ||||
-rw-r--r-- | client_main.c | 13 | ||||
-rw-r--r-- | common.h | 13 | ||||
-rw-r--r-- | server_main.c | 12 |
4 files changed, 237 insertions, 0 deletions
diff --git a/client.c b/client.c new file mode 100644 index 0000000..512e975 --- /dev/null +++ b/client.c @@ -0,0 +1,199 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <dirent.h> +#include <errno.h> +#include <error.h> +#include <X11/Xlib.h> +#include "common.h" + +#include <EGL/egl.h> + +struct x11_window { + Window win; + EGLSurface egl_surface; +}; + +struct window { + struct display *display; + int width, height; + struct x11_window x11; +}; + +struct x11_display { + Display *dpy; + EGLDisplay egl_display; + EGLConfig egl_conf; + EGLContext egl_ctx; +}; +struct display { + struct x11_display x11; +}; + +struct client { + struct display *d; + struct window *w; +}; + +static void x11_window_create(struct window *w) +{ + Window root; + EGLBoolean b; + struct display *d = w->display; + XSetWindowAttributes swa; + root = DefaultRootWindow(d->x11.dpy); + + memset(&swa, 0, sizeof(XSetWindowAttributes)); + w->x11.win = XCreateWindow(d->x11.dpy, root, 0, 0, + w->width, w->height, 0, + CopyFromParent, InputOutput, + CopyFromParent, CWEventMask, + &swa); + + XMapWindow(d->x11.dpy, w->x11.win); + + + w->x11.egl_surface = eglCreateWindowSurface( d->x11.egl_display, + d->x11.egl_conf, + (EGLNativeWindowType)w->x11.win, NULL); + + if (!w->x11.egl_surface) + error(1, errno, "failed to init egl surface"); + + b = eglMakeCurrent(d->x11.egl_display, + w->x11.egl_surface, + w->x11.egl_surface, + d->x11.egl_ctx); + if (!b) + error(1, errno, "Cannot activate EGL context"); +} +static struct window *window_create(struct display *d) +{ + + struct window *w; + + w = calloc(1, sizeof(struct window)); + if (!w) + error(1, errno, "can't allocate window"); + + w->display = d; + w->width = 250; + w->height = 250; + + x11_window_create(w); + return w; +} + +static void x11_window_destroy(struct window *w) +{ + struct display *d = w->display; + + eglMakeCurrent(d->x11.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + eglDestroySurface(d->x11.egl_display, w->x11.egl_surface); + XDestroyWindow(d->x11.dpy, w->x11.win); +} +static void window_destroy(struct window *window) +{ + x11_window_destroy(window); + free(window); +} + +static void x11_display_init(struct display *d) +{ + static const EGLint conf_att[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 0, + EGL_NONE, + }; + static const EGLint ctx_att[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + EGLBoolean b; + EGLenum api; + EGLint major, minor, n; + d->x11.dpy = XOpenDisplay(NULL); + if (!d->x11.dpy) + error(1, errno, "Unable to open X server display"); + + d->x11.egl_display = eglGetDisplay((EGLNativeDisplayType)d->x11.dpy); + if (d->x11.egl_display == EGL_NO_DISPLAY) + error(1, errno, "Failed to get EGL display"); + + if (!eglInitialize(d->x11.egl_display, &major, &minor)) + error(1, errno, "Failed to init EGL display"); + fprintf(stderr, "EGL major/minor: %d.%d\n", major, minor); + fprintf(stderr, "EGL version: %s\n", + eglQueryString(d->x11.egl_display, EGL_VERSION)); + fprintf(stderr, "EGL vendor: %s\n", + eglQueryString(d->x11.egl_display, EGL_VENDOR)); + fprintf(stderr, "EGL extensions: %s\n", + eglQueryString(d->x11.egl_display, EGL_EXTENSIONS)); + + api = EGL_OPENGL_API; + b = eglBindAPI(api); + if (!b) + error(1, errno, "cannot bind OpenGLES API"); + + b = eglChooseConfig(d->x11.egl_display, conf_att, &d->x11.egl_conf, + 1, &n); + + if (!b || n != 1) + error(1, errno, "cannot find suitable EGL config"); + + d->x11.egl_ctx = eglCreateContext(d->x11.egl_display, + d->x11.egl_conf, + EGL_NO_CONTEXT, + ctx_att); + if (!d->x11.egl_ctx) + error(1, errno, "cannot create EGL context"); + + eglMakeCurrent(d->x11.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); +} + +static struct display *display_create(void) +{ + struct display *d; + + d = calloc(1, sizeof(*d)); + if (!d) + error(1, errno, "cannot allocate memory"); + + x11_display_init(d); + return d; +} + +static void display_destroy(struct display *d) +{ + XCloseDisplay(d->x11.dpy); + free(d); +} + +struct client *client_create(void) +{ + struct client *client; + client = calloc(1, sizeof(struct client)); + if (!client) + error(1, errno, "cannot allocate memory"); + + client->d = display_create(); + client->w = window_create(client->d); + return client; +} + +void client_destroy(struct client *client) +{ + window_destroy(client->w); + display_destroy(client->d); + + free(client); + +} diff --git a/client_main.c b/client_main.c new file mode 100644 index 0000000..fdca5d8 --- /dev/null +++ b/client_main.c @@ -0,0 +1,13 @@ +#include "common.h" +#include <pthread.h> + +int main(void) +{ + struct client *client; + + client = client_create(); + + client_destroy(client); + + return 0; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..0f0b047 --- /dev/null +++ b/common.h @@ -0,0 +1,13 @@ +#ifndef COMMON_H +#define COMMON_H + +struct server; + +struct server *server_create(void); +void server_destroy(struct server *server); + +struct client; +struct client *client_create(void); +void client_destroy(struct client *client); + +#endif diff --git a/server_main.c b/server_main.c new file mode 100644 index 0000000..3cecd16 --- /dev/null +++ b/server_main.c @@ -0,0 +1,12 @@ +#include "common.h" +#include <pthread.h> + +int main(void) +{ + struct server *server; + struct client *client; + server = server_create(); + + server_destroy(server); + return 0; +} |