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