Revision: 2294 http://vexi.svn.sourceforge.net/vexi/?rev=2294&view=rev Author: mkpg2 Date: 2007-09-20 17:52:37 -0700 (Thu, 20 Sep 2007)
Log Message: ----------- Tidied up Picture loading and improved warning messages. Modified Paths: -------------- trunk/core/org.vexi.core/src/org/vexi/core/Box.jpp trunk/core/org.vexi.core/src/org/vexi/graphics/Picture.java trunk/core/org.vexi.core/src/org/vexi/plat/Platform.java Modified: trunk/core/org.vexi.core/src/org/vexi/core/Box.jpp =================================================================== --- trunk/core/org.vexi.core/src/org/vexi/core/Box.jpp 2007-09-20 22:32:21 UTC (rev 2293) +++ trunk/core/org.vexi.core/src/org/vexi/core/Box.jpp 2007-09-21 00:52:37 UTC (rev 2294) @@ -298,7 +298,10 @@ /** invoked when a resource needed to render ourselves finishes loading */ public Object run(Object o) throws JSExn { if (texture == null) { - Log.warn(Box.class, "Called run() on a Box with a null texture"); + // Pictures are loaded asynchronously, then the Box is scheduled. + // It is possible that the texture has been removed inbetween, + // not an error though it may indicate bad Vexi code. + Log.debug(Box.class, "Called run() on a Box with a null texture"); } else if (texture.isLoaded) { if (test(TILE_IMAGE)) { putAndTriggerTraps(SC_minwidth, JSU.N(texture.width)); Modified: trunk/core/org.vexi.core/src/org/vexi/graphics/Picture.java =================================================================== --- trunk/core/org.vexi.core/src/org/vexi/graphics/Picture.java 2007-09-20 22:32:21 UTC (rev 2293) +++ trunk/core/org.vexi.core/src/org/vexi/graphics/Picture.java 2007-09-21 00:52:37 UTC (rev 2294) @@ -4,7 +4,6 @@ package org.vexi.graphics; -import java.io.IOException; import java.io.InputStream; import java.io.PushbackInputStream; @@ -17,7 +16,6 @@ import org.ibex.util.Log; import org.ibex.util.Vec; import org.vexi.core.Blessing; -import org.vexi.core.Vexi; import org.vexi.plat.Platform; /** @@ -56,53 +54,57 @@ loadedCallbacks = null; } - /** turns a stream into a Picture.Source and passes it to the callback */ - public static Picture load(final JS stream, final Callable callback) { + /** turns a stream into aPicture.Source and passes it to the callback */ + public static Picture load(JS stream, Callable callback) { Picture ret = (Picture)cache.get(stream); if (ret == null) cache.put(stream, ret = Platform.createPicture(stream)); - final Picture p = ret; - - synchronized(p) { - if(p.isLoaded || callback == null) return p; + // can return ret here outside of sync block for caller to assign as callback + //is executed in the same interpreter as the caller, synchronously. + ret.load(callback); + return ret; + } + + /** turns a stream into a Picture.Source and passes it to the callback */ + private void load( final Callable callback) { + synchronized(this) { + if(isLoaded || callback == null) return; // Previous demand for this image means it is alreadly loading // add to callbacks and return - if(p.loadedCallbacks != null) { - p.loadedCallbacks.addElement(callback); - return p; + if(loadedCallbacks != null) { + loadedCallbacks.addElement(callback); + return; } final Blessing b = Blessing.getBlessing(stream); - p.loadedCallbacks = new Vec(); - p.loadedCallbacks.addElement(callback); + loadedCallbacks = new Vec(); + loadedCallbacks.addElement(callback); new java.lang.Thread() { public void run() { - InputStream in = null; - try { - in = b == null ? Fountain.getInputStream(stream) : b.getImage(); - } catch (IOException e) { p.loadFailed = true; return; //Log.error(Picture.class, e); - } catch (JSExn e) { p.loadFailed = true; return; //Log.error(Picture.class, e); - } - if (in == null) { p.loadFailed = true; return; } //Log.warn(Picture.class, "couldn't load image for stream " + stream.unclone()); return; } - try { + try { + InputStream in = b == null ? + Fountain.getInputStream(stream) : b.getImage(); + if(in == null) throw new JSExn("not a valid image stream"); + PushbackInputStream pbis = new PushbackInputStream(in); int firstByte = pbis.read(); if (firstByte == -1) throw new JSExn("empty stream reading image"); pbis.unread(firstByte); - if ((firstByte & 0xff) == 'G') GIF.load(pbis, p); - else if ((firstByte & 0xff) == 137) PNG.load(pbis, p); - else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, p); + if ((firstByte & 0xff) == 'G') GIF.load(pbis, Picture.this); + else if ((firstByte & 0xff) == 137) PNG.load(pbis, Picture.this); + else if ((firstByte & 0xff) == 0xff) Platform.decodeJPEG(pbis, Picture.this); else throw new JSExn("couldn't figure out image type from first byte"); - p.loaded(); + loaded(); } catch (Exception e) { - p.loadFailed = true; - return; - //Log.info(this, "exception while loading image"); - //Log.info(this, e); + loadFailed = true; + Log.warn(Picture.class,"Couldn't load picture from stream " + stream.unclone()); + if(e instanceof JSExn) Log.uWarn(Picture.class,e.getMessage()); + else { + Log.uWarn(Picture.class,"System error loading image"); + Log.warn(Picture.class, e); + } } } }.start(); } - - return ret; } } Modified: trunk/core/org.vexi.core/src/org/vexi/plat/Platform.java =================================================================== --- trunk/core/org.vexi.core/src/org/vexi/plat/Platform.java 2007-09-20 22:32:21 UTC (rev 2293) +++ trunk/core/org.vexi.core/src/org/vexi/plat/Platform.java 2007-09-21 00:52:37 UTC (rev 2294) @@ -300,6 +300,8 @@ public Scheduler(){this(false);} public Scheduler(boolean quitOnExn){super(quitOnExn);} + //DEBUG//protected void check(Callable t){} + private static volatile boolean rendering = false; private static volatile boolean again = false; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn