use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class TestAssetCache method runTest.
private static void runTest(boolean cloneAssets, boolean smartCache, boolean keepRefs, int limit) {
counter = 0;
List<Object> refs = new ArrayList<Object>(limit);
AssetCache cache;
AssetProcessor proc = null;
if (cloneAssets) {
proc = new CloneableAssetProcessor();
}
if (smartCache) {
if (cloneAssets) {
cache = new WeakRefCloneAssetCache();
} else {
cache = new WeakRefAssetCache();
}
} else {
cache = new SimpleAssetCache();
}
System.gc();
System.gc();
System.gc();
System.gc();
long memory = Runtime.getRuntime().freeMemory();
while (counter < limit) {
// Create a key
DummyKey key = new DummyKey();
// Create some data
DummyData data = new DummyData();
// Post process the data before placing it in the cache
if (proc != null) {
data = (DummyData) proc.postProcess(key, data);
}
if (data.key != null) {
// means the asset will never be collected => bug
throw new AssertionError();
}
cache.addToCache(key, data);
// Get the asset from the cache
AssetKey<DummyData> keyToGet = key.clone();
// Clone the asset
if (proc != null) {
// Data is now the clone!
data = (DummyData) proc.createClone(data);
if (smartCache) {
// Registering a clone is only needed
// if smart cache is used.
cache.registerAssetClone(keyToGet, data);
// otherwise => bug
if (data.key != key) {
throw new AssertionError();
}
}
}
// an out of memory error.
if (keepRefs) {
// Prevent the saved references from taking too much memory ..
if (cloneAssets) {
data.data = null;
}
refs.add(data);
}
if ((counter % 1000) == 0) {
long newMem = Runtime.getRuntime().freeMemory();
System.out.println("Allocated objects: " + counter);
System.out.println("Allocated memory: " + ((memory - newMem) / (1024 * 1024)) + " MB");
memory = newMem;
}
}
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class CursorLoader method load.
/**
* Loads and return a cursor file of one of the following format: .ani, .cur and .ico.
* @param info The {@link AssetInfo} describing the cursor file.
* @return A JmeCursor representation of the LWJGL's Cursor.
* @throws IOException if the file is not found.
*/
public JmeCursor load(AssetInfo info) throws IOException {
isIco = false;
isAni = false;
isCur = false;
isIco = ((AssetKey) info.getKey()).getExtension().equals("ico");
if (!isIco) {
isCur = ((AssetKey) info.getKey()).getExtension().equals("cur");
if (!isCur) {
isAni = ((AssetKey) info.getKey()).getExtension().equals("ani");
}
}
if (!isAni && !isIco && !isCur) {
throw new IllegalArgumentException("Cursors supported are .ico, .cur or .ani");
}
InputStream in = null;
try {
in = info.openStream();
return loadCursor(in);
} finally {
if (in != null) {
in.close();
}
}
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class DesktopAssetManager method loadAsset.
@Override
public <T> T loadAsset(AssetKey<T> key) {
if (key == null)
throw new IllegalArgumentException("key cannot be null");
for (AssetEventListener listener : eventListeners) {
listener.assetRequested(key);
}
AssetCache cache = handler.getCache(key.getCacheType());
AssetProcessor proc = handler.getProcessor(key.getProcessorType());
Object obj = cache != null ? cache.getFromCache(key) : null;
if (obj == null) {
// Asset not in cache, load it from file system.
AssetInfo info = handler.tryLocate(key);
if (info == null) {
if (handler.getParentKey() != null) {
// that something went wrong.
for (AssetEventListener listener : eventListeners) {
listener.assetDependencyNotFound(handler.getParentKey(), key);
}
}
throw new AssetNotFoundException(key.toString());
}
obj = loadLocatedAsset(key, info, proc, cache);
}
T clone = (T) obj;
if (obj instanceof CloneableSmartAsset) {
clone = registerAndCloneSmartAsset(key, clone, proc, cache);
}
return clone;
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class WeakRefCloneAssetCache method addToCache.
public <T> void addToCache(AssetKey<T> originalKey, T obj) {
// Make room for new asset
removeCollectedAssets();
CloneableSmartAsset asset = (CloneableSmartAsset) obj;
// No circular references, since the original asset is
// strongly referenced, we don't want the key strongly referenced.
asset.setKey(null);
// Start tracking the collection of originalKey
// (this adds the KeyRef to the ReferenceQueue)
KeyRef ref = new KeyRef(originalKey, refQueue);
// Place the asset in the cache, but use a clone of
// the original key.
smartCache.put(ref.clonedKey, new AssetRef(asset, originalKey));
// Push the original key used to load the asset
// so that it can be set on the clone later
ArrayList<AssetKey> loadStack = assetLoadStack.get();
loadStack.add(originalKey);
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class Context method createProgramFromSourceFilesWithInclude.
/**
* Creates a program object from the provided source code and files.
* The source code is made up from the specified include string first,
* then all files specified by the resource array (array of asset paths)
* are loaded by the provided asset manager and appended to the source code.
* <p>
* The typical use case is:
* <ul>
* <li>The include string contains some compiler constants like the grid size </li>
* <li>Some common OpenCL files used as libraries (Convention: file names end with {@code .clh}</li>
* <li>One main OpenCL file containing the actual kernels (Convention: file name ends with {@code .cl})</li>
* </ul>
*
* After the files were combined, additional include statements are resolved
* by {@link #createProgramFromSourceCodeWithDependencies(java.lang.String, com.jme3.asset.AssetManager) }.
*
* @param assetManager the asset manager used to load the files
* @param include an additional include string
* @param resources an array of asset paths pointing to OpenCL source files
* @return the new program objects
* @throws AssetNotFoundException if a file could not be loaded
*/
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
StringBuilder str = new StringBuilder();
str.append(include);
for (String res : resources) {
AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
if (info == null) {
throw new AssetNotFoundException("Unable to load source file \"" + res + "\"");
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
str.append(line).append('\n');
}
} catch (IOException ex) {
LOG.log(Level.WARNING, "unable to load source file '" + res + "'", ex);
}
}
return createProgramFromSourceCodeWithDependencies(str.toString(), assetManager);
}
Aggregations