< prev index next >

src/java.desktop/share/classes/sun/java2d/marlin/FloatArrayCache.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2016, 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 package sun.java2d.marlin;
  26 
  27 import static sun.java2d.marlin.ArrayCacheConst.ARRAY_SIZES;
  28 import static sun.java2d.marlin.ArrayCacheConst.BUCKETS;
  29 import static sun.java2d.marlin.ArrayCacheConst.MAX_ARRAY_SIZE;
  30 import static sun.java2d.marlin.MarlinUtils.logInfo;
  31 import static sun.java2d.marlin.MarlinUtils.logException;
  32 
  33 import java.lang.ref.WeakReference;
  34 import java.util.Arrays;
  35 
  36 import sun.java2d.marlin.ArrayCacheConst.BucketStats;
  37 import sun.java2d.marlin.ArrayCacheConst.CacheStats;
  38 
  39 /*
  40  * Note that the [BYTE/INT/FLOAT]ArrayCache files are nearly identical except
  41  * for a few type and name differences. Typically, the [BYTE]ArrayCache.java file
  42  * is edited manually and then [INT]ArrayCache.java and [FLOAT]ArrayCache.java
  43  * files are generated with the following command lines:
  44  */
  45 // % sed -e 's/(b\yte)[ ]*//g' -e 's/b\yte/int/g' -e 's/B\yte/Int/g' < B\yteArrayCache.java > IntArrayCache.java
  46 // % sed -e 's/(b\yte)[ ]*/(float) /g' -e 's/b\yte/float/g' -e 's/B\yte/Float/g' < B\yteArrayCache.java > FloatArrayCache.java

  47 
  48 final class FloatArrayCache implements MarlinConst {
  49 
  50     final boolean clean;
  51     private final int bucketCapacity;
  52     private WeakReference<Bucket[]> refBuckets = null;
  53     final CacheStats stats;
  54 
  55     FloatArrayCache(final boolean clean, final int bucketCapacity) {
  56         this.clean = clean;
  57         this.bucketCapacity = bucketCapacity;
  58         this.stats = (DO_STATS) ?
  59             new CacheStats(getLogPrefix(clean) + "FloatArrayCache") : null;
  60     }
  61 
  62     Bucket getCacheBucket(final int length) {
  63         final int bucket = ArrayCacheConst.getBucket(length);
  64         return getBuckets()[bucket];
  65     }
  66 


 142                 logInfo(getLogPrefix(clean) + "FloatArrayCache: "
 143                         + "widenArray[" + res.length
 144                         + "]: usedSize=\t" + usedSize + "\tlength=\t" + length
 145                         + "\tneeded length=\t" + needSize);
 146             }
 147             return res;
 148         }
 149 
 150         float[] putArray(final float[] array)
 151         {
 152             // dirty array helper:
 153             return putArray(array, 0, array.length);
 154         }
 155 
 156         float[] putArray(final float[] array, final int fromIndex,
 157                         final int toIndex)
 158         {
 159             if (array.length <= MAX_ARRAY_SIZE) {
 160                 if ((clean || DO_CLEAN_DIRTY) && (toIndex != 0)) {
 161                     // clean-up array of dirty part[fromIndex; toIndex[
 162                     fill(array, fromIndex, toIndex, (float) 0);
 163                 }
 164                 // ensure to never store initial arrays in cache:
 165                 if (array != initial) {
 166                     cache.getCacheBucket(array.length).putArray(array);
 167                 }
 168             }
 169             return initial;
 170         }
 171     }
 172 
 173     static final class Bucket {
 174 
 175         private int tail = 0;
 176         private final int arraySize;
 177         private final boolean clean;
 178         private final float[][] arrays;
 179         private final BucketStats stats;
 180 
 181         Bucket(final boolean clean, final int arraySize,
 182                final int capacity, final BucketStats stats)


 214                 stats.returnOp++;
 215             }
 216             // fill cache:
 217             if (arrays.length > tail) {
 218                 arrays[tail++] = array;
 219 
 220                 if (DO_STATS) {
 221                     stats.updateMaxSize(tail);
 222                 }
 223             } else if (DO_CHECKS) {
 224                 logInfo(getLogPrefix(clean) + "FloatArrayCache: "
 225                         + "array capacity exceeded !");
 226             }
 227         }
 228     }
 229 
 230     static float[] createArray(final int length, final boolean clean) {
 231         if (clean) {
 232             return new float[length];
 233         }
 234        // use JDK9 Unsafe.allocateUninitializedArray(class, length):
 235        return (float[]) OffHeapArray.UNSAFE.allocateUninitializedArray(float.class, length);
 236     }
 237 
 238     static void fill(final float[] array, final int fromIndex,
 239                      final int toIndex, final float value)
 240     {
 241         // clear array data:
 242         Arrays.fill(array, fromIndex, toIndex, value);
 243         if (DO_CHECKS) {
 244             check(array, fromIndex, toIndex, value);
 245         }
 246     }
 247 
 248     static void check(final float[] array, final int fromIndex,
 249                       final int toIndex, final float value)
 250     {
 251         if (DO_CHECKS) {
 252             // check zero on full array:
 253             for (int i = 0; i < array.length; i++) {
 254                 if (array[i] != value) {
 255                     logException("Invalid value at: " + i + " = " + array[i]
   1 /*
   2  * Copyright (c) 2015, 2017, 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 package sun.java2d.marlin;
  27 
  28 import static sun.java2d.marlin.ArrayCacheConst.ARRAY_SIZES;
  29 import static sun.java2d.marlin.ArrayCacheConst.BUCKETS;
  30 import static sun.java2d.marlin.ArrayCacheConst.MAX_ARRAY_SIZE;
  31 import static sun.java2d.marlin.MarlinUtils.logInfo;
  32 import static sun.java2d.marlin.MarlinUtils.logException;
  33 
  34 import java.lang.ref.WeakReference;
  35 import java.util.Arrays;
  36 
  37 import sun.java2d.marlin.ArrayCacheConst.BucketStats;
  38 import sun.java2d.marlin.ArrayCacheConst.CacheStats;
  39 
  40 /*
  41  * Note that the [BYTE/INT/FLOAT/DOUBLE]ArrayCache files are nearly identical except
  42  * for a few type and name differences. Typically, the [BYTE]ArrayCache.java file
  43  * is edited manually and then [INT/FLOAT/DOUBLE]ArrayCache.java
  44  * files are generated with the following command lines:
  45  */
  46 // % sed -e 's/(b\yte)[ ]*//g' -e 's/b\yte/int/g' -e 's/B\yte/Int/g' < B\yteArrayCache.java > IntArrayCache.java
  47 // % sed -e 's/(b\yte)[ ]*0/0.0f/g' -e 's/(b\yte)[ ]*/(float) /g' -e 's/b\yte/float/g' -e 's/B\yte/Float/g' < B\yteArrayCache.java > FloatArrayCache.java
  48 // % sed -e 's/(b\yte)[ ]*0/0.0d/g' -e 's/(b\yte)[ ]*/(double) /g' -e 's/b\yte/double/g' -e 's/B\yte/Double/g' < B\yteArrayCache.java > DoubleArrayCache.java
  49 
  50 final class FloatArrayCache implements MarlinConst {
  51 
  52     final boolean clean;
  53     private final int bucketCapacity;
  54     private WeakReference<Bucket[]> refBuckets = null;
  55     final CacheStats stats;
  56 
  57     FloatArrayCache(final boolean clean, final int bucketCapacity) {
  58         this.clean = clean;
  59         this.bucketCapacity = bucketCapacity;
  60         this.stats = (DO_STATS) ?
  61             new CacheStats(getLogPrefix(clean) + "FloatArrayCache") : null;
  62     }
  63 
  64     Bucket getCacheBucket(final int length) {
  65         final int bucket = ArrayCacheConst.getBucket(length);
  66         return getBuckets()[bucket];
  67     }
  68 


 144                 logInfo(getLogPrefix(clean) + "FloatArrayCache: "
 145                         + "widenArray[" + res.length
 146                         + "]: usedSize=\t" + usedSize + "\tlength=\t" + length
 147                         + "\tneeded length=\t" + needSize);
 148             }
 149             return res;
 150         }
 151 
 152         float[] putArray(final float[] array)
 153         {
 154             // dirty array helper:
 155             return putArray(array, 0, array.length);
 156         }
 157 
 158         float[] putArray(final float[] array, final int fromIndex,
 159                         final int toIndex)
 160         {
 161             if (array.length <= MAX_ARRAY_SIZE) {
 162                 if ((clean || DO_CLEAN_DIRTY) && (toIndex != 0)) {
 163                     // clean-up array of dirty part[fromIndex; toIndex[
 164                     fill(array, fromIndex, toIndex, 0.0f);
 165                 }
 166                 // ensure to never store initial arrays in cache:
 167                 if (array != initial) {
 168                     cache.getCacheBucket(array.length).putArray(array);
 169                 }
 170             }
 171             return initial;
 172         }
 173     }
 174 
 175     static final class Bucket {
 176 
 177         private int tail = 0;
 178         private final int arraySize;
 179         private final boolean clean;
 180         private final float[][] arrays;
 181         private final BucketStats stats;
 182 
 183         Bucket(final boolean clean, final int arraySize,
 184                final int capacity, final BucketStats stats)


 216                 stats.returnOp++;
 217             }
 218             // fill cache:
 219             if (arrays.length > tail) {
 220                 arrays[tail++] = array;
 221 
 222                 if (DO_STATS) {
 223                     stats.updateMaxSize(tail);
 224                 }
 225             } else if (DO_CHECKS) {
 226                 logInfo(getLogPrefix(clean) + "FloatArrayCache: "
 227                         + "array capacity exceeded !");
 228             }
 229         }
 230     }
 231 
 232     static float[] createArray(final int length, final boolean clean) {
 233         if (clean) {
 234             return new float[length];
 235         }
 236         // use JDK9 Unsafe.allocateUninitializedArray(class, length):
 237         return (float[]) OffHeapArray.UNSAFE.allocateUninitializedArray(float.class, length);
 238     }
 239 
 240     static void fill(final float[] array, final int fromIndex,
 241                      final int toIndex, final float value)
 242     {
 243         // clear array data:
 244         Arrays.fill(array, fromIndex, toIndex, value);
 245         if (DO_CHECKS) {
 246             check(array, fromIndex, toIndex, value);
 247         }
 248     }
 249 
 250     static void check(final float[] array, final int fromIndex,
 251                       final int toIndex, final float value)
 252     {
 253         if (DO_CHECKS) {
 254             // check zero on full array:
 255             for (int i = 0; i < array.length; i++) {
 256                 if (array[i] != value) {
 257                     logException("Invalid value at: " + i + " = " + array[i]
< prev index next >