]> git.cworth.org Git - apitrace/blobdiff - retrace/glws_glx.cpp
stash: Trace and replay of gnome-shell works
[apitrace] / retrace / glws_glx.cpp
index 1573bb9d69ec80c41fdb3db4d59db8d5bda2e2c4..b75945c8e8a8d3becbbf1239366356a9f3718dfb 100644 (file)
@@ -31,6 +31,8 @@
 #include "glproc.hpp"
 #include "glws.hpp"
 
+#include <cairo/cairo-xlib.h>
+
 
 namespace glws {
 
@@ -60,7 +62,8 @@ public:
 };
 
 
-static void describeEvent(const XEvent &event) {
+static void
+processEvent(XEvent &event) {
     if (0) {
         switch (event.type) {
         case ConfigureNotify:
@@ -83,6 +86,19 @@ static void describeEvent(const XEvent &event) {
         }
         std::cerr << " " << event.xany.window << "\n";
     }
+
+    switch (event.type) {
+    case KeyPress:
+        {
+            char buffer[32];
+            KeySym keysym;
+            XLookupString(&event.xkey, buffer, sizeof buffer - 1, &keysym, NULL);
+            if (keysym == XK_Escape) {
+                exit(0);
+            }
+        }
+        break;
+    }
 }
 
 class GlxDrawable : public Drawable
@@ -90,8 +106,8 @@ class GlxDrawable : public Drawable
 public:
     Window window;
 
-    GlxDrawable(const Visual *vis, int w, int h) :
-        Drawable(vis, w, h)
+    GlxDrawable(const Visual *vis, int w, int h, bool pbuffer) :
+        Drawable(vis, w, h, pbuffer)
     {
         XVisualInfo *visinfo = static_cast<const GlxVisual *>(visual)->visinfo;
 
@@ -102,7 +118,7 @@ public:
         attr.background_pixel = 0;
         attr.border_pixel = 0;
         attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
-        attr.event_mask = StructureNotifyMask;
+        attr.event_mask = StructureNotifyMask | KeyPressMask;
 
         unsigned long mask;
         mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
@@ -135,11 +151,24 @@ public:
         glXWaitX();
     }
 
+    GlxDrawable(const unsigned pixmap_id, int width, int height) :
+        Drawable(NULL, width, height, false)
+    {
+        window = pixmap_id;
+    }
+
+    void processKeys(void) {
+        XEvent event;
+        while (XCheckWindowEvent(display, window, StructureNotifyMask | KeyPressMask, &event)) {
+            processEvent(event);
+        }
+    }
+
     void waitForEvent(int type) {
         XEvent event;
         do {
-            XWindowEvent(display, window, StructureNotifyMask, &event);
-            describeEvent(event);
+            XWindowEvent(display, window, StructureNotifyMask | KeyPressMask, &event);
+            processEvent(event);
         } while (event.type != type);
     }
 
@@ -194,10 +223,11 @@ public:
 
     void swapBuffers(void) {
         glXSwapBuffers(display, window);
+
+        processKeys();
     }
 };
 
-
 class GlxContext : public Context
 {
 public:
@@ -215,6 +245,8 @@ public:
 
 void
 init(void) {
+    XInitThreads();
+
     display = XOpenDisplay(NULL);
     if (!display) {
         std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
@@ -290,9 +322,9 @@ createVisual(bool doubleBuffer, Profile profile) {
 }
 
 Drawable *
-createDrawable(const Visual *visual, int width, int height)
+createDrawable(const Visual *visual, int width, int height, bool pbuffer)
 {
-    return new GlxDrawable(visual, width, height);
+    return new GlxDrawable(visual, width, height, pbuffer);
 }
 
 Context *
@@ -368,10 +400,141 @@ processEvents(void) {
     while (XPending(display) > 0) {
         XEvent event;
         XNextEvent(display, &event);
-        describeEvent(event);
+        processEvent(event);
     }
     return true;
 }
 
+void
+createWindow(Drawable *drawable, const Visual *_visual)
+{
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+    const GlxVisual *visual = static_cast<const GlxVisual *>(_visual);
+
+    glXCreateWindow(display, visual->fbconfig, glxDrawable->window, NULL);
+}
+
+Drawable *
+createPixmap(unsigned width, unsigned height, unsigned depth)
+{
+    Pixmap pixmap;
+
+    pixmap = XCreatePixmap(display, DefaultRootWindow(display),
+                           width, height, depth);
+
+    return new GlxDrawable(pixmap, width, height);
+}
+
+Drawable *
+createGLPixmap(GLXFBConfig fbconfig, Drawable *_pixmap,
+             unsigned width, unsigned height, int *attrib_list)
+{
+    GlxDrawable *pixmap = static_cast<GlxDrawable *>(_pixmap);
+    Pixmap gl_pixmap;
+
+    gl_pixmap = glXCreatePixmap(display, fbconfig, pixmap->window, attrib_list);
+
+    return new GlxDrawable(gl_pixmap, width, height);
+}
+
+void
+destroyWindow(Drawable *drawable)
+{
+/*
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+
+    glXDestroyWindow(display, glxDrawable->window);
+*/
+}
+
+void
+bindTexImage(Drawable *drawable, int buffer)
+{
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+
+    glXBindTexImageEXT(display, glxDrawable->window, buffer, NULL);
+}
+
+void
+releaseTexImage(Drawable *drawable, int buffer)
+{
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+
+    glXReleaseTexImageEXT(display, glxDrawable->window, buffer);
+}
+
+void
+copySubBuffer(Drawable *drawable, int x, int y, int width, int height)
+{
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+
+    glXCopySubBufferMESA(display, glxDrawable->window, x, y, width, height);
+}
+
+void
+putImageData(glws::Drawable *drawable, char *data,
+             int width, int height, int depth,
+             int bits_per_pixel, int bytes_per_line, int byte_order)
+{
+    GlxDrawable *glxDrawable = static_cast<GlxDrawable *>(drawable);
+    GC gc;
+    XImage image;
+
+    image.width = width;
+    image.height = height;
+    image.format = ZPixmap;
+    image.data = data;
+    image.byte_order = byte_order;
+    image.bitmap_unit = 32;
+    image.bitmap_bit_order = byte_order;
+    image.bitmap_pad = 32;
+    image.depth = depth,
+    image.bytes_per_line = bytes_per_line;
+    image.bits_per_pixel = bits_per_pixel;
+    image.red_mask = 0;
+    image.green_mask = 0;
+    image.blue_mask = 0;
+    image.obdata = 0;
+
+    XInitImage(&image);
+
+    {
+        Window root;
+        int x_ignore, y_ignore;
+        unsigned width, height, depth, border_width_ignore;
+        XGetGeometry(display, glxDrawable->window, &root, &x_ignore, &y_ignore,
+                     &width, &height, &border_width_ignore, &depth);
+    }
+
+    gc = XCreateGC(display, glxDrawable->window, 0, NULL);
+    XPutImage(display, glxDrawable->window, gc, &image, 0, 0, 0, 0, width, height);
+
+    {
+        static int count = 0;
+        char filename[] = "put-image-data-X.png";
+        cairo_surface_t *surface;
+
+        surface = cairo_xlib_surface_create(display, glxDrawable->window, DefaultVisual(display, DefaultScreen(display)), width, height);
+
+        filename[15] = '0' + count;
+        cairo_surface_write_to_png(surface, filename);
+
+        cairo_surface_destroy(surface);
+        count++;
+    }
+
+}
+
+GLXFBConfig
+chooseConfig(int *attrib_list)
+{
+    GLXFBConfig *configs;
+    int nelements;
+    configs = glXChooseFBConfig(display, DefaultScreen(display), attrib_list, &nelements);
+    if (nelements)
+        return configs[nelements - 1];
+    else
+        return NULL;
+}
 
-} /* namespace glws */
+} /* drawable glws */