CCTexture2D.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /****************************************************************************
  2. Copyright (c) 2008 Apple Inc. All Rights Reserved.
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 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. * Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from:
  24. * https://devforums.apple.com/message/37855#37855 by a1studmuffin
  25. */
  26. #include "renderer/CCTexture2D.h"
  27. #include "platform/CCGL.h"
  28. #include "platform/CCImage.h"
  29. #include "base/ccUtils.h"
  30. #include "platform/CCDevice.h"
  31. #include "base/ccConfig.h"
  32. #include "base/ccMacros.h"
  33. #include "base/ccUTF8.h"
  34. #include "base/CCConfiguration.h"
  35. #include "platform/CCPlatformMacros.h"
  36. #include "base/CCDirector.h"
  37. #include "renderer/CCGLProgram.h"
  38. #include "renderer/ccGLStateCache.h"
  39. #include "renderer/CCGLProgramCache.h"
  40. #include "base/CCNinePatchImageParser.h"
  41. #if CC_ENABLE_CACHE_TEXTURE_DATA
  42. #include "renderer/CCTextureCache.h"
  43. #endif
  44. NS_CC_BEGIN
  45. namespace {
  46. typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue;
  47. static const PixelFormatInfoMapValue TexturePixelFormatInfoTablesValue[] =
  48. {
  49. PixelFormatInfoMapValue(Texture2D::PixelFormat::BGRA8888, Texture2D::PixelFormatInfo(GL_BGRA, GL_BGRA, GL_UNSIGNED_BYTE, 32, false, true)),
  50. PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA8888, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, false, true)),
  51. PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA4444, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16, false, true)),
  52. PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB5A1, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16, false, true)),
  53. PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB565, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16, false, false)),
  54. PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB888, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 24, false, false)),
  55. PixelFormatInfoMapValue(Texture2D::PixelFormat::A8, Texture2D::PixelFormatInfo(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, 8, false, false)),
  56. PixelFormatInfoMapValue(Texture2D::PixelFormat::I8, Texture2D::PixelFormatInfo(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8, false, false)),
  57. PixelFormatInfoMapValue(Texture2D::PixelFormat::AI88, Texture2D::PixelFormatInfo(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 16, false, true)),
  58. #ifdef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG
  59. PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC2, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 2, true, false)),
  60. PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC2A, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 2, true, true)),
  61. PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC4, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
  62. PixelFormatInfoMapValue(Texture2D::PixelFormat::PVRTC4A, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, true)),
  63. #endif
  64. #ifdef GL_ETC1_RGB8_OES
  65. PixelFormatInfoMapValue(Texture2D::PixelFormat::ETC, Texture2D::PixelFormatInfo(GL_ETC1_RGB8_OES, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
  66. #endif
  67. #ifdef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
  68. PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT1, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
  69. #endif
  70. #ifdef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
  71. PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT3, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
  72. #endif
  73. #ifdef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
  74. PixelFormatInfoMapValue(Texture2D::PixelFormat::S3TC_DXT5, Texture2D::PixelFormatInfo(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
  75. #endif
  76. #ifdef GL_ATC_RGB_AMD
  77. PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_RGB, Texture2D::PixelFormatInfo(GL_ATC_RGB_AMD,
  78. 0xFFFFFFFF, 0xFFFFFFFF, 4, true, false)),
  79. #endif
  80. #ifdef GL_ATC_RGBA_EXPLICIT_ALPHA_AMD
  81. PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_EXPLICIT_ALPHA, Texture2D::PixelFormatInfo(GL_ATC_RGBA_EXPLICIT_ALPHA_AMD,
  82. 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
  83. #endif
  84. #ifdef GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD
  85. PixelFormatInfoMapValue(Texture2D::PixelFormat::ATC_INTERPOLATED_ALPHA, Texture2D::PixelFormatInfo(GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD,
  86. 0xFFFFFFFF, 0xFFFFFFFF, 8, true, false)),
  87. #endif
  88. };
  89. }
  90. //CLASS IMPLEMENTATIONS:
  91. //The PixpelFormat corresponding information
  92. const Texture2D::PixelFormatInfoMap Texture2D::_pixelFormatInfoTables(TexturePixelFormatInfoTablesValue,
  93. TexturePixelFormatInfoTablesValue + sizeof(TexturePixelFormatInfoTablesValue) / sizeof(TexturePixelFormatInfoTablesValue[0]));
  94. // If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
  95. // Default is: RGBA8888 (32-bit textures)
  96. static Texture2D::PixelFormat g_defaultAlphaPixelFormat = Texture2D::PixelFormat::DEFAULT;
  97. //////////////////////////////////////////////////////////////////////////
  98. //convertor function
  99. // IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBB
  100. void Texture2D::convertI8ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  101. {
  102. for (ssize_t i=0; i < dataLen; ++i)
  103. {
  104. *outData++ = data[i]; //R
  105. *outData++ = data[i]; //G
  106. *outData++ = data[i]; //B
  107. }
  108. }
  109. // IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
  110. void Texture2D::convertAI88ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  111. {
  112. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  113. {
  114. *outData++ = data[i]; //R
  115. *outData++ = data[i]; //G
  116. *outData++ = data[i]; //B
  117. }
  118. }
  119. // IIIIIIII -> RRRRRRRRGGGGGGGGGBBBBBBBBAAAAAAAA
  120. void Texture2D::convertI8ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  121. {
  122. for (ssize_t i = 0; i < dataLen; ++i)
  123. {
  124. *outData++ = data[i]; //R
  125. *outData++ = data[i]; //G
  126. *outData++ = data[i]; //B
  127. *outData++ = 0xFF; //A
  128. }
  129. }
  130. // IIIIIIIIAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
  131. void Texture2D::convertAI88ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  132. {
  133. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  134. {
  135. *outData++ = data[i]; //R
  136. *outData++ = data[i]; //G
  137. *outData++ = data[i]; //B
  138. *outData++ = data[i + 1]; //A
  139. }
  140. }
  141. // IIIIIIII -> RRRRRGGGGGGBBBBB
  142. void Texture2D::convertI8ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  143. {
  144. unsigned short* out16 = (unsigned short*)outData;
  145. for (int i = 0; i < dataLen; ++i)
  146. {
  147. *out16++ = (data[i] & 0x00F8) << 8 //R
  148. | (data[i] & 0x00FC) << 3 //G
  149. | (data[i] & 0x00F8) >> 3; //B
  150. }
  151. }
  152. // IIIIIIIIAAAAAAAA -> RRRRRGGGGGGBBBBB
  153. void Texture2D::convertAI88ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  154. {
  155. unsigned short* out16 = (unsigned short*)outData;
  156. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  157. {
  158. *out16++ = (data[i] & 0x00F8) << 8 //R
  159. | (data[i] & 0x00FC) << 3 //G
  160. | (data[i] & 0x00F8) >> 3; //B
  161. }
  162. }
  163. // IIIIIIII -> RRRRGGGGBBBBAAAA
  164. void Texture2D::convertI8ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  165. {
  166. unsigned short* out16 = (unsigned short*)outData;
  167. for (ssize_t i = 0; i < dataLen; ++i)
  168. {
  169. *out16++ = (data[i] & 0x00F0) << 8 //R
  170. | (data[i] & 0x00F0) << 4 //G
  171. | (data[i] & 0x00F0) //B
  172. | 0x000F; //A
  173. }
  174. }
  175. // IIIIIIIIAAAAAAAA -> RRRRGGGGBBBBAAAA
  176. void Texture2D::convertAI88ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  177. {
  178. unsigned short* out16 = (unsigned short*)outData;
  179. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  180. {
  181. *out16++ = (data[i] & 0x00F0) << 8 //R
  182. | (data[i] & 0x00F0) << 4 //G
  183. | (data[i] & 0x00F0) //B
  184. | (data[i+1] & 0x00F0) >> 4; //A
  185. }
  186. }
  187. // IIIIIIII -> RRRRRGGGGGBBBBBA
  188. void Texture2D::convertI8ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  189. {
  190. unsigned short* out16 = (unsigned short*)outData;
  191. for (int i = 0; i < dataLen; ++i)
  192. {
  193. *out16++ = (data[i] & 0x00F8) << 8 //R
  194. | (data[i] & 0x00F8) << 3 //G
  195. | (data[i] & 0x00F8) >> 2 //B
  196. | 0x0001; //A
  197. }
  198. }
  199. // IIIIIIIIAAAAAAAA -> RRRRRGGGGGBBBBBA
  200. void Texture2D::convertAI88ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  201. {
  202. unsigned short* out16 = (unsigned short*)outData;
  203. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  204. {
  205. *out16++ = (data[i] & 0x00F8) << 8 //R
  206. | (data[i] & 0x00F8) << 3 //G
  207. | (data[i] & 0x00F8) >> 2 //B
  208. | (data[i + 1] & 0x0080) >> 7; //A
  209. }
  210. }
  211. // IIIIIIII -> IIIIIIIIAAAAAAAA
  212. void Texture2D::convertI8ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  213. {
  214. unsigned short* out16 = (unsigned short*)outData;
  215. for (ssize_t i = 0; i < dataLen; ++i)
  216. {
  217. *out16++ = 0xFF00 //A
  218. | data[i]; //I
  219. }
  220. }
  221. // IIIIIIIIAAAAAAAA -> AAAAAAAA
  222. void Texture2D::convertAI88ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  223. {
  224. for (ssize_t i = 1; i < dataLen; i += 2)
  225. {
  226. *outData++ = data[i]; //A
  227. }
  228. }
  229. // IIIIIIIIAAAAAAAA -> IIIIIIII
  230. void Texture2D::convertAI88ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  231. {
  232. for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2)
  233. {
  234. *outData++ = data[i]; //R
  235. }
  236. }
  237. // RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA
  238. void Texture2D::convertRGB888ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  239. {
  240. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  241. {
  242. *outData++ = data[i]; //R
  243. *outData++ = data[i + 1]; //G
  244. *outData++ = data[i + 2]; //B
  245. *outData++ = 0xFF; //A
  246. }
  247. }
  248. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRRRRGGGGGGGGBBBBBBBB
  249. void Texture2D::convertRGBA8888ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  250. {
  251. for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
  252. {
  253. *outData++ = data[i]; //R
  254. *outData++ = data[i + 1]; //G
  255. *outData++ = data[i + 2]; //B
  256. }
  257. }
  258. // RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGGBBBBB
  259. void Texture2D::convertRGB888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  260. {
  261. unsigned short* out16 = (unsigned short*)outData;
  262. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  263. {
  264. *out16++ = (data[i] & 0x00F8) << 8 //R
  265. | (data[i + 1] & 0x00FC) << 3 //G
  266. | (data[i + 2] & 0x00F8) >> 3; //B
  267. }
  268. }
  269. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRRGGGGGGBBBBB
  270. void Texture2D::convertRGBA8888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  271. {
  272. unsigned short* out16 = (unsigned short*)outData;
  273. for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
  274. {
  275. *out16++ = (data[i] & 0x00F8) << 8 //R
  276. | (data[i + 1] & 0x00FC) << 3 //G
  277. | (data[i + 2] & 0x00F8) >> 3; //B
  278. }
  279. }
  280. // RRRRRRRRGGGGGGGGBBBBBBBB -> AAAAAAAA
  281. void Texture2D::convertRGB888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  282. {
  283. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  284. {
  285. *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //A = (R*299 + G*587 + B*114 + 500) / 1000
  286. }
  287. }
  288. // RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIII
  289. void Texture2D::convertRGB888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  290. {
  291. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  292. {
  293. *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
  294. }
  295. }
  296. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIII
  297. void Texture2D::convertRGBA8888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  298. {
  299. for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
  300. {
  301. *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
  302. }
  303. }
  304. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> AAAAAAAA
  305. void Texture2D::convertRGBA8888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  306. {
  307. for (ssize_t i = 0, l = dataLen -3; i < l; i += 4)
  308. {
  309. *outData++ = data[i + 3]; //A
  310. }
  311. }
  312. // RRRRRRRRGGGGGGGGBBBBBBBB -> IIIIIIIIAAAAAAAA
  313. void Texture2D::convertRGB888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  314. {
  315. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  316. {
  317. *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
  318. *outData++ = 0xFF;
  319. }
  320. }
  321. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> IIIIIIIIAAAAAAAA
  322. void Texture2D::convertRGBA8888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  323. {
  324. for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
  325. {
  326. *outData++ = (data[i] * 299 + data[i + 1] * 587 + data[i + 2] * 114 + 500) / 1000; //I = (R*299 + G*587 + B*114 + 500) / 1000
  327. *outData++ = data[i + 3];
  328. }
  329. }
  330. // RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRGGGGBBBBAAAA
  331. void Texture2D::convertRGB888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  332. {
  333. unsigned short* out16 = (unsigned short*)outData;
  334. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  335. {
  336. *out16++ = ((data[i] & 0x00F0) << 8 //R
  337. | (data[i + 1] & 0x00F0) << 4 //G
  338. | (data[i + 2] & 0xF0) //B
  339. | 0x0F); //A
  340. }
  341. }
  342. // RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA -> RRRRGGGGBBBBAAAA
  343. void Texture2D::convertRGBA8888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  344. {
  345. unsigned short* out16 = (unsigned short*)outData;
  346. for (ssize_t i = 0, l = dataLen - 3; i < l; i += 4)
  347. {
  348. *out16++ = (data[i] & 0x00F0) << 8 //R
  349. | (data[i + 1] & 0x00F0) << 4 //G
  350. | (data[i + 2] & 0xF0) //B
  351. | (data[i + 3] & 0xF0) >> 4; //A
  352. }
  353. }
  354. // RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
  355. void Texture2D::convertRGB888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  356. {
  357. unsigned short* out16 = (unsigned short*)outData;
  358. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 3)
  359. {
  360. *out16++ = (data[i] & 0x00F8) << 8 //R
  361. | (data[i + 1] & 0x00F8) << 3 //G
  362. | (data[i + 2] & 0x00F8) >> 2 //B
  363. | 0x01; //A
  364. }
  365. }
  366. // RRRRRRRRGGGGGGGGBBBBBBBB -> RRRRRGGGGGBBBBBA
  367. void Texture2D::convertRGBA8888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData)
  368. {
  369. unsigned short* out16 = (unsigned short*)outData;
  370. for (ssize_t i = 0, l = dataLen - 2; i < l; i += 4)
  371. {
  372. *out16++ = (data[i] & 0x00F8) << 8 //R
  373. | (data[i + 1] & 0x00F8) << 3 //G
  374. | (data[i + 2] & 0x00F8) >> 2 //B
  375. | (data[i + 3] & 0x0080) >> 7; //A
  376. }
  377. }
  378. // converter function end
  379. //////////////////////////////////////////////////////////////////////////
  380. Texture2D::Texture2D()
  381. : _pixelFormat(Texture2D::PixelFormat::DEFAULT)
  382. , _pixelsWide(0)
  383. , _pixelsHigh(0)
  384. , _name(0)
  385. , _maxS(0.0)
  386. , _maxT(0.0)
  387. , _hasPremultipliedAlpha(false)
  388. , _hasMipmaps(false)
  389. , _shaderProgram(nullptr)
  390. , _antialiasEnabled(true)
  391. , _ninePatchInfo(nullptr)
  392. , _valid(true)
  393. , _alphaTexture(nullptr)
  394. {
  395. }
  396. Texture2D::~Texture2D()
  397. {
  398. #if CC_ENABLE_CACHE_TEXTURE_DATA
  399. VolatileTextureMgr::removeTexture(this);
  400. #endif
  401. CC_SAFE_RELEASE_NULL(_alphaTexture); // ETC1 ALPHA support.
  402. CCLOGINFO("deallocing Texture2D: %p - id=%u", this, _name);
  403. CC_SAFE_RELEASE(_shaderProgram);
  404. CC_SAFE_DELETE(_ninePatchInfo);
  405. if(_name)
  406. {
  407. GL::deleteTexture(_name);
  408. }
  409. }
  410. void Texture2D::releaseGLTexture()
  411. {
  412. if(_name)
  413. {
  414. GL::deleteTexture(_name);
  415. }
  416. _name = 0;
  417. }
  418. Texture2D::PixelFormat Texture2D::getPixelFormat() const
  419. {
  420. return _pixelFormat;
  421. }
  422. int Texture2D::getPixelsWide() const
  423. {
  424. return _pixelsWide;
  425. }
  426. int Texture2D::getPixelsHigh() const
  427. {
  428. return _pixelsHigh;
  429. }
  430. GLuint Texture2D::getName() const
  431. {
  432. return _name;
  433. }
  434. GLuint Texture2D::getAlphaTextureName() const
  435. {
  436. return _alphaTexture == nullptr ? 0 : _alphaTexture->getName();
  437. }
  438. Size Texture2D::getContentSize() const
  439. {
  440. Size ret;
  441. ret.width = _contentSize.width / CC_CONTENT_SCALE_FACTOR();
  442. ret.height = _contentSize.height / CC_CONTENT_SCALE_FACTOR();
  443. return ret;
  444. }
  445. const Size& Texture2D::getContentSizeInPixels()
  446. {
  447. return _contentSize;
  448. }
  449. GLfloat Texture2D::getMaxS() const
  450. {
  451. return _maxS;
  452. }
  453. void Texture2D::setMaxS(GLfloat maxS)
  454. {
  455. _maxS = maxS;
  456. }
  457. GLfloat Texture2D::getMaxT() const
  458. {
  459. return _maxT;
  460. }
  461. void Texture2D::setMaxT(GLfloat maxT)
  462. {
  463. _maxT = maxT;
  464. }
  465. GLProgram* Texture2D::getGLProgram() const
  466. {
  467. return _shaderProgram;
  468. }
  469. void Texture2D::setGLProgram(GLProgram* shaderProgram)
  470. {
  471. CC_SAFE_RETAIN(shaderProgram);
  472. CC_SAFE_RELEASE(_shaderProgram);
  473. _shaderProgram = shaderProgram;
  474. }
  475. bool Texture2D::hasPremultipliedAlpha() const
  476. {
  477. return _hasPremultipliedAlpha;
  478. }
  479. bool Texture2D::initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& /*contentSize*/)
  480. {
  481. CCASSERT(dataLen>0 && pixelsWide>0 && pixelsHigh>0, "Invalid size");
  482. //if data has no mipmaps, we will consider it has only one mipmap
  483. MipmapInfo mipmap;
  484. mipmap.address = (unsigned char*)data;
  485. mipmap.len = static_cast<int>(dataLen);
  486. return initWithMipmaps(&mipmap, 1, pixelFormat, pixelsWide, pixelsHigh);
  487. }
  488. bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh)
  489. {
  490. //the pixelFormat must be a certain value
  491. CCASSERT(pixelFormat != PixelFormat::NONE && pixelFormat != PixelFormat::AUTO, "the \"pixelFormat\" param must be a certain value!");
  492. CCASSERT(pixelsWide>0 && pixelsHigh>0, "Invalid size");
  493. if (mipmapsNum <= 0)
  494. {
  495. CCLOG("cocos2d: WARNING: mipmap number is less than 1");
  496. return false;
  497. }
  498. if(_pixelFormatInfoTables.find(pixelFormat) == _pixelFormatInfoTables.end())
  499. {
  500. CCLOG("cocos2d: WARNING: unsupported pixelformat: %lx", (unsigned long)pixelFormat );
  501. return false;
  502. }
  503. const PixelFormatInfo& info = _pixelFormatInfoTables.at(pixelFormat);
  504. if (info.compressed && !Configuration::getInstance()->supportsPVRTC()
  505. && !Configuration::getInstance()->supportsETC()
  506. && !Configuration::getInstance()->supportsS3TC()
  507. && !Configuration::getInstance()->supportsATITC())
  508. {
  509. CCLOG("cocos2d: WARNING: PVRTC/ETC images are not supported");
  510. return false;
  511. }
  512. //Set the row align only when mipmapsNum == 1 and the data is uncompressed
  513. if (mipmapsNum == 1 && !info.compressed)
  514. {
  515. unsigned int bytesPerRow = pixelsWide * info.bpp / 8;
  516. if(bytesPerRow % 8 == 0)
  517. {
  518. glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
  519. }
  520. else if(bytesPerRow % 4 == 0)
  521. {
  522. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  523. }
  524. else if(bytesPerRow % 2 == 0)
  525. {
  526. glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
  527. }
  528. else
  529. {
  530. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  531. }
  532. }else
  533. {
  534. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  535. }
  536. if(_name != 0)
  537. {
  538. GL::deleteTexture(_name);
  539. _name = 0;
  540. }
  541. glGenTextures(1, &_name);
  542. GL::bindTexture2D(_name);
  543. if (mipmapsNum == 1)
  544. {
  545. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST);
  546. }else
  547. {
  548. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST);
  549. }
  550. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST );
  551. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  552. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  553. #if CC_ENABLE_CACHE_TEXTURE_DATA
  554. if (_antialiasEnabled)
  555. {
  556. TexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE};
  557. VolatileTextureMgr::setTexParameters(this, texParams);
  558. }
  559. else
  560. {
  561. TexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE};
  562. VolatileTextureMgr::setTexParameters(this, texParams);
  563. }
  564. #endif
  565. // clean possible GL error
  566. GLenum err = glGetError();
  567. if (err != GL_NO_ERROR)
  568. {
  569. cocos2d::log("OpenGL error 0x%04X in %s %s %d\n", err, __FILE__, __FUNCTION__, __LINE__);
  570. }
  571. // Specify OpenGL texture image
  572. int width = pixelsWide;
  573. int height = pixelsHigh;
  574. for (int i = 0; i < mipmapsNum; ++i)
  575. {
  576. unsigned char *data = mipmaps[i].address;
  577. GLsizei datalen = mipmaps[i].len;
  578. if (info.compressed)
  579. {
  580. glCompressedTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, datalen, data);
  581. }
  582. else
  583. {
  584. glTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, info.format, info.type, data);
  585. }
  586. if (i > 0 && (width != height || ccNextPOT(width) != width ))
  587. {
  588. CCLOG("cocos2d: Texture2D. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%d != height=%d", i, width, height);
  589. }
  590. err = glGetError();
  591. if (err != GL_NO_ERROR)
  592. {
  593. CCLOG("cocos2d: Texture2D: Error uploading compressed texture level: %u . glError: 0x%04X", i, err);
  594. return false;
  595. }
  596. width = MAX(width >> 1, 1);
  597. height = MAX(height >> 1, 1);
  598. }
  599. _contentSize = Size((float)pixelsWide, (float)pixelsHigh);
  600. _pixelsWide = pixelsWide;
  601. _pixelsHigh = pixelsHigh;
  602. _pixelFormat = pixelFormat;
  603. _maxS = 1;
  604. _maxT = 1;
  605. _hasPremultipliedAlpha = false;
  606. _hasMipmaps = mipmapsNum > 1;
  607. // shader
  608. setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE));
  609. return true;
  610. }
  611. bool Texture2D::updateWithData(const void *data,int offsetX,int offsetY,int width,int height)
  612. {
  613. if (_name)
  614. {
  615. GL::bindTexture2D(_name);
  616. const PixelFormatInfo& info = _pixelFormatInfoTables.at(_pixelFormat);
  617. glTexSubImage2D(GL_TEXTURE_2D,0,offsetX,offsetY,width,height,info.format, info.type,data);
  618. return true;
  619. }
  620. return false;
  621. }
  622. std::string Texture2D::getDescription() const
  623. {
  624. return StringUtils::format("<Texture2D | Name = %u | Dimensions = %ld x %ld | Coordinates = (%.2f, %.2f)>", _name, (long)_pixelsWide, (long)_pixelsHigh, _maxS, _maxT);
  625. }
  626. // implementation Texture2D (Image)
  627. bool Texture2D::initWithImage(Image *image)
  628. {
  629. return initWithImage(image, g_defaultAlphaPixelFormat);
  630. }
  631. bool Texture2D::initWithImage(Image *image, PixelFormat format)
  632. {
  633. if (image == nullptr)
  634. {
  635. CCLOG("cocos2d: Texture2D. Can't create Texture. UIImage is nil");
  636. return false;
  637. }
  638. int imageWidth = image->getWidth();
  639. int imageHeight = image->getHeight();
  640. this->_filePath = image->getFilePath();
  641. Configuration *conf = Configuration::getInstance();
  642. int maxTextureSize = conf->getMaxTextureSize();
  643. if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
  644. {
  645. CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight, maxTextureSize, maxTextureSize);
  646. return false;
  647. }
  648. unsigned char* tempData = image->getData();
  649. Size imageSize = Size((float)imageWidth, (float)imageHeight);
  650. PixelFormat pixelFormat = ((PixelFormat::NONE == format) || (PixelFormat::AUTO == format)) ? image->getRenderFormat() : format;
  651. PixelFormat renderFormat = image->getRenderFormat();
  652. size_t tempDataLen = image->getDataLen();
  653. if (image->getNumberOfMipmaps() > 1)
  654. {
  655. if (pixelFormat != image->getRenderFormat())
  656. {
  657. CCLOG("cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format");
  658. }
  659. initWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getRenderFormat(), imageWidth, imageHeight);
  660. // set the premultiplied tag
  661. _hasPremultipliedAlpha = image->hasPremultipliedAlpha();
  662. return true;
  663. }
  664. else if (image->isCompressed())
  665. {
  666. if (pixelFormat != image->getRenderFormat())
  667. {
  668. CCLOG("cocos2d: WARNING: This image is compressed and we can't convert it for now");
  669. }
  670. initWithData(tempData, tempDataLen, image->getRenderFormat(), imageWidth, imageHeight, imageSize);
  671. // set the premultiplied tag
  672. _hasPremultipliedAlpha = image->hasPremultipliedAlpha();
  673. return true;
  674. }
  675. else
  676. {
  677. unsigned char* outTempData = nullptr;
  678. ssize_t outTempDataLen = 0;
  679. pixelFormat = convertDataToFormat(tempData, tempDataLen, renderFormat, pixelFormat, &outTempData, &outTempDataLen);
  680. initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize);
  681. if (outTempData != nullptr && outTempData != tempData)
  682. {
  683. free(outTempData);
  684. }
  685. // set the premultiplied tag
  686. _hasPremultipliedAlpha = image->hasPremultipliedAlpha();
  687. return true;
  688. }
  689. }
  690. Texture2D::PixelFormat Texture2D::convertI8ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
  691. {
  692. switch (format)
  693. {
  694. case PixelFormat::RGBA8888:
  695. *outDataLen = dataLen*4;
  696. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  697. convertI8ToRGBA8888(data, dataLen, *outData);
  698. break;
  699. case PixelFormat::RGB888:
  700. *outDataLen = dataLen*3;
  701. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  702. convertI8ToRGB888(data, dataLen, *outData);
  703. break;
  704. case PixelFormat::RGB565:
  705. *outDataLen = dataLen*2;
  706. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  707. convertI8ToRGB565(data, dataLen, *outData);
  708. break;
  709. case PixelFormat::AI88:
  710. *outDataLen = dataLen*2;
  711. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  712. convertI8ToAI88(data, dataLen, *outData);
  713. break;
  714. case PixelFormat::RGBA4444:
  715. *outDataLen = dataLen*2;
  716. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  717. convertI8ToRGBA4444(data, dataLen, *outData);
  718. break;
  719. case PixelFormat::RGB5A1:
  720. *outDataLen = dataLen*2;
  721. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  722. convertI8ToRGB5A1(data, dataLen, *outData);
  723. break;
  724. default:
  725. // unsupported conversion or don't need to convert
  726. if (format != PixelFormat::AUTO && format != PixelFormat::I8)
  727. {
  728. CCLOG("Can not convert image format PixelFormat::I8 to format ID:%d, we will use it's origin format PixelFormat::I8", static_cast<int>(format));
  729. }
  730. *outData = (unsigned char*)data;
  731. *outDataLen = dataLen;
  732. return PixelFormat::I8;
  733. }
  734. return format;
  735. }
  736. Texture2D::PixelFormat Texture2D::convertAI88ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
  737. {
  738. switch (format)
  739. {
  740. case PixelFormat::RGBA8888:
  741. *outDataLen = dataLen*2;
  742. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  743. convertAI88ToRGBA8888(data, dataLen, *outData);
  744. break;
  745. case PixelFormat::RGB888:
  746. *outDataLen = dataLen/2*3;
  747. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  748. convertAI88ToRGB888(data, dataLen, *outData);
  749. break;
  750. case PixelFormat::RGB565:
  751. *outDataLen = dataLen;
  752. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  753. convertAI88ToRGB565(data, dataLen, *outData);
  754. break;
  755. case PixelFormat::A8:
  756. *outDataLen = dataLen/2;
  757. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  758. convertAI88ToA8(data, dataLen, *outData);
  759. break;
  760. case PixelFormat::I8:
  761. *outDataLen = dataLen/2;
  762. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  763. convertAI88ToI8(data, dataLen, *outData);
  764. break;
  765. case PixelFormat::RGBA4444:
  766. *outDataLen = dataLen;
  767. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  768. convertAI88ToRGBA4444(data, dataLen, *outData);
  769. break;
  770. case PixelFormat::RGB5A1:
  771. *outDataLen = dataLen;
  772. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  773. convertAI88ToRGB5A1(data, dataLen, *outData);
  774. break;
  775. default:
  776. // unsupported conversion or don't need to convert
  777. if (format != PixelFormat::AUTO && format != PixelFormat::AI88)
  778. {
  779. CCLOG("Can not convert image format PixelFormat::AI88 to format ID:%d, we will use it's origin format PixelFormat::AI88", static_cast<int>(format));
  780. }
  781. *outData = (unsigned char*)data;
  782. *outDataLen = dataLen;
  783. return PixelFormat::AI88;
  784. break;
  785. }
  786. return format;
  787. }
  788. Texture2D::PixelFormat Texture2D::convertRGB888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
  789. {
  790. switch (format)
  791. {
  792. case PixelFormat::RGBA8888:
  793. *outDataLen = dataLen/3*4;
  794. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  795. convertRGB888ToRGBA8888(data, dataLen, *outData);
  796. break;
  797. case PixelFormat::RGB565:
  798. *outDataLen = dataLen/3*2;
  799. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  800. convertRGB888ToRGB565(data, dataLen, *outData);
  801. break;
  802. case PixelFormat::A8:
  803. *outDataLen = dataLen/3;
  804. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  805. convertRGB888ToA8(data, dataLen, *outData);
  806. break;
  807. case PixelFormat::I8:
  808. *outDataLen = dataLen/3;
  809. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  810. convertRGB888ToI8(data, dataLen, *outData);
  811. break;
  812. case PixelFormat::AI88:
  813. *outDataLen = dataLen/3*2;
  814. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  815. convertRGB888ToAI88(data, dataLen, *outData);
  816. break;
  817. case PixelFormat::RGBA4444:
  818. *outDataLen = dataLen/3*2;
  819. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  820. convertRGB888ToRGBA4444(data, dataLen, *outData);
  821. break;
  822. case PixelFormat::RGB5A1:
  823. *outDataLen = dataLen;
  824. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  825. convertRGB888ToRGB5A1(data, dataLen, *outData);
  826. break;
  827. default:
  828. // unsupported conversion or don't need to convert
  829. if (format != PixelFormat::AUTO && format != PixelFormat::RGB888)
  830. {
  831. CCLOG("Can not convert image format PixelFormat::RGB888 to format ID:%d, we will use it's origin format PixelFormat::RGB888", static_cast<int>(format));
  832. }
  833. *outData = (unsigned char*)data;
  834. *outDataLen = dataLen;
  835. return PixelFormat::RGB888;
  836. }
  837. return format;
  838. }
  839. Texture2D::PixelFormat Texture2D::convertRGBA8888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
  840. {
  841. switch (format)
  842. {
  843. case PixelFormat::RGB888:
  844. *outDataLen = dataLen/4*3;
  845. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  846. convertRGBA8888ToRGB888(data, dataLen, *outData);
  847. break;
  848. case PixelFormat::RGB565:
  849. *outDataLen = dataLen/2;
  850. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  851. convertRGBA8888ToRGB565(data, dataLen, *outData);
  852. break;
  853. case PixelFormat::A8:
  854. *outDataLen = dataLen/4;
  855. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  856. convertRGBA8888ToA8(data, dataLen, *outData);
  857. break;
  858. case PixelFormat::I8:
  859. *outDataLen = dataLen/4;
  860. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  861. convertRGBA8888ToI8(data, dataLen, *outData);
  862. break;
  863. case PixelFormat::AI88:
  864. *outDataLen = dataLen/2;
  865. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  866. convertRGBA8888ToAI88(data, dataLen, *outData);
  867. break;
  868. case PixelFormat::RGBA4444:
  869. *outDataLen = dataLen/2;
  870. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  871. convertRGBA8888ToRGBA4444(data, dataLen, *outData);
  872. break;
  873. case PixelFormat::RGB5A1:
  874. *outDataLen = dataLen/2;
  875. *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen));
  876. convertRGBA8888ToRGB5A1(data, dataLen, *outData);
  877. break;
  878. default:
  879. // unsupported conversion or don't need to convert
  880. if (format != PixelFormat::AUTO && format != PixelFormat::RGBA8888)
  881. {
  882. CCLOG("Can not convert image format PixelFormat::RGBA8888 to format ID:%d, we will use it's origin format PixelFormat::RGBA8888", static_cast<int>(format));
  883. }
  884. *outData = (unsigned char*)data;
  885. *outDataLen = dataLen;
  886. return PixelFormat::RGBA8888;
  887. }
  888. return format;
  889. }
  890. /*
  891. convert map:
  892. 1.PixelFormat::RGBA8888
  893. 2.PixelFormat::RGB888
  894. 3.PixelFormat::RGB565
  895. 4.PixelFormat::A8
  896. 5.PixelFormat::I8
  897. 6.PixelFormat::AI88
  898. 7.PixelFormat::RGBA4444
  899. 8.PixelFormat::RGB5A1
  900. gray(5) -> 1235678
  901. gray alpha(6) -> 12345678
  902. rgb(2) -> 1235678
  903. rgba(1) -> 12345678
  904. */
  905. Texture2D::PixelFormat Texture2D::convertDataToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat originFormat, PixelFormat format, unsigned char** outData, ssize_t* outDataLen)
  906. {
  907. // don't need to convert
  908. if (format == originFormat || format == PixelFormat::AUTO)
  909. {
  910. *outData = (unsigned char*)data;
  911. *outDataLen = dataLen;
  912. return originFormat;
  913. }
  914. switch (originFormat)
  915. {
  916. case PixelFormat::I8:
  917. return convertI8ToFormat(data, dataLen, format, outData, outDataLen);
  918. case PixelFormat::AI88:
  919. return convertAI88ToFormat(data, dataLen, format, outData, outDataLen);
  920. case PixelFormat::RGB888:
  921. return convertRGB888ToFormat(data, dataLen, format, outData, outDataLen);
  922. case PixelFormat::RGBA8888:
  923. return convertRGBA8888ToFormat(data, dataLen, format, outData, outDataLen);
  924. default:
  925. CCLOG("unsupported conversion from format %d to format %d", static_cast<int>(originFormat), static_cast<int>(format));
  926. *outData = (unsigned char*)data;
  927. *outDataLen = dataLen;
  928. return originFormat;
  929. }
  930. }
  931. // implementation Texture2D (Text)
  932. bool Texture2D::initWithString(const char *text, const std::string& fontName, float fontSize, const Size& dimensions/* = Size(0, 0)*/, TextHAlignment hAlignment/* = TextHAlignment::CENTER */, TextVAlignment vAlignment/* = TextVAlignment::TOP */, bool enableWrap /* = false */, int overflow /* = 0 */)
  933. {
  934. FontDefinition tempDef;
  935. tempDef._shadow._shadowEnabled = false;
  936. tempDef._stroke._strokeEnabled = false;
  937. tempDef._fontName = fontName;
  938. tempDef._fontSize = fontSize;
  939. tempDef._dimensions = dimensions;
  940. tempDef._alignment = hAlignment;
  941. tempDef._vertAlignment = vAlignment;
  942. tempDef._fontFillColor = Color3B::WHITE;
  943. tempDef._enableWrap = enableWrap;
  944. tempDef._overflow = overflow;
  945. return initWithString(text, tempDef);
  946. }
  947. bool Texture2D::initWithString(const char *text, const FontDefinition& textDefinition)
  948. {
  949. if(!text || 0 == strlen(text))
  950. {
  951. return false;
  952. }
  953. #if CC_ENABLE_CACHE_TEXTURE_DATA
  954. // cache the texture data
  955. VolatileTextureMgr::addStringTexture(this, text, textDefinition);
  956. #endif
  957. bool ret = false;
  958. Device::TextAlign align;
  959. if (TextVAlignment::TOP == textDefinition._vertAlignment)
  960. {
  961. align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::TOP
  962. : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::TOP_LEFT : Device::TextAlign::TOP_RIGHT;
  963. }
  964. else if (TextVAlignment::CENTER == textDefinition._vertAlignment)
  965. {
  966. align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::CENTER
  967. : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::LEFT : Device::TextAlign::RIGHT;
  968. }
  969. else if (TextVAlignment::BOTTOM == textDefinition._vertAlignment)
  970. {
  971. align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::BOTTOM
  972. : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::BOTTOM_LEFT : Device::TextAlign::BOTTOM_RIGHT;
  973. }
  974. else
  975. {
  976. CCASSERT(false, "Not supported alignment format!");
  977. return false;
  978. }
  979. #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
  980. CCASSERT(textDefinition._stroke._strokeEnabled == false, "Currently stroke only supported on iOS and Android!");
  981. #endif
  982. PixelFormat pixelFormat = g_defaultAlphaPixelFormat;
  983. unsigned char* outTempData = nullptr;
  984. ssize_t outTempDataLen = 0;
  985. int imageWidth;
  986. int imageHeight;
  987. auto textDef = textDefinition;
  988. auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
  989. textDef._fontSize *= contentScaleFactor;
  990. textDef._dimensions.width *= contentScaleFactor;
  991. textDef._dimensions.height *= contentScaleFactor;
  992. textDef._stroke._strokeSize *= contentScaleFactor;
  993. textDef._shadow._shadowEnabled = false;
  994. bool hasPremultipliedAlpha;
  995. Data outData = Device::getTextureDataForText(text, textDef, align, imageWidth, imageHeight, hasPremultipliedAlpha);
  996. if(outData.isNull())
  997. {
  998. return false;
  999. }
  1000. Size imageSize = Size((float)imageWidth, (float)imageHeight);
  1001. pixelFormat = convertDataToFormat(outData.getBytes(), imageWidth*imageHeight*4, PixelFormat::RGBA8888, pixelFormat, &outTempData, &outTempDataLen);
  1002. ret = initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight, imageSize);
  1003. if (outTempData != nullptr && outTempData != outData.getBytes())
  1004. {
  1005. free(outTempData);
  1006. }
  1007. _hasPremultipliedAlpha = hasPremultipliedAlpha;
  1008. return ret;
  1009. }
  1010. // implementation Texture2D (Drawing)
  1011. void Texture2D::drawAtPoint(const Vec2& point)
  1012. {
  1013. GLfloat coordinates[] = {
  1014. 0.0f, _maxT,
  1015. _maxS,_maxT,
  1016. 0.0f, 0.0f,
  1017. _maxS,0.0f };
  1018. GLfloat width = (GLfloat)_pixelsWide * _maxS,
  1019. height = (GLfloat)_pixelsHigh * _maxT;
  1020. GLfloat vertices[] = {
  1021. point.x, point.y,
  1022. width + point.x, point.y,
  1023. point.x, height + point.y,
  1024. width + point.x, height + point.y };
  1025. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
  1026. _shaderProgram->use();
  1027. _shaderProgram->setUniformsForBuiltins();
  1028. GL::bindTexture2D( _name );
  1029. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1030. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
  1031. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1032. }
  1033. void Texture2D::drawInRect(const Rect& rect)
  1034. {
  1035. GLfloat coordinates[] = {
  1036. 0.0f, _maxT,
  1037. _maxS,_maxT,
  1038. 0.0f, 0.0f,
  1039. _maxS,0.0f };
  1040. GLfloat vertices[] = { rect.origin.x, rect.origin.y, /*0.0f,*/
  1041. rect.origin.x + rect.size.width, rect.origin.y, /*0.0f,*/
  1042. rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
  1043. rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
  1044. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
  1045. _shaderProgram->use();
  1046. _shaderProgram->setUniformsForBuiltins();
  1047. GL::bindTexture2D( _name );
  1048. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  1049. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
  1050. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  1051. }
  1052. void Texture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
  1053. {
  1054. Image::setPVRImagesHavePremultipliedAlpha(haveAlphaPremultiplied);
  1055. }
  1056. //
  1057. // Use to apply MIN/MAG filter
  1058. //
  1059. // implementation Texture2D (GLFilter)
  1060. void Texture2D::generateMipmap()
  1061. {
  1062. CCASSERT(_pixelsWide == ccNextPOT(_pixelsWide) && _pixelsHigh == ccNextPOT(_pixelsHigh), "Mipmap texture only works in POT textures");
  1063. GL::bindTexture2D( _name );
  1064. glGenerateMipmap(GL_TEXTURE_2D);
  1065. _hasMipmaps = true;
  1066. #if CC_ENABLE_CACHE_TEXTURE_DATA
  1067. VolatileTextureMgr::setHasMipmaps(this, _hasMipmaps);
  1068. #endif
  1069. }
  1070. bool Texture2D::hasMipmaps() const
  1071. {
  1072. return _hasMipmaps;
  1073. }
  1074. void Texture2D::setTexParameters(const TexParams &texParams)
  1075. {
  1076. CCASSERT((_pixelsWide == ccNextPOT(_pixelsWide) || texParams.wrapS == GL_CLAMP_TO_EDGE) &&
  1077. (_pixelsHigh == ccNextPOT(_pixelsHigh) || texParams.wrapT == GL_CLAMP_TO_EDGE),
  1078. "GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
  1079. GL::bindTexture2D( _name );
  1080. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texParams.minFilter );
  1081. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texParams.magFilter );
  1082. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texParams.wrapS );
  1083. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texParams.wrapT );
  1084. #if CC_ENABLE_CACHE_TEXTURE_DATA
  1085. VolatileTextureMgr::setTexParameters(this, texParams);
  1086. #endif
  1087. }
  1088. void Texture2D::setAliasTexParameters()
  1089. {
  1090. if (! _antialiasEnabled)
  1091. {
  1092. return;
  1093. }
  1094. _antialiasEnabled = false;
  1095. if (_name == 0)
  1096. {
  1097. return;
  1098. }
  1099. GL::bindTexture2D( _name );
  1100. if( ! _hasMipmaps )
  1101. {
  1102. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  1103. }
  1104. else
  1105. {
  1106. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST );
  1107. }
  1108. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  1109. #if CC_ENABLE_CACHE_TEXTURE_DATA
  1110. TexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE};
  1111. VolatileTextureMgr::setTexParameters(this, texParams);
  1112. #endif
  1113. }
  1114. void Texture2D::setAntiAliasTexParameters()
  1115. {
  1116. if ( _antialiasEnabled )
  1117. {
  1118. return;
  1119. }
  1120. _antialiasEnabled = true;
  1121. if (_name == 0)
  1122. {
  1123. return;
  1124. }
  1125. GL::bindTexture2D( _name );
  1126. if( ! _hasMipmaps )
  1127. {
  1128. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  1129. }
  1130. else
  1131. {
  1132. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
  1133. }
  1134. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  1135. #if CC_ENABLE_CACHE_TEXTURE_DATA
  1136. TexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE};
  1137. VolatileTextureMgr::setTexParameters(this, texParams);
  1138. #endif
  1139. }
  1140. const char* Texture2D::getStringForFormat() const
  1141. {
  1142. switch (_pixelFormat)
  1143. {
  1144. case Texture2D::PixelFormat::RGBA8888:
  1145. return "RGBA8888";
  1146. case Texture2D::PixelFormat::RGB888:
  1147. return "RGB888";
  1148. case Texture2D::PixelFormat::RGB565:
  1149. return "RGB565";
  1150. case Texture2D::PixelFormat::RGBA4444:
  1151. return "RGBA4444";
  1152. case Texture2D::PixelFormat::RGB5A1:
  1153. return "RGB5A1";
  1154. case Texture2D::PixelFormat::AI88:
  1155. return "AI88";
  1156. case Texture2D::PixelFormat::A8:
  1157. return "A8";
  1158. case Texture2D::PixelFormat::I8:
  1159. return "I8";
  1160. case Texture2D::PixelFormat::PVRTC4:
  1161. return "PVRTC4";
  1162. case Texture2D::PixelFormat::PVRTC2:
  1163. return "PVRTC2";
  1164. case Texture2D::PixelFormat::PVRTC2A:
  1165. return "PVRTC2A";
  1166. case Texture2D::PixelFormat::PVRTC4A:
  1167. return "PVRTC4A";
  1168. case Texture2D::PixelFormat::ETC:
  1169. return "ETC";
  1170. case Texture2D::PixelFormat::S3TC_DXT1:
  1171. return "S3TC_DXT1";
  1172. case Texture2D::PixelFormat::S3TC_DXT3:
  1173. return "S3TC_DXT3";
  1174. case Texture2D::PixelFormat::S3TC_DXT5:
  1175. return "S3TC_DXT5";
  1176. case Texture2D::PixelFormat::ATC_RGB:
  1177. return "ATC_RGB";
  1178. case Texture2D::PixelFormat::ATC_EXPLICIT_ALPHA:
  1179. return "ATC_EXPLICIT_ALPHA";
  1180. case Texture2D::PixelFormat::ATC_INTERPOLATED_ALPHA:
  1181. return "ATC_INTERPOLATED_ALPHA";
  1182. default:
  1183. CCASSERT(false , "unrecognized pixel format");
  1184. CCLOG("stringForFormat: %ld, cannot give useful result", (long)_pixelFormat);
  1185. break;
  1186. }
  1187. return nullptr;
  1188. }
  1189. //
  1190. // Texture options for images that contains alpha
  1191. //
  1192. // implementation Texture2D (PixelFormat)
  1193. void Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat format)
  1194. {
  1195. g_defaultAlphaPixelFormat = format;
  1196. }
  1197. Texture2D::PixelFormat Texture2D::getDefaultAlphaPixelFormat()
  1198. {
  1199. return g_defaultAlphaPixelFormat;
  1200. }
  1201. unsigned int Texture2D::getBitsPerPixelForFormat(Texture2D::PixelFormat format) const
  1202. {
  1203. if (format == PixelFormat::NONE || format == PixelFormat::DEFAULT)
  1204. {
  1205. return 0;
  1206. }
  1207. return _pixelFormatInfoTables.at(format).bpp;
  1208. }
  1209. unsigned int Texture2D::getBitsPerPixelForFormat() const
  1210. {
  1211. return this->getBitsPerPixelForFormat(_pixelFormat);
  1212. }
  1213. const Texture2D::PixelFormatInfoMap& Texture2D::getPixelFormatInfoMap()
  1214. {
  1215. return _pixelFormatInfoTables;
  1216. }
  1217. void Texture2D::addSpriteFrameCapInset(SpriteFrame* spritframe, const Rect& capInsets)
  1218. {
  1219. if(nullptr == _ninePatchInfo)
  1220. {
  1221. _ninePatchInfo = new (std::nothrow) NinePatchInfo;
  1222. }
  1223. if(nullptr == spritframe)
  1224. {
  1225. _ninePatchInfo->capInsetSize = capInsets;
  1226. }
  1227. else
  1228. {
  1229. _ninePatchInfo->capInsetMap[spritframe] = capInsets;
  1230. }
  1231. }
  1232. bool Texture2D::isContain9PatchInfo()const
  1233. {
  1234. return nullptr != _ninePatchInfo;
  1235. }
  1236. const Rect& Texture2D::getSpriteFrameCapInset( cocos2d::SpriteFrame *spriteFrame )const
  1237. {
  1238. CCASSERT(_ninePatchInfo != nullptr,
  1239. "Can't get the sprite frame capInset when the texture contains no 9-patch info.");
  1240. if(nullptr == spriteFrame)
  1241. {
  1242. return this->_ninePatchInfo->capInsetSize;
  1243. }
  1244. else
  1245. {
  1246. auto &capInsetMap = this->_ninePatchInfo->capInsetMap;
  1247. if(capInsetMap.find(spriteFrame) != capInsetMap.end())
  1248. {
  1249. return capInsetMap.at(spriteFrame);
  1250. }
  1251. else
  1252. {
  1253. return this->_ninePatchInfo->capInsetSize;
  1254. }
  1255. }
  1256. }
  1257. void Texture2D::removeSpriteFrameCapInset(SpriteFrame* spriteFrame)
  1258. {
  1259. if(nullptr != this->_ninePatchInfo)
  1260. {
  1261. auto capInsetMap = this->_ninePatchInfo->capInsetMap;
  1262. if(capInsetMap.find(spriteFrame) != capInsetMap.end())
  1263. {
  1264. capInsetMap.erase(spriteFrame);
  1265. }
  1266. }
  1267. }
  1268. /// halx99 spec, ANDROID ETC1 ALPHA supports.
  1269. void Texture2D::setAlphaTexture(Texture2D* alphaTexture)
  1270. {
  1271. if (alphaTexture != nullptr) {
  1272. this->_alphaTexture = alphaTexture;
  1273. this->_alphaTexture->retain();
  1274. this->_hasPremultipliedAlpha = true; // PremultipliedAlpha should be true.
  1275. }
  1276. }
  1277. Texture2D* Texture2D::getAlphaTexture() const
  1278. {
  1279. return _alphaTexture;
  1280. }
  1281. NS_CC_END