moving font cache inside PGraphicsOpenGL

This commit is contained in:
benfry
2012-09-02 20:07:26 +00:00
parent 720d1ef25c
commit 662fec53e6
16 changed files with 157 additions and 143 deletions
@@ -3848,7 +3848,7 @@ public class PApplet extends Activity implements PConstants, Runnable {
AssetManager assets = getBaseContext().getAssets();
baseFont = Typeface.createFromAsset(assets, name);
} else {
baseFont = PFont.findTypeface(name);
baseFont = (Typeface) PFont.findNative(name);
}
return new PFont(baseFont, round(size), smooth, charset);
}
@@ -8976,8 +8976,8 @@ public class PApplet extends Activity implements PConstants, Runnable {
}
public Bitmap getBitmap() {
return g.getBitmap();
public Object getNative() {
return g.getNative();
}
+31 -31
View File
@@ -118,10 +118,10 @@ public class PFont implements PConstants {
/**
* True if this font should return 'null' for getFont(), so that the native
* font will be used to create a subset, but the native version of the font
* will not be used.
* will not be used.
*/
protected boolean subsetting;
protected boolean subsetting;
/**
* True if we've already tried to find the native version of this font.
*/
@@ -133,18 +133,18 @@ public class PFont implements PConstants {
* bug that they can't be bothered to fix.
*/
static protected Typeface[] typefaces;
// objects to handle creation of font characters only as they're needed
Bitmap lazyBitmap;
Canvas lazyCanvas;
Paint lazyPaint;
// FontMetrics lazyMetrics;
int[] lazySamples;
/** for subclasses that need to store metadata about the font */
protected HashMap<PGraphics, Object> cacheMap;
protected HashMap<PGraphics, Object> cacheMap;
public PFont() { } // for subclasses
@@ -212,7 +212,7 @@ public class PFont implements PConstants {
if (charset == null) {
lazy = true;
} else {
// charset needs to be sorted to make index lookup run more quickly
// http://dev.processing.org/bugs/show_bug.cgi?id=494
@@ -317,7 +317,7 @@ public class PFont implements PConstants {
}
}
/**
* Write this PFont to an OutputStream.
* <p>
@@ -408,20 +408,20 @@ public class PFont implements PConstants {
return name;
}
/**
* Return size of this font.
*/
public int getSize() {
return size;
}
public void setSubsetting() {
subsetting = true;
}
public String getPostScriptName() {
return psname;
}
@@ -430,15 +430,15 @@ public class PFont implements PConstants {
/**
* Set the native complement of this font.
*/
public void setTypeface(Typeface typeface) {
this.typeface = typeface;
public void setNative(Object typeface) {
this.typeface = (Typeface) typeface;
}
/**
* Return the native Typeface object associated with this PFont (if any).
*/
public Typeface getTypeface() {
public Typeface getNative() {
if (subsetting) {
return null;
}
@@ -450,7 +450,7 @@ public class PFont implements PConstants {
* Attempt to find the native version of this font.
* (Public so that it can be used by OpenGL or other renderers.)
*/
static public Typeface findTypeface(String name) {
static public Object findNative(String name) {
loadTypefaces();
return typefaceMap.get(name);
}
@@ -564,14 +564,14 @@ public class PFont implements PConstants {
//////////////////////////////////////////////////////////////
// METADATA REQUIRED BY THE RENDERERS
/**
* Store data of some kind for a renderer that requires extra metadata of
* some kind. Usually this is a renderer-specific representation of the
* font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
* @param renderer The PGraphics renderer associated to the font
* @param storage The metadata required by the renderer
* @param storage The metadata required by the renderer
*/
public void setCache(PGraphics renderer, Object storage) {
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
@@ -601,22 +601,22 @@ public class PFont implements PConstants {
if (cacheMap != null) {
cacheMap.remove(renderer);
}
}
//////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////
public int getGlyphCount() {
return glyphCount;
}
public Glyph getGlyph(int i) {
return glyphs[i];
}
return glyphs[i];
}
//////////////////////////////////////////////////////////////
static final char[] EXTRA_CHARS = {
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
@@ -725,7 +725,7 @@ public class PFont implements PConstants {
}
}
/////////////////////////////////////////////////////////////
/**
@@ -736,7 +736,7 @@ public class PFont implements PConstants {
public int value;
public int height;
public int width;
public int index;
public int index;
public int setWidth;
public int topExtent;
public int leftExtent;
@@ -1120,7 +1120,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
public void textFont(PFont which) {
super.textFont(which);
fillPaint.setTypeface(which.getTypeface());
fillPaint.setTypeface(which.getNative());
}
@@ -1151,7 +1151,7 @@ public class PGraphicsAndroid2D extends PGraphics {
defaultFontOrDeath("textSize", size);
}
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font != null) {
fillPaint.setTextSize(size);
}
@@ -1184,7 +1184,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
protected float textWidthImpl(char buffer[], int start, int stop) {
// Font font = textFont.getFont();
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font == null) {
return super.textWidthImpl(buffer, start, stop);
}
@@ -1217,7 +1217,7 @@ public class PGraphicsAndroid2D extends PGraphics {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
Typeface font = textFont.getTypeface();
Typeface font = textFont.getNative();
if (font == null) {
showWarning("Inefficient font rendering: use createFont() with a TTF/OTF instead of loadFont().");
//new Exception().printStackTrace(System.out);
+8 -16
View File
@@ -168,28 +168,20 @@ public class PImage implements PConstants, Cloneable {
* Construct a new PImage from an Android bitmap. The pixels[] array is not
* initialized, nor is data copied to it, until loadPixels() is called.
*/
public PImage(Bitmap image) {
this.bitmap = image;
this.width = image.getWidth();
this.height = image.getHeight();
public PImage(Object nativeObject) {
Bitmap bitmap = (Bitmap) nativeObject;
this.bitmap = bitmap;
this.width = bitmap.getWidth();
this.height = bitmap.getHeight();
this.pixels = null;
this.format = image.hasAlpha() ? ARGB : RGB;
this.format = bitmap.hasAlpha() ? ARGB : RGB;
}
/**
* Returns a BufferedImage from this PImage.
* Returns the native Bitmap object for this PImage.
*/
// public java.awt.Image getImage() {
// loadPixels();
// int type = (format == RGB) ?
// BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
// BufferedImage image = new BufferedImage(width, height, type);
// WritableRaster wr = image.getRaster();
// wr.setDataElements(0, 0, width, height, pixels);
// return image;
// }
public Bitmap getBitmap() {
public Object getNative() {
return bitmap;
}
@@ -46,7 +46,7 @@ import java.util.HashMap;
* over several textures.
* @author Andres Colubri
*/
class PFontTexture implements PConstants {
class FontTexture implements PConstants {
protected PApplet parent;
protected PGraphicsOpenGL pg;
protected PGL pgl;
@@ -65,7 +65,7 @@ class PFontTexture implements PConstants {
protected TextureInfo[] glyphTexinfos;
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
public PFontTexture(PApplet parent, PFont font, int maxw, int maxh,
public FontTexture(PApplet parent, PFont font, int maxw, int maxh,
boolean is3D) {
this.parent = parent;
this.font = font;
@@ -43,6 +43,10 @@ public class PGraphicsOpenGL extends PGraphics {
/** The renderer currently in use. */
protected static PGraphicsOpenGL pgCurrent = null;
/** Font cache for texture objects. */
protected WeakHashMap<PFont, FontTexture> fontMap =
new WeakHashMap<PFont, FontTexture>();
/** Additional image parameters not covered by the cache. */
protected WeakHashMap<PImage, Object> paramMap =
new WeakHashMap<PImage, Object>();
@@ -353,7 +357,7 @@ public class PGraphicsOpenGL extends PGraphics {
// Text:
/** Font texture of currently selected font. */
PFontTexture textTex;
FontTexture textTex;
// .......................................................
@@ -610,6 +614,24 @@ public class PGraphicsOpenGL extends PGraphics {
}
//////////////////////////////////////////////////////////////
protected void setFontTexture(PFont font, FontTexture fontTexture) {
fontMap.put(font, fontTexture);
}
protected FontTexture getFontTexture(PFont font) {
return fontMap.get(font);
}
protected void removeFontTexture(PFont font) {
fontMap.remove(font);
}
//////////////////////////////////////////////////////////////
/**
@@ -1654,8 +1676,8 @@ public class PGraphicsOpenGL extends PGraphics {
if (texture != null) {
// The screen texture should be deleted because it
// corresponds to the old window size.
removeCache(pgPrimary);
removeParams(pgPrimary);
pgPrimary.removeCache(this);
pgPrimary.removeParams(this);
texture = null;
loadTexture();
}
@@ -3507,17 +3529,17 @@ public class PGraphicsOpenGL extends PGraphics {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
textTex = (PFontTexture)textFont.getCache(pgPrimary);
textTex = pgPrimary.getFontTexture(textFont);
if (textTex == null) {
textTex = new PFontTexture(parent, textFont, maxTextureSize,
textTex = new FontTexture(parent, textFont, maxTextureSize,
maxTextureSize, is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
} else {
if (textTex.contextIsOutdated()) {
textTex = new PFontTexture(parent, textFont,
textTex = new FontTexture(parent, textFont,
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
}
}
textTex.begin();
@@ -3569,7 +3591,7 @@ public class PGraphicsOpenGL extends PGraphics {
PFont.Glyph glyph = textFont.getGlyph(ch);
if (glyph != null) {
PFontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
FontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
if (tinfo == null) {
// Adding new glyph to the font texture.
@@ -3593,7 +3615,7 @@ public class PGraphicsOpenGL extends PGraphics {
}
protected void textCharModelImpl(PFontTexture.TextureInfo info,
protected void textCharModelImpl(FontTexture.TextureInfo info,
float x0, float y0,
float x1, float y1) {
if (textTex.currentTex != info.texIndex) {
+2
View File
@@ -1,4 +1,6 @@
0209 android
A GL android sketch stops running after rotation
A http://code.google.com/p/processing/issues/detail?id=1146
_ inside AndroidPreprocessor
+5 -5
View File
@@ -4911,8 +4911,8 @@ public class PApplet extends Applet
}
// For jpeg, gif, and png, load them using createImage(),
// because the javax.imageio code was found to be much slower, see
// <A HREF="http://dev.processing.org/bugs/show_bug.cgi?id=392">Bug 392</A>.
// because the javax.imageio code was found to be much slower.
// http://dev.processing.org/bugs/show_bug.cgi?id=392
try {
if (extension.equals("jpg") || extension.equals("jpeg") ||
extension.equals("gif") || extension.equals("png") ||
@@ -4949,12 +4949,12 @@ public class PApplet extends Applet
if (loadImageFormats != null) {
for (int i = 0; i < loadImageFormats.length; i++) {
if (extension.equals(loadImageFormats[i])) {
PImage image;
image = loadImageIO(filename);
return loadImageIO(filename);
// PImage image = loadImageIO(filename);
// if (params != null) {
// image.setParams(g, params);
// }
return image;
// return image;
}
}
}
+7 -47
View File
@@ -390,7 +390,7 @@ public class PFont implements PConstants {
}
// See if there's a native version of this font that can be used,
// in case that's of interest later.
findFont();
findNative();
}
@@ -495,15 +495,15 @@ public class PFont implements PConstants {
* findFont() function, or externally by a deriveFont() call if the font
* is resized by PGraphicsJava2D.
*/
public void setFont(Font font) {
this.font = font;
public void setNative(Object font) {
this.font = (Font) font;
}
/**
* Return the native java.awt.Font associated with this PFont (if any).
*/
public Font getFont() {
public Object getNative() {
if (subsetting) {
return null; // don't return the font for use
}
@@ -533,7 +533,7 @@ public class PFont implements PConstants {
* Attempt to find the native version of this font.
* (Public so that it can be used by OpenGL or other renderers.)
*/
public Font findFont() {
public Object findNative() {
if (font == null) {
if (!fontSearched) {
// this font may or may not be installed
@@ -664,57 +664,17 @@ public class PFont implements PConstants {
//////////////////////////////////////////////////////////////
// METADATA REQUIRED BY THE RENDERERS
/**
* Store data of some kind for a renderer that requires extra metadata of
* some kind. Usually this is a renderer-specific representation of the
* font data, for instance a custom OpenGL texture for PGraphicsOpenGL2.
* @param renderer The PGraphics renderer associated to the font
* @param storage The metadata required by the renderer
*/
public void setCache(PGraphics renderer, Object storage) {
if (cacheMap == null) cacheMap = new HashMap<PGraphics, Object>();
cacheMap.put(renderer, storage);
}
/**
* Get cache storage data for the specified renderer. Because each renderer
* will cache data in different formats, it's necessary to store cache data
* keyed by the renderer object. Otherwise, attempting to draw the same
* image to both a PGraphicsJava2D and a PGraphicsOpenGL2 will cause errors.
* @param renderer The PGraphics renderer associated to the font
* @return metadata stored for the specified renderer
*/
public Object getCache(PGraphics renderer) {
if (cacheMap == null) return null;
return cacheMap.get(renderer);
}
/**
* Remove information associated with this renderer from the cache, if any.
* @param parent The PGraphics renderer whose cache data should be removed
*/
public void removeCache(PGraphics renderer) {
if (cacheMap != null) {
cacheMap.remove(renderer);
}
}
//////////////////////////////////////////////////////////////
public int getGlyphCount() {
return glyphCount;
}
public Glyph getGlyph(int i) {
return glyphs[i];
}
//////////////////////////////////////////////////////////////
+1 -1
View File
@@ -3785,7 +3785,7 @@ public class PGraphics extends PImage implements PConstants {
textFont = which;
if (hints[ENABLE_NATIVE_FONTS]) {
//if (which.font == null) {
which.findFont();
which.findNative();
//}
}
/*
@@ -1191,7 +1191,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
defaultFontOrDeath("textAscent");
}
Font font = textFont.getFont();
Font font = (Font) textFont.getNative();
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
if (font != null) {
FontMetrics metrics = parent.getFontMetrics(font);
@@ -1206,7 +1206,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
if (textFont == null) {
defaultFontOrDeath("textAscent");
}
Font font = textFont.getFont();
Font font = (Font) textFont.getNative();
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
if (font != null) {
FontMetrics metrics = parent.getFontMetrics(font);
@@ -1252,12 +1252,12 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
// g2.setFont(textFontNative);
// textFontNativeMetrics = g2.getFontMetrics(textFontNative);
// }
Font font = textFont.getFont();
Font font = (Font) textFont.getNative();
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
if (font != null) {
Font dfont = font.deriveFont(size);
g2.setFont(dfont);
textFont.setFont(dfont);
textFont.setNative(dfont);
}
// take care of setting the textSize and textLeading vars
@@ -1275,7 +1275,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
@Override
protected float textWidthImpl(char buffer[], int start, int stop) {
Font font = textFont.getFont();
Font font = (Font) textFont.getNative();
//if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
if (font != null) {
// maybe should use one of the newer/fancier functions for this?
@@ -1328,7 +1328,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
Font font = textFont.getFont();
Font font = (Font) textFont.getNative();
// if (font != null && (textFont.isStream() || hints[ENABLE_NATIVE_FONTS])) {
if (font != null) {
/*
@@ -2125,7 +2125,9 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ {
public void copy(PImage src,
int sx, int sy, int sw, int sh,
int dx, int dy, int dw, int dh) {
g2.drawImage(src.getImage(), dx, dy, dx + dw, dy + dh, sx, sy, sx + sw, sy + sh, null);
g2.drawImage((Image) src.getNative(),
dx, dy, dx + dw, dy + dh,
sx, sy, sx + sw, sy + sh, null);
}
+3 -2
View File
@@ -292,9 +292,10 @@ public class PImage implements PConstants, Cloneable {
/**
* Returns a BufferedImage from this PImage.
* Returns a native BufferedImage from this PImage.
*/
public java.awt.Image getImage() {
// public java.awt.Image getImage() {
public Object getNative() {
loadPixels();
int type = (format == RGB) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
@@ -46,7 +46,7 @@ import java.util.HashMap;
* over several textures.
* @author Andres Colubri
*/
class PFontTexture implements PConstants {
class FontTexture implements PConstants {
protected PApplet parent;
protected PGraphicsOpenGL pg;
protected PGL pgl;
@@ -65,7 +65,7 @@ class PFontTexture implements PConstants {
protected TextureInfo[] glyphTexinfos;
protected HashMap<PFont.Glyph, TextureInfo> texinfoMap;
public PFontTexture(PApplet parent, PFont font, int maxw, int maxh,
public FontTexture(PApplet parent, PFont font, int maxw, int maxh,
boolean is3D) {
this.parent = parent;
this.font = font;
@@ -43,6 +43,10 @@ public class PGraphicsOpenGL extends PGraphics {
/** The renderer currently in use. */
protected static PGraphicsOpenGL pgCurrent = null;
/** Font cache for texture objects. */
protected WeakHashMap<PFont, FontTexture> fontMap =
new WeakHashMap<PFont, FontTexture>();
/** Additional image parameters not covered by the cache. */
protected WeakHashMap<PImage, Object> paramMap =
new WeakHashMap<PImage, Object>();
@@ -353,7 +357,7 @@ public class PGraphicsOpenGL extends PGraphics {
// Text:
/** Font texture of currently selected font. */
PFontTexture textTex;
FontTexture textTex;
// .......................................................
@@ -610,6 +614,24 @@ public class PGraphicsOpenGL extends PGraphics {
}
//////////////////////////////////////////////////////////////
protected void setFontTexture(PFont font, FontTexture fontTexture) {
fontMap.put(font, fontTexture);
}
protected FontTexture getFontTexture(PFont font) {
return fontMap.get(font);
}
protected void removeFontTexture(PFont font) {
fontMap.remove(font);
}
//////////////////////////////////////////////////////////////
/**
@@ -3508,17 +3530,17 @@ public class PGraphicsOpenGL extends PGraphics {
@Override
protected void textLineImpl(char buffer[], int start, int stop,
float x, float y) {
textTex = (PFontTexture)textFont.getCache(pgPrimary);
textTex = pgPrimary.getFontTexture(textFont);
if (textTex == null) {
textTex = new PFontTexture(parent, textFont, maxTextureSize,
textTex = new FontTexture(parent, textFont, maxTextureSize,
maxTextureSize, is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
} else {
if (textTex.contextIsOutdated()) {
textTex = new PFontTexture(parent, textFont,
textTex = new FontTexture(parent, textFont,
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize),
PApplet.min(PGL.MAX_FONT_TEX_SIZE, maxTextureSize), is3D());
textFont.setCache(pgPrimary, textTex);
pgPrimary.setFontTexture(textFont, textTex);
}
}
textTex.begin();
@@ -3570,7 +3592,7 @@ public class PGraphicsOpenGL extends PGraphics {
PFont.Glyph glyph = textFont.getGlyph(ch);
if (glyph != null) {
PFontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
FontTexture.TextureInfo tinfo = textTex.getTexInfo(glyph);
if (tinfo == null) {
// Adding new glyph to the font texture.
@@ -3594,7 +3616,7 @@ public class PGraphicsOpenGL extends PGraphics {
}
protected void textCharModelImpl(PFontTexture.TextureInfo info,
protected void textCharModelImpl(FontTexture.TextureInfo info,
float x0, float y0,
float x1, float y1) {
if (textTex.currentTex != info.texIndex) {
+17 -4
View File
@@ -5,6 +5,18 @@ X maybe getResource() fails because spaces are not encoded?
X PVector limit() is inefficient
X http://code.google.com/p/processing/issues/detail?id=1122
X work on making API more generic and consistent
X PFont.getFont() changed to PFont.getNative() (returns an Object)
X PFont.getTypeface() changed to PFont.getNative()
earlier
X add breakShape to the public API -> begin/endContour
x or handle differently in general.. nested beginPath() calls?
_ beginning slash in getChild() threw an NPE
_ do we need a trim() method for XML? or enableWhiteSpace()? or?
_ memory leak when many createGraphics(..., JAVA2D) calls are used
_ http://code.google.com/p/processing/issues/detail?id=507
_ need to add clear() method so clear out ARGB surfaces
@@ -15,6 +27,9 @@ o or background(r, g, b, a) would be the thing
_ clear() with a color doesn't make (verbal) sense
_ provide a way to clear the PGraphics with plain alpha
_ use getNative() to return the native object for
_ PImage (BufferedImage and Bitmap), PFont (Typeface or awt.Font)
api to be fixed/removed
_ remove PImage.delete() and friends from PImage, Movie, etc.
_ delete()/dispose() being used in the movie
@@ -36,6 +51,7 @@ _ http://code.google.com/p/processing/issues/detail?id=1068
docs
_ textureWrap() CLAMP and REPEAT now added
_ begin/endContour()
_ implement callbacks for images and other loadXxxx() functions
_ remove requestImage() and friends
@@ -43,8 +59,6 @@ _ callback for requestImage()
_ http://code.google.com/p/processing/issues/detail?id=641
_ remove/update requestImage example
_ do we need a trim() method for XML? or enableWhiteSpace()? or?
PImage icon;
void setup() {
@@ -266,8 +280,7 @@ _ http://code.google.com/p/processing/issues/detail?id=135
_ make determination on shape(x,y,z,w,h,d) or no
_ new PGraphics(... OutputStream)
_ http://code.google.com/p/processing/issues/detail?id=246
_ add breakShape to the public API
_ or handle differently in general.. nested beginPath() calls?
_ already added for PDF, just need to work out the API
looping/events
_ key and mouse events delivered out of order
@@ -604,7 +604,7 @@ public class PGraphicsPDF extends PGraphicsJava2D {
* @return true if it's ok
*/
protected void checkFont() {
Font awtFont = textFont.getFont();
Font awtFont = textFont.getNative();
if (awtFont == null) { // always need a native font or reference to it
throw new RuntimeException("Use createFont() instead of loadFont() " +
"when drawing text using the PDF library.");