/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
A FontStrike is the keeper of scaled glyph image data which is expensive
to compute so needs to be cached.
So long as that data may be being used it cannot be invalidated.
Yet we also need to limit the amount of native memory and number of
strike objects in use.
For scaleability and ease of use, a key goal is multi-threaded read
access to a strike, so that it may be shared by multiple client objects,
potentially executing on different threads, with no special reference
burden of keeping track of strike references to the SG2D and other clients.
A cache of strikes is maintained via Reference objects.
This helps in two ways :
1. The VM will free references when memory is low or they have not been
used in a long time.
2. Reference queues provide a way to get notification of this so we can
free native memory resources.
*/
public final class StrikeCache {
/* Reference objects may have their referents cleared when GC chooses.
* During application client start-up there is typically at least one
* GC which causes the hotspot VM to clear soft (not just weak) references
* Thus not only is there a GC pause, but the work done do rasterise
* glyphs that are fairly certain to be needed again almost immediately
* is thrown away. So for performance reasons a simple optimisation is to
* keep up to 8 strong references to strikes to reduce the chance of
* GC'ing strikes that have been used recently. Note that this may not
* suffice in Solaris UTF-8 locales where a single composite strike may be
* composed of 15 individual strikes, plus the composite strike.
* And this assumes the new architecture doesn't maintain strikes for
* natively accessed bitmaps. It may be worth "tuning" the number of
* strikes kept around for the platform or locale.
* Since no attempt is made to ensure uniqueness or ensure synchronized
* access there is no guarantee that this cache will ensure that unique
* strikes are cached. Every time a strike is looked up it is added
* to the current index in this cache. All this cache has to do to be
* worthwhile is prevent excessive cache flushing of strikes that are
* referenced frequently. The logic that adds references here could be
* tweaked to keep only strikes that represent untransformed, screen
* sizes as that's the typical performance case.
*/
static boolean cacheRefTypeWeak;
/*
* Native sizes and offsets for glyph cache
* There are 10 values.
*/
static int nativeAddressSize;
static int glyphInfoSize;
static int xAdvanceOffset;
static int yAdvanceOffset;
static int boundsOffset;
static int widthOffset;
static int heightOffset;
static int rowBytesOffset;
static int topLeftXOffset;
static int topLeftYOffset;
static int pixelDataOffset;
static int cacheCellOffset;
static int managedOffset;
static long invisibleGlyphPtr;
/* Native method used to return information used for unsafe
* access to native data.
* return values as follows:-
* arr[1] = size of a GlyphInfo
* arr[2] = offset of advanceX
* arr[3] = offset of advanceY
* arr[4] = offset of width
* arr[5] = offset of height
* arr[6] = offset of rowBytes
* arr[7] = offset of topLeftX
* arr[8] = offset of topLeftY
* arr[9] = offset of pixel data.
* arr[10] = address of a GlyphImageRef representing the invisible glyph
*/
static {
long[] nativeInfo = new long[13];
//Can also get address size from Unsafe class :-
//nativeAddressSize = unsafe.addressSize();
if (nativeAddressSize < 4) {
throw new InternalError("Unexpected address size for font data: " +
}
/* Allow a client to override the reference type used to
* cache strikes. The default is "soft" which hints to keep
* the strikes around. This property allows the client to
* override this to "weak" which hint to the GC to free
* memory more agressively.
*/
if (minStrikesStr != null) {
try {
if (MINSTRIKES <= 0) {
MINSTRIKES = 1;
}
} catch (NumberFormatException e) {
}
}
return null;
}
});
}
int index = recentStrikeIndex;
index++;
if (index == MINSTRIKES) {
index = 0;
}
}
/* NB Now making multiple JNI calls in this case.
* But assuming that there's a reasonable amount of locality
* rather than sparse references then it should be OK.
*/
/* native will only free the scaler context once */
}
}
/* This may appear inefficient but it should only be invoked
* for a strike that never was asked to rasterise a glyph.
*/
}
}
}
}
/* Rarely a strike may have been created that never cached
* any glyphs. In this case we still want to free the scaler
* context.
*/
if (longAddresses()) {
} else {
}
}
}
private static boolean longAddresses() {
return nativeAddressSize == 8;
}
// we need to execute the strike disposal on the rendering thread
// because they may be accessed on that thread at the time of the
// disposal (for example, when the accel. cache is invalidated)
// Whilst this is a bit heavyweight, in most applications
// strike disposal is a relatively infrequent operation, so it
// doesn't matter. But in some tests that use vast numbers
// of strikes, the switching back and forth is measurable.
// So the "pollRemove" call is added to batch up the work.
// If we are polling we know we've already been called back
// and can directly dispose the record.
// Also worrisome is the necessity of getting a GC here.
if (Disposer.pollingQueue) {
return;
}
if (!ge.isHeadless()) {
if (gc instanceof AccelGraphicsConfig) {
}
}
}
try {
public void run() {
}
});
} finally {
}
} else {
}
}
synchronized(disposeListeners) {
}
}
}
// Any reference by the disposers to the native glyph ptrs
// must be done before this returns.
}
}
}
}
synchronized(disposeListeners) {
if (glyphPtrs[i] != 0
}
}
}
// Any reference by the disposers to the native glyph ptrs
// must be done before this returns.
}
}
}
}
synchronized(disposeListeners) {
}
}
}
}
}
/* Some strikes may have no disposer as there's nothing
* for them to free, as they allocated no native resource
* eg, if they did not allocate resources because of a problem,
* or they never hold native resources. So they create no disposer.
* But any strike that reaches here that has a null disposer is
* a potential memory leak.
*/
if (weak) {
return new WeakReference(strike);
} else {
return new SoftReference(strike);
}
}
if (weak) {
return new WeakDisposerRef(strike);
} else {
return new SoftDisposerRef(strike);
}
}
static interface DisposableStrike {
}
static class SoftDisposerRef
extends SoftReference implements DisposableStrike {
return disposer;
}
}
}
static class WeakDisposerRef
extends WeakReference implements DisposableStrike {
return disposer;
}
}
}
}