Core.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Core.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/Factory.h"
  32. #include "../../Include/RmlUi/Core/FileInterface.h"
  33. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  34. #include "../../Include/RmlUi/Core/Plugin.h"
  35. #include "../../Include/RmlUi/Core/RenderInterface.h"
  36. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  37. #include "../../Include/RmlUi/Core/SystemInterface.h"
  38. #include "../../Include/RmlUi/Core/Types.h"
  39. #include "EventSpecification.h"
  40. #include "FileInterfaceDefault.h"
  41. #include "GeometryDatabase.h"
  42. #include "PluginRegistry.h"
  43. #include "StyleSheetFactory.h"
  44. #include "StyleSheetParser.h"
  45. #include "TemplateCache.h"
  46. #include "TextureDatabase.h"
  47. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  48. #include "FontEngineDefault/FontEngineInterfaceDefault.h"
  49. #endif
  50. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  51. #include "../Lottie/LottiePlugin.h"
  52. #endif
  53. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  54. #include "../SVG/SVGPlugin.h"
  55. #endif
  56. #include "Pool.h"
  57. namespace Rml {
  58. // RmlUi's renderer interface.
  59. static RenderInterface* render_interface = nullptr;
  60. /// RmlUi's system interface.
  61. static SystemInterface* system_interface = nullptr;
  62. // RmlUi's file I/O interface.
  63. static FileInterface* file_interface = nullptr;
  64. // RmlUi's font engine interface.
  65. static FontEngineInterface* font_interface = nullptr;
  66. // Default interfaces should be created and destroyed on Initialise and Shutdown, respectively.
  67. static UniquePtr<FileInterface> default_file_interface;
  68. static UniquePtr<FontEngineInterface> default_font_interface;
  69. static bool initialised = false;
  70. using ContextMap = UnorderedMap<String, ContextPtr>;
  71. static ContextMap contexts;
  72. // The ObserverPtrBlock pool
  73. extern Pool<ObserverPtrBlock>* observerPtrBlockPool;
  74. #ifndef RMLUI_VERSION
  75. #define RMLUI_VERSION "custom"
  76. #endif
  77. bool Initialise()
  78. {
  79. RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");
  80. Log::Initialise();
  81. // Check for valid interfaces, or install default interfaces as appropriate.
  82. if (!system_interface)
  83. {
  84. Log::Message(Log::LT_ERROR, "No system interface set!");
  85. return false;
  86. }
  87. if (!render_interface)
  88. {
  89. Log::Message(Log::LT_ERROR, "No render interface set!");
  90. return false;
  91. }
  92. if (!file_interface)
  93. {
  94. #ifndef RMLUI_NO_FILE_INTERFACE_DEFAULT
  95. default_file_interface = MakeUnique<FileInterfaceDefault>();
  96. file_interface = default_file_interface.get();
  97. #else
  98. Log::Message(Log::LT_ERROR, "No file interface set!");
  99. return false;
  100. #endif
  101. }
  102. if (!font_interface)
  103. {
  104. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  105. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  106. font_interface = default_font_interface.get();
  107. #else
  108. Log::Message(Log::LT_ERROR, "No font engine interface set!");
  109. return false;
  110. #endif
  111. }
  112. EventSpecificationInterface::Initialize();
  113. TextureDatabase::Initialise();
  114. font_interface->Initialize();
  115. StyleSheetSpecification::Initialise();
  116. StyleSheetParser::Initialise();
  117. StyleSheetFactory::Initialise();
  118. TemplateCache::Initialise();
  119. Factory::Initialise();
  120. // Initialise plugins integrated with Core.
  121. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  122. Lottie::Initialise();
  123. #endif
  124. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  125. SVG::Initialise();
  126. #endif
  127. // Notify all plugins we're starting up.
  128. PluginRegistry::NotifyInitialise();
  129. initialised = true;
  130. return true;
  131. }
  132. void Shutdown()
  133. {
  134. RMLUI_ASSERTMSG(initialised, "Rml::Shutdown() called, but RmlUi is not initialised!");
  135. // Clear out all contexts, which should also clean up all attached elements.
  136. contexts.clear();
  137. // Notify all plugins we're being shutdown.
  138. PluginRegistry::NotifyShutdown();
  139. Factory::Shutdown();
  140. TemplateCache::Shutdown();
  141. StyleSheetFactory::Shutdown();
  142. StyleSheetParser::Shutdown();
  143. StyleSheetSpecification::Shutdown();
  144. font_interface->Shutdown();
  145. TextureDatabase::Shutdown();
  146. initialised = false;
  147. font_interface = nullptr;
  148. render_interface = nullptr;
  149. file_interface = nullptr;
  150. system_interface = nullptr;
  151. default_font_interface.reset();
  152. default_file_interface.reset();
  153. Log::Shutdown();
  154. // Release any memory pools
  155. ReleaseMemoryPools();
  156. }
  157. String GetVersion()
  158. {
  159. return RMLUI_VERSION;
  160. }
  161. void SetSystemInterface(SystemInterface* _system_interface)
  162. {
  163. system_interface = _system_interface;
  164. }
  165. SystemInterface* GetSystemInterface()
  166. {
  167. return system_interface;
  168. }
  169. void SetRenderInterface(RenderInterface* _render_interface)
  170. {
  171. if (initialised)
  172. {
  173. Log::Message(Log::LT_ERROR, "The render interface is not allowed to be set or changed after RmlUi has been initialised.");
  174. return;
  175. }
  176. render_interface = _render_interface;
  177. }
  178. RenderInterface* GetRenderInterface()
  179. {
  180. return render_interface;
  181. }
  182. void SetFileInterface(FileInterface* _file_interface)
  183. {
  184. file_interface = _file_interface;
  185. }
  186. FileInterface* GetFileInterface()
  187. {
  188. return file_interface;
  189. }
  190. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  191. {
  192. font_interface = _font_interface;
  193. }
  194. FontEngineInterface* GetFontEngineInterface()
  195. {
  196. return font_interface;
  197. }
  198. Context* CreateContext(const String& name, const Vector2i dimensions)
  199. {
  200. if (!initialised)
  201. return nullptr;
  202. if (GetContext(name))
  203. {
  204. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  205. return nullptr;
  206. }
  207. ContextPtr new_context = Factory::InstanceContext(name);
  208. if (!new_context)
  209. {
  210. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  211. return nullptr;
  212. }
  213. new_context->SetDimensions(dimensions);
  214. Context* new_context_raw = new_context.get();
  215. contexts[name] = std::move(new_context);
  216. PluginRegistry::NotifyContextCreate(new_context_raw);
  217. return new_context_raw;
  218. }
  219. bool RemoveContext(const String& name)
  220. {
  221. auto it = contexts.find(name);
  222. if (it != contexts.end())
  223. {
  224. contexts.erase(it);
  225. return true;
  226. }
  227. return false;
  228. }
  229. Context* GetContext(const String& name)
  230. {
  231. ContextMap::iterator i = contexts.find(name);
  232. if (i == contexts.end())
  233. return nullptr;
  234. return i->second.get();
  235. }
  236. Context* GetContext(int index)
  237. {
  238. ContextMap::iterator i = contexts.begin();
  239. int count = 0;
  240. if (index < 0 || index >= GetNumContexts())
  241. return nullptr;
  242. while (count < index)
  243. {
  244. ++i;
  245. ++count;
  246. }
  247. if (i == contexts.end())
  248. return nullptr;
  249. return i->second.get();
  250. }
  251. int GetNumContexts()
  252. {
  253. return (int)contexts.size();
  254. }
  255. bool LoadFontFace(const String& file_path, bool fallback_face, Style::FontWeight weight)
  256. {
  257. return font_interface->LoadFontFace(file_path, fallback_face, weight);
  258. }
  259. bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  260. {
  261. return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
  262. }
  263. void RegisterPlugin(Plugin* plugin)
  264. {
  265. if (initialised)
  266. plugin->OnInitialise();
  267. PluginRegistry::RegisterPlugin(plugin);
  268. }
  269. void UnregisterPlugin(Plugin* plugin)
  270. {
  271. PluginRegistry::UnregisterPlugin(plugin);
  272. if (initialised)
  273. plugin->OnShutdown();
  274. }
  275. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  276. {
  277. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  278. }
  279. StringList GetTextureSourceList()
  280. {
  281. return TextureDatabase::GetSourceList();
  282. }
  283. void ReleaseTextures()
  284. {
  285. TextureDatabase::ReleaseTextures();
  286. }
  287. bool ReleaseTexture(const String& source)
  288. {
  289. return TextureDatabase::ReleaseTexture(source);
  290. }
  291. void ReleaseCompiledGeometry()
  292. {
  293. return GeometryDatabase::ReleaseAll();
  294. }
  295. void ReleaseMemoryPools()
  296. {
  297. if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
  298. {
  299. delete observerPtrBlockPool;
  300. observerPtrBlockPool = nullptr;
  301. }
  302. }
  303. void ReleaseFontResources()
  304. {
  305. if (font_interface)
  306. {
  307. for (const auto& name_context : contexts)
  308. name_context.second->GetRootElement()->DirtyFontFaceRecursive();
  309. font_interface->ReleaseFontResources();
  310. for (const auto& name_context : contexts)
  311. name_context.second->Update();
  312. }
  313. }
  314. } // namespace Rml