/src/away3d/core/managers/BitmapDataTextureCache.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 54 lines · 42 code · 9 blank · 3 comment · 4 complexity · caffe80409fc1c288f612ec3627ede34 MD5 · raw file

  1. /**
  2. *
  3. */
  4. package away3d.core.managers
  5. {
  6. import away3d.core.managers.Texture3DProxy;
  7. import flash.display.BitmapData;
  8. import flash.utils.Dictionary;
  9. public class BitmapDataTextureCache
  10. {
  11. private static var _instance : BitmapDataTextureCache;
  12. private var _textures : Dictionary;
  13. private var _usages : Dictionary;
  14. public function BitmapDataTextureCache(singletonEnforcer : SingletonEnforcer)
  15. {
  16. if (!singletonEnforcer) throw new Error("Cannot instantiate a singleton class. Use static getInstance instead.");
  17. _textures = new Dictionary();
  18. _usages = new Dictionary();
  19. }
  20. public static function getInstance() : BitmapDataTextureCache
  21. {
  22. return _instance ||= new BitmapDataTextureCache(new SingletonEnforcer());
  23. }
  24. public function getTexture(bitmapData : BitmapData) : Texture3DProxy
  25. {
  26. var texture : Texture3DProxy;
  27. if (!_textures[bitmapData]) {
  28. texture = new Texture3DProxy(bitmapData);
  29. _textures[bitmapData] = texture;
  30. _usages[texture] = 0;
  31. }
  32. _usages[texture]++;
  33. return _textures[bitmapData];
  34. }
  35. public function freeTexture(texture : Texture3DProxy) : void
  36. {
  37. _usages[texture]--;
  38. if (_usages[texture] == 0) {
  39. _textures[Texture3DProxy(texture).bitmapData] = null;
  40. texture.dispose(false);
  41. }
  42. }
  43. }
  44. }
  45. class SingletonEnforcer {}