jdk/src/share/classes/sun/font/StrikeCache.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8508 a7e8d7418c54
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8508
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4252
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4252
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4252
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4252
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4252
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    28
import java.awt.GraphicsConfiguration;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    29
import java.awt.GraphicsEnvironment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.ref.WeakReference;
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
    34
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.java2d.Disposer;
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    37
import sun.java2d.pipe.BufferedContext;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    38
import sun.java2d.pipe.RenderQueue;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    39
import sun.java2d.pipe.hw.AccelGraphicsConfig;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
A FontStrike is the keeper of scaled glyph image data which is expensive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
to compute so needs to be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
So long as that data may be being used it cannot be invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
Yet we also need to limit the amount of native memory and number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
strike objects in use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
For scaleability and ease of use, a key goal is multi-threaded read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
access to a strike, so that it may be shared by multiple client objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
potentially executing on different threads, with no special reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
counting or "check-out/check-in" requirements which would pass on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
burden of keeping track of strike references to the SG2D and other clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
A cache of strikes is maintained via Reference objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
This helps in two ways :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
1. The VM will free references when memory is low or they have not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
used in a long time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
2. Reference queues provide a way to get notification of this so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
free native memory resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public final class StrikeCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static ReferenceQueue refQueue = Disposer.getQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
    70
    static ArrayList<GlyphDisposedListener> disposeListeners = new ArrayList<GlyphDisposedListener>(1);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
    71
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* Reference objects may have their referents cleared when GC chooses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * During application client start-up there is typically at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * GC which causes the hotspot VM to clear soft (not just weak) references
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Thus not only is there a GC pause, but the work done do rasterise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * glyphs that are fairly certain to be needed again almost immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * is thrown away. So for performance reasons a simple optimisation is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * keep up to 8 strong references to strikes to reduce the chance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * GC'ing strikes that have been used recently. Note that this may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * suffice in Solaris UTF-8 locales where a single composite strike may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * composed of 15 individual strikes, plus the composite strike.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * And this assumes the new architecture doesn't maintain strikes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * natively accessed bitmaps. It may be worth "tuning" the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * strikes kept around for the platform or locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Since no attempt is made to ensure uniqueness or ensure synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * access there is no guarantee that this cache will ensure that unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * strikes are cached. Every time a strike is looked up it is added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * to the current index in this cache. All this cache has to do to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * worthwhile is prevent excessive cache flushing of strikes that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * referenced frequently. The logic that adds references here could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * tweaked to keep only strikes  that represent untransformed, screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * sizes as that's the typical performance case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static int MINSTRIKES = 8; // can be overridden by property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static int recentStrikeIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static FontStrike[] recentStrikes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static boolean cacheRefTypeWeak;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Native sizes and offsets for glyph cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * There are 10 values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static int nativeAddressSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static int glyphInfoSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static int xAdvanceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static int yAdvanceOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static int boundsOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static int widthOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static int heightOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static int rowBytesOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static int topLeftXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static int topLeftYOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static int pixelDataOffset;
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   115
    static int cacheCellOffset;
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   116
    static int managedOffset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static long invisibleGlyphPtr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /* Native method used to return information used for unsafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * access to native data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * return values as follows:-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * arr[0] = size of an address/pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * arr[1] = size of a GlyphInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * arr[2] = offset of advanceX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * arr[3] = offset of advanceY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * arr[4] = offset of width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * arr[5] = offset of height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * arr[6] = offset of rowBytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * arr[7] = offset of topLeftX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * arr[8] = offset of topLeftY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * arr[9] = offset of pixel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * arr[10] = address of a GlyphImageRef representing the invisible glyph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static native void getGlyphCacheDescription(long[] infoArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   138
        long[] nativeInfo = new long[13];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        getGlyphCacheDescription(nativeInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        //Can also get address size from Unsafe class :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        //nativeAddressSize = unsafe.addressSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        nativeAddressSize = (int)nativeInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        glyphInfoSize     = (int)nativeInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        xAdvanceOffset    = (int)nativeInfo[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        yAdvanceOffset    = (int)nativeInfo[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        widthOffset       = (int)nativeInfo[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        heightOffset      = (int)nativeInfo[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        rowBytesOffset    = (int)nativeInfo[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        topLeftXOffset    = (int)nativeInfo[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        topLeftYOffset    = (int)nativeInfo[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        pixelDataOffset   = (int)nativeInfo[9];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        invisibleGlyphPtr = nativeInfo[10];
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   153
        cacheCellOffset = (int) nativeInfo[11];
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   154
        managedOffset = (int) nativeInfo[12];
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (nativeAddressSize < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new InternalError("Unexpected address size for font data: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                    nativeAddressSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                    new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
               /* Allow a client to override the reference type used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                * cache strikes. The default is "soft" which hints to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                * the strikes around. This property allows the client to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                * override this to "weak" which hint to the GC to free
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                * memory more agressively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
               String refType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                   System.getProperty("sun.java2d.font.reftype", "soft");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
               cacheRefTypeWeak = refType.equals("weak");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                String minStrikesStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    System.getProperty("sun.java2d.font.minstrikes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (minStrikesStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        MINSTRIKES = Integer.parseInt(minStrikesStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        if (MINSTRIKES <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            MINSTRIKES = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                recentStrikes = new FontStrike[MINSTRIKES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void refStrike(FontStrike strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int index = recentStrikeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        recentStrikes[index] = strike;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (index == MINSTRIKES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        recentStrikeIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   205
    private static final void doDispose(FontStrikeDisposer disposer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (disposer.intGlyphImages != null) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   207
            freeCachedIntMemory(disposer.intGlyphImages,
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   208
                    disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else if (disposer.longGlyphImages != null) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   210
            freeCachedLongMemory(disposer.longGlyphImages,
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   211
                    disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else if (disposer.segIntGlyphImages != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            /* NB Now making multiple JNI calls in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
             * But assuming that there's a reasonable amount of locality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
             * rather than sparse references then it should be OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            for (int i=0; i<disposer.segIntGlyphImages.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (disposer.segIntGlyphImages[i] != null) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   219
                    freeCachedIntMemory(disposer.segIntGlyphImages[i],
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   220
                            disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    /* native will only free the scaler context once */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    disposer.pScalerContext = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    disposer.segIntGlyphImages[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            /* This may appear inefficient but it should only be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
             * for a strike that never was asked to rasterise a glyph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (disposer.pScalerContext != 0L) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   230
                freeCachedIntMemory(new int[0], disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else if (disposer.segLongGlyphImages != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            for (int i=0; i<disposer.segLongGlyphImages.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (disposer.segLongGlyphImages[i] != null) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   235
                    freeCachedLongMemory(disposer.segLongGlyphImages[i],
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   236
                            disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    disposer.pScalerContext = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    disposer.segLongGlyphImages[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (disposer.pScalerContext != 0L) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   242
                freeCachedLongMemory(new long[0], disposer.pScalerContext);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
2371
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   244
        } else if (disposer.pScalerContext != 0L) {
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   245
            /* Rarely a strike may have been created that never cached
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   246
             * any glyphs. In this case we still want to free the scaler
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   247
             * context.
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   248
             */
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2371
diff changeset
   249
            if (longAddresses()) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   250
                freeCachedLongMemory(new long[0], disposer.pScalerContext);
2371
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   251
            } else {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   252
                freeCachedIntMemory(new int[0], disposer.pScalerContext);
2371
021228e73859 6728838: Native memory leak in StrikeCache.java
prr
parents: 888
diff changeset
   253
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2371
diff changeset
   257
    private static boolean longAddresses() {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2371
diff changeset
   258
        return nativeAddressSize == 8;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2371
diff changeset
   259
    }
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 2371
diff changeset
   260
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   261
    static void disposeStrike(final FontStrikeDisposer disposer) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   262
        // we need to execute the strike disposal on the rendering thread
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   263
        // because they may be accessed on that thread at the time of the
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   264
        // disposal (for example, when the accel. cache is invalidated)
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   265
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   266
        // Whilst this is a bit heavyweight, in most applications
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   267
        // strike disposal is a relatively infrequent operation, so it
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   268
        // doesn't matter. But in some tests that use vast numbers
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   269
        // of strikes, the switching back and forth is measurable.
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   270
        // So the "pollRemove" call is added to batch up the work.
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   271
        // If we are polling we know we've already been called back
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   272
        // and can directly dispose the record.
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   273
        // Also worrisome is the necessity of getting a GC here.
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   274
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   275
        if (Disposer.pollingQueue) {
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   276
            doDispose(disposer);
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   277
            return;
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   278
        }
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   279
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   280
        RenderQueue rq = null;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   281
        GraphicsEnvironment ge =
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   282
            GraphicsEnvironment.getLocalGraphicsEnvironment();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   283
        if (!ge.isHeadless()) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   284
            GraphicsConfiguration gc =
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   285
                ge.getDefaultScreenDevice().getDefaultConfiguration();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   286
            if (gc instanceof AccelGraphicsConfig) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   287
                AccelGraphicsConfig agc = (AccelGraphicsConfig)gc;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   288
                BufferedContext bc = agc.getContext();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   289
                if (bc != null) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   290
                    rq = bc.getRenderQueue();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   291
                }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   292
            }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   293
        }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   294
        if (rq != null) {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   295
            rq.lock();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   296
            try {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   297
                rq.flushAndInvokeNow(new Runnable() {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   298
                    public void run() {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   299
                        doDispose(disposer);
4252
0e645080f74b 6899078: potential deadlock and performance issue in freeing strike resources with D3D pipeline
prr
parents: 3928
diff changeset
   300
                        Disposer.pollRemove();
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   301
                    }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   302
                });
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   303
            } finally {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   304
                rq.unlock();
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   305
            }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   306
        } else {
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   307
            doDispose(disposer);
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   308
        }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   309
    }
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   310
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    static native void freeIntPointer(int ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    static native void freeLongPointer(long ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static native void freeIntMemory(int[] glyphPtrs, long pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static native void freeLongMemory(long[] glyphPtrs, long pContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   316
    private static void freeCachedIntMemory(int[] glyphPtrs, long pContext) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   317
        synchronized(disposeListeners) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   318
            if (disposeListeners.size() > 0) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   319
                ArrayList<Long> gids = null;
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   320
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   321
                for (int i = 0; i < glyphPtrs.length; i++) {
8508
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   322
                    if (glyphPtrs[i] != 0 && unsafe.getByte(glyphPtrs[i] + managedOffset) == 0) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   323
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   324
                        if (gids == null) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   325
                            gids = new ArrayList<Long>();
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   326
                        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   327
                        gids.add((long) glyphPtrs[i]);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   328
                    }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   329
                }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   330
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   331
                if (gids != null) {
8508
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   332
                    // Any reference by the disposers to the native glyph ptrs
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   333
                    // must be done before this returns.
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   334
                    notifyDisposeListeners(gids);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   335
                }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   336
            }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   337
        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   338
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   339
        freeIntMemory(glyphPtrs, pContext);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   340
    }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   341
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   342
    private static void  freeCachedLongMemory(long[] glyphPtrs, long pContext) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   343
        synchronized(disposeListeners) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   344
        if (disposeListeners.size() > 0)  {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   345
                ArrayList<Long> gids = null;
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   346
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   347
                for (int i=0; i < glyphPtrs.length; i++) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   348
                    if (glyphPtrs[i] != 0
8508
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   349
                            && unsafe.getByte(glyphPtrs[i] + managedOffset) == 0) {
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   350
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   351
                        if (gids == null) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   352
                            gids = new ArrayList<Long>();
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   353
                        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   354
                        gids.add((long) glyphPtrs[i]);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   355
                    }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   356
                }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   357
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   358
                if (gids != null) {
8508
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   359
                    // Any reference by the disposers to the native glyph ptrs
a7e8d7418c54 7023589: Xrender : NullPointerException in sun.font.XRGlyphCache.freeGlyphs running Java 2D demo
ceisserer
parents: 7668
diff changeset
   360
                    // must be done before this returns.
5579
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   361
                    notifyDisposeListeners(gids);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   362
                }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   363
        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   364
        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   365
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   366
        freeLongMemory(glyphPtrs, pContext);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   367
    }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   368
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   369
    public static void addGlyphDisposedListener(GlyphDisposedListener listener) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   370
        synchronized(disposeListeners) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   371
            disposeListeners.add(listener);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   372
        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   373
    }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   374
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   375
    private static void notifyDisposeListeners(ArrayList<Long> glyphs) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   376
        for (GlyphDisposedListener listener : disposeListeners) {
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   377
            listener.glyphDisposed(glyphs);
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   378
        }
1a5e995a710b 6307603: [X11] Use RENDER extension for complex operations done in software
ceisserer
parents: 4252
diff changeset
   379
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public static Reference getStrikeRef(FontStrike strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return getStrikeRef(strike, cacheRefTypeWeak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public static Reference getStrikeRef(FontStrike strike, boolean weak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        /* Some strikes may have no disposer as there's nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * for them to free, as they allocated no native resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * eg, if they did not allocate resources because of a problem,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * or they never hold native resources. So they create no disposer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * But any strike that reaches here that has a null disposer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * a potential memory leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (strike.disposer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (weak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                return new WeakReference(strike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return new SoftReference(strike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (weak) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return new WeakDisposerRef(strike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return new SoftDisposerRef(strike);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    static interface DisposableStrike {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        FontStrikeDisposer getDisposer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    static class SoftDisposerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        extends SoftReference implements DisposableStrike {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        private FontStrikeDisposer disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        public FontStrikeDisposer getDisposer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        SoftDisposerRef(FontStrike strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            super(strike, StrikeCache.refQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            disposer = strike.disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            Disposer.addReference(this, disposer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    static class WeakDisposerRef
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        extends WeakReference implements DisposableStrike {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        private FontStrikeDisposer disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public FontStrikeDisposer getDisposer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        WeakDisposerRef(FontStrike strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            super(strike, StrikeCache.refQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            disposer = strike.disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            Disposer.addReference(this, disposer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}