diff options
author | Sven Gothel <[email protected]> | 2011-04-08 14:55:40 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-08 14:55:40 +0200 |
commit | 349a22d3a87f6e3dd87c3b4151447588969de8b6 (patch) | |
tree | 880b10d5d3605338cf949358d5e35d5224508471 /src/jogl | |
parent | ea9517a1bb3144f4ab6f3b8f1a41ced9046fd9b4 (diff) |
Fix: Cached GlyphString handling
- Destroy removed ones
- Handle overwrite/update on same key
- Clarified names (size/limit)
- Handle cache limit unlimited (limit := -1) and no cache (limit := 0)
- Default cache limit := 256
- Comments
Diffstat (limited to 'src/jogl')
-rw-r--r-- | src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java | 82 |
1 files changed, 62 insertions, 20 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java index 45f78fc23..4b7299a33 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java @@ -15,7 +15,6 @@ import com.jogamp.graph.font.Font; import com.jogamp.graph.geom.Vertex; public abstract class TextRenderer extends Renderer { - /** * Create a Hardware accelerated Text Renderer. * @param factory optional Point.Factory for Vertex construction. Default is Vertex.Factory. @@ -50,6 +49,10 @@ public abstract class TextRenderer extends Renderer { * @return the resulting GlyphString inclusive the generated region */ protected GlyphString createString(GL2ES2 gl, Font font, int size, String str, float sharpness) { + if(DEBUG) { + System.err.println("createString: "+getCacheSize()+"/"+getCacheLimit()+" - "+Font.NAME_UNIQUNAME + " - " + str + " - " + size); + } + AffineTransform affineTransform = new AffineTransform(pointFactory); Path2D[] paths = new Path2D[str.length()]; @@ -77,32 +80,71 @@ public abstract class TextRenderer extends Renderer { flushCache(); } - public final void setCacheMaxSize(int newSize ) { stringCacheMaxSize = newSize; validateCache(0); } - public final int getCacheMaxSize() { return stringCacheMaxSize; } + /** + * Sets the cache limit for reusing GlyphString's and their Region. + * Default is {@link #DEFAULT_CACHE_LIMIT}, -1 unlimited, 0 turns cache off, >0 limited + * + * @param newLimit new cache size + * + * @see #DEFAULT_CACHE_LIMIT + */ + public final void setCacheLimit(int newLimit ) { stringCacheLimit = newLimit; validateCache(0); } + public final int getCacheLimit() { return stringCacheLimit; } + + /** + * @return the current utilized cache size, <= {@link #getCacheLimit()} + */ public final int getCacheSize() { return stringCacheArray.size(); } - protected void validateCache(int space) { - while ( getCacheSize() + space > getCacheMaxSize() ) { - String key = stringCacheArray.remove(0); - stringCacheMap.remove(key); + protected final void validateCache(int space) { + if ( getCacheLimit() > 0 ) { + while ( getCacheSize() + space > getCacheLimit() ) { + removeCachedGlyphString(0); + } } } - protected GlyphString getCachedGlyphString(Font font, String str, int fontSize) { - final String key = font.getName(Font.NAME_UNIQUNAME) + "." + str.hashCode() + "." + fontSize; - return stringCacheMap.get(key); + protected final GlyphString getCachedGlyphString(Font font, String str, int fontSize) { + return stringCacheMap.get(getKey(font, str, fontSize)); } - protected void addCachedGlyphString(Font font, String str, int fontSize, GlyphString glyphString) { - final String key = font.getName(Font.NAME_UNIQUNAME) + "." + str.hashCode() + "." + fontSize; - validateCache(1); - stringCacheMap.put(key, glyphString); - stringCacheArray.add(stringCacheArray.size(), key); + protected final void addCachedGlyphString(Font font, String str, int fontSize, GlyphString glyphString) { + if ( 0 != getCacheLimit() ) { + final String key = getKey(font, str, fontSize); + GlyphString oldGlyphString = stringCacheMap.put(key, glyphString); + if ( null == oldGlyphString ) { + // new entry .. + validateCache(1); + stringCacheArray.add(stringCacheArray.size(), key); + } /// else overwrite is nop .. + } + } + + protected final void removeCachedGlyphString(Font font, String str, int fontSize) { + final String key = getKey(font, str, fontSize); + GlyphString glyphString = stringCacheMap.remove(key); + if(null != glyphString) { + glyphString.destroy(); + } + stringCacheArray.remove(key); } - // Cache is adding at the end of the array - public static final int DEFAULT_CACHE_SIZE = 32; - private HashMap<String, GlyphString> stringCacheMap = new HashMap<String, GlyphString>(DEFAULT_CACHE_SIZE); - private ArrayList<String> stringCacheArray = new ArrayList<String>(DEFAULT_CACHE_SIZE); - private int stringCacheMaxSize = DEFAULT_CACHE_SIZE; // -1 unlimited, 0 off, >0 limited + protected final void removeCachedGlyphString(int idx) { + final String key = stringCacheArray.remove(idx); + final GlyphString glyphString = stringCacheMap.remove(key); + if(null != glyphString) { + glyphString.destroy(); + } + } + + protected final String getKey(Font font, String str, int fontSize) { + return font.getName(Font.NAME_UNIQUNAME) + "." + str.hashCode() + "." + fontSize; + } + + /** Default cache limit, see {@link #setCacheLimit(int)} */ + public static final int DEFAULT_CACHE_LIMIT = 256; + + private HashMap<String, GlyphString> stringCacheMap = new HashMap<String, GlyphString>(DEFAULT_CACHE_LIMIT); + private ArrayList<String> stringCacheArray = new ArrayList<String>(DEFAULT_CACHE_LIMIT); + private int stringCacheLimit = DEFAULT_CACHE_LIMIT; }
\ No newline at end of file |