Revision: 4294 http://vexi.svn.sourceforge.net/vexi/?rev=4294&view=rev Author: clrg Date: 2011-11-10 05:11:34 +0000 (Thu, 10 Nov 2011) Log Message: ----------- Optimize picture loading - only schedule once when loading images - previously all boxes with loaded images would get scheduled, which is potentially a lot - should end the 'step by step' building of a Vexi UI inolving images
Modified Paths: -------------- trunk/org.vexi-core.main/src/main/java/org/vexi/graphics/Picture.java Modified: trunk/org.vexi-core.main/src/main/java/org/vexi/graphics/Picture.java =================================================================== --- trunk/org.vexi-core.main/src/main/java/org/vexi/graphics/Picture.java 2011-10-28 13:40:34 UTC (rev 4293) +++ trunk/org.vexi-core.main/src/main/java/org/vexi/graphics/Picture.java 2011-11-10 05:11:34 UTC (rev 4294) @@ -42,7 +42,7 @@ // FIXME: this is a hack to disguise isLoaded somehow being false when callbacks are run public JSExn loadFailed = null; ///< not null iff the image loading failed - private Vec loadedCallbacks; + private Vec loadedCallbacks; ///< list of callbacks interested in this Picture /** invoked when an image is fully loaded; subclasses can use this to initialize platform-specific constructs */ protected synchronized void loaded() { @@ -50,13 +50,44 @@ return; } - int numcallbacks = loadedCallbacks.size(); - for (int i=0; i<numcallbacks; i++) { - Main.SCHEDULER.add((Callable)loadedCallbacks.elementAt(i)); + synchronized (worker) { + if (loadedPictures != null) { + loadedPictures.addElement(this); + return; + } + + loadedPictures = new Vec(); + loadedPictures.addElement(this); + Main.SCHEDULER.add(worker); } - loadedCallbacks.removeAllElements(); - loadedCallbacks = null; } + + /* + * The 'worker' Callable invokes the other Callables; needed + * to be done this way because if using a lot of images, there + * is a lot of scheduling required if scheduling each Callable + * individually, and this can lead to 'step by step' building + * of the UI - not really ideal. + */ + private static Vec loadedPictures; + private static Callable worker = new Callable() { + public Object run(Object o) throws Exception { + synchronized (worker) { + for (int j=0; loadedPictures.size()>j; j++) { + Picture pic = (Picture)loadedPictures.elementAt(j); + int numcallbacks = pic.loadedCallbacks.size(); + for (int i=0; i<numcallbacks; i++) { + ((Callable)pic.loadedCallbacks.elementAt(i)).run(null); + } + pic.loadedCallbacks.removeAllElements(); + pic.loadedCallbacks = null; + } + loadedPictures.removeAllElements(); + loadedPictures = null; + return null; + } + } + }; /** turns a stream into a Picture.Source and passes it to the callback */ public static Picture load(JS stream, Callable callback) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ RSA(R) Conference 2012 Save $700 by Nov 18 Register now http://p.sf.net/sfu/rsa-sfdev2dev1 _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn