6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer
authorjgodinez
Wed, 15 Apr 2009 08:47:21 -0700
changeset 2696 1189480fe090
parent 2695 d75c6ebf3ac6
child 2697 b27edaf0767e
6827989: Use Unsafe.copyMemory for array->Unsafe copy operations in RenderBuffer Reviewed-by: campbell, flar Contributed-by: linuxhippy <linuxhippy@gmail.com>
jdk/make/sun/awt/FILES_c_unix.gmk
jdk/make/sun/awt/FILES_c_windows.gmk
jdk/make/sun/awt/mapfile-vers
jdk/make/sun/awt/mapfile-vers-linux
jdk/src/share/classes/sun/java2d/pipe/RenderBuffer.java
jdk/src/share/native/sun/java2d/pipe/RenderBuffer.c
--- a/jdk/make/sun/awt/FILES_c_unix.gmk	Tue Apr 14 17:43:45 2009 -0700
+++ b/jdk/make/sun/awt/FILES_c_unix.gmk	Wed Apr 15 08:47:21 2009 -0700
@@ -125,7 +125,6 @@
         FourByteAbgrPre.c \
 	BufferedMaskBlit.c \
 	BufferedRenderPipe.c \
-	RenderBuffer.c \
 	ShapeSpanIterator.c \
 	SpanClipRenderer.c \
 	awt_ImageRep.c \
--- a/jdk/make/sun/awt/FILES_c_windows.gmk	Tue Apr 14 17:43:45 2009 -0700
+++ b/jdk/make/sun/awt/FILES_c_windows.gmk	Wed Apr 15 08:47:21 2009 -0700
@@ -70,7 +70,6 @@
         FourByteAbgrPre.c \
 	BufferedMaskBlit.c \
 	BufferedRenderPipe.c \
-	RenderBuffer.c \
 	ShapeSpanIterator.c \
 	SpanClipRenderer.c \
 	SurfaceData.c \
--- a/jdk/make/sun/awt/mapfile-vers	Tue Apr 14 17:43:45 2009 -0700
+++ b/jdk/make/sun/awt/mapfile-vers	Wed Apr 15 08:47:21 2009 -0700
@@ -65,7 +65,6 @@
 		Java_sun_awt_image_ShortComponentRaster_initIDs;
                 Java_sun_java2d_pipe_BufferedMaskBlit_enqueueTile;
                 Java_sun_java2d_pipe_BufferedRenderPipe_fillSpans;
-                Java_sun_java2d_pipe_RenderBuffer_copyFromArray;
 		Java_sun_java2d_pipe_SpanClipRenderer_eraseTile;
 		Java_sun_java2d_pipe_SpanClipRenderer_fillTile;
                 Java_sun_java2d_pipe_ShapeSpanIterator_addSegment;
--- a/jdk/make/sun/awt/mapfile-vers-linux	Tue Apr 14 17:43:45 2009 -0700
+++ b/jdk/make/sun/awt/mapfile-vers-linux	Wed Apr 15 08:47:21 2009 -0700
@@ -117,7 +117,6 @@
 		Java_sun_java2d_loops_MaskBlit_MaskBlit;
 		Java_sun_java2d_loops_MaskFill_MaskFill;
                 Java_sun_java2d_pipe_BufferedRenderPipe_fillSpans;
-                Java_sun_java2d_pipe_RenderBuffer_copyFromArray;
 		Java_sun_java2d_pipe_SpanClipRenderer_initIDs;
 		sun_awt_image_GifImageDecoder_initIDs;
 
--- a/jdk/src/share/classes/sun/java2d/pipe/RenderBuffer.java	Tue Apr 14 17:43:45 2009 -0700
+++ b/jdk/src/share/classes/sun/java2d/pipe/RenderBuffer.java	Wed Apr 15 08:47:21 2009 -0700
@@ -63,7 +63,7 @@
      * (This value can be adjusted if the cost of JNI downcalls is reduced
      * in a future release.)
      */
-    private static final int COPY_FROM_ARRAY_THRESHOLD = 28;
+    private static final int COPY_FROM_ARRAY_THRESHOLD = 6;
 
     protected final Unsafe unsafe;
     protected final long baseAddress;
@@ -93,20 +93,6 @@
     }
 
     /**
-     * Copies length bytes from the Java-level srcArray to the native
-     * memory located at dstAddr.  Note that this method performs no bounds
-     * checking.  Verification that the copy will not result in memory
-     * corruption should be done by the caller prior to invocation.
-     *
-     * @param srcArray the source array
-     * @param srcPos the starting position of the source array (in bytes)
-     * @param dstAddr pointer to the destination block of native memory
-     * @param length the number of bytes to copy from source to destination
-     */
-    private static native void copyFromArray(Object srcArray, long srcPos,
-                                             long dstAddr, long length);
-
-    /**
      * The behavior (and names) of the following methods are nearly
      * identical to their counterparts in the various NIO Buffer classes.
      */
@@ -147,9 +133,9 @@
 
     public RenderBuffer put(byte[] x, int offset, int length) {
         if (length > COPY_FROM_ARRAY_THRESHOLD) {
-            long offsetInBytes = offset * SIZEOF_BYTE;
+            long offsetInBytes = offset * SIZEOF_BYTE + Unsafe.ARRAY_BYTE_BASE_OFFSET;
             long lengthInBytes = length * SIZEOF_BYTE;
-            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
+            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
             position(position() + lengthInBytes);
         } else {
             int end = offset + length;
@@ -178,9 +164,9 @@
     public RenderBuffer put(short[] x, int offset, int length) {
         // assert (position() % SIZEOF_SHORT == 0);
         if (length > COPY_FROM_ARRAY_THRESHOLD) {
-            long offsetInBytes = offset * SIZEOF_SHORT;
+            long offsetInBytes = offset * SIZEOF_SHORT + Unsafe.ARRAY_SHORT_BASE_OFFSET;
             long lengthInBytes = length * SIZEOF_SHORT;
-            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
+            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
             position(position() + lengthInBytes);
         } else {
             int end = offset + length;
@@ -215,9 +201,9 @@
     public RenderBuffer put(int[] x, int offset, int length) {
         // assert (position() % SIZEOF_INT == 0);
         if (length > COPY_FROM_ARRAY_THRESHOLD) {
-            long offsetInBytes = offset * SIZEOF_INT;
+            long offsetInBytes = offset * SIZEOF_INT + Unsafe.ARRAY_INT_BASE_OFFSET;
             long lengthInBytes = length * SIZEOF_INT;
-            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
+            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
             position(position() + lengthInBytes);
         } else {
             int end = offset + length;
@@ -246,9 +232,9 @@
     public RenderBuffer put(float[] x, int offset, int length) {
         // assert (position() % SIZEOF_FLOAT == 0);
         if (length > COPY_FROM_ARRAY_THRESHOLD) {
-            long offsetInBytes = offset * SIZEOF_FLOAT;
+            long offsetInBytes = offset * SIZEOF_FLOAT + Unsafe.ARRAY_FLOAT_BASE_OFFSET;
             long lengthInBytes = length * SIZEOF_FLOAT;
-            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
+            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
             position(position() + lengthInBytes);
         } else {
             int end = offset + length;
@@ -277,9 +263,9 @@
     public RenderBuffer put(long[] x, int offset, int length) {
         // assert (position() % SIZEOF_LONG == 0);
         if (length > COPY_FROM_ARRAY_THRESHOLD) {
-            long offsetInBytes = offset * SIZEOF_LONG;
+            long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
             long lengthInBytes = length * SIZEOF_LONG;
-            copyFromArray(x, offsetInBytes, curAddress, lengthInBytes);
+            unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
             position(position() + lengthInBytes);
         } else {
             int end = offset + length;
--- a/jdk/src/share/native/sun/java2d/pipe/RenderBuffer.c	Tue Apr 14 17:43:45 2009 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,71 +0,0 @@
-/*
- * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
- * 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
- * published by the Free Software Foundation.  Sun designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Sun in the LICENSE file that accompanied this code.
- *
- * 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.
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
- */
-
-#include "jni.h"
-#include "jni_util.h"
-#include "jlong.h"
-#include <string.h>
-
-#include "sun_java2d_pipe_RenderBuffer.h"
-
-/**
- * Note: The code in this file is nearly identical to that in
- *       java/nio/Bits.c...
- */
-
-#define MBYTE 1048576
-
-JNIEXPORT void JNICALL
-Java_sun_java2d_pipe_RenderBuffer_copyFromArray
-    (JNIEnv *env, jclass rb,
-     jobject srcArray, jlong srcPos, jlong dstAddr, jlong length)
-{
-    jbyte *bytes;
-    size_t size;
-
-    while (length > 0) {
-        /*
-         * Copy no more than one megabyte at a time, to allow for GC.
-         * (Probably not an issue for STR, since our buffer size is likely
-         * much smaller than a megabyte, but just in case...)
-         */
-        size = (size_t)(length > MBYTE ? MBYTE : length);
-
-        bytes = (*env)->GetPrimitiveArrayCritical(env, srcArray, NULL);
-        if (bytes == NULL) {
-            JNU_ThrowInternalError(env, "Unable to get array");
-            return;
-        }
-
-        memcpy(jlong_to_ptr(dstAddr), bytes + srcPos, size);
-
-        (*env)->ReleasePrimitiveArrayCritical(env, srcArray,
-                                              bytes, JNI_ABORT);
-
-        length -= size;
-        dstAddr += size;
-        srcPos += size;
-    }
-}