jdk/src/java.desktop/share/classes/sun/java2d/marlin/IntArrayCache.java
author flar
Mon, 23 Nov 2015 14:35:55 -0800
changeset 34417 57a3863abbb4
child 34816 5ff696b1bbac
permissions -rw-r--r--
8076529: Marlin antialiasing renderer integration Reviewed-by: flar, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34417
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     1
/*
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     4
 *
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    10
 *
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    15
 * accompanied this code).
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    16
 *
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    20
 *
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    23
 * questions.
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    24
 */
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    25
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    26
package sun.java2d.marlin;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    27
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    28
import java.util.ArrayDeque;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    29
import java.util.Arrays;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    30
import static sun.java2d.marlin.MarlinUtils.logException;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    31
import static sun.java2d.marlin.MarlinUtils.logInfo;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    32
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    33
final class IntArrayCache implements MarlinConst {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    34
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    35
    private final int arraySize;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    36
    private final ArrayDeque<int[]> intArrays;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    37
    // stats
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    38
    private int getOp = 0;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    39
    private int createOp = 0;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    40
    private int returnOp = 0;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    41
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    42
    void dumpStats() {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    43
        if (getOp > 0) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    44
            logInfo("IntArrayCache[" + arraySize + "]: get: " + getOp
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    45
                    + " created: " + createOp + " - returned: " + returnOp
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    46
                    + " :: cache size: " + intArrays.size());
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    47
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    48
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    49
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    50
    IntArrayCache(final int arraySize) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    51
        this.arraySize = arraySize;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    52
        // small but enough: almost 1 cache line
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    53
        this.intArrays = new ArrayDeque<int[]>(6);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    54
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    55
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    56
    int[] getArray() {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    57
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    58
            getOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    59
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    60
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    61
        // use cache:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    62
        final int[] array = intArrays.pollLast();
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    63
        if (array != null) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    64
            return array;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    65
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    66
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    67
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    68
            createOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    69
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    70
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    71
        return new int[arraySize];
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    72
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    73
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    74
    void putDirtyArray(final int[] array, final int length) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    75
        if (length != arraySize) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    76
            if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    77
                System.out.println("ArrayCache: bad length = " + length);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    78
            }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    79
            return;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    80
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    81
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    82
            returnOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    83
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    84
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    85
        // NO clean-up of array data = DIRTY ARRAY
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    86
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    87
        if (doCleanDirty) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    88
            // Force zero-fill dirty arrays:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    89
            Arrays.fill(array, 0, array.length, 0);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    90
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    91
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    92
        // fill cache:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    93
        intArrays.addLast(array);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    94
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    95
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    96
    void putArray(final int[] array, final int length,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    97
                  final int fromIndex, final int toIndex)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    98
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    99
        if (length != arraySize) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   100
            if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   101
                System.out.println("ArrayCache: bad length = " + length);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   102
            }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   103
            return;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   104
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   105
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   106
            returnOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   107
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   108
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   109
        // clean-up array of dirty part[fromIndex; toIndex[
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   110
        fill(array, fromIndex, toIndex, 0);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   111
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   112
        // fill cache:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   113
        intArrays.addLast(array);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   114
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   115
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   116
    static void fill(final int[] array, final int fromIndex,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   117
                     final int toIndex, final int value)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   118
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   119
        // clear array data:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   120
        /*
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   121
         * Arrays.fill is faster than System.arraycopy(empty array)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   122
         * or Unsafe.setMemory(byte 0)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   123
         */
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   124
        if (toIndex != 0) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   125
            Arrays.fill(array, fromIndex, toIndex, value);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   126
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   127
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   128
        if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   129
            check(array, 0, array.length, value);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   130
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   131
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   132
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   133
    static void check(final int[] array, final int fromIndex,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   134
                      final int toIndex, final int value)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   135
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   136
        if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   137
            // check zero on full array:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   138
            for (int i = fromIndex; i < toIndex; i++) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   139
                if (array[i] != value) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   140
                    logException("Invalid array value at " + i + "\n"
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   141
                            + Arrays.toString(array), new Throwable());
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   142
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   143
                    // ensure array is correctly filled:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   144
                    Arrays.fill(array, value);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   145
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   146
                    return;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   147
                }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   148
            }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   149
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   150
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   151
}