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