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