PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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