jdk/src/java.desktop/share/classes/sun/java2d/marlin/FloatArrayCache.java
author lbourges
Thu, 10 Dec 2015 15:58:01 -0800
changeset 34816 5ff696b1bbac
parent 34417 57a3863abbb4
child 36458 25786a73a5fc
permissions -rw-r--r--
8144654: Improve Marlin logging Reviewed-by: prr, flar
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 FloatArrayCache 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<float[]> floatArrays;
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("FloatArrayCache[" + 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: " + floatArrays.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
    FloatArrayCache(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.floatArrays = new ArrayDeque<float[]>(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
    float[] 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 float[] array = floatArrays.pollLast();
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    63
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    64
        if (array != null) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    65
            return array;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    66
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    67
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    68
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    69
            createOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    70
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    71
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    72
        return new float[arraySize];
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    73
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    74
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    75
    void putDirtyArray(final float[] array, final int length) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    76
        if (length != arraySize) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    77
            if (doChecks) {
34816
5ff696b1bbac 8144654: Improve Marlin logging
lbourges
parents: 34417
diff changeset
    78
                MarlinUtils.logInfo("ArrayCache: bad length = " + length);
34417
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    79
            }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    80
            return;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    81
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    82
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    83
            returnOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    84
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    85
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    86
        // NO clean-up of array data = DIRTY ARRAY
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    87
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    88
        if (doCleanDirty) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    89
            // Force zero-fill dirty arrays:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    90
            Arrays.fill(array, 0, array.length, 0f);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    91
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    92
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    93
        // fill cache:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    94
        floatArrays.addLast(array);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    95
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    96
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    97
    void putArray(final float[] array, final int length,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    98
                  final int fromIndex, final int toIndex)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
    99
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   100
        if (length != arraySize) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   101
            if (doChecks) {
34816
5ff696b1bbac 8144654: Improve Marlin logging
lbourges
parents: 34417
diff changeset
   102
                MarlinUtils.logInfo("ArrayCache: bad length = " + length);
34417
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   103
            }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   104
            return;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   105
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   106
        if (doStats) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   107
            returnOp++;
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   108
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   109
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   110
        // clean-up array of dirty part[fromIndex; toIndex[
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   111
        fill(array, fromIndex, toIndex, 0f);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   112
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   113
        // fill cache:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   114
        floatArrays.addLast(array);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   115
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   116
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   117
    static void fill(final float[] array, final int fromIndex,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   118
                     final int toIndex, final float value)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   119
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   120
        // clear array data:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   121
        /*
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   122
         * Arrays.fill is faster than System.arraycopy(empty array)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   123
         * or Unsafe.setMemory(byte 0)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   124
         */
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   125
        if (toIndex != 0) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   126
            Arrays.fill(array, fromIndex, toIndex, value);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   127
        }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   128
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   129
        if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   130
            check(array, 0, array.length, value);
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
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   134
    static void check(final float[] array, final int fromIndex,
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   135
                      final int toIndex, final float value)
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   136
    {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   137
        if (doChecks) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   138
            // check zero on full array:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   139
            for (int i = fromIndex; i < toIndex; i++) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   140
                if (array[i] != value) {
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   141
                    logException("Invalid array value at " + i + "\n"
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   142
                            + Arrays.toString(array), new Throwable());
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   143
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   144
                    // ensure array is correctly filled:
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   145
                    Arrays.fill(array, value);
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   146
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   147
                    return;
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
    }
57a3863abbb4 8076529: Marlin antialiasing renderer integration
flar
parents:
diff changeset
   152
}