Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / cache / BasicTextureCache.java
blob09881aa8757b927107e0bbcd7b2cde8ad0318cdc
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.cache;
9 import com.sun.opengl.util.texture.Texture;
11 /**
12 * @author tag
13 * @version $Id: BasicTextureCache.java 2450 2007-07-27 17:50:43Z tgaskins $
15 public class BasicTextureCache implements TextureCache
17 public static class TextureEntry implements Cacheable
19 private final Texture texture;
21 public TextureEntry(Texture texture)
23 this.texture = texture;
26 public Texture getTexture()
28 return texture;
31 public long getSizeInBytes()
33 long size = this.texture.getEstimatedMemorySize();
35 // JOGL returns a zero estimated memory size for some textures, so calculate a size ourselves.
36 if (size < 1)
37 size = this.texture.getHeight() * this.texture.getWidth() * 4;
39 return size;
43 private final BasicMemoryCache textures;
45 public BasicTextureCache(long loWater, long hiWater)
47 this.textures = new BasicMemoryCache(loWater, hiWater);
48 this.textures.setName("Texture Cache");
49 this.textures.addCacheListener(new MemoryCache.CacheListener()
51 public void entryRemoved(Object key, Object clientObject)
53 // Unbind a tile's texture when the tile leaves the cache.
54 if (clientObject != null) // shouldn't be null, but check anyway
56 ((TextureEntry) clientObject).texture.dispose();
59 });
62 public void put(Object key, Texture texture)
64 TextureEntry te = new TextureEntry(texture);
65 this.textures.add(key, te);
68 public Texture get(Object key)
70 TextureEntry entry = (TextureEntry) this.textures.getObject(key);
71 return entry != null ? entry.texture : null;
74 public void remove(Object key)
76 textures.remove(key);
79 public int getNumObjects()
81 return textures.getNumObjects();
84 public long getCapacity()
86 return textures.getCapacity();
89 public long getUsedCapacity()
91 return textures.getUsedCapacity();
94 public long getFreeCapacity()
96 return textures.getFreeCapacity();
99 public boolean contains(Object key)
101 return textures.contains(key);
104 public void clear()
106 textures.clear();