X11SurfaceData.c 49.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 */

#include "X11SurfaceData.h"
#include "GraphicsPrimitiveMgr.h"
#include "Region.h"
#include "Trace.h"

/* Needed to define intptr_t */
#include "gdefs.h"

#include "jni_util.h"
#include "awt_Component.h"
#include "awt_GraphicsEnv.h"

#include <dlfcn.h>

#ifndef HEADLESS
static JDgaLibInfo DgaLibInfoStub;
static JDgaLibInfo theJDgaInfo;
static JDgaLibInfo *pJDgaInfo = &DgaLibInfoStub;


/**
 * This file contains support code for loops using the SurfaceData
 * interface to talk to an X11 drawable from native code.
 */

typedef struct _X11RIPrivate {
    jint                lockType;
    jint                lockFlags;
    XImage              *img;
    int                 x, y;
} X11RIPrivate;

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))

static LockFunc X11SD_Lock;
static GetRasInfoFunc X11SD_GetRasInfo;
static UnlockFunc X11SD_Unlock;
static DisposeFunc X11SD_Dispose;
static GetPixmapBgFunc X11SD_GetPixmapWithBg;
static ReleasePixmapBgFunc X11SD_ReleasePixmapWithBg;
extern int J2DXErrHandler(Display *display, XErrorEvent *xerr);
extern AwtGraphicsConfigDataPtr
    getGraphicsConfigFromComponentPeer(JNIEnv *env, jobject this);
extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
71

D
duke 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static int X11SD_FindClip(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
                          X11SDOps *xsdo);
static int X11SD_ClipToRoot(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
                            X11SDOps *xsdo);
static void X11SD_SwapBytes(X11SDOps *xsdo, XImage *img, int depth, int bpp);
static XImage * X11SD_GetImage(JNIEnv *env, X11SDOps *xsdo,
                               SurfaceDataBounds *bounds,
                               jint lockFlags);

extern jfieldID validID;

static int nativeByteOrder;
static jboolean dgaAvailable = JNI_FALSE;
static jboolean useDGAWithPixmaps = JNI_FALSE;
static jclass xorCompClass;

jint useMitShmExt = CANT_USE_MITSHM;
jint useMitShmPixmaps = CANT_USE_MITSHM;
jint forceSharedPixmaps = JNI_FALSE;

/* Cached shared image, one for all surface datas. */
static XImage * cachedXImage;

#endif /* !HEADLESS */

97
jboolean XShared_initIDs(JNIEnv *env, jboolean allowShmPixmaps)
D
duke 已提交
98 99
{
#ifndef HEADLESS
100
   union {
D
duke 已提交
101 102 103 104 105 106 107
        char c[4];
        int i;
    } endian;

    endian.i = 0xff000000;
    nativeByteOrder = (endian.c[0]) ? MSBFirst : LSBFirst;

108 109
    dgaAvailable = JNI_FALSE;

D
duke 已提交
110 111 112 113
    cachedXImage = NULL;

    if (sizeof(X11RIPrivate) > SD_RASINFO_PRIVATE_SIZE) {
        JNU_ThrowInternalError(env, "Private RasInfo structure too large!");
114
        return JNI_FALSE;
D
duke 已提交
115 116
    }

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#ifdef MITSHM
    if (getenv("NO_AWT_MITSHM") == NULL &&
        getenv("NO_J2D_MITSHM") == NULL) {
        char * force;
        TryInitMITShm(env, &useMitShmExt, &useMitShmPixmaps);

        if(allowShmPixmaps) {
          useMitShmPixmaps = (useMitShmPixmaps == CAN_USE_MITSHM);
          force = getenv("J2D_PIXMAPS");
          if (force != NULL) {
              if (useMitShmPixmaps && (strcmp(force, "shared") == 0)) {
                  forceSharedPixmaps = JNI_TRUE;
              } else if (strcmp(force, "server") == 0) {
                  useMitShmPixmaps = JNI_FALSE;
              }
          }
        }else {
          useMitShmPixmaps = JNI_FALSE;
        }
    }

    return JNI_TRUE;
#endif /* MITSHM */

#endif /* !HEADLESS */
}


/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    initIDs
 * Signature: (Ljava/lang/Class;Z)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11SurfaceData_initIDs(JNIEnv *env, jclass xsd,
                                           jclass XORComp, jboolean tryDGA)
{
#ifndef HEADLESS
  if(XShared_initIDs(env, JNI_TRUE))
  {
    void *lib = 0;

D
duke 已提交
159 160 161 162 163 164
    xorCompClass = (*env)->NewGlobalRef(env, XORComp);

    if (tryDGA && (getenv("NO_J2D_DGA") == NULL)) {
    /* we use RTLD_NOW because of bug 4032715 */
        lib = dlopen("libsunwjdga.so", RTLD_NOW);
    }
165

D
duke 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    if (lib != NULL) {
        JDgaStatus ret = JDGA_FAILED;
        void *sym = dlsym(lib, "JDgaLibInit");
        if (sym != NULL) {
            theJDgaInfo.display = awt_display;
            AWT_LOCK();
            ret = (*(JDgaLibInitFunc *)sym)(env, &theJDgaInfo);
            AWT_UNLOCK();
        }
        if (ret == JDGA_SUCCESS) {
            pJDgaInfo = &theJDgaInfo;
            dgaAvailable = JNI_TRUE;
            useDGAWithPixmaps = (getenv("USE_DGA_PIXMAPS") != NULL);
        } else {
            dlclose(lib);
            lib = NULL;
        }
    }
184
  }
D
duke 已提交
185 186 187 188 189 190 191 192 193
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    isDrawableValid
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL
194
Java_sun_java2d_x11_XSurfaceData_isDrawableValid(JNIEnv *env, jobject this)
D
duke 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
    jboolean ret = JNI_FALSE;

#ifndef HEADLESS
    X11SDOps *xsdo = X11SurfaceData_GetOps(env, this);

    AWT_LOCK();
    if (xsdo->drawable != 0 || X11SD_InitWindow(env, xsdo) == SD_SUCCESS) {
        ret = JNI_TRUE;
    }
    AWT_UNLOCK();
#endif /* !HEADLESS */

    return ret;
}

/*
212 213
 * Class: sun_java2d_x11_X11SurfaceData
 * Method: isShmPMAvailable
D
duke 已提交
214 215 216
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL
217
Java_sun_java2d_x11_X11SurfaceData_isShmPMAvailable(JNIEnv *env, jobject this)
D
duke 已提交
218
{
219
#if defined(HEADLESS) || !defined(MITSHM)
D
duke 已提交
220 221
    return JNI_FALSE;
#else
222 223
    return useMitShmPixmaps;
#endif /* HEADLESS, MITSHM */
D
duke 已提交
224 225
}

226 227
/*
 * Class:     sun_java2d_x11_X11SurfaceData
228
 * Method:    isDgaAvailable
229 230 231
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL
232
Java_sun_java2d_x11_X11SurfaceData_isDgaAvailable(JNIEnv *env, jobject this)
233
{
234
#if defined(HEADLESS) || defined(__linux__)
235 236
    return JNI_FALSE;
#else
237 238
    return dgaAvailable;
#endif /* HEADLESS */
239 240
}

D
duke 已提交
241 242 243 244 245 246
/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    initOps
 * Signature: (Ljava/lang/Object;I)V
 */
JNIEXPORT void JNICALL
247
Java_sun_java2d_x11_XSurfaceData_initOps(JNIEnv *env, jobject xsd,
D
duke 已提交
248 249 250 251 252
                                           jobject peer,
                                           jobject graphicsConfig, jint depth)
{
#ifndef HEADLESS
    X11SDOps *xsdo = (X11SDOps*)SurfaceData_InitOps(env, xsd, sizeof(X11SDOps));
253 254 255 256
    if (xsdo == NULL) {
        JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
        return;
    }
D
duke 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    xsdo->sdOps.Lock = X11SD_Lock;
    xsdo->sdOps.GetRasInfo = X11SD_GetRasInfo;
    xsdo->sdOps.Unlock = X11SD_Unlock;
    xsdo->sdOps.Dispose = X11SD_Dispose;
    xsdo->GetPixmapWithBg = X11SD_GetPixmapWithBg;
    xsdo->ReleasePixmapWithBg = X11SD_ReleasePixmapWithBg;
    xsdo->widget = NULL;
    if (peer != NULL) {
        xsdo->drawable = JNU_CallMethodByName(env, NULL, peer, "getWindow", "()J").j;
    } else {
        xsdo->drawable = 0;
    }
    xsdo->depth = depth;
    xsdo->dgaAvailable = dgaAvailable;
    xsdo->isPixmap = JNI_FALSE;
    xsdo->bitmask = 0;
    xsdo->bgPixel = 0;
    xsdo->isBgInitialized = JNI_FALSE;
#ifdef MITSHM
    xsdo->shmPMData.shmSegInfo = NULL;
    xsdo->shmPMData.xRequestSent = JNI_FALSE;
    xsdo->shmPMData.pmSize = 0;
    xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
    xsdo->shmPMData.pixmap = 0;
    xsdo->shmPMData.shmPixmap = 0;
    xsdo->shmPMData.numBltsSinceRead = 0;
    xsdo->shmPMData.pixelsReadSinceBlt = 0;
    xsdo->shmPMData.numBltsThreshold = 2;
#endif /* MITSHM */

    xsdo->configData = (AwtGraphicsConfigDataPtr)
        JNU_GetLongFieldAsPtr(env,
                              graphicsConfig,
                              x11GraphicsConfigIDs.aData);
    if (xsdo->configData == NULL) {
        JNU_ThrowNullPointerException(env,
                                      "Native GraphicsConfig data block missing");
        return;
    }
    if (depth > 12) {
        xsdo->pixelmask = (xsdo->configData->awt_visInfo.red_mask |
                           xsdo->configData->awt_visInfo.green_mask |
                           xsdo->configData->awt_visInfo.blue_mask);
    } else if (depth == 12) {
        xsdo->pixelmask = 0xfff;
    } else {
        xsdo->pixelmask = 0xff;
    }
305 306

    xsdo->xrPic = None;
D
duke 已提交
307 308 309 310 311 312 313 314 315
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    flushNativeSurface
 * Signature: ()V
 */
JNIEXPORT void JNICALL
316
Java_sun_java2d_x11_XSurfaceData_flushNativeSurface(JNIEnv *env, jobject xsd)
D
duke 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
{
#ifndef HEADLESS
    SurfaceDataOps *ops = SurfaceData_GetOps(env, xsd);

    if (ops != NULL) {
        X11SD_Dispose(env, ops);
    }
#endif /* !HEADLESS */
}


JNIEXPORT X11SDOps * JNICALL
X11SurfaceData_GetOps(JNIEnv *env, jobject sData)
{
#ifdef HEADLESS
    return NULL;
#else
    SurfaceDataOps *ops = SurfaceData_GetOps(env, sData);
    if (ops != NULL && ops->Lock != X11SD_Lock) {
        SurfaceData_ThrowInvalidPipeException(env, "not an X11 SurfaceData");
        ops = NULL;
    }
    return (X11SDOps *) ops;
#endif /* !HEADLESS */
}

/*
 * Method for disposing X11SD-specific data
 */
static void
X11SD_Dispose(JNIEnv *env, SurfaceDataOps *ops)
{
#ifndef HEADLESS
    /* ops is assumed non-null as it is checked in SurfaceData_DisposeOps */
    X11SDOps * xsdo = (X11SDOps*)ops;

    AWT_LOCK();

    xsdo->invalid = JNI_TRUE;
356 357 358 359 360 361

    if (xsdo->xrPic != None) {
        XRenderFreePicture(awt_display, xsdo->xrPic);
        xsdo->xrPic = None;
     }

D
duke 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    if (xsdo->isPixmap == JNI_TRUE && xsdo->drawable != 0) {
#ifdef MITSHM
        if (xsdo->shmPMData.shmSegInfo != NULL) {
            X11SD_DropSharedSegment(xsdo->shmPMData.shmSegInfo);
            xsdo->shmPMData.shmSegInfo = NULL;
        }
        if (xsdo->shmPMData.pixmap) {
            XFreePixmap(awt_display, xsdo->shmPMData.pixmap);
            xsdo->shmPMData.pixmap = 0;
        }
        if (xsdo->shmPMData.shmPixmap) {
            XFreePixmap(awt_display, xsdo->shmPMData.shmPixmap);
            xsdo->shmPMData.shmPixmap = 0;
        }
#else
        XFreePixmap(awt_display, xsdo->drawable);
#endif /* MITSHM */
        xsdo->drawable = 0;
    }
    if (xsdo->bitmask != 0) {
        XFreePixmap(awt_display, xsdo->bitmask);
        xsdo->bitmask = 0;
    }
    if (xsdo->javaGC != NULL) {
        XFreeGC(awt_display, xsdo->javaGC);
        xsdo->javaGC = NULL;
    }
    if (xsdo->cachedGC != NULL) {
        XFreeGC(awt_display, xsdo->cachedGC);
        xsdo->cachedGC = NULL;
    }
393 394 395 396 397

    if(xsdo->xrPic != None) {
      XRenderFreePicture(awt_display, xsdo->xrPic);
    }

D
duke 已提交
398 399 400 401 402 403 404 405 406
    AWT_UNLOCK();
#endif /* !HEADLESS */
}
/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    setInvalid
 * Signature: ()V
 */
JNIEXPORT void JNICALL
407
Java_sun_java2d_x11_XSurfaceData_setInvalid(JNIEnv *env, jobject xsd)
D
duke 已提交
408 409 410 411 412 413 414 415 416 417
{
#ifndef HEADLESS
    X11SDOps *xsdo = (X11SDOps *) SurfaceData_GetOps(env, xsd);

    if (xsdo != NULL) {
        xsdo->invalid = JNI_TRUE;
    }
#endif /* !HEADLESS */
}

418 419

jboolean XShared_initSurface(JNIEnv *env, X11SDOps *xsdo, jint depth, jint width, jint height, jlong drawable)
D
duke 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
{
#ifndef HEADLESS

    if (drawable != (jlong)0) {
        /* Double-buffering */
        xsdo->drawable = drawable;
        xsdo->isPixmap = JNI_FALSE;
    } else {
        xsdo->isPixmap = JNI_TRUE;
        /* REMIND: workaround for bug 4420220 on pgx32 boards:
           don't use DGA with pixmaps unless USE_DGA_PIXMAPS is set.
         */
        xsdo->dgaAvailable = useDGAWithPixmaps;

        xsdo->pmWidth = width;
        xsdo->pmHeight = height;

#ifdef MITSHM
        xsdo->shmPMData.pmSize = width * height * depth;
        xsdo->shmPMData.pixelsReadThreshold = width * height / 8;
        if (forceSharedPixmaps) {
            AWT_LOCK();
            xsdo->drawable = X11SD_CreateSharedPixmap(xsdo);
            AWT_UNLOCK();
            if (xsdo->drawable) {
                xsdo->shmPMData.usingShmPixmap = JNI_TRUE;
                xsdo->shmPMData.shmPixmap = xsdo->drawable;
447
                return JNI_TRUE;
D
duke 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
            }
        }
#endif /* MITSHM */

        AWT_LOCK();
        xsdo->drawable =
            XCreatePixmap(awt_display,
                          RootWindow(awt_display,
                                     xsdo->configData->awt_visInfo.screen),
                          width, height, depth);
        AWT_UNLOCK();
#ifdef MITSHM
        xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
        xsdo->shmPMData.pixmap = xsdo->drawable;
#endif /* MITSHM */
    }
    if (xsdo->drawable == 0) {
        JNU_ThrowOutOfMemoryError(env,
                                  "Can't create offscreen surface");
467
        return JNI_FALSE;
D
duke 已提交
468
    }
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

    return JNI_TRUE;
#endif /* !HEADLESS */
}


/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    initSurface
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11SurfaceData_initSurface(JNIEnv *env, jclass xsd,
                                               jint depth,
                                               jint width, jint height,
                                               jlong drawable)
{
#ifndef HEADLESS
    X11SDOps *xsdo = X11SurfaceData_GetOps(env, xsd);
    if (xsdo == NULL) {
        return;
    }

    if (xsdo->configData->awt_cmap == (Colormap)NULL) {
        awtJNI_CreateColorData(env, xsdo->configData, 1);
    }
    /* color_data will be initialized in awtJNI_CreateColorData for
       8-bit visuals */
    xsdo->cData = xsdo->configData->color_data;

    XShared_initSurface(env, xsdo, depth, width, height, drawable);
    xsdo->xrPic = NULL;
D
duke 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
#endif /* !HEADLESS */
}

#ifndef HEADLESS

#ifdef MITSHM

void X11SD_DropSharedSegment(XShmSegmentInfo *shminfo)
{
    if (shminfo != NULL) {
        XShmDetach(awt_display, shminfo);
        shmdt(shminfo->shmaddr);
/*      REMIND: we don't need shmctl(shminfo->shmid, IPC_RMID, 0); here. */
/*      Check X11SD_CreateSharedImage() for the explanation */
    }
}

XImage* X11SD_CreateSharedImage(X11SDOps *xsdo,
                                   jint width, jint height)
{
    XImage *img = NULL;
    XShmSegmentInfo *shminfo;

    shminfo = malloc(sizeof(XShmSegmentInfo));
    if (shminfo == NULL) {
        return NULL;
    }
    memset(shminfo, 0, sizeof(XShmSegmentInfo));

    img = XShmCreateImage(awt_display, xsdo->configData->awt_visInfo.visual,
                          xsdo->depth, ZPixmap, NULL, shminfo,
                          width, height);
    if (img == NULL) {
        free((void *)shminfo);
        return NULL;
    }
    shminfo->shmid =
        shmget(IPC_PRIVATE, height * img->bytes_per_line, IPC_CREAT|0777);
    if (shminfo->shmid < 0) {
        J2dRlsTraceLn1(J2D_TRACE_ERROR,
                       "X11SD_SetupSharedSegment shmget has failed: %s",
                       strerror(errno));
543 544
        free((void *)shminfo);
        XDestroyImage(img);
D
duke 已提交
545 546 547 548 549 550 551 552 553
        return NULL;
    }

    shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
    if (shminfo->shmaddr == ((char *) -1)) {
        shmctl(shminfo->shmid, IPC_RMID, 0);
        J2dRlsTraceLn1(J2D_TRACE_ERROR,
                       "X11SD_SetupSharedSegment shmat has failed: %s",
                       strerror(errno));
554 555
        free((void *)shminfo);
        XDestroyImage(img);
D
duke 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        return NULL;
    }

    shminfo->readOnly = False;

    resetXShmAttachFailed();
    EXEC_WITH_XERROR_HANDLER(J2DXErrHandler,
                             XShmAttach(awt_display, shminfo));

    /*
     * Once the XSync round trip has finished then we
     * can get rid of the id so that this segment does not stick
     * around after we go away, holding system resources.
     */
    shmctl(shminfo->shmid, IPC_RMID, 0);

    if (isXShmAttachFailed() == JNI_TRUE) {
        J2dRlsTraceLn1(J2D_TRACE_ERROR,
                       "X11SD_SetupSharedSegment XShmAttach has failed: %s",
                       strerror(errno));
576 577 578
        shmdt(shminfo->shmaddr);
        free((void *)shminfo);
        XDestroyImage(img);
D
duke 已提交
579 580 581 582 583 584 585 586 587 588
        return NULL;
    }

    img->data = shminfo->shmaddr;
    img->obdata = (char *)shminfo;

    return img;
}

XImage* X11SD_GetSharedImage(X11SDOps *xsdo, jint width, jint height,
589
                             jint maxWidth, jint maxHeight, jboolean readBits)
D
duke 已提交
590 591 592
{
    XImage * retImage = NULL;
    if (cachedXImage != NULL &&
593 594 595 596 597 598
        X11SD_CachedXImageFits(width, height, maxWidth, maxHeight,
                               xsdo->depth, readBits)) {
        /* sync so previous data gets flushed */
        XSync(awt_display, False);
        retImage = cachedXImage;
        cachedXImage = (XImage *)NULL;
D
duke 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    } else if (width * height * xsdo->depth > 0x10000) {
        retImage = X11SD_CreateSharedImage(xsdo, width, height);
    }
    return retImage;
}

Drawable X11SD_CreateSharedPixmap(X11SDOps *xsdo)
{
    XShmSegmentInfo *shminfo;
    XImage *img = NULL;
    Drawable pixmap;
    int scan;
    int width = xsdo->pmWidth;
    int height = xsdo->pmHeight;

    if (xsdo->shmPMData.pmSize < 0x10000) {
        /* only use shared mem pixmaps for relatively big images */
        return 0;
    }

    /* need to create shared(!) image to get bytes_per_line */
    img = X11SD_CreateSharedImage(xsdo, width, height);
    if (img == NULL) {
        return 0;
    }
    scan = img->bytes_per_line;
    shminfo = (XShmSegmentInfo*)img->obdata;
    XFree(img);

    pixmap =
        XShmCreatePixmap(awt_display,
                         RootWindow(awt_display,
                                    xsdo->configData->awt_visInfo.screen),
                         shminfo->shmaddr, shminfo,
                         width, height, xsdo->depth);
    if (pixmap == 0) {
        X11SD_DropSharedSegment(shminfo);
        return 0;
    }

    xsdo->shmPMData.shmSegInfo = shminfo;
    xsdo->shmPMData.bytesPerLine = scan;
    return pixmap;
}

void X11SD_PuntPixmap(X11SDOps *xsdo, jint width, jint height)
{

    if (useMitShmPixmaps != CAN_USE_MITSHM || forceSharedPixmaps) {
        return;
    }

    /* we wouldn't be here if it's a shared pixmap, so no check
     * for !usingShmPixmap.
     */

    xsdo->shmPMData.numBltsSinceRead = 0;

    xsdo->shmPMData.pixelsReadSinceBlt += width * height;
    if (xsdo->shmPMData.pixelsReadSinceBlt >
        xsdo->shmPMData.pixelsReadThreshold) {
        if (!xsdo->shmPMData.shmPixmap) {
            xsdo->shmPMData.shmPixmap =
                X11SD_CreateSharedPixmap(xsdo);
        }
        if (xsdo->shmPMData.shmPixmap) {
            GC xgc = XCreateGC(awt_display, xsdo->shmPMData.shmPixmap, 0L, NULL);
            if (xgc != NULL) {
                xsdo->shmPMData.usingShmPixmap = JNI_TRUE;
                xsdo->drawable = xsdo->shmPMData.shmPixmap;
                XCopyArea(awt_display,
                          xsdo->shmPMData.pixmap, xsdo->drawable, xgc,
                          0, 0, xsdo->pmWidth, xsdo->pmHeight, 0, 0);
                XSync(awt_display, False);
                xsdo->shmPMData.xRequestSent = JNI_FALSE;
                XFreeGC(awt_display, xgc);
            }
        }
    }
}

void X11SD_UnPuntPixmap(X11SDOps *xsdo)
{
    if (useMitShmPixmaps != CAN_USE_MITSHM || forceSharedPixmaps) {
        return;
    }
    xsdo->shmPMData.pixelsReadSinceBlt = 0;
    if (xsdo->shmPMData.numBltsSinceRead >=
        xsdo->shmPMData.numBltsThreshold)
    {
        if (xsdo->shmPMData.usingShmPixmap) {
            if (!xsdo->shmPMData.pixmap) {
                xsdo->shmPMData.pixmap =
                    XCreatePixmap(awt_display,
                                  RootWindow(awt_display,
                                             xsdo->configData->awt_visInfo.screen),
                                  xsdo->pmWidth, xsdo->pmHeight, xsdo->depth);
            }
            if (xsdo->shmPMData.pixmap) {
                GC xgc = XCreateGC(awt_display, xsdo->shmPMData.pixmap, 0L, NULL);
                if (xgc != NULL) {
                    xsdo->drawable = xsdo->shmPMData.pixmap;
                    XCopyArea(awt_display,
                              xsdo->shmPMData.shmPixmap, xsdo->drawable, xgc,
                              0, 0, xsdo->pmWidth, xsdo->pmHeight, 0, 0);
                    XSync(awt_display, False);
                    XFreeGC(awt_display, xgc);
                    xsdo->shmPMData.xRequestSent = JNI_FALSE;
                    xsdo->shmPMData.usingShmPixmap = JNI_FALSE;
                    xsdo->shmPMData.numBltsThreshold *= 2;
                }
            }
        }
    } else {
        xsdo->shmPMData.numBltsSinceRead++;
    }
}

/**
 * Determines if the cached image can be used for current operation.
 * If the image is to be used to be read into by XShmGetImage,
 * it must be close enough to avoid excessive reading from the screen;
 * otherwise it should just be at least the size requested.
 */
723 724
jboolean X11SD_CachedXImageFits(jint width, jint height, jint maxWidth,
                                jint maxHeight, jint depth, jboolean readBits)
D
duke 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
{
    /* we assume here that the cached image exists */
    jint imgWidth = cachedXImage->width;
    jint imgHeight = cachedXImage->height;

    if (imgWidth < width || imgHeight < height || depth != cachedXImage->depth)  {
        /* doesn't fit if any of the cached image dimensions is smaller
           or the depths are different */
        return JNI_FALSE;
    }

    if (!readBits) {
        /* Not reading from this image, so any image at least of the
           size requested will do */
        return JNI_TRUE;
    }

742 743 744
    if ((imgWidth < width + 64) && (imgHeight < height + 64)
         && imgWidth <= maxWidth && imgHeight <= maxHeight)
    {
D
duke 已提交
745 746 747
        /* Cached image's width/height shouldn't be more than 64 pixels
         * larger than requested, because the region in XShmGetImage
         * can't be specified and we don't want to read too much.
748 749
         * Furthermore it has to be smaller than maxWidth/Height
         * so drawables are not read out of bounds.
D
duke 已提交
750 751 752 753 754 755 756 757
         */
        return JNI_TRUE;
    }

    return JNI_FALSE;
}
#endif /* MITSHM */

758
jint X11SD_InitWindow(JNIEnv *env, X11SDOps *xsdo)
D
duke 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
{
    if (xsdo->isPixmap == JNI_TRUE) {
        return SD_FAILURE;
    }
    xsdo->cData = xsdo->configData->color_data;

    return SD_SUCCESS;
}

static jint X11SD_Lock(JNIEnv *env,
                       SurfaceDataOps *ops,
                       SurfaceDataRasInfo *pRasInfo,
                       jint lockflags)
{
    X11SDOps *xsdo = (X11SDOps *) ops;
    X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);
    int ret = SD_SUCCESS;

    AWT_LOCK();

    if (xsdo->invalid) {
        AWT_UNLOCK();
        SurfaceData_ThrowInvalidPipeException(env, "bounds changed");
        return SD_FAILURE;
    }
    xsdo->cData = xsdo->configData->color_data;
    if (xsdo->drawable == 0 && X11SD_InitWindow(env, xsdo) == SD_FAILURE) {
        AWT_UNLOCK();
        return SD_FAILURE;
    }
    if ((lockflags & SD_LOCK_LUT) != 0 &&
        (xsdo->cData == NULL ||
         xsdo->cData->awt_icmLUT == NULL))
    {
        AWT_UNLOCK();
        JNU_ThrowNullPointerException(env, "colormap lookup table");
        return SD_FAILURE;
    }
    if ((lockflags & SD_LOCK_INVCOLOR) != 0 &&
        (xsdo->cData == NULL ||
         xsdo->cData->img_clr_tbl == NULL ||
         xsdo->cData->img_oda_red == NULL ||
         xsdo->cData->img_oda_green == NULL ||
         xsdo->cData->img_oda_blue == NULL))
    {
        AWT_UNLOCK();
        JNU_ThrowNullPointerException(env, "inverse colormap lookup table");
        return SD_FAILURE;
    }
    if ((lockflags & SD_LOCK_INVGRAY) != 0 &&
        (xsdo->cData == NULL ||
         xsdo->cData->pGrayInverseLutData == NULL))
    {
        AWT_UNLOCK();
        JNU_ThrowNullPointerException(env, "inverse gray lookup table");
        return SD_FAILURE;
    }
    if (xsdo->dgaAvailable && (lockflags & (SD_LOCK_RD_WR))) {
        int dgaret;

        dgaret = (*pJDgaInfo->pGetLock)(env, awt_display, &xsdo->dgaDev,
                                        xsdo->drawable, &xsdo->surfInfo,
                                        pRasInfo->bounds.x1,
                                        pRasInfo->bounds.y1,
                                        pRasInfo->bounds.x2,
                                        pRasInfo->bounds.y2);
        if (dgaret == JDGA_SUCCESS) {
            int wx = xsdo->surfInfo.window.lox;
            int wy = xsdo->surfInfo.window.loy;
            pRasInfo->bounds.x1 = xsdo->surfInfo.visible.lox - wx;
            pRasInfo->bounds.y1 = xsdo->surfInfo.visible.loy - wy;
            pRasInfo->bounds.x2 = xsdo->surfInfo.visible.hix - wx;
            pRasInfo->bounds.y2 = xsdo->surfInfo.visible.hiy - wy;
            xpriv->lockType = X11SD_LOCK_BY_DGA;
            xpriv->lockFlags = lockflags;
            return SD_SUCCESS;
        } else if (dgaret == JDGA_UNAVAILABLE) {
            xsdo->dgaAvailable = JNI_FALSE;
        }
    }
    if (lockflags & SD_LOCK_RD_WR) {
        if (lockflags & SD_LOCK_FASTEST) {
            ret = SD_SLOWLOCK;
        }
        xpriv->lockType = X11SD_LOCK_BY_XIMAGE;
        if (xsdo->isPixmap) {
#ifdef MITSHM
            if (xsdo->shmPMData.usingShmPixmap) {
                xpriv->lockType = X11SD_LOCK_BY_SHMEM;
            }
#endif /* MITSHM */
            if (pRasInfo->bounds.x1 < 0) {
                pRasInfo->bounds.x1 = 0;
            }
            if (pRasInfo->bounds.y1 < 0) {
                pRasInfo->bounds.y1 = 0;
            }
            if (pRasInfo->bounds.x2 > xsdo->pmWidth) {
                pRasInfo->bounds.x2 = xsdo->pmWidth;
            }
            if (pRasInfo->bounds.y2 > xsdo->pmHeight) {
                pRasInfo->bounds.y2 = xsdo->pmHeight;
            }
        }
    } else {
        /* They didn't lock for anything - we won't give them anything */
        xpriv->lockType = X11SD_LOCK_BY_NULL;
    }
    xpriv->lockFlags = lockflags;
    xpriv->img = NULL;

    return ret;
    /* AWT_UNLOCK() called in Unlock */
}

static void X11SD_GetRasInfo(JNIEnv *env,
                             SurfaceDataOps *ops,
                             SurfaceDataRasInfo *pRasInfo)
{
    X11SDOps *xsdo = (X11SDOps *) ops;
    X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);
    jint lockFlags = xpriv->lockFlags;
    jint depth = xsdo->depth;
    int mult = xsdo->configData->pixelStride;

    if (xsdo->dgaAvailable &&
        xpriv->lockType == X11SD_LOCK_BY_XIMAGE &&
        (lockFlags & SD_LOCK_FASTEST))
    {
        /* Try one more time to use DGA (now with smaller bounds)... */
        int dgaret;

        dgaret = (*pJDgaInfo->pGetLock)(env, awt_display, &xsdo->dgaDev,
                                        xsdo->drawable, &xsdo->surfInfo,
                                        pRasInfo->bounds.x1,
                                        pRasInfo->bounds.y1,
                                        pRasInfo->bounds.x2,
                                        pRasInfo->bounds.y2);
        if (dgaret == JDGA_SUCCESS) {
            int wx = xsdo->surfInfo.window.lox;
            int wy = xsdo->surfInfo.window.loy;
            pRasInfo->bounds.x1 = xsdo->surfInfo.visible.lox - wx;
            pRasInfo->bounds.y1 = xsdo->surfInfo.visible.loy - wy;
            pRasInfo->bounds.x2 = xsdo->surfInfo.visible.hix - wx;
            pRasInfo->bounds.y2 = xsdo->surfInfo.visible.hiy - wy;
            xpriv->lockType = X11SD_LOCK_BY_DGA;
        } else if (dgaret == JDGA_UNAVAILABLE) {
            xsdo->dgaAvailable = JNI_FALSE;
        }
    }

    if (xpriv->lockType == X11SD_LOCK_BY_DGA) {
        int scan = xsdo->surfInfo.surfaceScan;
        int wx = xsdo->surfInfo.window.lox;
        int wy = xsdo->surfInfo.window.loy;
        pRasInfo->rasBase =
            (void *)(((uintptr_t) xsdo->surfInfo.basePtr) + (scan*wy + wx) * mult);
        pRasInfo->pixelStride = mult;
        pRasInfo->pixelBitOffset = 0;
        pRasInfo->scanStride = scan * mult;
#ifdef MITSHM
    } else if (xpriv->lockType == X11SD_LOCK_BY_SHMEM) {
        if (xsdo->shmPMData.xRequestSent == JNI_TRUE) {
            /* need to sync before using shared mem pixmap
             if any x calls were issued for this pixmap */
            XSync(awt_display, False);
            xsdo->shmPMData.xRequestSent = JNI_FALSE;
        }
        xpriv->x = pRasInfo->bounds.x1;
        xpriv->y = pRasInfo->bounds.y1;
        pRasInfo->rasBase = xsdo->shmPMData.shmSegInfo->shmaddr;
        pRasInfo->pixelStride = mult;
        pRasInfo->pixelBitOffset = 0;
        pRasInfo->scanStride = xsdo->shmPMData.bytesPerLine;
#endif /* MITSHM */
    } else if (xpriv->lockType == X11SD_LOCK_BY_XIMAGE) {
        int x, y, w, h;
        x = pRasInfo->bounds.x1;
        y = pRasInfo->bounds.y1;
        w = pRasInfo->bounds.x2 - x;
        h = pRasInfo->bounds.y2 - y;

        xpriv->img = X11SD_GetImage(env, xsdo, &pRasInfo->bounds, lockFlags);
        if (xpriv->img) {
            int scan = xpriv->img->bytes_per_line;
            xpriv->x = x;
            xpriv->y = y;
            pRasInfo->rasBase = xpriv->img->data - x * mult - y * scan;
            pRasInfo->pixelStride = mult;
            pRasInfo->pixelBitOffset = 0;
            pRasInfo->scanStride = scan;
        } else {
            pRasInfo->rasBase = NULL;
            pRasInfo->pixelStride = 0;
            pRasInfo->pixelBitOffset = 0;
            pRasInfo->scanStride = 0;
        }
    } else {
        /* They didn't lock for anything - we won't give them anything */
        pRasInfo->rasBase = NULL;
        pRasInfo->pixelStride = 0;
        pRasInfo->pixelBitOffset = 0;
        pRasInfo->scanStride = 0;
    }
    if (lockFlags & SD_LOCK_LUT) {
        pRasInfo->lutBase = (jint *) xsdo->cData->awt_icmLUT;
        pRasInfo->lutSize = xsdo->cData->awt_numICMcolors;
    } else {
        pRasInfo->lutBase = NULL;
        pRasInfo->lutSize = 0;
    }
    if (lockFlags & SD_LOCK_INVCOLOR) {
        pRasInfo->invColorTable = xsdo->cData->img_clr_tbl;
        pRasInfo->redErrTable = xsdo->cData->img_oda_red;
        pRasInfo->grnErrTable = xsdo->cData->img_oda_green;
        pRasInfo->bluErrTable = xsdo->cData->img_oda_blue;
    } else {
        pRasInfo->invColorTable = NULL;
        pRasInfo->redErrTable = NULL;
        pRasInfo->grnErrTable = NULL;
        pRasInfo->bluErrTable = NULL;
    }
    if (lockFlags & SD_LOCK_INVGRAY) {
        pRasInfo->invGrayTable = xsdo->cData->pGrayInverseLutData;
    } else {
        pRasInfo->invGrayTable = NULL;
    }
}

static void X11SD_Unlock(JNIEnv *env,
                         SurfaceDataOps *ops,
                         SurfaceDataRasInfo *pRasInfo)
{
    X11SDOps *xsdo = (X11SDOps *) ops;
    X11RIPrivate *xpriv = (X11RIPrivate *) &(pRasInfo->priv);

    if (xpriv->lockType == X11SD_LOCK_BY_DGA) {
        (*pJDgaInfo->pReleaseLock)(env, xsdo->dgaDev, xsdo->drawable);
    } else if (xpriv->lockType == X11SD_LOCK_BY_XIMAGE &&
               xpriv->img != NULL)
    {
        if (xpriv->lockFlags & SD_LOCK_WRITE) {
            int x = xpriv->x;
            int y = xpriv->y;
            int w = pRasInfo->bounds.x2 - x;
            int h = pRasInfo->bounds.y2 - y;
            Drawable drawable = xsdo->drawable;
            GC xgc = xsdo->cachedGC;
            if (xgc == NULL) {
                xsdo->cachedGC = xgc =
                    XCreateGC(awt_display, drawable, 0L, NULL);
            }

            if (xpriv->img->byte_order != nativeByteOrder) {
                /* switching bytes back in 24 and 32 bpp cases. */
                /* For 16 bit XLib will switch for us.          */
                if (xsdo->depth > 16) {
                    X11SD_SwapBytes(xsdo, xpriv->img, xsdo->depth,
                        xsdo->configData->awtImage->wsImageFormat.bits_per_pixel);
                }
            }

#ifdef MITSHM
            if (xpriv->img->obdata != NULL) {
                XShmPutImage(awt_display, drawable, xgc,
                             xpriv->img, 0, 0, x, y, w, h, False);
                XFlush(awt_display);
            } else {
                XPutImage(awt_display, drawable, xgc,
                          xpriv->img, 0, 0, x, y, w, h);
            }
            if (xsdo->shmPMData.usingShmPixmap) {
                xsdo->shmPMData.xRequestSent = JNI_TRUE;
            }
#else
            XPutImage(awt_display, drawable, xgc,
                      xpriv->img, 0, 0, x, y, w, h);
#endif /* MITSHM */

            (*pJDgaInfo->pXRequestSent)(env, xsdo->dgaDev, drawable);
        }
        X11SD_DisposeOrCacheXImage(xpriv->img);
        xpriv->img = (XImage *)NULL;
    }
    /* the background pixel is not valid anymore */
    if (xpriv->lockFlags & SD_LOCK_WRITE) {
        xsdo->isBgInitialized = JNI_FALSE;
    }
    xpriv->lockType = X11SD_LOCK_UNLOCKED;
    AWT_UNLOCK();
}

static int
X11SD_ClipToRoot(SurfaceDataBounds *b, SurfaceDataBounds *bounds,
                 X11SDOps *xsdo)
{
    Position x1=0, y1=0, x2=0, y2=0;
    int tmpx, tmpy;
    Window tmpchild;

    Window window = (Window)(xsdo->drawable); /* is always a Window */
    XWindowAttributes winAttr;

    Status status = XGetWindowAttributes(awt_display, window, &winAttr);
    if (status == 0) {
        /* Failure, X window no longer valid. */
        return FALSE;
    }
    if (!XTranslateCoordinates(awt_display, window,
                               RootWindowOfScreen(winAttr.screen),
                               0, 0, &tmpx, &tmpy, &tmpchild)) {
        return FALSE;
    }

    x1 = -(x1 + tmpx);
    y1 = -(y1 + tmpy);

    x2 = x1 + DisplayWidth(awt_display, xsdo->configData->awt_visInfo.screen);
    y2 = y1 + DisplayHeight(awt_display, xsdo->configData->awt_visInfo.screen);

    x1 = MAX(bounds->x1, x1);
    y1 = MAX(bounds->y1, y1);
    x2 = MIN(bounds->x2, x2);
    y2 = MIN(bounds->y2, y2);
    if ((x1 >= x2) || (y1 >= y2)) {
        return FALSE;
    }
    b->x1 = x1;
    b->y1 = y1;
    b->x2 = x2;
    b->y2 = y2;

    return TRUE;
}

/*
 * x1, y1, x2, y2 - our rectangle in the coord system of
 * the widget
 * px1, xy1, px2, py2 - current parent rect coords in the
 * same system
 */
static int
X11SD_FindClip(SurfaceDataBounds *b, SurfaceDataBounds *bounds, X11SDOps *xsdo)
{
    return TRUE;
}

static void
X11SD_SwapBytes(X11SDOps *xsdo, XImage * img, int depth, int bpp) {
    int lengthInBytes = img->height * img->bytes_per_line;
    int i;

    switch (depth) {
    case 12:
    case 15:
    case 16:
        {
            /* AB -> BA */
            unsigned short *d = (unsigned short *)img->data;
            unsigned short t;
            for (i = 0; i < lengthInBytes/2; i++) {
                t = *d;
                *d++ = (t >> 8) | (t << 8);
            }
            img->byte_order = nativeByteOrder;
            img->bitmap_bit_order = nativeByteOrder;
            break;
        }
    case 24:
        {
            /* ABC -> CBA */
            if (bpp == 24) {
                // 4517321: Only swap if we have a "real" ThreeByteBgr
                // visual (denoted by a red_mask of 0xff).  Due to ambiguity
                // in the X11 spec, it appears that the swap is not required
                // on Linux configurations that use 24 bits per pixel (denoted
                // by a red_mask of 0xff0000).
                if (xsdo->configData->awt_visInfo.red_mask == 0xff) {
                    int scan = img->bytes_per_line;
                    unsigned char *d = (unsigned char *) img->data;
                    unsigned char *d1;
                    unsigned int t;
                    int j;

                    for (i = 0; i < img->height; i++, d += scan) {
                        d1 = d;
                        for (j = 0; j < img->width; j++, d1 += 3) {
                            /* not obvious opt from XLib src */
                            t = d1[0]; d1[0] = d1[2]; d1[2] = t;
                        }
                    }
                }
                break;
            }
        }
        /* FALL THROUGH for 32-bit case */
    case 32:
        {
            /* ABCD -> DCBA */
            unsigned int *d = (unsigned int *) img->data;
            unsigned int t;
            for (i = 0; i < lengthInBytes/4; i++) {
                t = *d;
                *d++ = ((t >> 24) |
                        ((t >> 8) & 0xff00) |
                        ((t & 0xff00) << 8) |
                        (t << 24));
            }
            break;
        }
    }
}

static XImage * X11SD_GetImage(JNIEnv *env, X11SDOps *xsdo,
                               SurfaceDataBounds *bounds,
                               jint lockFlags)
{
1176
    int x, y, w, h, maxWidth, maxHeight;
D
duke 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    int scan;
    XImage * img = NULL;
    Drawable drawable;
    int depth = xsdo->depth;
    int mult = xsdo->configData->pixelStride;
    int pad = (mult == 3) ? 32 : mult * 8; // pad must be 8, 16, or 32
    jboolean readBits = lockFlags & SD_LOCK_NEED_PIXELS;

    x = bounds->x1;
    y = bounds->y1;
    w = bounds->x2 - x;
    h = bounds->y2 - y;

#ifdef MITSHM
    if (useMitShmExt == CAN_USE_MITSHM) {
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
        if (xsdo->isPixmap) {
            if (readBits) {
                X11SD_PuntPixmap(xsdo, w, h);
            }
            maxWidth = xsdo->pmWidth;
            maxHeight = xsdo->pmHeight;
        } else {
            XWindowAttributes winAttr;
            if (XGetWindowAttributes(awt_display,
                                     (Window) xsdo->drawable, &winAttr) != 0) {
                maxWidth = winAttr.width;
                maxHeight = winAttr.height;
           } else {
                /* XGWA failed which isn't a good thing. Defaulting to using
                 * x,y means that after the subtraction of these we will use
                 * w=0, h=0 which is a reasonable default on such a failure.
                 */
                maxWidth = x;
                maxHeight = y;
           }
D
duke 已提交
1212
        }
1213 1214 1215 1216
        maxWidth -= x;
        maxHeight -= y;

        img = X11SD_GetSharedImage(xsdo, w, h, maxWidth, maxHeight, readBits);
D
duke 已提交
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
    }
#endif /* MITSHM */
    drawable = xsdo->drawable;

    if (readBits) {
#ifdef MITSHM
        if (img != NULL) {
            if (!XShmGetImage(awt_display, drawable, img, x, y, -1)) {
                X11SD_DisposeOrCacheXImage(img);
                img = NULL;
            }
        }
        if (img == NULL) {
            img = XGetImage(awt_display, drawable, x, y, w, h, -1, ZPixmap);
            if (img != NULL) {
                img->obdata = NULL;
            }
        }
#else
        img = XGetImage(awt_display, drawable, x, y, w, h, -1, ZPixmap);
#endif /* MITSHM */
        if (img == NULL) {
            SurfaceDataBounds temp;
            img = XCreateImage(awt_display,
                               xsdo->configData->awt_visInfo.visual,
                               depth, ZPixmap, 0, NULL, w, h, pad, 0);
            if (img == NULL) {
                return NULL;
            }

            scan = img->bytes_per_line;
            img->data = malloc(h * scan);
            if (img->data == NULL) {
                XFree(img);
                return NULL;
            }

            if (xsdo->isPixmap == JNI_FALSE &&
                X11SD_ClipToRoot(&temp, bounds, xsdo)) {

                XImage * temp_image;
                temp_image = XGetImage(awt_display, drawable,
                                       temp.x1, temp.y1,
                                       temp.x2 - temp.x1,
                                       temp.y2 - temp.y1,
                                       -1, ZPixmap);
                if (temp_image == NULL) {
                    XGrabServer(awt_display);
                    if (X11SD_FindClip(&temp, bounds, xsdo)) {
                        temp_image =
                            XGetImage(awt_display, drawable,
                                      temp.x1, temp.y1,
                                      temp.x2 - temp.x1,
                                      temp.y2 - temp.y1,
                                      -1, ZPixmap);
                    }
                    XUngrabServer(awt_display);
                    /* Workaround for bug 5039226 */
                    XSync(awt_display, False);
                }
                if (temp_image != NULL) {
                    int temp_scan, bytes_to_copy;
                    char * img_addr, * temp_addr;
                    int i;

                    img_addr = img->data +
                        (temp.y1 - y) * scan + (temp.x1 - x) * mult;
                    temp_scan = temp_image->bytes_per_line;
                    temp_addr = temp_image->data;
                    bytes_to_copy = (temp.x2 - temp.x1) * mult;
                    for (i = temp.y1; i < temp.y2; i++) {
                        memcpy(img_addr, temp_addr, bytes_to_copy);
                        img_addr += scan;
                        temp_addr += temp_scan;
                    }
                    XDestroyImage(temp_image);
                }
            }
            img->obdata = NULL;
        }
        if (depth > 8 && img->byte_order != nativeByteOrder) {
            X11SD_SwapBytes(xsdo, img, depth,
                xsdo->configData->awtImage->wsImageFormat.bits_per_pixel);
        }
    } else {
        /*
         * REMIND: This might be better to move to the Lock function
         * to avoid lengthy I/O pauses inside what may be a critical
         * section.  This will be more critical when SD_LOCK_READ is
         * implemented.  Another solution is to cache the pixels
         * to avoid reading for every operation.
         */
        if (img == NULL) {
            img = XCreateImage(awt_display,
                               xsdo->configData->awt_visInfo.visual,
                               depth, ZPixmap, 0, NULL, w, h, pad, 0);
            if (img == NULL) {
                return NULL;
            }

            img->data = malloc(h * img->bytes_per_line);
            if (img->data == NULL) {
                XFree(img);
                return NULL;
            }

            img->obdata = NULL;

            if (img->byte_order != nativeByteOrder &&
                (depth == 15 || depth == 16 || depth == 12)) {
                /* bytes will be swapped by XLib. */
                img->byte_order = nativeByteOrder;
                img->bitmap_bit_order = nativeByteOrder;
            }
        }
    }
    return img;
}

void X11SD_DisposeOrCacheXImage(XImage * image) {
    /* REMIND: might want to check if the new image worth caching. */
    /* Cache only shared images. Passed image is assumed to be non-null. */
    if (image->obdata != NULL) {
        if (cachedXImage != NULL) {
            X11SD_DisposeXImage(cachedXImage);
        }
        cachedXImage = image;
    } else {
        X11SD_DisposeXImage(image);
    }
}

void X11SD_DisposeXImage(XImage * image) {
    if (image != NULL) {
#ifdef MITSHM
        if (image->obdata != NULL) {
            X11SD_DropSharedSegment((XShmSegmentInfo*)image->obdata);
1354
            image->obdata = NULL;
D
duke 已提交
1355 1356
        }
#endif /* MITSHM */
1357
        XDestroyImage(image);
D
duke 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
    }
}

static JDgaStatus
    GetLockStub(JNIEnv *env, Display *display, void **dgaDev,
                Drawable d, JDgaSurfaceInfo *pSurface,
                jint lox, jint loy, jint hix, jint hiy)
{
    return JDGA_UNAVAILABLE;
}

static JDgaStatus
    ReleaseLockStub(JNIEnv *env, void *dgaDev, Drawable d)
{
    return JDGA_FAILED;
}

static void
    XRequestSentStub(JNIEnv *env, void *dgaDev, Drawable d)
{
}

static void
    LibDisposeStub(JNIEnv *env)
{
}

static JDgaLibInfo DgaLibInfoStub = {
    NULL,
    GetLockStub,
    ReleaseLockStub,
    XRequestSentStub,
    LibDisposeStub,
};

void X11SD_LibDispose(JNIEnv *env) {
    AWT_LOCK();
    if (pJDgaInfo != NULL) {
        pJDgaInfo->pLibDispose(env);
        pJDgaInfo = &DgaLibInfoStub;
    }
    AWT_UNLOCK();
}

void
X11SD_DirectRenderNotify(JNIEnv *env, X11SDOps *xsdo)
{
#ifdef MITSHM
    if (xsdo->shmPMData.usingShmPixmap) {
        xsdo->shmPMData.xRequestSent = JNI_TRUE;
    }
#endif /* MITSHM */
    (*pJDgaInfo->pXRequestSent)(env, xsdo->dgaDev, xsdo->drawable);
    awt_output_flush();
}

/*
 * Sets transparent pixels in the pixmap to
 * the specified solid background color and returns it.
 * Doesn't update source pixmap unless the color of the
 * transparent pixels is different from the specified color.
 *
 * Note: The AWT lock must be held by the current thread
 * while calling into this method.
 */
static Drawable
X11SD_GetPixmapWithBg(JNIEnv *env, X11SDOps *xsdo, jint pixel)
{
    /* assert AWT_CHECK_HAVE_LOCK(); */

    if (xsdo->invalid) {
        AWT_UNLOCK();
        SurfaceData_ThrowInvalidPipeException(env, "bounds changed");
        return 0;
    }

    /* the image doesn't have transparency, just return it */
    if (xsdo->bitmask == 0) {
        /* don't need to unlock here, the caller will unlock through
           the release call */
        return xsdo->drawable;
    }

    /* Check if current color of the transparent pixels is different
       from the specified one */
    if (xsdo->isBgInitialized == JNI_FALSE || xsdo->bgPixel != pixel) {
        GC srcGC;
        GC bmGC;

        if (xsdo->drawable == 0) {
            AWT_UNLOCK();
            return 0;
        }

        bmGC = XCreateGC(awt_display, xsdo->bitmask, 0, NULL);
        if (bmGC == NULL) {
            AWT_UNLOCK();
            return 0;
        }

        /* invert the bitmask */
        XSetFunction(awt_display, bmGC, GXxor);
        XSetForeground(awt_display, bmGC, 1);
        XFillRectangle(awt_display, xsdo->bitmask, bmGC,
                       0, 0, xsdo->pmWidth, xsdo->pmHeight);

        srcGC = XCreateGC(awt_display, xsdo->drawable, 0L, NULL);
        if (srcGC == NULL) {
            XFreeGC(awt_display, bmGC);
            AWT_UNLOCK();
            return 0;
        }

        /* set transparent pixels in the source pm to the bg color */
        XSetClipMask(awt_display, srcGC, xsdo->bitmask);
        XSetForeground(awt_display, srcGC, pixel);
        XFillRectangle(awt_display, xsdo->drawable, srcGC,
                       0, 0, xsdo->pmWidth, xsdo->pmHeight);

        /* invert the mask back */
        XFillRectangle(awt_display, xsdo->bitmask, bmGC,
                       0, 0, xsdo->pmWidth, xsdo->pmHeight);

        XFreeGC(awt_display, bmGC);
        XFreeGC(awt_display, srcGC);
        xsdo->bgPixel = pixel;
        xsdo->isBgInitialized = JNI_TRUE;
    }

    return xsdo->drawable;
}

static void
X11SD_ReleasePixmapWithBg(JNIEnv *env, X11SDOps *xsdo)
{
#ifdef MITSHM
    if (xsdo->shmPMData.usingShmPixmap) {
        xsdo->shmPMData.xRequestSent = JNI_TRUE;
    }
#endif /* MITSHM */
}

#endif /* !HEADLESS */

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XCreateGC
 * Signature: (I)J
 */
JNIEXPORT jlong JNICALL
1508
Java_sun_java2d_x11_XSurfaceData_XCreateGC
D
duke 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    (JNIEnv *env, jclass xsd, jlong pXSData)
{
    jlong ret;

#ifndef HEADLESS
    X11SDOps *xsdo;

    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XCreateGC");

    xsdo = (X11SDOps *) pXSData;
    if (xsdo == NULL) {
        return 0L;
    }

    xsdo->javaGC = XCreateGC(awt_display, xsdo->drawable, 0, NULL);
    ret = (jlong) xsdo->javaGC;
#else /* !HEADLESS */
    ret = 0L;
#endif /* !HEADLESS */

    return ret;
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XResetClip
 * Signature: (JIIIILsun/java2d/pipe/Region;)V
 */
JNIEXPORT void JNICALL
1538
Java_sun_java2d_x11_XSurfaceData_XResetClip
D
duke 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
    (JNIEnv *env, jclass xsd, jlong xgc)
{
#ifndef HEADLESS
    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XResetClip");
    XSetClipMask(awt_display, (GC) xgc, None);
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XSetClip
 * Signature: (JIIIILsun/java2d/pipe/Region;)V
 */
JNIEXPORT void JNICALL
1553
Java_sun_java2d_x11_XSurfaceData_XSetClip
D
duke 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    (JNIEnv *env, jclass xsd, jlong xgc,
     jint x1, jint y1, jint x2, jint y2,
     jobject complexclip)
{
#ifndef HEADLESS
    int numrects;
    XRectangle rects[256];
    XRectangle *pRect = rects;

    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetClip");

    numrects = RegionToYXBandedRectangles(env,
            x1, y1, x2, y2, complexclip,
            &pRect, 256);

    XSetClipRectangles(awt_display, (GC) xgc, 0, 0, pRect, numrects, YXBanded);

    if (pRect != rects) {
        free(pRect);
    }
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XSetCopyMode
 * Signature: (J)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11SurfaceData_XSetCopyMode
    (JNIEnv *env, jclass xsd, jlong xgc)
{
#ifndef HEADLESS
    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetCopyMode");
    XSetFunction(awt_display, (GC) xgc, GXcopy);
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XSetXorMode
 * Signature: (J)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11SurfaceData_XSetXorMode
    (JNIEnv *env, jclass xr, jlong xgc)
{
#ifndef HEADLESS
    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetXorMode");
    XSetFunction(awt_display, (GC) xgc, GXxor);
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XSetForeground
 * Signature: (JI)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11SurfaceData_XSetForeground
    (JNIEnv *env, jclass xsd, jlong xgc, jint pixel)
{
#ifndef HEADLESS
    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetForeground");
    XSetForeground(awt_display, (GC) xgc, pixel);
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11SurfaceData
 * Method:    XSetGraphicsExposures
 * Signature: (JZ)V
 */
JNIEXPORT void JNICALL
1628
Java_sun_java2d_x11_XSurfaceData_XSetGraphicsExposures
D
duke 已提交
1629 1630 1631 1632 1633 1634 1635
    (JNIEnv *env, jclass xsd, jlong xgc, jboolean needExposures)
{
#ifndef HEADLESS
    J2dTraceLn(J2D_TRACE_INFO, "in X11SurfaceData_XSetGraphicsExposures");
    XSetGraphicsExposures(awt_display, (GC) xgc, needExposures ? True : False);
#endif /* !HEADLESS */
}