CCConfiguration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /****************************************************************************
  2. Copyright (c) 2010 Ricardo Quesada
  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. #include "base/CCConfiguration.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "base/CCEventCustom.h"
  25. #include "base/CCDirector.h"
  26. #include "base/CCEventDispatcher.h"
  27. NS_CC_BEGIN
  28. extern const char* cocos2dVersion();
  29. Configuration* Configuration::s_sharedConfiguration = nullptr;
  30. const char* Configuration::CONFIG_FILE_LOADED = "config_file_loaded";
  31. Configuration::Configuration()
  32. : _maxTextureSize(0)
  33. , _maxModelviewStackDepth(0)
  34. , _supportsPVRTC(false)
  35. , _supportsETC1(false)
  36. , _supportsS3TC(false)
  37. , _supportsATITC(false)
  38. , _supportsNPOT(false)
  39. , _supportsBGRA8888(false)
  40. , _supportsDiscardFramebuffer(false)
  41. , _supportsShareableVAO(false)
  42. , _supportsOESDepth24(false)
  43. , _supportsOESPackedDepthStencil(false)
  44. , _supportsOESMapBuffer(false)
  45. , _maxSamplesAllowed(0)
  46. , _maxTextureUnits(0)
  47. , _glExtensions(nullptr)
  48. , _maxDirLightInShader(1)
  49. , _maxPointLightInShader(1)
  50. , _maxSpotLightInShader(1)
  51. , _animate3DQuality(Animate3DQuality::QUALITY_LOW)
  52. {
  53. _loadedEvent = new (std::nothrow) EventCustom(CONFIG_FILE_LOADED);
  54. }
  55. bool Configuration::init()
  56. {
  57. _valueDict["cocos2d.x.version"] = Value(cocos2dVersion());
  58. #if CC_ENABLE_PROFILERS
  59. _valueDict["cocos2d.x.compiled_with_profiler"] = Value(true);
  60. #else
  61. _valueDict["cocos2d.x.compiled_with_profiler"] = Value(false);
  62. #endif
  63. #if CC_ENABLE_GL_STATE_CACHE == 0
  64. _valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(false);
  65. #else
  66. _valueDict["cocos2d.x.compiled_with_gl_state_cache"] = Value(true);
  67. #endif
  68. #if COCOS2D_DEBUG
  69. _valueDict["cocos2d.x.build_type"] = Value("DEBUG");
  70. #else
  71. _valueDict["cocos2d.x.build_type"] = Value("RELEASE");
  72. #endif
  73. return true;
  74. }
  75. Configuration::~Configuration()
  76. {
  77. CC_SAFE_DELETE(_loadedEvent);
  78. }
  79. std::string Configuration::getInfo() const
  80. {
  81. // And Dump some warnings as well
  82. #if CC_ENABLE_PROFILERS
  83. CCLOG("cocos2d: **** WARNING **** CC_ENABLE_PROFILERS is defined. Disable it when you finish profiling (from ccConfig.h)\n");
  84. #endif
  85. #if CC_ENABLE_GL_STATE_CACHE == 0
  86. CCLOG("cocos2d: **** WARNING **** CC_ENABLE_GL_STATE_CACHE is disabled. To improve performance, enable it (from ccConfig.h)\n");
  87. #endif
  88. // Dump
  89. Value forDump = Value(_valueDict);
  90. return forDump.getDescription();
  91. }
  92. void Configuration::gatherGPUInfo()
  93. {
  94. _valueDict["gl.vendor"] = Value((const char*)glGetString(GL_VENDOR));
  95. _valueDict["gl.renderer"] = Value((const char*)glGetString(GL_RENDERER));
  96. _valueDict["gl.version"] = Value((const char*)glGetString(GL_VERSION));
  97. _glExtensions = (char *)glGetString(GL_EXTENSIONS);
  98. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_maxTextureSize);
  99. _valueDict["gl.max_texture_size"] = Value((int)_maxTextureSize);
  100. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_maxTextureUnits);
  101. _valueDict["gl.max_texture_units"] = Value((int)_maxTextureUnits);
  102. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  103. glGetIntegerv(GL_MAX_SAMPLES_APPLE, &_maxSamplesAllowed);
  104. _valueDict["gl.max_samples_allowed"] = Value((int)_maxSamplesAllowed);
  105. #endif
  106. _supportsETC1 = checkForGLExtension("GL_OES_compressed_ETC1_RGB8_texture");
  107. _valueDict["gl.supports_ETC1"] = Value(_supportsETC1);
  108. _supportsS3TC = checkForGLExtension("GL_EXT_texture_compression_s3tc");
  109. _valueDict["gl.supports_S3TC"] = Value(_supportsS3TC);
  110. _supportsATITC = checkForGLExtension("GL_AMD_compressed_ATC_texture");
  111. _valueDict["gl.supports_ATITC"] = Value(_supportsATITC);
  112. _supportsPVRTC = checkForGLExtension("GL_IMG_texture_compression_pvrtc");
  113. _valueDict["gl.supports_PVRTC"] = Value(_supportsPVRTC);
  114. _supportsNPOT = true;
  115. _valueDict["gl.supports_NPOT"] = Value(_supportsNPOT);
  116. _supportsBGRA8888 = checkForGLExtension("GL_IMG_texture_format_BGRA8888");
  117. _valueDict["gl.supports_BGRA8888"] = Value(_supportsBGRA8888);
  118. _supportsDiscardFramebuffer = checkForGLExtension("GL_EXT_discard_framebuffer");
  119. _valueDict["gl.supports_discard_framebuffer"] = Value(_supportsDiscardFramebuffer);
  120. #ifdef CC_PLATFORM_PC
  121. _supportsShareableVAO = checkForGLExtension("vertex_array_object");
  122. #else
  123. _supportsShareableVAO = checkForGLExtension("GL_OES_vertex_array_object");
  124. #endif
  125. _valueDict["gl.supports_vertex_array_object"] = Value(_supportsShareableVAO);
  126. _supportsOESMapBuffer = checkForGLExtension("GL_OES_mapbuffer");
  127. _valueDict["gl.supports_OES_map_buffer"] = Value(_supportsOESMapBuffer);
  128. _supportsOESDepth24 = checkForGLExtension("GL_OES_depth24");
  129. _valueDict["gl.supports_OES_depth24"] = Value(_supportsOESDepth24);
  130. _supportsOESPackedDepthStencil = checkForGLExtension("GL_OES_packed_depth_stencil");
  131. _valueDict["gl.supports_OES_packed_depth_stencil"] = Value(_supportsOESPackedDepthStencil);
  132. CHECK_GL_ERROR_DEBUG();
  133. }
  134. Configuration* Configuration::getInstance()
  135. {
  136. if (! s_sharedConfiguration)
  137. {
  138. s_sharedConfiguration = new (std::nothrow) Configuration();
  139. s_sharedConfiguration->init();
  140. }
  141. return s_sharedConfiguration;
  142. }
  143. void Configuration::destroyInstance()
  144. {
  145. CC_SAFE_RELEASE_NULL(s_sharedConfiguration);
  146. }
  147. // FIXME: deprecated
  148. Configuration* Configuration::sharedConfiguration()
  149. {
  150. return Configuration::getInstance();
  151. }
  152. // FIXME: deprecated
  153. void Configuration::purgeConfiguration()
  154. {
  155. Configuration::destroyInstance();
  156. }
  157. bool Configuration::checkForGLExtension(const std::string &searchName) const
  158. {
  159. return (_glExtensions && strstr(_glExtensions, searchName.c_str() ) ) ? true : false;
  160. }
  161. //
  162. // getters for specific variables.
  163. // Maintained for backward compatibility reasons only.
  164. //
  165. int Configuration::getMaxTextureSize() const
  166. {
  167. return _maxTextureSize;
  168. }
  169. int Configuration::getMaxModelviewStackDepth() const
  170. {
  171. return _maxModelviewStackDepth;
  172. }
  173. int Configuration::getMaxTextureUnits() const
  174. {
  175. return _maxTextureUnits;
  176. }
  177. bool Configuration::supportsNPOT() const
  178. {
  179. return _supportsNPOT;
  180. }
  181. bool Configuration::supportsPVRTC() const
  182. {
  183. return _supportsPVRTC;
  184. }
  185. bool Configuration::supportsETC() const
  186. {
  187. //GL_ETC1_RGB8_OES is not defined in old opengl version
  188. #ifdef GL_ETC1_RGB8_OES
  189. return _supportsETC1;
  190. #else
  191. return false;
  192. #endif
  193. }
  194. bool Configuration::supportsS3TC() const
  195. {
  196. #ifdef GL_EXT_texture_compression_s3tc
  197. return _supportsS3TC;
  198. #else
  199. return false;
  200. #endif
  201. }
  202. bool Configuration::supportsATITC() const
  203. {
  204. return _supportsATITC;
  205. }
  206. bool Configuration::supportsBGRA8888() const
  207. {
  208. return _supportsBGRA8888;
  209. }
  210. bool Configuration::supportsDiscardFramebuffer() const
  211. {
  212. return _supportsDiscardFramebuffer;
  213. }
  214. bool Configuration::supportsShareableVAO() const
  215. {
  216. #if CC_TEXTURE_ATLAS_USE_VAO
  217. return _supportsShareableVAO;
  218. #else
  219. return false;
  220. #endif
  221. }
  222. bool Configuration::supportsMapBuffer() const
  223. {
  224. // Fixes Github issue #16123
  225. //
  226. // XXX: Fixme. Should check GL ES and not iOS or Android
  227. // For example, linux could be compiled with GL ES. Or perhaps in the future Android will
  228. // support OpenGL. This is because glMapBufferOES() is an extension of OpenGL ES. And glMapBuffer()
  229. // is always implemented in OpenGL.
  230. // XXX: Warning. On iOS this is always `true`. Avoiding the comparison.
  231. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  232. return _supportsOESMapBuffer;
  233. #else
  234. return true;
  235. #endif
  236. }
  237. bool Configuration::supportsOESDepth24() const
  238. {
  239. return _supportsOESDepth24;
  240. }
  241. bool Configuration::supportsOESPackedDepthStencil() const
  242. {
  243. return _supportsOESPackedDepthStencil;
  244. }
  245. int Configuration::getMaxSupportDirLightInShader() const
  246. {
  247. return _maxDirLightInShader;
  248. }
  249. int Configuration::getMaxSupportPointLightInShader() const
  250. {
  251. return _maxPointLightInShader;
  252. }
  253. int Configuration::getMaxSupportSpotLightInShader() const
  254. {
  255. return _maxSpotLightInShader;
  256. }
  257. Animate3DQuality Configuration::getAnimate3DQuality() const
  258. {
  259. return _animate3DQuality;
  260. }
  261. //
  262. // generic getters for properties
  263. //
  264. const Value& Configuration::getValue(const std::string& key, const Value& defaultValue) const
  265. {
  266. auto iter = _valueDict.find(key);
  267. if (iter != _valueDict.cend())
  268. return _valueDict.at(key);
  269. return defaultValue;
  270. }
  271. void Configuration::setValue(const std::string& key, const Value& value)
  272. {
  273. _valueDict[key] = value;
  274. }
  275. //
  276. // load file
  277. //
  278. void Configuration::loadConfigFile(const std::string& filename)
  279. {
  280. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(filename);
  281. CCASSERT(!dict.empty(), "cannot create dictionary");
  282. // search for metadata
  283. bool validMetadata = false;
  284. auto metadataIter = dict.find("metadata");
  285. if (metadataIter != dict.cend() && metadataIter->second.getType() == Value::Type::MAP)
  286. {
  287. const auto& metadata = metadataIter->second.asValueMap();
  288. auto formatIter = metadata.find("format");
  289. if (formatIter != metadata.cend())
  290. {
  291. int format = formatIter->second.asInt();
  292. // Support format: 1
  293. if (format == 1)
  294. {
  295. validMetadata = true;
  296. }
  297. }
  298. }
  299. if (! validMetadata)
  300. {
  301. CCLOG("Invalid config format for file: %s", filename.c_str());
  302. return;
  303. }
  304. auto dataIter = dict.find("data");
  305. if (dataIter == dict.cend() || dataIter->second.getType() != Value::Type::MAP)
  306. {
  307. CCLOG("Expected 'data' dict, but not found. Config file: %s", filename.c_str());
  308. return;
  309. }
  310. // Add all keys in the existing dictionary
  311. const auto& dataMap = dataIter->second.asValueMap();
  312. for (const auto& dataMapIter : dataMap)
  313. {
  314. if (_valueDict.find(dataMapIter.first) == _valueDict.cend())
  315. _valueDict[dataMapIter.first] = dataMapIter.second;
  316. else
  317. CCLOG("Key already present. Ignoring '%s'",dataMapIter.first.c_str());
  318. }
  319. //light info
  320. std::string name = "cocos2d.x.3d.max_dir_light_in_shader";
  321. if (_valueDict.find(name) != _valueDict.end())
  322. _maxDirLightInShader = _valueDict[name].asInt();
  323. else
  324. _valueDict[name] = Value(_maxDirLightInShader);
  325. name = "cocos2d.x.3d.max_point_light_in_shader";
  326. if (_valueDict.find(name) != _valueDict.end())
  327. _maxPointLightInShader = _valueDict[name].asInt();
  328. else
  329. _valueDict[name] = Value(_maxPointLightInShader);
  330. name = "cocos2d.x.3d.max_spot_light_in_shader";
  331. if (_valueDict.find(name) != _valueDict.end())
  332. _maxSpotLightInShader = _valueDict[name].asInt();
  333. else
  334. _valueDict[name] = Value(_maxSpotLightInShader);
  335. name = "cocos2d.x.3d.animate_quality";
  336. if (_valueDict.find(name) != _valueDict.end())
  337. _animate3DQuality = (Animate3DQuality)_valueDict[name].asInt();
  338. else
  339. _valueDict[name] = Value((int)_animate3DQuality);
  340. Director::getInstance()->getEventDispatcher()->dispatchEvent(_loadedEvent);
  341. }
  342. NS_CC_END