CCTextureCache.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * cc.textureCache is a singleton object, it's the global cache for cc.Texture2D
  24. * @class
  25. * @name cc.textureCache
  26. */
  27. cc.textureCache = /** @lends cc.textureCache# */{
  28. _textures: {},
  29. _textureColorsCache: {},
  30. _textureKeySeq: (0 | Math.random() * 1000),
  31. _loadedTexturesBefore: {},
  32. //handleLoadedTexture move to Canvas/WebGL
  33. _initializingRenderer: function () {
  34. var selPath;
  35. //init texture from _loadedTexturesBefore
  36. var locLoadedTexturesBefore = this._loadedTexturesBefore, locTextures = this._textures;
  37. for (selPath in locLoadedTexturesBefore) {
  38. var tex2d = locLoadedTexturesBefore[selPath];
  39. tex2d.handleLoadedTexture();
  40. locTextures[selPath] = tex2d;
  41. }
  42. this._loadedTexturesBefore = {};
  43. },
  44. /**
  45. * <p>
  46. * Returns a Texture2D object given an PVR filename <br/>
  47. * If the file image was not previously loaded, it will create a new CCTexture2D <br/>
  48. * object and it will return it. Otherwise it will return a reference of a previously loaded image <br/>
  49. * note: AddPVRTCImage does not support on HTML5
  50. * </p>
  51. * @param {String} filename
  52. * @return {cc.Texture2D}
  53. */
  54. addPVRTCImage: function (filename) {
  55. cc.log(cc._LogInfos.textureCache_addPVRTCImage);
  56. },
  57. /**
  58. * <p>
  59. * Returns a Texture2D object given an ETC filename <br/>
  60. * If the file image was not previously loaded, it will create a new CCTexture2D <br/>
  61. * object and it will return it. Otherwise it will return a reference of a previously loaded image <br/>
  62. * note:addETCImage does not support on HTML5
  63. * </p>
  64. * @param {String} filename
  65. * @return {cc.Texture2D}
  66. */
  67. addETCImage: function (filename) {
  68. cc.log(cc._LogInfos.textureCache_addETCImage);
  69. },
  70. /**
  71. * Description
  72. * @return {String}
  73. */
  74. description: function () {
  75. return "<TextureCache | Number of textures = " + this._textures.length + ">";
  76. },
  77. /**
  78. * Returns an already created texture. Returns null if the texture doesn't exist.
  79. * @param {String} textureKeyName
  80. * @return {cc.Texture2D|Null}
  81. * @deprecated
  82. * @example
  83. * //example
  84. * var key = cc.textureCache.textureForKey("hello.png");
  85. */
  86. textureForKey: function (textureKeyName) {
  87. cc.log(cc._LogInfos.textureCache_textureForKey);
  88. return this.getTextureForKey(textureKeyName);
  89. },
  90. /**
  91. * Returns an already created texture. Returns null if the texture doesn't exist.
  92. * @param {String} textureKeyName
  93. * @return {cc.Texture2D|Null}
  94. * @example
  95. * //example
  96. * var key = cc.textureCache.getTextureForKey("hello.png");
  97. */
  98. getTextureForKey: function(textureKeyName){
  99. return this._textures[textureKeyName] || this._textures[cc.loader._aliases[textureKeyName]];
  100. },
  101. /**
  102. * @param {Image} texture
  103. * @return {String|Null}
  104. * @example
  105. * //example
  106. * var key = cc.textureCache.getKeyByTexture(texture);
  107. */
  108. getKeyByTexture: function (texture) {
  109. for (var key in this._textures) {
  110. if (this._textures[key] == texture) {
  111. return key;
  112. }
  113. }
  114. return null;
  115. },
  116. _generalTextureKey: function () {
  117. this._textureKeySeq++;
  118. return "_textureKey_" + this._textureKeySeq;
  119. },
  120. /**
  121. * @param {Image} texture
  122. * @return {Array}
  123. * @example
  124. * //example
  125. * var cacheTextureForColor = cc.textureCache.getTextureColors(texture);
  126. */
  127. getTextureColors: function (texture) {
  128. var key = this.getKeyByTexture(texture);
  129. if (!key) {
  130. if (texture instanceof HTMLImageElement)
  131. key = texture.src;
  132. else
  133. key = this._generalTextureKey();
  134. }
  135. if (!this._textureColorsCache[key])
  136. this._textureColorsCache[key] = cc.generateTextureCacheForColor(texture);
  137. return this._textureColorsCache[key];
  138. },
  139. /**
  140. * <p>Returns a Texture2D object given an PVR filename<br />
  141. * If the file image was not previously loaded, it will create a new Texture2D<br />
  142. * object and it will return it. Otherwise it will return a reference of a previously loaded image </p>
  143. * @param {String} path
  144. * @return {cc.Texture2D}
  145. */
  146. addPVRImage: function (path) {
  147. cc.log(cc._LogInfos.textureCache_addPVRImage);
  148. },
  149. /**
  150. * <p>Purges the dictionary of loaded textures. <br />
  151. * Call this method if you receive the "Memory Warning" <br />
  152. * In the short term: it will free some resources preventing your app from being killed <br />
  153. * In the medium term: it will allocate more resources <br />
  154. * In the long term: it will be the same</p>
  155. * @example
  156. * //example
  157. * cc.textureCache.removeAllTextures();
  158. */
  159. removeAllTextures: function () {
  160. var locTextures = this._textures;
  161. for (var selKey in locTextures) {
  162. if (locTextures[selKey])
  163. locTextures[selKey].releaseTexture();
  164. }
  165. this._textures = {};
  166. },
  167. /**
  168. * Deletes a texture from the cache given a texture
  169. * @param {Image} texture
  170. * @example
  171. * //example
  172. * cc.textureCache.removeTexture(texture);
  173. */
  174. removeTexture: function (texture) {
  175. if (!texture)
  176. return;
  177. var locTextures = this._textures;
  178. for (var selKey in locTextures) {
  179. if (locTextures[selKey] == texture) {
  180. locTextures[selKey].releaseTexture();
  181. delete(locTextures[selKey]);
  182. }
  183. }
  184. },
  185. /**
  186. * Deletes a texture from the cache given a its key name
  187. * @param {String} textureKeyName
  188. * @example
  189. * //example
  190. * cc.textureCache.removeTexture("hello.png");
  191. */
  192. removeTextureForKey: function (textureKeyName) {
  193. if (textureKeyName == null)
  194. return;
  195. if (this._textures[textureKeyName])
  196. delete(this._textures[textureKeyName]);
  197. },
  198. //addImage move to Canvas/WebGL
  199. /**
  200. * Cache the image data
  201. * @param {String} path
  202. * @param {Image|HTMLImageElement|HTMLCanvasElement} texture
  203. */
  204. cacheImage: function (path, texture) {
  205. if (texture instanceof cc.Texture2D) {
  206. this._textures[path] = texture;
  207. return;
  208. }
  209. var texture2d = new cc.Texture2D();
  210. texture2d.initWithElement(texture);
  211. texture2d.handleLoadedTexture();
  212. this._textures[path] = texture2d;
  213. },
  214. /**
  215. * <p>Returns a Texture2D object given an UIImage image<br />
  216. * If the image was not previously loaded, it will create a new Texture2D object and it will return it.<br />
  217. * Otherwise it will return a reference of a previously loaded image<br />
  218. * The "key" parameter will be used as the "key" for the cache.<br />
  219. * If "key" is null, then a new texture will be created each time.</p>
  220. * @param {HTMLImageElement|HTMLCanvasElement} image
  221. * @param {String} key
  222. * @return {cc.Texture2D}
  223. */
  224. addUIImage: function (image, key) {
  225. cc.assert(image, cc._LogInfos.textureCache_addUIImage_2);
  226. if (key) {
  227. if (this._textures[key])
  228. return this._textures[key];
  229. }
  230. // prevents overloading the autorelease pool
  231. var texture = new cc.Texture2D();
  232. texture.initWithImage(image);
  233. if ((key != null) && (texture != null))
  234. this._textures[key] = texture;
  235. else
  236. cc.log(cc._LogInfos.textureCache_addUIImage);
  237. return texture;
  238. },
  239. /**
  240. * <p>Output to cc.log the current contents of this TextureCache <br />
  241. * This will attempt to calculate the size of each texture, and the total texture memory in use. </p>
  242. */
  243. dumpCachedTextureInfo: function () {
  244. var count = 0;
  245. var totalBytes = 0, locTextures = this._textures;
  246. for (var key in locTextures) {
  247. var selTexture = locTextures[key];
  248. count++;
  249. if (selTexture.getHtmlElementObj() instanceof HTMLImageElement)
  250. cc.log(cc._LogInfos.textureCache_dumpCachedTextureInfo, key, selTexture.getHtmlElementObj().src, selTexture.pixelsWidth, selTexture.pixelsHeight);
  251. else {
  252. cc.log(cc._LogInfos.textureCache_dumpCachedTextureInfo_2, key, selTexture.pixelsWidth, selTexture.pixelsHeight);
  253. }
  254. totalBytes += selTexture.pixelsWidth * selTexture.pixelsHeight * 4;
  255. }
  256. var locTextureColorsCache = this._textureColorsCache;
  257. for (key in locTextureColorsCache) {
  258. var selCanvasColorsArr = locTextureColorsCache[key];
  259. for (var selCanvasKey in selCanvasColorsArr) {
  260. var selCanvas = selCanvasColorsArr[selCanvasKey];
  261. count++;
  262. cc.log(cc._LogInfos.textureCache_dumpCachedTextureInfo_2, key, selCanvas.width, selCanvas.height);
  263. totalBytes += selCanvas.width * selCanvas.height * 4;
  264. }
  265. }
  266. cc.log(cc._LogInfos.textureCache_dumpCachedTextureInfo_3, count, totalBytes / 1024, (totalBytes / (1024.0 * 1024.0)).toFixed(2));
  267. },
  268. _clear: function () {
  269. this._textures = {};
  270. this._textureColorsCache = {};
  271. this._textureKeySeq = (0 | Math.random() * 1000);
  272. this._loadedTexturesBefore = {};
  273. }
  274. };
  275. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  276. var _p = cc.textureCache;
  277. _p.handleLoadedTexture = function (url) {
  278. var locTexs = this._textures;
  279. //remove judge
  280. var tex = locTexs[url];
  281. if (!tex) {
  282. tex = locTexs[url] = new cc.Texture2D();
  283. tex.url = url;
  284. }
  285. tex.handleLoadedTexture();
  286. };
  287. /**
  288. * <p>Returns a Texture2D object given an file image <br />
  289. * If the file image was not previously loaded, it will create a new Texture2D <br />
  290. * object and it will return it. It will use the filename as a key.<br />
  291. * Otherwise it will return a reference of a previously loaded image. <br />
  292. * Supported image extensions: .png, .jpg, .gif</p>
  293. * @param {String} url
  294. * @param {Function} cb
  295. * @param {Object} target
  296. * @return {cc.Texture2D}
  297. * @example
  298. * //example
  299. * cc.textureCache.addImage("hello.png");
  300. */
  301. _p.addImage = function (url, cb, target) {
  302. cc.assert(url, cc._LogInfos.Texture2D_addImage);
  303. var locTexs = this._textures;
  304. //remove judge
  305. var tex = locTexs[url] || locTexs[cc.loader._aliases[url]];
  306. if (tex) {
  307. cb && cb.call(target);
  308. return tex;
  309. }
  310. tex = locTexs[url] = new cc.Texture2D();
  311. tex.url = url;
  312. if (!cc.loader.getRes(url)) {
  313. if (cc.loader._checkIsImageURL(url)) {
  314. cc.loader.load(url, function (err) {
  315. cb && cb.call(target);
  316. });
  317. } else {
  318. cc.loader.cache[url] = cc.loader.loadImg(url, function (err, img) {
  319. if (err)
  320. return cb ? cb(err) : err;
  321. cc.textureCache.handleLoadedTexture(url);
  322. cb && cb(target, img);
  323. });
  324. }
  325. }
  326. else {
  327. tex.handleLoadedTexture();
  328. }
  329. return tex;
  330. };
  331. _p = null;
  332. } else {
  333. cc.assert(typeof cc._tmp.WebGLTextureCache === "function", cc._LogInfos.MissingFile, "TexturesWebGL.js");
  334. cc._tmp.WebGLTextureCache();
  335. delete cc._tmp.WebGLTextureCache;
  336. }