diff options
author | Julien Gouesse <[email protected]> | 2019-05-28 11:23:12 +0200 |
---|---|---|
committer | Julien Gouesse <[email protected]> | 2019-05-28 11:23:12 +0200 |
commit | f019b6dd39a173576488a98cd05a7adbb85b9832 (patch) | |
tree | e27c4da4a34cb20f7b69b488e112b448d068a3e6 /ardor3d-core | |
parent | e62932af3b5b280aa7a27bd8b15725478bcb3709 (diff) |
Replaces Guava's Multimap by Java Map in TextureManager and adds some null checks into AbstractBufferData (issue #13)
Diffstat (limited to 'ardor3d-core')
-rw-r--r-- | ardor3d-core/src/main/java/com/ardor3d/scenegraph/AbstractBufferData.java | 6 | ||||
-rw-r--r-- | ardor3d-core/src/main/java/com/ardor3d/util/TextureManager.java | 97 |
2 files changed, 80 insertions, 23 deletions
diff --git a/ardor3d-core/src/main/java/com/ardor3d/scenegraph/AbstractBufferData.java b/ardor3d-core/src/main/java/com/ardor3d/scenegraph/AbstractBufferData.java index f18f899..1ddc258 100644 --- a/ardor3d-core/src/main/java/com/ardor3d/scenegraph/AbstractBufferData.java +++ b/ardor3d-core/src/main/java/com/ardor3d/scenegraph/AbstractBufferData.java @@ -345,7 +345,7 @@ public abstract class AbstractBufferData<T extends Buffer> { for (final Map.Entry<Object, List<Integer>> idMapEntry : idMap.entrySet()) { final Object glref = idMapEntry.getKey(); // If we have a deleter and the context is current, immediately delete - if (deleter != null && glref.equals(currentGLRef)) { + if (deleter != null && glref.equals(currentGLRef) && idMapEntry.getValue() != null) { deleter.deleteVBOs(idMapEntry.getValue()); } // Otherwise, add a delete request to that context's render task queue. @@ -354,7 +354,9 @@ public abstract class AbstractBufferData<T extends Buffer> { .render(new RendererCallable<Void>() { @Override public Void call() throws Exception { - getRenderer().deleteVBOs(idMapEntry.getValue()); + if (idMapEntry.getValue() != null) { + getRenderer().deleteVBOs(idMapEntry.getValue()); + } return null; } }); diff --git a/ardor3d-core/src/main/java/com/ardor3d/util/TextureManager.java b/ardor3d-core/src/main/java/com/ardor3d/util/TextureManager.java index d90d2c0..086b267 100644 --- a/ardor3d-core/src/main/java/com/ardor3d/util/TextureManager.java +++ b/ardor3d-core/src/main/java/com/ardor3d/util/TextureManager.java @@ -11,7 +11,10 @@ package com.ardor3d.util; import java.lang.ref.ReferenceQueue; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; @@ -35,8 +38,6 @@ import com.ardor3d.renderer.RendererCallable; import com.ardor3d.renderer.state.TextureState; import com.ardor3d.util.resource.ResourceLocatorTool; import com.ardor3d.util.resource.ResourceSource; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; /** * <code>TextureManager</code> provides static methods for building or retrieving a <code>Texture</code> object from @@ -59,7 +60,8 @@ final public class TextureManager { }); } - private TextureManager() {} + private TextureManager() { + } /** * Loads a texture by attempting to locate the given name using ResourceLocatorTool. @@ -297,23 +299,42 @@ final public class TextureManager { */ public static void cleanAllTextures(final Renderer deleter, final Map<Object, Future<Void>> futureStore) { // gather up expired textures... these don't exist in our cache - Multimap<Object, Integer> idMap = gatherGCdIds(); + Map<Object, List<Integer>> idMap = gatherGCdIds(); // Walk through the cached items and gather those too. for (final TextureKey key : _tCache.keySet()) { // possibly lazy init if (idMap == null) { - idMap = ArrayListMultimap.create(); + idMap = new HashMap<>(); } if (Constants.useMultipleContexts) { final Set<Object> contextObjects = key.getContextObjects(); for (final Object o : contextObjects) { // Add id to map - idMap.put(o, key.getTextureIdForContext(o)); + idMap.compute(o, (final Object currentKey, final List<Integer> oldValue) -> { + final List<Integer> newValue; + if (oldValue == null) { + newValue = new ArrayList<>(); + } else { + newValue = oldValue; + } + newValue.add(key.getTextureIdForContext(o)); + return newValue; + }); } } else { - idMap.put(ContextManager.getCurrentContext().getGlContextRep(), key.getTextureIdForContext(null)); + idMap.compute(ContextManager.getCurrentContext().getGlContextRep(), + (final Object currentKey, final List<Integer> oldValue) -> { + final List<Integer> newValue; + if (oldValue == null) { + newValue = new ArrayList<>(); + } else { + newValue = oldValue; + } + newValue.add(key.getTextureIdForContext(null)); + return newValue; + }); } key.removeFromIdCache(); } @@ -345,19 +366,28 @@ final public class TextureManager { public static void cleanAllTextures(final Renderer deleter, final RenderContext context, final Map<Object, Future<Void>> futureStore) { // gather up expired textures... these don't exist in our cache - Multimap<Object, Integer> idMap = gatherGCdIds(); + Map<Object, List<Integer>> idMap = gatherGCdIds(); final Object glRep = context.getGlContextRep(); // Walk through the cached items and gather those too. for (final TextureKey key : _tCache.keySet()) { // possibly lazy init if (idMap == null) { - idMap = ArrayListMultimap.create(); + idMap = new HashMap<>(); } final Integer id = key.getTextureIdForContext(glRep); if (id != 0) { - idMap.put(context.getGlContextRep(), id); + idMap.compute(context.getGlContextRep(), (final Object currentKey, final List<Integer> oldValue) -> { + final List<Integer> newValue; + if (oldValue == null) { + newValue = new ArrayList<>(); + } else { + newValue = oldValue; + } + newValue.add(id); + return newValue; + }); key.removeFromIdCache(glRep); } } @@ -399,7 +429,7 @@ final public class TextureManager { */ public static void cleanExpiredTextures(final Renderer deleter, final Map<Object, Future<Void>> futureStore) { // gather up expired textures... - final Multimap<Object, Integer> idMap = gatherGCdIds(); + final Map<Object, List<Integer>> idMap = gatherGCdIds(); // send to be deleted on next render. if (idMap != null) { @@ -408,15 +438,15 @@ final public class TextureManager { } @SuppressWarnings("unchecked") - private static Multimap<Object, Integer> gatherGCdIds() { - Multimap<Object, Integer> idMap = null; + private static Map<Object, List<Integer>> gatherGCdIds() { + Map<Object, List<Integer>> idMap = null; // Pull all expired textures from ref queue and add to an id multimap. ContextIdReference<TextureKey> ref; Integer id; while ((ref = (ContextIdReference<TextureKey>) _textureRefQueue.poll()) != null) { // lazy init if (idMap == null) { - idMap = ArrayListMultimap.create(); + idMap = new HashMap<>(); } if (Constants.useMultipleContexts) { final Set<Object> contextObjects = ref.getContextObjects(); @@ -424,13 +454,34 @@ final public class TextureManager { id = ref.getValue(o); if (id != null && id.intValue() != 0) { // Add id to map - idMap.put(o, id); + final Integer currentId = id; + idMap.compute(o, (final Object currentKey, final List<Integer> oldValue) -> { + final List<Integer> newValue; + if (oldValue == null) { + newValue = new ArrayList<>(); + } else { + newValue = oldValue; + } + newValue.add(currentId); + return newValue; + }); } } } else { id = ref.getValue(null); if (id != null && id.intValue() != 0) { - idMap.put(ContextManager.getCurrentContext().getGlContextRep(), id); + final Integer currentId = id; + idMap.compute(ContextManager.getCurrentContext().getGlContextRep(), + (final Object currentKey, final List<Integer> oldValue) -> { + final List<Integer> newValue; + if (oldValue == null) { + newValue = new ArrayList<>(); + } else { + newValue = oldValue; + } + newValue.add(currentId); + return newValue; + }); } } ref.clear(); @@ -438,7 +489,7 @@ final public class TextureManager { return idMap; } - private static void handleTextureDelete(final Renderer deleter, final Multimap<Object, Integer> idMap, + private static void handleTextureDelete(final Renderer deleter, final Map<Object, List<Integer>> idMap, final Map<Object, Future<Void>> futureStore) { if (deleter == null) { return; @@ -449,10 +500,12 @@ final public class TextureManager { currentGLRef = ContextManager.getCurrentContext().getGlContextRep(); } // For each affected context... - for (final Object glref : idMap.keySet()) { + for (final Map.Entry<Object, List<Integer>> idMapEntry : idMap.entrySet()) { + final Object glref = idMapEntry.getKey(); // If we have a deleter and the context is current, immediately delete - if (currentGLRef != null && (!Constants.useMultipleContexts || glref.equals(currentGLRef))) { - deleter.deleteTextureIds(idMap.get(glref)); + if (currentGLRef != null && (!Constants.useMultipleContexts || glref.equals(currentGLRef)) + && idMapEntry.getValue() != null) { + deleter.deleteTextureIds(idMapEntry.getValue()); } // Otherwise, add a delete request to that context's render task queue. else { @@ -460,7 +513,9 @@ final public class TextureManager { .render(new RendererCallable<Void>() { @Override public Void call() throws Exception { - getRenderer().deleteTextureIds(idMap.get(glref)); + if (idMapEntry.getValue() != null) { + getRenderer().deleteTextureIds(idMapEntry.getValue()); + } return null; } }); |