1 /*
   2  * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "X11SurfaceData.h"
  27 #include "GraphicsPrimitiveMgr.h"
  28 #include "Region.h"
  29 #include "Trace.h"
  30 
  31 /* Needed to define intptr_t */
  32 #include "gdefs.h"
  33 
  34 #include "jni_util.h"
  35 #include "awt_Component.h"
  36 #include "awt_GraphicsEnv.h"
  37 
  38 #include <dlfcn.h>
  39 
  40 #ifndef HEADLESS
  41 static JDgaLibInfo DgaLibInfoStub;
  42 static JDgaLibInfo theJDgaInfo;
  43 static JDgaLibInfo *pJDgaInfo = &DgaLibInfoStub;
  44 
  45 
  46 /**
  47  * This file contains support code for loops using the SurfaceData
  48  * interface to talk to an X11 drawable from native code.
  49  */
  50 
  51 typedef struct _X11RIPrivate {
  52     jint                lockType;
  53     jint                lockFlags;
  54     XImage              *img;
  55     int                 x, y;
  56 } X11RIPrivate;
  57 
  58 #define MAX(a,b) ((a) > (b) ? (a) : (b))
  59 #define MIN(a,b) ((a) < (b) ? (a) : (b))
  60 
  61 static LockFunc X11SD_Lock;
  62 static GetRasInfoFunc X11SD_GetRasInfo;
  63 static UnlockFunc X11SD_Unlock;
  64 static DisposeFunc X11SD_Dispose;
  65 static GetPixmapBgFunc X11SD_GetPixmapWithBg;
  66 static ReleasePixmapBgFunc X11SD_ReleasePixmapWithBg;
  67 #ifndef XAWT
  68 extern struct MComponentPeerIDs mComponentPeerIDs;
  69 #endif
  70 extern int J2DXErrHandler(Display *display, XErrorEvent *xerr);
  71 extern AwtGraphicsConfigDataPtr
  72     getGraphicsConfigFromComponentPeer(JNIEnv *env, jobject this);
  73 extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
  74 
  75 static int X11SD_FindClip(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
  76                           X11SDOps *xsdo);
  77 static int X11SD_ClipToRoot(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
  78                             X11SDOps *xsdo);
  79 static void X11SD_SwapBytes(X11SDOps *xsdo, XImage *img, int depth, int bpp);
  80 static XImage * X11SD_GetImage(JNIEnv *env, X11SDOps *xsdo,
  81                                SurfaceDataBounds *bounds,
  82                                jint lockFlags);
  83 
  84 extern jfieldID validID;
  85 
  86 static int nativeByteOrder;
  87 static jboolean dgaAvailable = JNI_FALSE;
  88 static jboolean useDGAWithPixmaps = JNI_FALSE;
  89 static jclass xorCompClass;
  90 
  91 jint useMitShmExt = CANT_USE_MITSHM;
  92 jint useMitShmPixmaps = CANT_USE_MITSHM;
  93 jint forceSharedPixmaps = JNI_FALSE;
  94 
  95 /* Cached shared image, one for all surface datas. */
  96 static XImage * cachedXImage;
  97 
  98 #endif /* !HEADLESS */
  99 
 100 jboolean XShared_initIDs(JNIEnv *env, jboolean allowShmPixmaps)
 101 {
 102 #ifndef HEADLESS
 103    union {
 104         char c[4];
 105         int i;
 106     } endian;
 107 
 108     endian.i = 0xff000000;
 109     nativeByteOrder = (endian.c[0]) ? MSBFirst : LSBFirst;
 110 
 111     dgaAvailable = JNI_FALSE;
 112 
 113     cachedXImage = NULL;
 114 
 115     if (sizeof(X11RIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
 116         JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
 117         return JNI_FALSE;
 118     }
 119 
 120 #ifdef MITSHM
 121     if (getenv("NO_AWT_MITSHM") == NULL &&
 122         getenv("NO_J2D_MITSHM") == NULL) {
 123         char * force;
 124         TryInitMITShm(env, &useMitShmExt, &useMitShmPixmaps);
 125 
 126         if(allowShmPixmaps) {
 127           useMitShmPixmaps = (useMitShmPixmaps == CAN_USE_MITSHM);
 128           force = getenv("J2D_PIXMAPS");
 129           if (force != NULL) {
 130               if (useMitShmPixmaps && (strcmp(force, "shared") == 0)) {
 131                   forceSharedPixmaps = JNI_TRUE;
 132               } else if (strcmp(force, "server") == 0) {
 133                   useMitShmPixmaps = JNI_FALSE;
 134               }
 135           }
 136         }else {
 137           useMitShmPixmaps = JNI_FALSE;
 138         }
 139     }
 140 
 141     return JNI_TRUE;
 142 #endif /* MITSHM */
 143 
 144 #endif /* !HEADLESS */
 145 }
 146 
 147 
 148 /*
 149  * Class:     sun_java2d_x11_X11SurfaceData
 150  * Method:    initIDs
 151  * Signature: (Ljava/lang/Class;Z)V
 152  */
 153 JNIEXPORT void JNICALL
 154 Java_sun_java2d_x11_X11SurfaceData_initIDs(JNIEnv *env, jclass xsd,
 155                                            jclass XORComp, jboolean tryDGA)
 156 {
 157 #ifndef HEADLESS
 158   if(XShared_initIDs(env, JNI_TRUE))
 159   {
 160     void *lib = 0;
 161 
 162     xorCompClass = (*env)->NewGlobalRef(env, XORComp);
 163 
 164     if (tryDGA && (getenv("NO_J2D_DGA") == NULL)) {
 165     /* we use RTLD_NOW because of bug 4032715 */
 166         lib = dlopen("libsunwjdga.so", RTLD_NOW);
 167     }
 168 
 169     if (lib != NULL) {
 170         JDgaStatus ret = JDGA_FAILED;
 171         void *sym = dlsym(lib, "JDgaLibInit");
 172         if (sym != NULL) {
 173             theJDgaInfo.display = awt_display;
 174             AWT_LOCK();
 175             ret = (*(JDgaLibInitFunc *)sym)(env, &theJDgaInfo);
 176             AWT_UNLOCK();
 177         }
 178         if (ret == JDGA_SUCCESS) {
 179             pJDgaInfo = &theJDgaInfo;
 180             dgaAvailable = JNI_TRUE;
 181             useDGAWithPixmaps = (getenv("USE_DGA_PIXMAPS") != NULL);
 182         } else {
 183             dlclose(lib);
 184             lib = NULL;
 185         }
 186     }
 187   }
 188 #endif /* !HEADLESS */
 189 }
 190 
 191 /*
 192  * Class:     sun_java2d_x11_X11SurfaceData
 193  * Method:    isDrawableValid
 194  * Signature: ()Z
 195  */
 196 JNIEXPORT jboolean JNICALL
 197 Java_sun_java2d_x11_XSurfaceData_isDrawableValid(JNIEnv *env, jobject this)
 198 {
 199     jboolean ret = JNI_FALSE;
 200 
 201 #ifndef HEADLESS
 202     X11SDOps *xsdo = X11SurfaceData_GetOps(env, this);
 203 
 204     AWT_LOCK();
 205     if (xsdo->drawable != 0 || X11SD_InitWindow(env, xsdo) == SD_SUCCESS) {
 206         ret = JNI_TRUE;
 207     }
 208     AWT_UNLOCK();
 209 #endif /* !HEADLESS */
 210 
 211     return ret;
 212 }
 213 
 214 /*
 215  * Class: sun_java2d_x11_X11SurfaceData
 216  * Method: isShmPMAvailable
 217  * Signature: ()Z
 218  */
 219 JNIEXPORT jboolean JNICALL
 220 Java_sun_java2d_x11_X11SurfaceData_isShmPMAvailable(JNIEnv *env, jobject this)
 221 {
 222 #if defined(HEADLESS) || !defined(MITSHM)
 223     return JNI_FALSE;
 224 #else
 225     return useMitShmPixmaps;
 226 #endif /* HEADLESS, MITSHM */
 227 }
 228 
 229 /*
 230  * Class:     sun_java2d_x11_X11SurfaceData
 231  * Method:    isDgaAvailable
 232  * Signature: ()Z
 233  */
 234 JNIEXPORT jboolean JNICALL
 235 Java_sun_java2d_x11_X11SurfaceData_isDgaAvailable(JNIEnv *env, jobject this)
 236 {
 237 #if defined(HEADLESS) || defined(__linux__)
 238     return JNI_FALSE;
 239 #else
 240     return dgaAvailable;
 241 #endif /* HEADLESS */
 242 }
 243 
 244 /*
 245  * Class:     sun_java2d_x11_X11SurfaceData
 246  * Method:    initOps
 247  * Signature: (Ljava/lang/Object;I)V
 248  */
 249 JNIEXPORT void JNICALL
 250 Java_sun_java2d_x11_XSurfaceData_initOps(JNIEnv *env, jobject xsd,
 251                                            jobject peer,
 252                                            jobject graphicsConfig, jint depth)
 253 {
 254 #ifndef HEADLESS
 255     X11SDOps *xsdo = (X11SDOps*)SurfaceData_InitOps(env, xsd, sizeof(X11SDOps));
 256     if (xsdo == NULL) {
 257         JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
 258         return;
 259     }
 260     xsdo->sdOps.Lock = X11SD_Lock;
 261     xsdo->sdOps.GetRasInfo = X11SD_GetRasInfo;
 262     xsdo->sdOps.Unlock = X11SD_Unlock;
 263     xsdo->sdOps.Dispose = X11SD_Dispose;
 264     xsdo->GetPixmapWithBg = X11SD_GetPixmapWithBg;
 265     xsdo->ReleasePixmapWithBg = X11SD_ReleasePixmapWithBg;
 266 #ifndef XAWT
 267     if (peer != NULL) {
 268         struct ComponentData *cdata;
 269         cdata = (struct ComponentData *)
 270             JNU_GetLongFieldAsPtr(env, peer, mComponentPeerIDs.pData);
 271         if (cdata == NULL) {
 272             JNU_ThrowNullPointerException(env, "Component data missing");
 273             return;
 274         }
 275         if (cdata->widget == NULL) {
 276             JNU_ThrowInternalError(env, "Widget is NULL in initOps");
 277             return;
 278         }
 279         xsdo->widget = cdata->widget;
 280     } else {
 281         xsdo->widget = NULL;
 282     }
 283 #else
 284     xsdo->widget = NULL;
 285     if (peer != NULL) {
 286         xsdo->drawable = JNU_CallMethodByName(env, NULL, peer, "getWindow", "()J").j;
 287     } else {
 288         xsdo->drawable = 0;
 289     }
 290 #endif
 291     xsdo->depth = depth;
 292     xsdo->dgaAvailable = dgaAvailable;
 293     xsdo->isPixmap = JNI_FALSE;
 294     xsdo->bitmask = 0;
 295     xsdo->bgPixel = 0;
 296     xsdo->isBgInitialized = JNI_FALSE;
 297 #ifdef MITSHM
 298     xsdo->shmPMData.shmSegInfo = NULL;
 299     xsdo->shmPMData.xRequestSent = JNI_FALSE;
 300     xsdo->shmPMData.pmSize = 0;
 301     xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
 302     xsdo->shmPMData.pixmap = 0;
 303     xsdo->shmPMData.shmPixmap = 0;
 304     xsdo->shmPMData.numBltsSinceRead = 0;
 305     xsdo->shmPMData.pixelsReadSinceBlt = 0;
 306     xsdo->shmPMData.numBltsThreshold = 2;
 307 #endif /* MITSHM */
 308 
 309     xsdo->configData = (AwtGraphicsConfigDataPtr)
 310         JNU_GetLongFieldAsPtr(env,
 311                               graphicsConfig,
 312                               x11GraphicsConfigIDs.aData);
 313     if (xsdo->configData == NULL) {
 314         JNU_ThrowNullPointerException(env,
 315                                       "Native GraphicsConfig data block missing");
 316         return;
 317     }
 318     if (depth > 12) {
 319         xsdo->pixelmask = (xsdo->configData->awt_visInfo.red_mask |
 320                            xsdo->configData->awt_visInfo.green_mask |
 321                            xsdo->configData->awt_visInfo.blue_mask);
 322     } else if (depth == 12) {
 323         xsdo->pixelmask = 0xfff;
 324     } else {
 325         xsdo->pixelmask = 0xff;
 326     }
 327 
 328     xsdo->xrPic = None;
 329 #endif /* !HEADLESS */
 330 }
 331 
 332 /*
 333  * Class:     sun_java2d_x11_X11SurfaceData
 334  * Method:    flushNativeSurface
 335  * Signature: ()V
 336  */
 337 JNIEXPORT void JNICALL
 338 Java_sun_java2d_x11_XSurfaceData_flushNativeSurface(JNIEnv *env, jobject xsd)
 339 {
 340 #ifndef HEADLESS
 341     SurfaceDataOps *ops = SurfaceData_GetOps(env, xsd);
 342 
 343     if (ops != NULL) {
 344         X11SD_Dispose(env, ops);
 345     }
 346 #endif /* !HEADLESS */
 347 }
 348 
 349 
 350 JNIEXPORT X11SDOps * JNICALL
 351 X11SurfaceData_GetOps(JNIEnv *env, jobject sData)
 352 {
 353 #ifdef HEADLESS
 354     return NULL;
 355 #else
 356     SurfaceDataOps *ops = SurfaceData_GetOps(env, sData);
 357     if (ops != NULL && ops->Lock != X11SD_Lock) {
 358         SurfaceData_ThrowInvalidPipeException(env, "not an X11 SurfaceData");
 359         ops = NULL;
 360     }
 361     return (X11SDOps *) ops;
 362 #endif /* !HEADLESS */
 363 }
 364 
 365 /*
 366  * Method for disposing X11SD-specific data
 367  */
 368 static void
 369 X11SD_Dispose(JNIEnv *env, SurfaceDataOps *ops)
 370 {
 371 #ifndef HEADLESS
 372     /* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
 373     X11SDOps * xsdo = (X11SDOps*)ops;
 374 
 375     AWT_LOCK();
 376 
 377     xsdo->invalid = JNI_TRUE;
 378     if (xsdo->isPixmap == JNI_TRUE && xsdo->drawable != 0) {
 379 #ifdef MITSHM
 380         if (xsdo->shmPMData.shmSegInfo != NULL) {
 381             X11SD_DropSharedSegment(xsdo->shmPMData.shmSegInfo);
 382             xsdo->shmPMData.shmSegInfo = NULL;
 383         }
 384         if (xsdo->shmPMData.pixmap) {
 385             XFreePixmap(awt_display, xsdo->shmPMData.pixmap);
 386             xsdo->shmPMData.pixmap = 0;
 387         }
 388         if (xsdo->shmPMData.shmPixmap) {
 389             XFreePixmap(awt_display, xsdo->shmPMData.shmPixmap);
 390             xsdo->shmPMData.shmPixmap = 0;
 391         }
 392 #else
 393         XFreePixmap(awt_display, xsdo->drawable);
 394 #endif /* MITSHM */
 395         xsdo->drawable = 0;
 396     }
 397     if (xsdo->bitmask != 0) {
 398         XFreePixmap(awt_display, xsdo->bitmask);
 399         xsdo->bitmask = 0;
 400     }
 401     if (xsdo->javaGC != NULL) {
 402         XFreeGC(awt_display, xsdo->javaGC);
 403         xsdo->javaGC = NULL;
 404     }
 405     if (xsdo->cachedGC != NULL) {
 406         XFreeGC(awt_display, xsdo->cachedGC);
 407         xsdo->cachedGC = NULL;
 408     }
 409 
 410     if(xsdo->xrPic != None) {
 411       XRenderFreePicture(awt_display, xsdo->xrPic);
 412     }
 413 
 414     AWT_UNLOCK();
 415 #endif /* !HEADLESS */
 416 }
 417 /*
 418  * Class:     sun_java2d_x11_X11SurfaceData
 419  * Method:    setInvalid
 420  * Signature: ()V
 421  */
 422 JNIEXPORT void JNICALL
 423 Java_sun_java2d_x11_XSurfaceData_setInvalid(JNIEnv *env, jobject xsd)
 424 {
 425 #ifndef HEADLESS
 426     X11SDOps *xsdo = (X11SDOps *) SurfaceData_GetOps(env, xsd);
 427 
 428     if (xsdo != NULL) {
 429         xsdo->invalid = JNI_TRUE;
 430     }
 431 #endif /* !HEADLESS */
 432 }
 433 
 434 
 435 jboolean XShared_initSurface(JNIEnv *env, X11SDOps *xsdo, jint depth, jint width, jint height, jlong drawable)
 436 {
 437 #ifndef HEADLESS
 438 
 439     if (drawable != (jlong)0) {
 440         /* Double-buffering */
 441         xsdo->drawable = drawable;
 442         xsdo->isPixmap = JNI_FALSE;
 443     } else {
 444         xsdo->isPixmap = JNI_TRUE;
 445         /* REMIND: workaround for bug 4420220 on pgx32 boards:
 446            don't use DGA with pixmaps unless USE_DGA_PIXMAPS is set.
 447          */
 448         xsdo->dgaAvailable = useDGAWithPixmaps;
 449 
 450         xsdo->pmWidth = width;
 451         xsdo->pmHeight = height;
 452 
 453 #ifdef MITSHM
 454         xsdo->shmPMData.pmSize = width * height * depth;
 455         xsdo->shmPMData.pixelsReadThreshold = width * height / 8;
 456         if (forceSharedPixmaps) {
 457             AWT_LOCK();
 458             xsdo->drawable = X11SD_CreateSharedPixmap(xsdo);
 459             AWT_UNLOCK();
 460             if (xsdo->drawable) {
 461                 xsdo->shmPMData.usingShmPixmap = JNI_TRUE;
 462                 xsdo->shmPMData.shmPixmap = xsdo->drawable;
 463                 return JNI_TRUE;
 464             }
 465         }
 466 #endif /* MITSHM */
 467 
 468         AWT_LOCK();
 469         xsdo->drawable =
 470             XCreatePixmap(awt_display,
 471                           RootWindow(awt_display,
 472                                      xsdo->configData->awt_visInfo.screen),
 473                           width, height, depth);
 474         AWT_UNLOCK();
 475 #ifdef MITSHM
 476         xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
 477         xsdo->shmPMData.pixmap = xsdo->drawable;
 478 #endif /* MITSHM */
 479     }
 480     if (xsdo->drawable == 0) {
 481         JNU_ThrowOutOfMemoryError(env,
 482                                   "Can't create offscreen surface");
 483         return JNI_FALSE;
 484     }
 485 
 486     return JNI_TRUE;
 487 #endif /* !HEADLESS */
 488 }
 489 
 490 
 491 /*
 492  * Class:     sun_java2d_x11_X11SurfaceData
 493  * Method:    initSurface
 494  * Signature: ()V
 495  */
 496 JNIEXPORT void JNICALL
 497 Java_sun_java2d_x11_X11SurfaceData_initSurface(JNIEnv *env, jclass xsd,
 498                                                jint depth,
 499                                                jint width, jint height,
 500                                                jlong drawable)
 501 {
 502 #ifndef HEADLESS
 503     X11SDOps *xsdo = X11SurfaceData_GetOps(env, xsd);
 504     if (xsdo == NULL) {
 505         return;
 506     }
 507 
 508     if (xsdo->configData->awt_cmap == (Colormap)NULL) {
 509         awtJNI_CreateColorData(env, xsdo->configData, 1);
 510     }
 511     /* color_data will be initialized in awtJNI_CreateColorData for
 512        8-bit visuals */
 513     xsdo->cData = xsdo->configData->color_data;
 514 
 515     XShared_initSurface(env, xsdo, depth, width, height, drawable);
 516     xsdo->xrPic = NULL;
 517 #endif /* !HEADLESS */
 518 }
 519 
 520 #ifndef HEADLESS
 521 
 522 #ifdef MITSHM
 523 
 524 void X11SD_DropSharedSegment(XShmSegmentInfo *shminfo)
 525 {
 526     if (shminfo != NULL) {
 527         XShmDetach(awt_display, shminfo);
 528         shmdt(shminfo->shmaddr);
 529 /*      REMIND: we don't need shmctl(shminfo->shmid, IPC_RMID, 0); here. */
 530 /*      Check X11SD_CreateSharedImage() for the explanation */
 531     }
 532 }
 533 
 534 XImage* X11SD_CreateSharedImage(X11SDOps *xsdo,
 535                                    jint width, jint height)
 536 {
 537     XImage *img = NULL;
 538     XShmSegmentInfo *shminfo;
 539 
 540     shminfo = malloc(sizeof(XShmSegmentInfo));
 541     if (shminfo == NULL) {
 542         return NULL;
 543     }
 544     memset(shminfo, 0, sizeof(XShmSegmentInfo));
 545 
 546     img = XShmCreateImage(awt_display, xsdo->configData->awt_visInfo.visual,
 547                           xsdo->depth, ZPixmap, NULL, shminfo,
 548                           width, height);
 549     if (img == NULL) {
 550         free((void *)shminfo);
 551         return NULL;
 552     }
 553     shminfo->shmid =
 554         shmget(IPC_PRIVATE, height * img->bytes_per_line, IPC_CREAT|0777);
 555     if (shminfo->shmid < 0) {
 556         J2dRlsTraceLn1(J2D_TRACE_ERROR,
 557                        "X11SD_SetupSharedSegment shmget has failed: %s",
 558                        strerror(errno));
 559         return NULL;
 560     }
 561 
 562     shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
 563     if (shminfo->shmaddr == ((char *) -1)) {
 564         shmctl(shminfo->shmid, IPC_RMID, 0);
 565         J2dRlsTraceLn1(J2D_TRACE_ERROR,
 566                        "X11SD_SetupSharedSegment shmat has failed: %s",
 567                        strerror(errno));
 568         return NULL;
 569     }
 570 
 571     shminfo->readOnly = False;
 572 
 573     resetXShmAttachFailed();
 574     EXEC_WITH_XERROR_HANDLER(J2DXErrHandler,
 575                              XShmAttach(awt_display, shminfo));
 576 
 577     /*
 578      * Once the XSync round trip has finished then we
 579      * can get rid of the id so that this segment does not stick
 580      * around after we go away, holding system resources.
 581      */
 582     shmctl(shminfo->shmid, IPC_RMID, 0);
 583 
 584     if (isXShmAttachFailed() == JNI_TRUE) {
 585         J2dRlsTraceLn1(J2D_TRACE_ERROR,
 586                        "X11SD_SetupSharedSegment XShmAttach has failed: %s",
 587                        strerror(errno));
 588         return NULL;
 589     }
 590 
 591     img->data = shminfo->shmaddr;
 592     img->obdata = (char *)shminfo;
 593 
 594     return img;
 595 }
 596 
 597 XImage* X11SD_GetSharedImage(X11SDOps *xsdo, jint width, jint height,
 598                              jboolean readBits)
 599 {
 600     XImage * retImage = NULL;
 601     if (cachedXImage != NULL &&
 602         X11SD_CachedXImageFits(width, height, xsdo->depth, readBits)) {
 603             /* sync so previous data gets flushed */
 604             XSync(awt_display, False);
 605             retImage = cachedXImage;
 606             cachedXImage = (XImage *)NULL;
 607     } else if (width * height * xsdo->depth > 0x10000) {
 608         retImage = X11SD_CreateSharedImage(xsdo, width, height);
 609     }
 610     return retImage;
 611 }
 612 
 613 Drawable X11SD_CreateSharedPixmap(X11SDOps *xsdo)
 614 {
 615     XShmSegmentInfo *shminfo;
 616     XImage *img = NULL;
 617     Drawable pixmap;
 618     int scan;
 619     int width = xsdo->pmWidth;
 620     int height = xsdo->pmHeight;
 621 
 622     if (xsdo->shmPMData.pmSize < 0x10000) {
 623         /* only use shared mem pixmaps for relatively big images */
 624         return 0;
 625     }
 626 
 627     /* need to create shared(!) image to get bytes_per_line */
 628     img = X11SD_CreateSharedImage(xsdo, width, height);
 629     if (img == NULL) {
 630         return 0;
 631     }
 632     scan = img->bytes_per_line;
 633     shminfo = (XShmSegmentInfo*)img->obdata;
 634     XFree(img);
 635 
 636     pixmap =
 637         XShmCreatePixmap(awt_display,
 638                          RootWindow(awt_display,
 639                                     xsdo->configData->awt_visInfo.screen),
 640                          shminfo->shmaddr, shminfo,
 641                          width, height, xsdo->depth);
 642     if (pixmap == 0) {
 643         X11SD_DropSharedSegment(shminfo);
 644         return 0;
 645     }
 646 
 647     xsdo->shmPMData.shmSegInfo = shminfo;
 648     xsdo->shmPMData.bytesPerLine = scan;
 649     return pixmap;
 650 }
 651 
 652 void X11SD_PuntPixmap(X11SDOps *xsdo, jint width, jint height)
 653 {
 654 
 655     if (useMitShmPixmaps != CAN_USE_MITSHM || forceSharedPixmaps) {
 656         return;
 657     }
 658 
 659     /* we wouldn't be here if it's a shared pixmap, so no check
 660      * for !usingShmPixmap.
 661      */
 662 
 663     xsdo->shmPMData.numBltsSinceRead = 0;
 664 
 665     xsdo->shmPMData.pixelsReadSinceBlt += width * height;
 666     if (xsdo->shmPMData.pixelsReadSinceBlt >
 667         xsdo->shmPMData.pixelsReadThreshold) {
 668         if (!xsdo->shmPMData.shmPixmap) {
 669             xsdo->shmPMData.shmPixmap =
 670                 X11SD_CreateSharedPixmap(xsdo);
 671         }
 672         if (xsdo->shmPMData.shmPixmap) {
 673             GC xgc = XCreateGC(awt_display, xsdo->shmPMData.shmPixmap, 0L, NULL);
 674             if (xgc != NULL) {
 675                 xsdo->shmPMData.usingShmPixmap = JNI_TRUE;
 676                 xsdo->drawable = xsdo->shmPMData.shmPixmap;
 677                 XCopyArea(awt_display,
 678                           xsdo->shmPMData.pixmap, xsdo->drawable, xgc,
 679                           0, 0, xsdo->pmWidth, xsdo->pmHeight, 0, 0);
 680                 XSync(awt_display, False);
 681                 xsdo->shmPMData.xRequestSent = JNI_FALSE;
 682                 XFreeGC(awt_display, xgc);
 683             }
 684         }
 685     }
 686 }
 687 
 688 void X11SD_UnPuntPixmap(X11SDOps *xsdo)
 689 {
 690     if (useMitShmPixmaps != CAN_USE_MITSHM || forceSharedPixmaps) {
 691         return;
 692     }
 693     xsdo->shmPMData.pixelsReadSinceBlt = 0;
 694     if (xsdo->shmPMData.numBltsSinceRead >=
 695         xsdo->shmPMData.numBltsThreshold)
 696     {
 697         if (xsdo->shmPMData.usingShmPixmap) {
 698             if (!xsdo->shmPMData.pixmap) {
 699                 xsdo->shmPMData.pixmap =
 700                     XCreatePixmap(awt_display,
 701                                   RootWindow(awt_display,
 702                                              xsdo->configData->awt_visInfo.screen),
 703                                   xsdo->pmWidth, xsdo->pmHeight, xsdo->depth);
 704             }
 705             if (xsdo->shmPMData.pixmap) {
 706                 GC xgc = XCreateGC(awt_display, xsdo->shmPMData.pixmap, 0L, NULL);
 707                 if (xgc != NULL) {
 708                     xsdo->drawable = xsdo->shmPMData.pixmap;
 709                     XCopyArea(awt_display,
 710                               xsdo->shmPMData.shmPixmap, xsdo->drawable, xgc,
 711                               0, 0, xsdo->pmWidth, xsdo->pmHeight, 0, 0);
 712                     XSync(awt_display, False);
 713                     XFreeGC(awt_display, xgc);
 714                     xsdo->shmPMData.xRequestSent = JNI_FALSE;
 715                     xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
 716                     xsdo->shmPMData.numBltsThreshold *= 2;
 717                 }
 718             }
 719         }
 720     } else {
 721         xsdo->shmPMData.numBltsSinceRead++;
 722     }
 723 }
 724 
 725 /**
 726  * Determines if the cached image can be used for current operation.
 727  * If the image is to be used to be read into by XShmGetImage,
 728  * it must be close enough to avoid excessive reading from the screen;
 729  * otherwise it should just be at least the size requested.
 730  */
 731 jboolean X11SD_CachedXImageFits(jint width, jint height, jint depth,
 732                                 jboolean readBits)
 733 {
 734     /* we assume here that the cached image exists */
 735     jint imgWidth = cachedXImage->width;
 736     jint imgHeight = cachedXImage->height;
 737 
 738     if (imgWidth < width || imgHeight < height || depth != cachedXImage->depth)  {
 739         /* doesn't fit if any of the cached image dimensions is smaller
 740            or the depths are different */
 741         return JNI_FALSE;
 742     }
 743 
 744     if (!readBits) {
 745         /* Not reading from this image, so any image at least of the
 746            size requested will do */
 747         return JNI_TRUE;
 748     }
 749 
 750     if ((imgWidth < width + 64) && (imgHeight < height + 64)) {
 751         /* Cached image's width/height shouldn't be more than 64 pixels
 752          * larger than requested, because the region in XShmGetImage
 753          * can't be specified and we don't want to read too much.
 754          */
 755         return JNI_TRUE;
 756     }
 757 
 758     return JNI_FALSE;
 759 }
 760 #endif /* MITSHM */
 761 
 762 jint X11SD_InitWindow(JNIEnv *env, X11SDOps *xsdo)
 763 {
 764     if (xsdo->isPixmap == JNI_TRUE) {
 765         return SD_FAILURE;
 766     }
 767 #ifndef XAWT
 768     if (!XtIsRealized(xsdo->widget)) {
 769         J2dTraceLn(J2D_TRACE_WARNING, "X11SD_InitWindow: widget is unrealized");
 770         /* AWT_UNLOCK(); unlock it in caller */
 771         return SD_FAILURE;
 772     }
 773     xsdo->drawable = XtWindow(xsdo->widget);
 774 #endif
 775     xsdo->cData = xsdo->configData->color_data;
 776 
 777     return SD_SUCCESS;
 778 }
 779 
 780 static jint X11SD_Lock(JNIEnv *env,
 781                        SurfaceDataOps *ops,
 782                        SurfaceDataRasInfo *pRasInfo,
 783                        jint lockflags)
 784 {
 785     X11SDOps *xsdo = (X11SDOps *) ops;
 786     X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);
 787     int ret = SD_SUCCESS;
 788 
 789     AWT_LOCK();
 790 
 791     if (xsdo->invalid) {
 792         AWT_UNLOCK();
 793         SurfaceData_ThrowInvalidPipeException(env, "bounds changed");
 794         return SD_FAILURE;
 795     }
 796 #ifdef XAWT
 797     xsdo->cData = xsdo->configData->color_data;
 798 #endif
 799     if (xsdo->drawable == 0 && X11SD_InitWindow(env, xsdo) == SD_FAILURE) {
 800         AWT_UNLOCK();
 801         return SD_FAILURE;
 802     }
 803     if ((lockflags & SD_LOCK_LUT) != 0 &&
 804         (xsdo->cData == NULL ||
 805          xsdo->cData->awt_icmLUT == NULL))
 806     {
 807         AWT_UNLOCK();
 808         JNU_ThrowNullPointerException(env, "colormap lookup table");
 809         return SD_FAILURE;
 810     }
 811     if ((lockflags & SD_LOCK_INVCOLOR) != 0 &&
 812         (xsdo->cData == NULL ||
 813          xsdo->cData->img_clr_tbl == NULL ||
 814          xsdo->cData->img_oda_red == NULL ||
 815          xsdo->cData->img_oda_green == NULL ||
 816          xsdo->cData->img_oda_blue == NULL))
 817     {
 818         AWT_UNLOCK();
 819         JNU_ThrowNullPointerException(env, "inverse colormap lookup table");
 820         return SD_FAILURE;
 821     }
 822     if ((lockflags & SD_LOCK_INVGRAY) != 0 &&
 823         (xsdo->cData == NULL ||
 824          xsdo->cData->pGrayInverseLutData == NULL))
 825     {
 826         AWT_UNLOCK();
 827         JNU_ThrowNullPointerException(env, "inverse gray lookup table");
 828         return SD_FAILURE;
 829     }
 830     if (xsdo->dgaAvailable && (lockflags & (SD_LOCK_RD_WR))) {
 831         int dgaret;
 832 
 833         dgaret = (*pJDgaInfo->pGetLock)(env, awt_display, &xsdo->dgaDev,
 834                                         xsdo->drawable, &xsdo->surfInfo,
 835                                         pRasInfo->bounds.x1,
 836                                         pRasInfo->bounds.y1,
 837                                         pRasInfo->bounds.x2,
 838                                         pRasInfo->bounds.y2);
 839         if (dgaret == JDGA_SUCCESS) {
 840             int wx = xsdo->surfInfo.window.lox;
 841             int wy = xsdo->surfInfo.window.loy;
 842             pRasInfo->bounds.x1 = xsdo->surfInfo.visible.lox - wx;
 843             pRasInfo->bounds.y1 = xsdo->surfInfo.visible.loy - wy;
 844             pRasInfo->bounds.x2 = xsdo->surfInfo.visible.hix - wx;
 845             pRasInfo->bounds.y2 = xsdo->surfInfo.visible.hiy - wy;
 846             xpriv->lockType = X11SD_LOCK_BY_DGA;
 847             xpriv->lockFlags = lockflags;
 848             return SD_SUCCESS;
 849         } else if (dgaret == JDGA_UNAVAILABLE) {
 850             xsdo->dgaAvailable = JNI_FALSE;
 851         }
 852     }
 853     if (lockflags & SD_LOCK_RD_WR) {
 854         if (lockflags & SD_LOCK_FASTEST) {
 855             ret = SD_SLOWLOCK;
 856         }
 857         xpriv->lockType = X11SD_LOCK_BY_XIMAGE;
 858         if (xsdo->isPixmap) {
 859 #ifdef MITSHM
 860             if (xsdo->shmPMData.usingShmPixmap) {
 861                 xpriv->lockType = X11SD_LOCK_BY_SHMEM;
 862             }
 863 #endif /* MITSHM */
 864             if (pRasInfo->bounds.x1 < 0) {
 865                 pRasInfo->bounds.x1 = 0;
 866             }
 867             if (pRasInfo->bounds.y1 < 0) {
 868                 pRasInfo->bounds.y1 = 0;
 869             }
 870             if (pRasInfo->bounds.x2 > xsdo->pmWidth) {
 871                 pRasInfo->bounds.x2 = xsdo->pmWidth;
 872             }
 873             if (pRasInfo->bounds.y2 > xsdo->pmHeight) {
 874                 pRasInfo->bounds.y2 = xsdo->pmHeight;
 875             }
 876         }
 877     } else {
 878         /* They didn't lock for anything - we won't give them anything */
 879         xpriv->lockType = X11SD_LOCK_BY_NULL;
 880     }
 881     xpriv->lockFlags = lockflags;
 882     xpriv->img = NULL;
 883 
 884     return ret;
 885     /* AWT_UNLOCK() called in Unlock */
 886 }
 887 
 888 static void X11SD_GetRasInfo(JNIEnv *env,
 889                              SurfaceDataOps *ops,
 890                              SurfaceDataRasInfo *pRasInfo)
 891 {
 892     X11SDOps *xsdo = (X11SDOps *) ops;
 893     X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);
 894     jint lockFlags = xpriv->lockFlags;
 895     jint depth = xsdo->depth;
 896     int mult = xsdo->configData->pixelStride;
 897 
 898     if (xsdo->dgaAvailable &&
 899         xpriv->lockType == X11SD_LOCK_BY_XIMAGE &&
 900         (lockFlags & SD_LOCK_FASTEST))
 901     {
 902         /* Try one more time to use DGA (now with smaller bounds)... */
 903         int dgaret;
 904 
 905         dgaret = (*pJDgaInfo->pGetLock)(env, awt_display, &xsdo->dgaDev,
 906                                         xsdo->drawable, &xsdo->surfInfo,
 907                                         pRasInfo->bounds.x1,
 908                                         pRasInfo->bounds.y1,
 909                                         pRasInfo->bounds.x2,
 910                                         pRasInfo->bounds.y2);
 911         if (dgaret == JDGA_SUCCESS) {
 912             int wx = xsdo->surfInfo.window.lox;
 913             int wy = xsdo->surfInfo.window.loy;
 914             pRasInfo->bounds.x1 = xsdo->surfInfo.visible.lox - wx;
 915             pRasInfo->bounds.y1 = xsdo->surfInfo.visible.loy - wy;
 916             pRasInfo->bounds.x2 = xsdo->surfInfo.visible.hix - wx;
 917             pRasInfo->bounds.y2 = xsdo->surfInfo.visible.hiy - wy;
 918             xpriv->lockType = X11SD_LOCK_BY_DGA;
 919         } else if (dgaret == JDGA_UNAVAILABLE) {
 920             xsdo->dgaAvailable = JNI_FALSE;
 921         }
 922     }
 923 
 924     if (xpriv->lockType == X11SD_LOCK_BY_DGA) {
 925         int scan = xsdo->surfInfo.surfaceScan;
 926         int wx = xsdo->surfInfo.window.lox;
 927         int wy = xsdo->surfInfo.window.loy;
 928         pRasInfo->rasBase =
 929             (void *)(((uintptr_t) xsdo->surfInfo.basePtr) + (scan*wy + wx) * mult);
 930         pRasInfo->pixelStride = mult;
 931         pRasInfo->pixelBitOffset = 0;
 932         pRasInfo->scanStride = scan * mult;
 933 #ifdef MITSHM
 934     } else if (xpriv->lockType == X11SD_LOCK_BY_SHMEM) {
 935         if (xsdo->shmPMData.xRequestSent == JNI_TRUE) {
 936             /* need to sync before using shared mem pixmap
 937              if any x calls were issued for this pixmap */
 938             XSync(awt_display, False);
 939             xsdo->shmPMData.xRequestSent = JNI_FALSE;
 940         }
 941         xpriv->x = pRasInfo->bounds.x1;
 942         xpriv->y = pRasInfo->bounds.y1;
 943         pRasInfo->rasBase = xsdo->shmPMData.shmSegInfo->shmaddr;
 944         pRasInfo->pixelStride = mult;
 945         pRasInfo->pixelBitOffset = 0;
 946         pRasInfo->scanStride = xsdo->shmPMData.bytesPerLine;
 947 #endif /* MITSHM */
 948     } else if (xpriv->lockType == X11SD_LOCK_BY_XIMAGE) {
 949         int x, y, w, h;
 950         x = pRasInfo->bounds.x1;
 951         y = pRasInfo->bounds.y1;
 952         w = pRasInfo->bounds.x2 - x;
 953         h = pRasInfo->bounds.y2 - y;
 954 
 955         xpriv->img = X11SD_GetImage(env, xsdo, &pRasInfo->bounds, lockFlags);
 956         if (xpriv->img) {
 957             int scan = xpriv->img->bytes_per_line;
 958             xpriv->x = x;
 959             xpriv->y = y;
 960             pRasInfo->rasBase = xpriv->img->data - x * mult - y * scan;
 961             pRasInfo->pixelStride = mult;
 962             pRasInfo->pixelBitOffset = 0;
 963             pRasInfo->scanStride = scan;
 964         } else {
 965             pRasInfo->rasBase = NULL;
 966             pRasInfo->pixelStride = 0;
 967             pRasInfo->pixelBitOffset = 0;
 968             pRasInfo->scanStride = 0;
 969         }
 970     } else {
 971         /* They didn't lock for anything - we won't give them anything */
 972         pRasInfo->rasBase = NULL;
 973         pRasInfo->pixelStride = 0;
 974         pRasInfo->pixelBitOffset = 0;
 975         pRasInfo->scanStride = 0;
 976     }
 977     if (lockFlags & SD_LOCK_LUT) {
 978         pRasInfo->lutBase = (jint *) xsdo->cData->awt_icmLUT;
 979         pRasInfo->lutSize = xsdo->cData->awt_numICMcolors;
 980     } else {
 981         pRasInfo->lutBase = NULL;
 982         pRasInfo->lutSize = 0;
 983     }
 984     if (lockFlags & SD_LOCK_INVCOLOR) {
 985         pRasInfo->invColorTable = xsdo->cData->img_clr_tbl;
 986         pRasInfo->redErrTable = xsdo->cData->img_oda_red;
 987         pRasInfo->grnErrTable = xsdo->cData->img_oda_green;
 988         pRasInfo->bluErrTable = xsdo->cData->img_oda_blue;
 989     } else {
 990         pRasInfo->invColorTable = NULL;
 991         pRasInfo->redErrTable = NULL;
 992         pRasInfo->grnErrTable = NULL;
 993         pRasInfo->bluErrTable = NULL;
 994     }
 995     if (lockFlags & SD_LOCK_INVGRAY) {
 996         pRasInfo->invGrayTable = xsdo->cData->pGrayInverseLutData;
 997     } else {
 998         pRasInfo->invGrayTable = NULL;
 999     }
1000 }
1001 
1002 static void X11SD_Unlock(JNIEnv *env,
1003                          SurfaceDataOps *ops,
1004                          SurfaceDataRasInfo *pRasInfo)
1005 {
1006     X11SDOps *xsdo = (X11SDOps *) ops;
1007     X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);
1008 
1009     if (xpriv->lockType == X11SD_LOCK_BY_DGA) {
1010         (*pJDgaInfo->pReleaseLock)(env, xsdo->dgaDev, xsdo->drawable);
1011     } else if (xpriv->lockType == X11SD_LOCK_BY_XIMAGE &&
1012                xpriv->img != NULL)
1013     {
1014         if (xpriv->lockFlags & SD_LOCK_WRITE) {
1015             int x = xpriv->x;
1016             int y = xpriv->y;
1017             int w = pRasInfo->bounds.x2 - x;
1018             int h = pRasInfo->bounds.y2 - y;
1019             Drawable drawable = xsdo->drawable;
1020             GC xgc = xsdo->cachedGC;
1021             if (xgc == NULL) {
1022                 xsdo->cachedGC = xgc =
1023                     XCreateGC(awt_display, drawable, 0L, NULL);
1024             }
1025 
1026             if (xpriv->img->byte_order != nativeByteOrder) {
1027                 /* switching bytes back in 24 and 32 bpp cases. */
1028                 /* For 16 bit XLib will switch for us.          */
1029                 if (xsdo->depth > 16) {
1030                     X11SD_SwapBytes(xsdo, xpriv->img, xsdo->depth,
1031                         xsdo->configData->awtImage->wsImageFormat.bits_per_pixel);
1032                 }
1033             }
1034 
1035 #ifdef MITSHM
1036             if (xpriv->img->obdata != NULL) {
1037                 XShmPutImage(awt_display, drawable, xgc,
1038                              xpriv->img, 0, 0, x, y, w, h, False);
1039                 XFlush(awt_display);
1040             } else {
1041                 XPutImage(awt_display, drawable, xgc,
1042                           xpriv->img, 0, 0, x, y, w, h);
1043             }
1044             if (xsdo->shmPMData.usingShmPixmap) {
1045                 xsdo->shmPMData.xRequestSent = JNI_TRUE;
1046             }
1047 #else
1048             XPutImage(awt_display, drawable, xgc,
1049                       xpriv->img, 0, 0, x, y, w, h);
1050 #endif /* MITSHM */
1051 
1052             (*pJDgaInfo->pXRequestSent)(env, xsdo->dgaDev, drawable);
1053         }
1054         X11SD_DisposeOrCacheXImage(xpriv->img);
1055         xpriv->img = (XImage *)NULL;
1056     }
1057     /* the background pixel is not valid anymore */
1058     if (xpriv->lockFlags & SD_LOCK_WRITE) {
1059         xsdo->isBgInitialized = JNI_FALSE;
1060     }
1061     xpriv->lockType = X11SD_LOCK_UNLOCKED;
1062     AWT_UNLOCK();
1063 }
1064 
1065 static int
1066 X11SD_ClipToRoot(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
1067                  X11SDOps *xsdo)
1068 {
1069     Position x1=0, y1=0, x2=0, y2=0;
1070     int tmpx, tmpy;
1071     Window tmpchild;
1072 
1073 #ifndef XAWT
1074     Widget w = xsdo->widget;
1075 
1076     x1 = y1 = 0;
1077     for (; w != NULL && ! XtIsShell(w); w = w->core.parent) {
1078         x1 += w->core.x + w->core.border_width;
1079         y1 += w->core.y + w->core.border_width;
1080     }
1081     if (w == NULL) {
1082         return FALSE;
1083     }
1084 
1085     /*
1086      * REMIND: We should not be offsetting here by border_width
1087      * but for some unknown reason if we do not do that the
1088      * results will be off exactly by border_width. We were unable
1089      * to find cause of this.
1090      */
1091     (void) XTranslateCoordinates(XtDisplay(w), XtWindow(w),
1092                                  RootWindowOfScreen(XtScreen(w)),
1093                                  (int) w->core.border_width,
1094                                  (int) w->core.border_width,
1095                                  &tmpx, &tmpy, &tmpchild);
1096 #else
1097     Window window = (Window)(xsdo->drawable); /* is always a Window */
1098     XWindowAttributes winAttr;
1099 
1100     Status status = XGetWindowAttributes(awt_display, window, &winAttr);
1101     if (status == 0) {
1102         /* Failure, X window no longer valid. */
1103         return FALSE;
1104     }
1105     if (!XTranslateCoordinates(awt_display, window,
1106                                RootWindowOfScreen(winAttr.screen),
1107                                0, 0, &tmpx, &tmpy, &tmpchild)) {
1108         return FALSE;
1109     }
1110 #endif
1111 
1112     x1 = -(x1 + tmpx);
1113     y1 = -(y1 + tmpy);
1114 
1115     x2 = x1 + DisplayWidth(awt_display, xsdo->configData->awt_visInfo.screen);
1116     y2 = y1 + DisplayHeight(awt_display, xsdo->configData->awt_visInfo.screen);
1117 
1118     x1 = MAX(bounds->x1, x1);
1119     y1 = MAX(bounds->y1, y1);
1120     x2 = MIN(bounds->x2, x2);
1121     y2 = MIN(bounds->y2, y2);
1122     if ((x1 >= x2) || (y1 >= y2)) {
1123         return FALSE;
1124     }
1125     b->x1 = x1;
1126     b->y1 = y1;
1127     b->x2 = x2;
1128     b->y2 = y2;
1129 
1130     return TRUE;
1131 }
1132 
1133 /*
1134  * x1, y1, x2, y2 - our rectangle in the coord system of
1135  * the widget
1136  * px1, xy1, px2, py2 - current parent rect coords in the
1137  * same system
1138  */
1139 static int
1140 X11SD_FindClip(SurfaceDataBounds *b, SurfaceDataBounds *bounds, X11SDOps *xsdo)
1141 {
1142 #ifndef XAWT
1143     int x1, y1, x2, y2, px1, py1, px2, py2, child_x, child_y;
1144     Widget current_widget, child_widget;
1145 
1146     XWindowAttributes attr;
1147     Window ignore_root, current_window, *ignore_children;
1148     unsigned int pborder, ignore_uint;
1149 
1150     x1 = bounds->x1;
1151     y1 = bounds->y1;
1152     x2 = bounds->x2;
1153     y2 = bounds->y2;
1154 
1155     px1 = py1 = 0;
1156 
1157     child_widget = xsdo->widget;
1158     current_widget = XtParent(xsdo->widget);
1159     while (current_widget != NULL && !XtIsShell(current_widget)) {
1160         px1 = px1 - (child_widget->core.x + child_widget->core.border_width);
1161         py1 = py1 - (child_widget->core.y + child_widget->core.border_width);
1162         px2 = px1 + current_widget->core.width;
1163         py2 = py1 + current_widget->core.height;
1164         x1 = MAX(x1, px1);
1165         y1 = MAX(y1, py1);
1166         x2 = MIN(x2, px2);
1167         y2 = MIN(y2, py2);
1168         if ((x1 >= x2) || (y1 >= y2)) {
1169             return FALSE;
1170         }
1171 
1172         child_widget = current_widget;
1173         current_widget = current_widget->core.parent;
1174     }
1175 
1176     if (current_widget == NULL) {
1177         XQueryTree(awt_display,
1178                    XtWindow(child_widget),
1179                    &ignore_root,
1180                    &current_window,
1181                    &ignore_children,
1182                    &ignore_uint);
1183         XFree(ignore_children);
1184     } else {
1185         current_window = XtWindow(current_widget);
1186     }
1187 
1188     child_x = child_widget->core.x + child_widget->core.border_width;
1189     child_y = child_widget->core.y + child_widget->core.border_width;
1190     while (current_window != 0) {
1191         px1 = px1 - child_x;
1192         py1 = py1 - child_y;
1193         if (!XGetGeometry(awt_display, current_window, &ignore_root,
1194                           &child_x, &child_y,
1195                           (unsigned int *)&px2, (unsigned int *)&py2,
1196                           &pborder, &ignore_uint)) {
1197             return FALSE;
1198         }
1199         child_x += pborder;
1200         child_y += pborder;
1201         px2 += px1;
1202         py2 += py1;
1203 
1204         x1 = MAX(x1, px1);
1205         y1 = MAX(y1, py1);
1206         x2 = MIN(x2, px2);
1207         y2 = MIN(y2, py2);
1208         if ((x1 >= x2) || (y1 >= y2)) {
1209             return FALSE;
1210         }
1211         XQueryTree(awt_display,
1212                    current_window,
1213                    &ignore_root,
1214                    &current_window,
1215                    &ignore_children,
1216                    &ignore_uint);
1217         XFree(ignore_children);
1218     }
1219 
1220     b->x1 = x1;
1221     b->y1 = y1;
1222     b->x2 = x2;
1223     b->y2 = y2;
1224 #endif
1225     return TRUE;
1226 }
1227 
1228 static void
1229 X11SD_SwapBytes(X11SDOps *xsdo, XImage * img, int depth, int bpp) {
1230     int lengthInBytes = img->height * img->bytes_per_line;
1231     int i;
1232 
1233     switch (depth) {
1234     case 12:
1235     case 15:
1236     case 16:
1237         {
1238             /* AB -> BA */
1239             unsigned short *d = (unsigned short *)img->data;
1240             unsigned short t;
1241             for (i = 0; i < lengthInBytes/2; i++) {
1242                 t = *d;
1243                 *d++ = (t >> 8) | (t << 8);
1244             }
1245             img->byte_order = nativeByteOrder;
1246             img->bitmap_bit_order = nativeByteOrder;
1247             break;
1248         }
1249     case 24:
1250         {
1251             /* ABC -> CBA */
1252             if (bpp == 24) {
1253                 // 4517321: Only swap if we have a "real" ThreeByteBgr
1254                 // visual (denoted by a red_mask of 0xff).  Due to ambiguity
1255                 // in the X11 spec, it appears that the swap is not required
1256                 // on Linux configurations that use 24 bits per pixel (denoted
1257                 // by a red_mask of 0xff0000).
1258                 if (xsdo->configData->awt_visInfo.red_mask == 0xff) {
1259                     int scan = img->bytes_per_line;
1260                     unsigned char *d = (unsigned char *) img->data;
1261                     unsigned char *d1;
1262                     unsigned int t;
1263                     int j;
1264 
1265                     for (i = 0; i < img->height; i++, d += scan) {
1266                         d1 = d;
1267                         for (j = 0; j < img->width; j++, d1 += 3) {
1268                             /* not obvious opt from XLib src */
1269                             t = d1[0]; d1[0] = d1[2]; d1[2] = t;
1270                         }
1271                     }
1272                 }
1273                 break;
1274             }
1275         }
1276         /* FALL THROUGH for 32-bit case */
1277     case 32:
1278         {
1279             /* ABCD -> DCBA */
1280             unsigned int *d = (unsigned int *) img->data;
1281             unsigned int t;
1282             for (i = 0; i < lengthInBytes/4; i++) {
1283                 t = *d;
1284                 *d++ = ((t >> 24) |
1285                         ((t >> 8) & 0xff00) |
1286                         ((t & 0xff00) << 8) |
1287                         (t << 24));
1288             }
1289             break;
1290         }
1291     }
1292 }
1293 
1294 static XImage * X11SD_GetImage(JNIEnv *env, X11SDOps *xsdo,
1295                                SurfaceDataBounds *bounds,
1296                                jint lockFlags)
1297 {
1298     int x, y, w, h;
1299     int scan;
1300     XImage * img = NULL;
1301     Drawable drawable;
1302     int depth = xsdo->depth;
1303     int mult = xsdo->configData->pixelStride;
1304     int pad = (mult == 3) ? 32 : mult * 8; // pad must be 8, 16, or 32
1305     jboolean readBits = lockFlags & SD_LOCK_NEED_PIXELS;
1306 
1307     x = bounds->x1;
1308     y = bounds->y1;
1309     w = bounds->x2 - x;
1310     h = bounds->y2 - y;
1311 
1312 #ifdef MITSHM
1313     if (useMitShmExt == CAN_USE_MITSHM) {
1314         if (xsdo->isPixmap && readBits) {
1315             X11SD_PuntPixmap(xsdo, w, h);
1316         }
1317         img = X11SD_GetSharedImage(xsdo, w, h, readBits);
1318     }
1319 #endif /* MITSHM */
1320     drawable = xsdo->drawable;
1321 
1322     if (readBits) {
1323 #ifdef MITSHM
1324         if (img != NULL) {
1325             if (!XShmGetImage(awt_display, drawable, img, x, y, -1)) {
1326                 X11SD_DisposeOrCacheXImage(img);
1327                 img = NULL;
1328             }
1329         }
1330         if (img == NULL) {
1331             img = XGetImage(awt_display, drawable, x, y, w, h, -1, ZPixmap);
1332             if (img != NULL) {
1333                 img->obdata = NULL;
1334             }
1335         }
1336 #else
1337         img = XGetImage(awt_display, drawable, x, y, w, h, -1, ZPixmap);
1338 #endif /* MITSHM */
1339         if (img == NULL) {
1340             SurfaceDataBounds temp;
1341             img = XCreateImage(awt_display,
1342                                xsdo->configData->awt_visInfo.visual,
1343                                depth, ZPixmap, 0, NULL, w, h, pad, 0);
1344             if (img == NULL) {
1345                 return NULL;
1346             }
1347 
1348             scan = img->bytes_per_line;
1349             img->data = malloc(h * scan);
1350             if (img->data == NULL) {
1351                 XFree(img);
1352                 return NULL;
1353             }
1354 
1355             if (xsdo->isPixmap == JNI_FALSE &&
1356                 X11SD_ClipToRoot(&temp, bounds, xsdo)) {
1357 
1358                 XImage * temp_image;
1359                 temp_image = XGetImage(awt_display, drawable,
1360                                        temp.x1, temp.y1,
1361                                        temp.x2 - temp.x1,
1362                                        temp.y2 - temp.y1,
1363                                        -1, ZPixmap);
1364                 if (temp_image == NULL) {
1365                     XGrabServer(awt_display);
1366                     if (X11SD_FindClip(&temp, bounds, xsdo)) {
1367                         temp_image =
1368                             XGetImage(awt_display, drawable,
1369                                       temp.x1, temp.y1,
1370                                       temp.x2 - temp.x1,
1371                                       temp.y2 - temp.y1,
1372                                       -1, ZPixmap);
1373                     }
1374                     XUngrabServer(awt_display);
1375                     /* Workaround for bug 5039226 */
1376                     XSync(awt_display, False);
1377                 }
1378                 if (temp_image != NULL) {
1379                     int temp_scan, bytes_to_copy;
1380                     char * img_addr, * temp_addr;
1381                     int i;
1382 
1383                     img_addr = img->data +
1384                         (temp.y1 - y) * scan + (temp.x1 - x) * mult;
1385                     temp_scan = temp_image->bytes_per_line;
1386                     temp_addr = temp_image->data;
1387                     bytes_to_copy = (temp.x2 - temp.x1) * mult;
1388                     for (i = temp.y1; i < temp.y2; i++) {
1389                         memcpy(img_addr, temp_addr, bytes_to_copy);
1390                         img_addr += scan;
1391                         temp_addr += temp_scan;
1392                     }
1393                     XDestroyImage(temp_image);
1394                 }
1395             }
1396             img->obdata = NULL;
1397         }
1398         if (depth > 8 && img->byte_order != nativeByteOrder) {
1399             X11SD_SwapBytes(xsdo, img, depth,
1400                 xsdo->configData->awtImage->wsImageFormat.bits_per_pixel);
1401         }
1402     } else {
1403         /*
1404          * REMIND: This might be better to move to the Lock function
1405          * to avoid lengthy I/O pauses inside what may be a critical
1406          * section.  This will be more critical when SD_LOCK_READ is
1407          * implemented.  Another solution is to cache the pixels
1408          * to avoid reading for every operation.
1409          */
1410         if (img == NULL) {
1411             img = XCreateImage(awt_display,
1412                                xsdo->configData->awt_visInfo.visual,
1413                                depth, ZPixmap, 0, NULL, w, h, pad, 0);
1414             if (img == NULL) {
1415                 return NULL;
1416             }
1417 
1418             img->data = malloc(h * img->bytes_per_line);
1419             if (img->data == NULL) {
1420                 XFree(img);
1421                 return NULL;
1422             }
1423 
1424             img->obdata = NULL;
1425 
1426             if (img->byte_order != nativeByteOrder &&
1427                 (depth == 15 || depth == 16 || depth == 12)) {
1428                 /* bytes will be swapped by XLib. */
1429                 img->byte_order = nativeByteOrder;
1430                 img->bitmap_bit_order = nativeByteOrder;
1431             }
1432         }
1433     }
1434     return img;
1435 }
1436 
1437 void X11SD_DisposeOrCacheXImage(XImage * image) {
1438     /* REMIND: might want to check if the new image worth caching. */
1439     /* Cache only shared images. Passed image is assumed to be non-null. */
1440     if (image->obdata != NULL) {
1441         if (cachedXImage != NULL) {
1442             X11SD_DisposeXImage(cachedXImage);
1443         }
1444         cachedXImage = image;
1445     } else {
1446         X11SD_DisposeXImage(image);
1447     }
1448 }
1449 
1450 void X11SD_DisposeXImage(XImage * image) {
1451     if (image != NULL) {
1452 #ifdef MITSHM
1453         if (image->obdata != NULL) {
1454             X11SD_DropSharedSegment((XShmSegmentInfo*)image->obdata);
1455         } else {
1456             free(image->data);
1457         }
1458 #else
1459         free(image->data);
1460 #endif /* MITSHM */
1461         XFree(image);
1462     }
1463 }
1464 
1465 static JDgaStatus
1466     GetLockStub(JNIEnv *env, Display *display, void **dgaDev,
1467                 Drawable d, JDgaSurfaceInfo *pSurface,
1468                 jint lox, jint loy, jint hix, jint hiy)
1469 {
1470     return JDGA_UNAVAILABLE;
1471 }
1472 
1473 static JDgaStatus
1474     ReleaseLockStub(JNIEnv *env, void *dgaDev, Drawable d)
1475 {
1476     return JDGA_FAILED;
1477 }
1478 
1479 static void
1480     XRequestSentStub(JNIEnv *env, void *dgaDev, Drawable d)
1481 {
1482 }
1483 
1484 static void
1485     LibDisposeStub(JNIEnv *env)
1486 {
1487 }
1488 
1489 static JDgaLibInfo DgaLibInfoStub = {
1490     NULL,
1491     GetLockStub,
1492     ReleaseLockStub,
1493     XRequestSentStub,
1494     LibDisposeStub,
1495 };
1496 
1497 void X11SD_LibDispose(JNIEnv *env) {
1498     AWT_LOCK();
1499     if (pJDgaInfo != NULL) {
1500         pJDgaInfo->pLibDispose(env);
1501         pJDgaInfo = &DgaLibInfoStub;
1502     }
1503     AWT_UNLOCK();
1504 }
1505 
1506 void
1507 X11SD_DirectRenderNotify(JNIEnv *env, X11SDOps *xsdo)
1508 {
1509 #ifdef MITSHM
1510     if (xsdo->shmPMData.usingShmPixmap) {
1511         xsdo->shmPMData.xRequestSent = JNI_TRUE;
1512     }
1513 #endif /* MITSHM */
1514     (*pJDgaInfo->pXRequestSent)(env, xsdo->dgaDev, xsdo->drawable);
1515     awt_output_flush();
1516 }
1517 
1518 /*
1519  * Sets transparent pixels in the pixmap to
1520  * the specified solid background color and returns it.
1521  * Doesn't update source pixmap unless the color of the
1522  * transparent pixels is different from the specified color.
1523  *
1524  * Note: The AWT lock must be held by the current thread
1525  * while calling into this method.
1526  */
1527 static Drawable
1528 X11SD_GetPixmapWithBg(JNIEnv *env, X11SDOps *xsdo, jint pixel)
1529 {
1530     /* assert AWT_CHECK_HAVE_LOCK(); */
1531 
1532     if (xsdo->invalid) {
1533         AWT_UNLOCK();
1534         SurfaceData_ThrowInvalidPipeException(env, "bounds changed");
1535         return 0;
1536     }
1537 
1538     /* the image doesn't have transparency, just return it */
1539     if (xsdo->bitmask == 0) {
1540         /* don't need to unlock here, the caller will unlock through
1541            the release call */
1542         return xsdo->drawable;
1543     }
1544 
1545     /* Check if current color of the transparent pixels is different
1546        from the specified one */
1547     if (xsdo->isBgInitialized == JNI_FALSE || xsdo->bgPixel != pixel) {
1548         GC srcGC;
1549         GC bmGC;
1550 
1551         if (xsdo->drawable == 0) {
1552             AWT_UNLOCK();
1553             return 0;
1554         }
1555 
1556         bmGC = XCreateGC(awt_display, xsdo->bitmask, 0, NULL);
1557         if (bmGC == NULL) {
1558             AWT_UNLOCK();
1559             return 0;
1560         }
1561 
1562         /* invert the bitmask */
1563         XSetFunction(awt_display, bmGC, GXxor);
1564         XSetForeground(awt_display, bmGC, 1);
1565         XFillRectangle(awt_display, xsdo->bitmask, bmGC,
1566                        0, 0, xsdo->pmWidth, xsdo->pmHeight);
1567 
1568         srcGC = XCreateGC(awt_display, xsdo->drawable, 0L, NULL);
1569         if (srcGC == NULL) {
1570             XFreeGC(awt_display, bmGC);
1571             AWT_UNLOCK();
1572             return 0;
1573         }
1574 
1575         /* set transparent pixels in the source pm to the bg color */
1576         XSetClipMask(awt_display, srcGC, xsdo->bitmask);
1577         XSetForeground(awt_display, srcGC, pixel);
1578         XFillRectangle(awt_display, xsdo->drawable, srcGC,
1579                        0, 0, xsdo->pmWidth, xsdo->pmHeight);
1580 
1581         /* invert the mask back */
1582         XFillRectangle(awt_display, xsdo->bitmask, bmGC,
1583                        0, 0, xsdo->pmWidth, xsdo->pmHeight);
1584 
1585         XFreeGC(awt_display, bmGC);
1586         XFreeGC(awt_display, srcGC);
1587         xsdo->bgPixel = pixel;
1588         xsdo->isBgInitialized = JNI_TRUE;
1589     }
1590 
1591     return xsdo->drawable;
1592 }
1593 
1594 static void
1595 X11SD_ReleasePixmapWithBg(JNIEnv *env, X11SDOps *xsdo)
1596 {
1597 #ifdef MITSHM
1598     if (xsdo->shmPMData.usingShmPixmap) {
1599         xsdo->shmPMData.xRequestSent = JNI_TRUE;
1600     }
1601 #endif /* MITSHM */
1602 }
1603 
1604 #endif /* !HEADLESS */
1605 
1606 /*
1607  * Class:     sun_java2d_x11_X11SurfaceData
1608  * Method:    XCreateGC
1609  * Signature: (I)J
1610  */
1611 JNIEXPORT jlong JNICALL
1612 Java_sun_java2d_x11_XSurfaceData_XCreateGC
1613     (JNIEnv *env, jclass xsd, jlong pXSData)
1614 {
1615     jlong ret;
1616 
1617 #ifndef HEADLESS
1618     X11SDOps *xsdo;
1619 
1620     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XCreateGC");
1621 
1622     xsdo = (X11SDOps *) pXSData;
1623     if (xsdo == NULL) {
1624         return 0L;
1625     }
1626 
1627     xsdo->javaGC = XCreateGC(awt_display, xsdo->drawable, 0, NULL);
1628     ret = (jlong) xsdo->javaGC;
1629 #else /* !HEADLESS */
1630     ret = 0L;
1631 #endif /* !HEADLESS */
1632 
1633     return ret;
1634 }
1635 
1636 /*
1637  * Class:     sun_java2d_x11_X11SurfaceData
1638  * Method:    XResetClip
1639  * Signature: (JIIIILsun/java2d/pipe/Region;)V
1640  */
1641 JNIEXPORT void JNICALL
1642 Java_sun_java2d_x11_XSurfaceData_XResetClip
1643     (JNIEnv *env, jclass xsd, jlong xgc)
1644 {
1645 #ifndef HEADLESS
1646     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XResetClip");
1647     XSetClipMask(awt_display, (GC) xgc, None);
1648 #endif /* !HEADLESS */
1649 }
1650 
1651 /*
1652  * Class:     sun_java2d_x11_X11SurfaceData
1653  * Method:    XSetClip
1654  * Signature: (JIIIILsun/java2d/pipe/Region;)V
1655  */
1656 JNIEXPORT void JNICALL
1657 Java_sun_java2d_x11_XSurfaceData_XSetClip
1658     (JNIEnv *env, jclass xsd, jlong xgc,
1659      jint x1, jint y1, jint x2, jint y2,
1660      jobject complexclip)
1661 {
1662 #ifndef HEADLESS
1663     int numrects;
1664     XRectangle rects[256];
1665     XRectangle *pRect = rects;
1666 
1667     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetClip");
1668 
1669     numrects = RegionToYXBandedRectangles(env,
1670             x1, y1, x2, y2, complexclip,
1671             &pRect, 256);
1672 
1673     XSetClipRectangles(awt_display, (GC) xgc, 0, 0, pRect, numrects, YXBanded);
1674 
1675     if (pRect != rects) {
1676         free(pRect);
1677     }
1678 #endif /* !HEADLESS */
1679 }
1680 
1681 /*
1682  * Class:     sun_java2d_x11_X11SurfaceData
1683  * Method:    XSetCopyMode
1684  * Signature: (J)V
1685  */
1686 JNIEXPORT void JNICALL
1687 Java_sun_java2d_x11_X11SurfaceData_XSetCopyMode
1688     (JNIEnv *env, jclass xsd, jlong xgc)
1689 {
1690 #ifndef HEADLESS
1691     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetCopyMode");
1692     XSetFunction(awt_display, (GC) xgc, GXcopy);
1693 #endif /* !HEADLESS */
1694 }
1695 
1696 /*
1697  * Class:     sun_java2d_x11_X11SurfaceData
1698  * Method:    XSetXorMode
1699  * Signature: (J)V
1700  */
1701 JNIEXPORT void JNICALL
1702 Java_sun_java2d_x11_X11SurfaceData_XSetXorMode
1703     (JNIEnv *env, jclass xr, jlong xgc)
1704 {
1705 #ifndef HEADLESS
1706     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetXorMode");
1707     XSetFunction(awt_display, (GC) xgc, GXxor);
1708 #endif /* !HEADLESS */
1709 }
1710 
1711 /*
1712  * Class:     sun_java2d_x11_X11SurfaceData
1713  * Method:    XSetForeground
1714  * Signature: (JI)V
1715  */
1716 JNIEXPORT void JNICALL
1717 Java_sun_java2d_x11_X11SurfaceData_XSetForeground
1718     (JNIEnv *env, jclass xsd, jlong xgc, jint pixel)
1719 {
1720 #ifndef HEADLESS
1721     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetForeground");
1722     XSetForeground(awt_display, (GC) xgc, pixel);
1723 #endif /* !HEADLESS */
1724 }
1725 
1726 /*
1727  * Class:     sun_java2d_x11_X11SurfaceData
1728  * Method:    XSetGraphicsExposures
1729  * Signature: (JZ)V
1730  */
1731 JNIEXPORT void JNICALL
1732 Java_sun_java2d_x11_XSurfaceData_XSetGraphicsExposures
1733     (JNIEnv *env, jclass xsd, jlong xgc, jboolean needExposures)
1734 {
1735 #ifndef HEADLESS
1736     J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetGraphicsExposures");
1737     XSetGraphicsExposures(awt_display, (GC) xgc, needExposures ? True : False);
1738 #endif /* !HEADLESS */
1739 }