6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
authorjgodinez
Wed, 15 Apr 2009 08:47:21 -0700
changeset 1182d609ae2faac2
parent 1181 e61d93fc8ed1
child 1183 c3aaa11e4eb6
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
Reviewed-by: campbell, flar
Contributed-by: linuxhippy <linuxhippy@gmail.com>
make/sun/awt/FILES_c_unix.gmk
make/sun/awt/FILES_c_windows.gmk
make/sun/awt/mapfile-vers
make/sun/awt/mapfile-vers-linux
src/share/classes/sun/java2d/pipe/RenderBuffer.java
src/share/native/sun/java2d/pipe/RenderBuffer.c
     1.1 --- a/make/sun/awt/FILES_c_unix.gmk	Tue Apr 14 17:43:45 2009 -0700
     1.2 +++ b/make/sun/awt/FILES_c_unix.gmk	Wed Apr 15 08:47:21 2009 -0700
     1.3 @@ -125,7 +125,6 @@
     1.4          FourByteAbgrPre.c \
     1.5  	BufferedMaskBlit.c \
     1.6  	BufferedRenderPipe.c \
     1.7 -	RenderBuffer.c \
     1.8  	ShapeSpanIterator.c \
     1.9  	SpanClipRenderer.c \
    1.10  	awt_ImageRep.c \
     2.1 --- a/make/sun/awt/FILES_c_windows.gmk	Tue Apr 14 17:43:45 2009 -0700
     2.2 +++ b/make/sun/awt/FILES_c_windows.gmk	Wed Apr 15 08:47:21 2009 -0700
     2.3 @@ -70,7 +70,6 @@
     2.4          FourByteAbgrPre.c \
     2.5  	BufferedMaskBlit.c \
     2.6  	BufferedRenderPipe.c \
     2.7 -	RenderBuffer.c \
     2.8  	ShapeSpanIterator.c \
     2.9  	SpanClipRenderer.c \
    2.10  	SurfaceData.c \
     3.1 --- a/make/sun/awt/mapfile-vers	Tue Apr 14 17:43:45 2009 -0700
     3.2 +++ b/make/sun/awt/mapfile-vers	Wed Apr 15 08:47:21 2009 -0700
     3.3 @@ -65,7 +65,6 @@
     3.4  		Java_sun_awt_image_ShortComponentRaster_initIDs;
     3.5                  Java_sun_java2d_pipe_BufferedMaskBlit_enqueueTile;
     3.6                  Java_sun_java2d_pipe_BufferedRenderPipe_fillSpans;
     3.7 -                Java_sun_java2d_pipe_RenderBuffer_copyFromArray;
     3.8  		Java_sun_java2d_pipe_SpanClipRenderer_eraseTile;
     3.9  		Java_sun_java2d_pipe_SpanClipRenderer_fillTile;
    3.10                  Java_sun_java2d_pipe_ShapeSpanIterator_addSegment;
     4.1 --- a/make/sun/awt/mapfile-vers-linux	Tue Apr 14 17:43:45 2009 -0700
     4.2 +++ b/make/sun/awt/mapfile-vers-linux	Wed Apr 15 08:47:21 2009 -0700
     4.3 @@ -117,7 +117,6 @@
     4.4  		Java_sun_java2d_loops_MaskBlit_MaskBlit;
     4.5  		Java_sun_java2d_loops_MaskFill_MaskFill;
     4.6                  Java_sun_java2d_pipe_BufferedRenderPipe_fillSpans;
     4.7 -                Java_sun_java2d_pipe_RenderBuffer_copyFromArray;
     4.8  		Java_sun_java2d_pipe_SpanClipRenderer_initIDs;
     4.9  		sun_awt_image_GifImageDecoder_initIDs;
    4.10  
     5.1 --- a/src/share/classes/sun/java2d/pipe/RenderBuffer.java	Tue Apr 14 17:43:45 2009 -0700
     5.2 +++ b/src/share/classes/sun/java2d/pipe/RenderBuffer.java	Wed Apr 15 08:47:21 2009 -0700
     5.3 @@ -63,7 +63,7 @@
     5.4       * (This value can be adjusted if the cost of JNI downcalls is reduced
     5.5       * in a future release.)
     5.6       */
     5.7 -    private static final int COPY_FROM_ARRAY_THRESHOLD = 28;
     5.8 +    private static final int COPY_FROM_ARRAY_THRESHOLD = 6;
     5.9  
    5.10      protected final Unsafe unsafe;
    5.11      protected final long baseAddress;
    5.12 @@ -93,20 +93,6 @@
    5.13      }
    5.14  
    5.15      /**
    5.16 -     * Copies length bytes from the Java-level srcArray to the native
    5.17 -     * memory located at dstAddr.  Note that this method performs no bounds
    5.18 -     * checking.  Verification that the copy will not result in memory
    5.19 -     * corruption should be done by the caller prior to invocation.
    5.20 -     *
    5.21 -     * @param srcArray the source array
    5.22 -     * @param srcPos the starting position of the source array (in bytes)
    5.23 -     * @param dstAddr pointer to the destination block of native memory
    5.24 -     * @param length the number of bytes to copy from source to destination
    5.25 -     */
    5.26 -    private static native void copyFromArray(Object srcArray, long srcPos,
    5.27 -                                             long dstAddr, long length);
    5.28 -
    5.29 -    /**
    5.30       * The behavior (and names) of the following methods are nearly
    5.31       * identical to their counterparts in the various NIO Buffer classes.
    5.32       */
    5.33 @@ -147,9 +133,9 @@
    5.34  
    5.35      public RenderBuffer put(byte[] x, int offset, int length) {
    5.36          if (length > COPY_FROM_ARRAY_THRESHOLD) {
    5.37 -            long offsetInBytes = offset * SIZEOF_BYTE;
    5.38 +            long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
    5.39              long lengthInBytes = length * SIZEOF_BYTE;
    5.40 -            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
    5.41 +            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
    5.42              position(position() + lengthInBytes);
    5.43          } else {
    5.44              int end = offset + length;
    5.45 @@ -178,9 +164,9 @@
    5.46      public RenderBuffer put(short[] x, int offset, int length) {
    5.47          // assert (position() % SIZEOF_SHORT == 0);
    5.48          if (length > COPY_FROM_ARRAY_THRESHOLD) {
    5.49 -            long offsetInBytes = offset * SIZEOF_SHORT;
    5.50 +            long offsetInBytes = offset * SIZEOF_SHORT + Unsafe.ARRAY_SHORT_BASE_OFFSET;
    5.51              long lengthInBytes = length * SIZEOF_SHORT;
    5.52 -            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
    5.53 +            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
    5.54              position(position() + lengthInBytes);
    5.55          } else {
    5.56              int end = offset + length;
    5.57 @@ -215,9 +201,9 @@
    5.58      public RenderBuffer put(int[] x, int offset, int length) {
    5.59          // assert (position() % SIZEOF_INT == 0);
    5.60          if (length > COPY_FROM_ARRAY_THRESHOLD) {
    5.61 -            long offsetInBytes = offset * SIZEOF_INT;
    5.62 +            long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
    5.63              long lengthInBytes = length * SIZEOF_INT;
    5.64 -            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
    5.65 +            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
    5.66              position(position() + lengthInBytes);
    5.67          } else {
    5.68              int end = offset + length;
    5.69 @@ -246,9 +232,9 @@
    5.70      public RenderBuffer put(float[] x, int offset, int length) {
    5.71          // assert (position() % SIZEOF_FLOAT == 0);
    5.72          if (length > COPY_FROM_ARRAY_THRESHOLD) {
    5.73 -            long offsetInBytes = offset * SIZEOF_FLOAT;
    5.74 +            long offsetInBytes = offset * SIZEOF_FLOAT + Unsafe.ARRAY_FLOAT_BASE_OFFSET;
    5.75              long lengthInBytes = length * SIZEOF_FLOAT;
    5.76 -            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
    5.77 +            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
    5.78              position(position() + lengthInBytes);
    5.79          } else {
    5.80              int end = offset + length;
    5.81 @@ -277,9 +263,9 @@
    5.82      public RenderBuffer put(long[] x, int offset, int length) {
    5.83          // assert (position() % SIZEOF_LONG == 0);
    5.84          if (length > COPY_FROM_ARRAY_THRESHOLD) {
    5.85 -            long offsetInBytes = offset * SIZEOF_LONG;
    5.86 +            long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
    5.87              long lengthInBytes = length * SIZEOF_LONG;
    5.88 -            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
    5.89 +            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
    5.90              position(position() + lengthInBytes);
    5.91          } else {
    5.92              int end = offset + length;
     6.1 --- a/src/share/native/sun/java2d/pipe/RenderBuffer.c	Tue Apr 14 17:43:45 2009 -0700
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,71 +0,0 @@
     6.4 -/*
     6.5 - * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
     6.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.7 - *
     6.8 - * This code is free software; you can redistribute it and/or modify it
     6.9 - * under the terms of the GNU General Public License version 2 only, as
    6.10 - * published by the Free Software Foundation.  Sun designates this
    6.11 - * particular file as subject to the "Classpath" exception as provided
    6.12 - * by Sun in the LICENSE file that accompanied this code.
    6.13 - *
    6.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    6.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    6.17 - * version 2 for more details (a copy is included in the LICENSE file that
    6.18 - * accompanied this code).
    6.19 - *
    6.20 - * You should have received a copy of the GNU General Public License version
    6.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    6.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    6.23 - *
    6.24 - * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    6.25 - * CA 95054 USA or visit www.sun.com if you need additional information or
    6.26 - * have any questions.
    6.27 - */
    6.28 -
    6.29 -#include "jni.h"
    6.30 -#include "jni_util.h"
    6.31 -#include "jlong.h"
    6.32 -#include <string.h>
    6.33 -
    6.34 -#include "sun_java2d_pipe_RenderBuffer.h"
    6.35 -
    6.36 -/**
    6.37 - * Note: The code in this file is nearly identical to that in
    6.38 - *       java/nio/Bits.c...
    6.39 - */
    6.40 -
    6.41 -#define MBYTE 1048576
    6.42 -
    6.43 -JNIEXPORT void JNICALL
    6.44 -Java_sun_java2d_pipe_RenderBuffer_copyFromArray
    6.45 -    (JNIEnv *env, jclass rb,
    6.46 -     jobject srcArray, jlong srcPos, jlong dstAddr, jlong length)
    6.47 -{
    6.48 -    jbyte *bytes;
    6.49 -    size_t size;
    6.50 -
    6.51 -    while (length > 0) {
    6.52 -        /*
    6.53 -         * Copy no more than one megabyte at a time, to allow for GC.
    6.54 -         * (Probably not an issue for STR, since our buffer size is likely
    6.55 -         * much smaller than a megabyte, but just in case...)
    6.56 -         */
    6.57 -        size = (size_t)(length > MBYTE ? MBYTE : length);
    6.58 -
    6.59 -        bytes = (*env)->GetPrimitiveArrayCritical(env, srcArray, NULL);
    6.60 -        if (bytes == NULL) {
    6.61 -            JNU_ThrowInternalError(env, "Unable to get array");
    6.62 -            return;
    6.63 -        }
    6.64 -
    6.65 -        memcpy(jlong_to_ptr(dstAddr), bytes + srcPos, size);
    6.66 -
    6.67 -        (*env)->ReleasePrimitiveArrayCritical(env, srcArray,
    6.68 -                                              bytes, JNI_ABORT);
    6.69 -
    6.70 -        length -= size;
    6.71 -        dstAddr += size;
    6.72 -        srcPos += size;
    6.73 -    }
    6.74 -}