/* * Create 2 GLX windows/contexts of possibly different types. * Attempt to share all texture objects between contexts. * (This test is based on mesa-demos/xdemos/manywin test.) * * Copyright (C) 2011 Imagination Technologies Ltd. All Rights Reserved. * * Based on manywin.c * Copyright (C) 2000 Brian Paul All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include /* * Each display/window/context: */ struct head { char DisplayName[1000]; Display *Dpy; Window Win; GLXContext Context; float Angle; char Renderer[1000]; char Vendor[1000]; char Version[1000]; }; #define MAX_HEADS 2 static struct head Heads[MAX_HEADS]; static int NumHeads = 0; static GLuint TexObj = 0; static void Error(const char *display, const char *msg) { fprintf(stderr, "Error on display %s - %s\n", XDisplayName(display), msg); exit(1); } static struct head * AddHead(const char *displayName, const char *name, Bool direct) { Display *dpy; Window win; GLXContext ctx; int attrib[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1, GLX_DOUBLEBUFFER, None }; int scrnum; XSetWindowAttributes attr; unsigned long mask; Window root; XVisualInfo *visinfo; int width = 90, height = 90; int xpos = 0, ypos = 0; if (NumHeads >= MAX_HEADS) return NULL; dpy = XOpenDisplay(displayName); if (!dpy) { Error(displayName, "Unable to open display"); return NULL; } scrnum = DefaultScreen(dpy); root = RootWindow(dpy, scrnum); visinfo = glXChooseVisual(dpy, scrnum, attrib); if (!visinfo) { Error(displayName, "Unable to find RGB, double-buffered visual"); return NULL; } /* window attributes */ xpos = (NumHeads % 10) * 100; ypos = (NumHeads / 10) * 100; printf("%d, %d\n", xpos, ypos); attr.background_pixel = 0; attr.border_pixel = 0; attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone); attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; win = XCreateWindow(dpy, root, xpos, ypos, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr); if (!win) { Error(displayName, "Couldn't create window"); return NULL; } { XSizeHints sizehints; sizehints.x = xpos; sizehints.y = ypos; sizehints.width = width; sizehints.height = height; sizehints.flags = USSize | USPosition; XSetNormalHints(dpy, win, &sizehints); XSetStandardProperties(dpy, win, name, name, None, (char **)NULL, 0, &sizehints); } ctx = glXCreateContext(dpy, visinfo, Heads[0].Context, direct); if (!ctx) { Error(displayName, "Couldn't create GLX context"); return NULL; } XMapWindow(dpy, win); if (!glXMakeCurrent(dpy, win, ctx)) { Error(displayName, "glXMakeCurrent failed"); printf("glXMakeCurrent failed in Redraw()\n"); return NULL; } if (NumHeads == 0) { /* create texture object now */ static const GLubyte checker[2][2][4] = { { {255, 255, 255, 255}, { 0, 0, 0, 255} }, { { 0, 0, 0, 0}, {255, 255, 255, 255} } }; glGenTextures(1, &TexObj); assert(TexObj); glBindTexture(GL_TEXTURE_2D, TexObj); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, checker); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } else { /* bind 0th context's texture in this context too */ assert(TexObj); glBindTexture(GL_TEXTURE_2D, TexObj); } glEnable(GL_TEXTURE_2D); /* save the info for this head */ { struct head *h = &Heads[NumHeads]; const char * tmp; if (strlen(name) + 1 > sizeof(h->DisplayName)) { Error(displayName, "name string overflow"); return NULL; } strcpy(h->DisplayName, name); h->Dpy = dpy; h->Win = win; h->Context = ctx; h->Angle = 0.0; tmp = (char *) glGetString(GL_VERSION); if (strlen(tmp) + 1 > sizeof(h->Version)) { Error(displayName, "GL_VERSION string overflow"); return NULL; } strcpy(h->Version, tmp); tmp = (char *) glGetString(GL_VENDOR); if (strlen(tmp) + 1 > sizeof(h->Vendor)) { Error(displayName, "GL_VENDOR string overflow"); return NULL; } strcpy(h->Vendor, tmp); tmp = (char *) glGetString(GL_RENDERER); if (strlen(tmp) + 1 > sizeof(h->Renderer)) { Error(displayName, "GL_RENDERER string overflow"); return NULL; } strcpy(h->Renderer, tmp); NumHeads++; return &Heads[NumHeads-1]; } } static void DestroyHeads(void) { int i; for (i = 0; i < NumHeads; i++) { XDestroyWindow(Heads[i].Dpy, Heads[i].Win); glXDestroyContext(Heads[i].Dpy, Heads[i].Context); XCloseDisplay(Heads[i].Dpy); } } static void Redraw(struct head *h) { if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) { Error(h->DisplayName, "glXMakeCurrent failed"); printf("glXMakeCurrent failed in Redraw()\n"); return; } h->Angle += 1.0; glShadeModel(GL_FLAT); glClearColor(0.5, 0.5, 0.5, 1.0); glClear(GL_COLOR_BUFFER_BIT); /* draw green triangle */ glColor3f(0.0, 1.0, 0.0); glPushMatrix(); glRotatef(h->Angle, 0, 0, 1); glBegin(GL_TRIANGLES); glTexCoord2f(0.5, 1.0); glVertex2f(0, 0.8); glTexCoord2f(0.0, 0.0); glVertex2f(-0.8, -0.7); glTexCoord2f(1.0, 0.0); glVertex2f(0.8, -0.7); glEnd(); glPopMatrix(); glXSwapBuffers(h->Dpy, h->Win); } static void Swap(struct head *h) { glXSwapBuffers(h->Dpy, h->Win); } static void Resize(const struct head *h, unsigned int width, unsigned int height) { if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) { Error(h->DisplayName, "glXMakeCurrent failed in Resize()"); return; } glFlush(); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } static void EventLoop(void) { while (1) { int i; for (i = 0; i < NumHeads; i++) { struct head *h = &Heads[i]; while (XPending(h->Dpy) > 0) { XEvent event; XNextEvent(h->Dpy, &event); if (event.xany.window == h->Win) { switch (event.type) { case Expose: Redraw(h); break; case ConfigureNotify: Resize(h, event.xconfigure.width, event.xconfigure.height); break; default: /*no-op*/ ; } } else { printf("window mismatch\n"); } } } /* redraw all windows */ for (i = 0; i < NumHeads; i++) { Redraw(&Heads[i]); } usleep(1); } } static void PrintInfo(const struct head *h) { printf("Name: %s\n", h->DisplayName); printf(" Display: %p\n", (void *) h->Dpy); printf(" Window: 0x%x\n", (int) h->Win); printf(" Context: 0x%lx\n", (long) h->Context); printf(" GL_VERSION: %s\n", h->Version); printf(" GL_VENDOR: %s\n", h->Vendor); printf(" GL_RENDERER: %s\n", h->Renderer); } void usage() { printf("heterogenous: open 2 glx windows with potentially heterogenous contexts\n"); printf("Usage:\n"); printf(" heterogenous [ style ]\n"); printf("Options:\n"); printf(" style - one of\n"); printf(" 1 (default) - First context is direct, second is indirect.\n"); printf(" 2 - First context is indirect, second is direct.\n"); printf(" 3 - Both contexts are direct.\n"); printf(" 4 - Both contexts are indirect.\n"); } int main(int argc, char *argv[]) { int firstDirect = True; int secondDirect = False; char name[100]; struct head *h; if (argc > 2) { usage(); return 0; } if (argc == 2) { switch(atoi(argv[1])) { case 1: firstDirect = True; secondDirect = False; break; case 2: firstDirect = False; secondDirect = True; break; case 3: firstDirect = True; secondDirect = True; break; case 4: firstDirect = False; secondDirect = False; break; default: printf("Unrecognised option %s\n", argv[1]); usage(); return 0; } } printf("Creating first context (%s)\n", firstDirect ? "Direct": "Indirect"); sprintf(name, "First (%s)", firstDirect ? "Direct": "Indirect"); h = AddHead(NULL, name, firstDirect); if (h) { PrintInfo(h); } printf("Creating second context (%s)\n", secondDirect ? "Direct": "Indirect"); sprintf(name, "Second (%s)", secondDirect ? "Direct": "Indirect"); h = AddHead(NULL, name, secondDirect); if (h) { PrintInfo(h); } EventLoop(); DestroyHeads(); return 0; }