drisw: Simplify GC setup
[mesa.git] / src / glx / drisw_glx.c
index 7b99e49f000fec5204e7493668aadf779eaa379a..b3e00f9a4e010b47cca0e6b8a762efba830006ee 100644 (file)
 #include <assert.h>
 
 static Bool
-XCreateGCs(struct drisw_drawable * pdp,
-           Display * dpy, XID drawable, int visualid)
+driswCreateGCs(struct drisw_drawable * pdp,
+               Display * dpy, XID drawable, int visualid)
 {
-   XGCValues gcvalues;
    long visMask;
    XVisualInfo visTemp;
    int num_visuals;
 
-   /* create GC's */
    pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
-   pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
-
-   gcvalues.function = GXcopy;
-   gcvalues.graphics_exposures = False;
-   XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
-   XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
-   XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
 
    /* visual */
    visTemp.visualid = visualid;
@@ -73,11 +64,10 @@ handle_xerror(Display *dpy, XErrorEvent *event)
    (void) dpy;
 
    assert(xshm_opcode != -1);
-   if (event->request_code != xshm_opcode ||
-       event->minor_code != X_ShmAttach)
+   if (event->request_code != xshm_opcode)
       return 0;
 
-   xshm_error = 1;
+   xshm_error = event->error_code;
    return 0;
 }
 
@@ -147,10 +137,12 @@ XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
    if (pdp->ximage)
       XDestroyImage(pdp->ximage);
 
+   if (pdp->shminfo.shmid > 0)
+      XShmDetach(dpy, &pdp->shminfo);
+
    free(pdp->visinfo);
 
    XFreeGC(dpy, pdp->gc);
-   XFreeGC(dpy, pdp->swapgc);
 }
 
 /**
@@ -201,7 +193,8 @@ bytes_per_line(unsigned pitch_bits, unsigned mul)
 
 static void
 swrastXPutImage(__DRIdrawable * draw, int op,
-                int x, int y, int w, int h, int stride,
+                int srcx, int srcy, int x, int y,
+                int w, int h, int stride,
                 int shmid, char *data, void *loaderPrivate)
 {
    struct drisw_drawable *pdp = loaderPrivate;
@@ -209,24 +202,13 @@ swrastXPutImage(__DRIdrawable * draw, int op,
    Display *dpy = pdraw->psc->dpy;
    Drawable drawable;
    XImage *ximage;
-   GC gc;
+   GC gc = pdp->gc;
 
    if (!pdp->ximage || shmid != pdp->shminfo.shmid) {
       if (!XCreateDrawable(pdp, shmid, dpy))
          return;
    }
 
-   switch (op) {
-   case __DRI_SWRAST_IMAGE_OP_DRAW:
-      gc = pdp->gc;
-      break;
-   case __DRI_SWRAST_IMAGE_OP_SWAP:
-      gc = pdp->swapgc;
-      break;
-   default:
-      return;
-   }
-
    drawable = pdraw->xDrawable;
    ximage = pdp->ximage;
    ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
@@ -235,12 +217,12 @@ swrastXPutImage(__DRIdrawable * draw, int op,
    if (pdp->shminfo.shmid >= 0) {
       ximage->width = ximage->bytes_per_line / ((ximage->bits_per_pixel + 7)/ 8);
       ximage->height = h;
-      XShmPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h, False);
+      XShmPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h, False);
       XSync(dpy, False);
    } else {
       ximage->width = w;
       ximage->height = h;
-      XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
+      XPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h);
    }
    ximage->data = NULL;
 }
@@ -254,7 +236,21 @@ swrastPutImageShm(__DRIdrawable * draw, int op,
    struct drisw_drawable *pdp = loaderPrivate;
 
    pdp->shminfo.shmaddr = shmaddr;
-   swrastXPutImage(draw, op, x, y, w, h, stride, shmid,
+   swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, shmid,
+                   shmaddr + offset, loaderPrivate);
+}
+
+static void
+swrastPutImageShm2(__DRIdrawable * draw, int op,
+                   int x, int y,
+                   int w, int h, int stride,
+                  int shmid, char *shmaddr, unsigned offset,
+                  void *loaderPrivate)
+{
+   struct drisw_drawable *pdp = loaderPrivate;
+
+   pdp->shminfo.shmaddr = shmaddr;
+   swrastXPutImage(draw, op, x, 0, x, y, w, h, stride, shmid,
                    shmaddr + offset, loaderPrivate);
 }
 
@@ -263,7 +259,7 @@ swrastPutImage2(__DRIdrawable * draw, int op,
                 int x, int y, int w, int h, int stride,
                 char *data, void *loaderPrivate)
 {
-   swrastXPutImage(draw, op, x, y, w, h, stride, -1,
+   swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, -1,
                    data, loaderPrivate);
 }
 
@@ -272,7 +268,7 @@ swrastPutImage(__DRIdrawable * draw, int op,
                int x, int y, int w, int h,
                char *data, void *loaderPrivate)
 {
-   swrastXPutImage(draw, op, x, y, w, h, 0, -1,
+   swrastXPutImage(draw, op, 0, 0, x, y, w, h, 0, -1,
                    data, loaderPrivate);
 }
 
@@ -340,7 +336,7 @@ swrastGetImageShm(__DRIdrawable * read,
 }
 
 static const __DRIswrastLoaderExtension swrastLoaderExtension_shm = {
-   .base = {__DRI_SWRAST_LOADER, 4 },
+   .base = {__DRI_SWRAST_LOADER, 5 },
 
    .getDrawableInfo     = swrastGetDrawableInfo,
    .putImage            = swrastPutImage,
@@ -349,6 +345,7 @@ static const __DRIswrastLoaderExtension swrastLoaderExtension_shm = {
    .getImage2           = swrastGetImage2,
    .putImageShm         = swrastPutImageShm,
    .getImageShm         = swrastGetImageShm,
+   .putImageShm2        = swrastPutImageShm2,
 };
 
 static const __DRIextension *loader_extensions_shm[] = {
@@ -580,6 +577,9 @@ drisw_create_context_attribs(struct glx_screen *base,
                                  &api, &reset, &release, error))
       return NULL;
 
+   if (!dri2_check_no_error(flags, shareList, major_ver, error))
+      return NULL;
+
    /* Check the renderType value */
    if (!validate_renderType_against_config(config_base, renderType)) {
        return NULL;
@@ -622,6 +622,9 @@ drisw_create_context_attribs(struct glx_screen *base,
        * GLX_CONTEXT_*_BIT values.
        */
       ctx_attribs[num_ctx_attribs++] = flags;
+
+      if (flags & __DRI_CTX_FLAG_NO_ERROR)
+         pcp->base.noError = GL_TRUE;
    }
 
    pcp->base.renderType = renderType;
@@ -675,7 +678,7 @@ driswCreateDrawable(struct glx_screen *base, XID xDrawable,
    pdp->base.drawable = drawable;
    pdp->base.psc = &psc->base;
 
-   ret = XCreateGCs(pdp, psc->base.dpy, xDrawable, modes->visualID);
+   ret = driswCreateGCs(pdp, psc->base.dpy, xDrawable, modes->visualID);
    if (!ret) {
       free(pdp);
       return NULL;
@@ -801,9 +804,27 @@ driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
 static int
 check_xshm(Display *dpy)
 {
+   int (*old_handler)(Display *, XErrorEvent *);
+
    int ignore;
+   XShmSegmentInfo info = { 0, };
+
+   if (!XQueryExtension(dpy, "MIT-SHM", &xshm_opcode, &ignore, &ignore))
+      return False;
+
+   old_handler = XSetErrorHandler(handle_xerror);
+   XShmDetach(dpy, &info);
+   XSync(dpy, False);
+   (void) XSetErrorHandler(old_handler);
+
+   /* BadRequest means we're a remote client. If we were local we'd
+    * expect BadValue since 'info' has an invalid segment name.
+    */
+   if (xshm_error == BadRequest)
+      return False;
 
-   return XQueryExtension(dpy, "MIT-SHM", &xshm_opcode, &ignore, &ignore);
+   xshm_error = 0;
+   return True;
 }
 
 static struct glx_screen *