Search in sources :

Example 11 with AssetKey

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;
        }
    }
}
Also used : SimpleAssetCache(com.jme3.asset.cache.SimpleAssetCache) WeakRefCloneAssetCache(com.jme3.asset.cache.WeakRefCloneAssetCache) AssetCache(com.jme3.asset.cache.AssetCache) WeakRefAssetCache(com.jme3.asset.cache.WeakRefAssetCache) SimpleAssetCache(com.jme3.asset.cache.SimpleAssetCache) ArrayList(java.util.ArrayList) WeakRefCloneAssetCache(com.jme3.asset.cache.WeakRefCloneAssetCache) CloneableAssetProcessor(com.jme3.asset.CloneableAssetProcessor) AssetProcessor(com.jme3.asset.AssetProcessor) CloneableAssetProcessor(com.jme3.asset.CloneableAssetProcessor) WeakRefAssetCache(com.jme3.asset.cache.WeakRefAssetCache)

Example 12 with AssetKey

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();
        }
    }
}
Also used : AssetKey(com.jme3.asset.AssetKey) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 13 with AssetKey

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;
}
Also used : AssetCache(com.jme3.asset.cache.AssetCache)

Example 14 with AssetKey

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);
}
Also used : CloneableSmartAsset(com.jme3.asset.CloneableSmartAsset) AssetKey(com.jme3.asset.AssetKey)

Example 15 with AssetKey

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) IOException(java.io.IOException) AssetInfo(com.jme3.asset.AssetInfo)

Aggregations

AssetKey (com.jme3.asset.AssetKey)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)5 AssetInfo (com.jme3.asset.AssetInfo)4 AssetLoadException (com.jme3.asset.AssetLoadException)4 ModelKey (com.jme3.asset.ModelKey)4 Material (com.jme3.material.Material)4 Statement (com.jme3.util.blockparser.Statement)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 AssetCache (com.jme3.asset.cache.AssetCache)3 Texture (com.jme3.texture.Texture)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 TextureKey (com.jme3.asset.TextureKey)2 MaterialList (com.jme3.material.MaterialList)2 J3MLoader (com.jme3.material.plugins.J3MLoader)2 Node (com.jme3.scene.Node)2 Spatial (com.jme3.scene.Spatial)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2