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