X11PMBlitLoops.c 12.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
 */

#include <stdlib.h>
#include <jni.h>
#include <jlong.h>
#include "X11SurfaceData.h"
#include "Region.h"

JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11PMBlitLoops_nativeBlit
    (JNIEnv *env, jobject joSelf,
     jlong srcData, jlong dstData,
     jlong gc, jobject clip,
     jint srcx, jint srcy,
     jint dstx, jint dsty,
     jint width, jint height)
{
#ifndef HEADLESS
    X11SDOps *srcXsdo, *dstXsdo;
    SurfaceDataBounds span, srcBounds;
    RegionData clipInfo;
    GC xgc;

    if (width <= 0 || height <= 0) {
        return;
    }

    srcXsdo = (X11SDOps *)jlong_to_ptr(srcData);
    if (srcXsdo == NULL) {
        return;
    }
    dstXsdo = (X11SDOps *)jlong_to_ptr(dstData);
    if (dstXsdo == NULL) {
        return;
    }
    if (Region_GetInfo(env, clip, &clipInfo)) {
        return;
    }

    xgc = (GC)gc;
    if (xgc == NULL) {
        return;
    }

#ifdef MITSHM
    if (srcXsdo->isPixmap) {
        X11SD_UnPuntPixmap(srcXsdo);
    }
#endif /* MITSHM */

    /* clip the source rect to the source pixmap's dimensions */
    srcBounds.x1 = srcx;
    srcBounds.y1 = srcy;
    srcBounds.x2 = srcx + width;
    srcBounds.y2 = srcy + height;
    SurfaceData_IntersectBoundsXYXY(&srcBounds,
                                    0, 0, srcXsdo->pmWidth, srcXsdo->pmHeight);
    span.x1 = dstx;
    span.y1 = dsty;
    span.x2 = dstx + width;
    span.y2 = dsty + height;

    /* intersect the source and dest rects */
    SurfaceData_IntersectBlitBounds(&srcBounds, &span,
                                    dstx - srcx, dsty - srcy);
    srcx = srcBounds.x1;
    srcy = srcBounds.y1;
    dstx = span.x1;
    dsty = span.y1;

    if (srcXsdo->bitmask != 0) {
        XSetClipOrigin(awt_display, xgc, dstx - srcx, dsty - srcy);
        XSetClipMask(awt_display, xgc, srcXsdo->bitmask);
    }

    Region_IntersectBounds(&clipInfo, &span);
    if (!Region_IsEmpty(&clipInfo)) {
        Region_StartIteration(env, &clipInfo);
        srcx -= dstx;
        srcy -= dsty;
        while (Region_NextIteration(&clipInfo, &span)) {
            XCopyArea(awt_display, srcXsdo->drawable, dstXsdo->drawable, xgc,
                      srcx + span.x1, srcy + span.y1,
                      span.x2 - span.x1, span.y2 - span.y1,
                      span.x1, span.y1);
        }
        Region_EndIteration(env, &clipInfo);
    }

    if (srcXsdo->bitmask != 0) {
        XSetClipMask(awt_display, xgc, None);
    }

#ifdef MITSHM
    if (srcXsdo->shmPMData.usingShmPixmap) {
        srcXsdo->shmPMData.xRequestSent = JNI_TRUE;
    }
#endif /* MITSHM */
    X11SD_DirectRenderNotify(env, dstXsdo);
#endif /* !HEADLESS */
}

JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11PMBlitBgLoops_nativeBlitBg
    (JNIEnv *env, jobject joSelf,
     jlong srcData, jlong dstData,
     jlong xgc, jint pixel,
     jint srcx, jint srcy,
     jint dstx, jint dsty,
     jint width, jint height)
{
#ifndef HEADLESS
    X11SDOps *srcXsdo, *dstXsdo;
    GC dstGC;
    SurfaceDataBounds dstBounds, srcBounds;
    Drawable srcDrawable;

    if (width <= 0 || height <= 0) {
        return;
    }

    srcXsdo = (X11SDOps *)jlong_to_ptr(srcData);
    if (srcXsdo == NULL) {
        return;
    }
    dstXsdo = (X11SDOps *)jlong_to_ptr(dstData);
    if (dstXsdo == NULL) {
        return;
    }

    dstGC = (GC)xgc;
    if (dstGC == NULL) {
        return;
    }

#ifdef MITSHM
    if (srcXsdo->isPixmap) {
        X11SD_UnPuntPixmap(srcXsdo);
    }
#endif /* MITSHM */

    srcDrawable = srcXsdo->GetPixmapWithBg(env, srcXsdo, pixel);
    if (srcDrawable == 0) {
        return;
    }

    /* clip the source rect to the source pixmap's dimensions */
    srcBounds.x1 = srcx;
    srcBounds.y1 = srcy;
    srcBounds.x2 = srcx + width;
    srcBounds.y2 = srcy + height;
    SurfaceData_IntersectBoundsXYXY(&srcBounds,
                                    0, 0, srcXsdo->pmWidth, srcXsdo->pmHeight);
    dstBounds.x1 = dstx;
    dstBounds.y1 = dsty;
    dstBounds.x2 = dstx + width;
    dstBounds.y2 = dsty + height;

    /* intersect the source and dest rects */
    SurfaceData_IntersectBlitBounds(&srcBounds, &dstBounds,
                                    dstx - srcx, dsty - srcy);
    srcx = srcBounds.x1;
    srcy = srcBounds.y1;
    dstx = dstBounds.x1;
    dsty = dstBounds.y1;
    width = srcBounds.x2 - srcBounds.x1;
    height = srcBounds.y2 - srcBounds.y1;

    /* do an unmasked copy as we've already filled transparent
       pixels of the source image with the desired color */
    XCopyArea(awt_display, srcDrawable, dstXsdo->drawable, dstGC,
              srcx, srcy, width, height, dstx, dsty);

    srcXsdo->ReleasePixmapWithBg(env, srcXsdo);
    X11SD_DirectRenderNotify(env, dstXsdo);
#endif /* !HEADLESS */
}

/*
 * Class:     sun_java2d_x11_X11PMBlitLoops
 * Method:    updateBitmask
 * Signature: (Lsun/java2d/SurfaceData;Lsun/java2d/SurfaceData;)V
 */
JNIEXPORT void JNICALL
Java_sun_java2d_x11_X11PMBlitLoops_updateBitmask
    (JNIEnv *env, jclass xpmbl, jobject srcsd, jobject dstsd, jboolean isICM)
{
#ifndef HEADLESS
    SurfaceDataOps *srcOps = SurfaceData_GetOps(env, srcsd);
    X11SDOps *xsdo = (X11SDOps *) SurfaceData_GetOps(env, dstsd);
    SurfaceDataRasInfo srcInfo;

    int flags;
    int screen;
    int width;
    int height;
    jint srcScan, dstScan;
    int rowCount;
    unsigned char *pDst;
    XImage *image;
    GC xgc;

    if (srcOps == NULL || xsdo == NULL) {
        JNU_ThrowNullPointerException(env, "Null BISD in updateMaskRegion");
        return;
    }

    AWT_LOCK();

    screen = xsdo->configData->awt_visInfo.screen;
    width = xsdo->pmWidth;
    height = xsdo->pmHeight;

    if (xsdo->bitmask == 0) {
        /* create the bitmask if it is not yet created */
        xsdo->bitmask = XCreatePixmap(awt_display,
                                      RootWindow(awt_display, screen),
                                      width, height, 1);
        if (xsdo->bitmask == 0) {
            AWT_UNLOCK();
244 245 246 247 248 249
            if (!(*env)->ExceptionCheck(env))
            {
                JNU_ThrowOutOfMemoryError(env,
                                          "Cannot create bitmask for "
                                          "offscreen surface");
            }
D
duke 已提交
250 251 252 253 254 255 256 257 258
            return;
        }
    }

    /* Create a bitmask image and then blit it to the pixmap. */
    image = XCreateImage(awt_display, DefaultVisual(awt_display, screen),
                         1, XYBitmap, 0, NULL, width, height, 32, 0);
    if (image == NULL) {
        AWT_UNLOCK();
259 260 261 262
        if (!(*env)->ExceptionCheck(env))
        {
             JNU_ThrowOutOfMemoryError(env, "Cannot allocate bitmask for mask");
        }
D
duke 已提交
263 264 265
        return;
    }
    dstScan = image->bytes_per_line;
B
bae 已提交
266
    image->data = malloc((size_t) dstScan * height);
D
duke 已提交
267 268 269
    if (image->data == NULL) {
        XFree(image);
        AWT_UNLOCK();
270 271 272 273
        if (!(*env)->ExceptionCheck(env))
        {
            JNU_ThrowOutOfMemoryError(env, "Cannot allocate bitmask for mask");
        }
D
duke 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        return;
    }
    pDst = (unsigned char *)image->data;

    srcInfo.bounds.x1 = 0;
    srcInfo.bounds.y1 = 0;
    srcInfo.bounds.x2 = width;
    srcInfo.bounds.y2 = height;

    flags = (isICM ? (SD_LOCK_LUT | SD_LOCK_READ) : SD_LOCK_READ);
    if (srcOps->Lock(env, srcOps, &srcInfo, flags) != SD_SUCCESS) {
        XDestroyImage(image);
        AWT_UNLOCK();
        return;
    }
    srcOps->GetRasInfo(env, srcOps, &srcInfo);

    rowCount = height;
    if (isICM) {
        unsigned char *pSrc;
        jint *srcLut;

        srcScan = srcInfo.scanStride;
        srcLut = srcInfo.lutBase;
        pSrc = (unsigned char *)srcInfo.rasBase;

        if (image->bitmap_bit_order == MSBFirst) {
            do {
                int x = 0, bx = 0;
                unsigned int pix = 0;
                unsigned int bit = 0x80;
                unsigned char *srcPixel = pSrc;
                do {
                    if (bit == 0) {
                        pDst[bx++] = (unsigned char)pix;
                        pix = 0;
                        bit = 0x80;
                    }
                    pix |= bit & (srcLut[*srcPixel++] >> 31);
                    bit >>= 1;
                } while (++x < width);
                pDst[bx] = (unsigned char)pix;
                pDst += dstScan;
                pSrc = (unsigned char *) (((intptr_t)pSrc) + srcScan);
            } while (--rowCount > 0);
        } else {
            do {
                int x = 0, bx = 0;
                unsigned int pix = 0;
                unsigned int bit = 1;
                unsigned char *srcPixel = pSrc;
                do {
                    if ((bit >> 8) != 0) {
                        pDst[bx++] = (unsigned char) pix;
                        pix = 0;
                        bit = 1;
                    }
                    pix |= bit & (srcLut[*srcPixel++] >> 31);
                    bit <<= 1;
                } while (++x < width);
                pDst[bx] = (unsigned char) pix;
                pDst += dstScan;
                pSrc = (unsigned char *) (((intptr_t)pSrc) + srcScan);
            } while (--rowCount > 0);
        }
    } else /*DCM with ARGB*/ {
        unsigned int *pSrc;

        /* this is a number of pixels in a row, not number of bytes */
        srcScan = srcInfo.scanStride;
        pSrc = (unsigned int *)srcInfo.rasBase;

        if (image->bitmap_bit_order == MSBFirst) {
            do {
                int x = 0, bx = 0;
                unsigned int pix = 0;
                unsigned int bit = 0x80;
                int *srcPixel = (int *) pSrc;
                do {
                    if (bit == 0) {
                        /* next word */
                        pDst[bx++] = (unsigned char)pix;
                        pix = 0;
                        bit = 0x80;
                    }
                    if (*srcPixel++ & 0xff000000) {
                        /* if src pixel is opaque, set the bit in the bitmap */
                        pix |= bit;
                    }
                    bit >>= 1;
                } while (++x < width);
                /* last pixels in a row */
                pDst[bx] = (unsigned char)pix;

                pDst += dstScan;
                pSrc = (unsigned int *) (((intptr_t)pSrc) + srcScan);
            } while (--rowCount > 0);
        } else {
            do {
                int x = 0, bx = 0;
                unsigned int pix = 0;
                unsigned int bit = 1;
                int *srcPixel = (int *) pSrc;
                do {
                    if ((bit >> 8) != 0) {
                        pDst[bx++] = (unsigned char)pix;
                        pix = 0;
                        bit = 1;
                    }
                    if (*srcPixel++ & 0xff000000) {
                        /* if src pixel is opaque, set the bit in the bitmap */
                        pix |= bit;
                    }
                    bit <<= 1;
                } while (++x < width);
                pDst[bx] = (unsigned char)pix;
                pDst += dstScan;
                pSrc = (unsigned int *) (((intptr_t)pSrc) + srcScan);
            } while (--rowCount > 0);
        }
    }
    SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
    SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);

    xgc = XCreateGC(awt_display, xsdo->bitmask, 0L, NULL);
    XSetForeground(awt_display, xgc, 1);
    XSetBackground(awt_display, xgc, 0);
    XPutImage(awt_display, xsdo->bitmask, xgc,
              image, 0, 0, 0, 0, width, height);

    XFreeGC(awt_display, xgc);
    XDestroyImage(image);

    AWT_UNLOCK();
#endif /* !HEADLESS */
}