Core.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/RenderManager.h"
  37. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  38. #include "../../Include/RmlUi/Core/SystemInterface.h"
  39. #include "../../Include/RmlUi/Core/Types.h"
  40. #include "EventSpecification.h"
  41. #include "FileInterfaceDefault.h"
  42. #include "PluginRegistry.h"
  43. #include "RenderManagerAccess.h"
  44. #include "StyleSheetFactory.h"
  45. #include "StyleSheetParser.h"
  46. #include "TemplateCache.h"
  47. #include "TextureDatabase.h"
  48. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  49. #include "FontEngineDefault/FontEngineInterfaceDefault.h"
  50. #endif
  51. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  52. #include "../Lottie/LottiePlugin.h"
  53. #endif
  54. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  55. #include "../SVG/SVGPlugin.h"
  56. #endif
  57. #include "Pool.h"
  58. namespace Rml {
  59. // RmlUi's renderer interface.
  60. static RenderInterface* render_interface = nullptr;
  61. /// RmlUi's system interface.
  62. static SystemInterface* system_interface = nullptr;
  63. // RmlUi's file I/O interface.
  64. static FileInterface* file_interface = nullptr;
  65. // RmlUi's font engine interface.
  66. static FontEngineInterface* font_interface = nullptr;
  67. // Default interfaces should be created and destroyed on Initialise and Shutdown, respectively.
  68. static UniquePtr<FileInterface> default_file_interface;
  69. static UniquePtr<FontEngineInterface> default_font_interface;
  70. static UniquePtr<SmallUnorderedMap<RenderInterface*, UniquePtr<RenderManager>>> render_managers;
  71. static bool initialised = false;
  72. using ContextMap = UnorderedMap<String, ContextPtr>;
  73. static ContextMap contexts;
  74. // The ObserverPtrBlock pool
  75. extern Pool<ObserverPtrBlock>* observerPtrBlockPool;
  76. #ifndef RMLUI_VERSION
  77. #define RMLUI_VERSION "custom"
  78. #endif
  79. bool Initialise()
  80. {
  81. RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");
  82. Log::Initialise();
  83. // Check for valid interfaces, or install default interfaces as appropriate.
  84. if (!system_interface)
  85. {
  86. Log::Message(Log::LT_ERROR, "No system interface set!");
  87. return false;
  88. }
  89. if (!file_interface)
  90. {
  91. #ifndef RMLUI_NO_FILE_INTERFACE_DEFAULT
  92. default_file_interface = MakeUnique<FileInterfaceDefault>();
  93. file_interface = default_file_interface.get();
  94. #else
  95. Log::Message(Log::LT_ERROR, "No file interface set!");
  96. return false;
  97. #endif
  98. }
  99. EventSpecificationInterface::Initialize();
  100. render_managers = MakeUnique<SmallUnorderedMap<RenderInterface*, UniquePtr<RenderManager>>>();
  101. if (render_interface)
  102. (*render_managers)[render_interface] = MakeUnique<RenderManager>(render_interface);
  103. if (!font_interface)
  104. {
  105. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  106. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  107. font_interface = default_font_interface.get();
  108. #else
  109. Log::Message(Log::LT_ERROR, "No font engine interface set!");
  110. return false;
  111. #endif
  112. }
  113. StyleSheetSpecification::Initialise();
  114. StyleSheetParser::Initialise();
  115. StyleSheetFactory::Initialise();
  116. TemplateCache::Initialise();
  117. Factory::Initialise();
  118. // Initialise plugins integrated with Core.
  119. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  120. Lottie::Initialise();
  121. #endif
  122. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  123. SVG::Initialise();
  124. #endif
  125. // Notify all plugins we're starting up.
  126. PluginRegistry::NotifyInitialise();
  127. initialised = true;
  128. return true;
  129. }
  130. void Shutdown()
  131. {
  132. RMLUI_ASSERTMSG(initialised, "Rml::Shutdown() called, but RmlUi is not initialised!");
  133. // Clear out all contexts, which should also clean up all attached elements.
  134. contexts.clear();
  135. // Notify all plugins we're being shutdown.
  136. PluginRegistry::NotifyShutdown();
  137. Factory::Shutdown();
  138. TemplateCache::Shutdown();
  139. StyleSheetFactory::Shutdown();
  140. StyleSheetParser::Shutdown();
  141. StyleSheetSpecification::Shutdown();
  142. font_interface = nullptr;
  143. default_font_interface.reset();
  144. render_managers.reset();
  145. initialised = false;
  146. render_interface = nullptr;
  147. file_interface = nullptr;
  148. system_interface = nullptr;
  149. default_file_interface.reset();
  150. Log::Shutdown();
  151. // Release any memory pools
  152. ReleaseMemoryPools();
  153. }
  154. String GetVersion()
  155. {
  156. return RMLUI_VERSION;
  157. }
  158. void SetSystemInterface(SystemInterface* _system_interface)
  159. {
  160. system_interface = _system_interface;
  161. }
  162. SystemInterface* GetSystemInterface()
  163. {
  164. return system_interface;
  165. }
  166. void SetRenderInterface(RenderInterface* _render_interface)
  167. {
  168. render_interface = _render_interface;
  169. }
  170. RenderInterface* GetRenderInterface()
  171. {
  172. return render_interface;
  173. }
  174. void SetFileInterface(FileInterface* _file_interface)
  175. {
  176. file_interface = _file_interface;
  177. }
  178. FileInterface* GetFileInterface()
  179. {
  180. return file_interface;
  181. }
  182. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  183. {
  184. font_interface = _font_interface;
  185. }
  186. FontEngineInterface* GetFontEngineInterface()
  187. {
  188. return font_interface;
  189. }
  190. Context* CreateContext(const String& name, const Vector2i dimensions, RenderInterface* render_interface_for_context)
  191. {
  192. if (!initialised)
  193. return nullptr;
  194. if (!render_interface_for_context)
  195. render_interface_for_context = render_interface;
  196. if (!render_interface_for_context)
  197. {
  198. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.",
  199. name.c_str());
  200. return nullptr;
  201. }
  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. // Each unique render interface gets its own render manager.
  208. auto& render_manager = (*render_managers)[render_interface_for_context];
  209. if (!render_manager)
  210. render_manager = MakeUnique<RenderManager>(render_interface_for_context);
  211. ContextPtr new_context = Factory::InstanceContext(name, render_manager.get());
  212. if (!new_context)
  213. {
  214. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  215. return nullptr;
  216. }
  217. new_context->SetDimensions(dimensions);
  218. Context* new_context_raw = new_context.get();
  219. contexts[name] = std::move(new_context);
  220. PluginRegistry::NotifyContextCreate(new_context_raw);
  221. return new_context_raw;
  222. }
  223. bool RemoveContext(const String& name)
  224. {
  225. auto it = contexts.find(name);
  226. if (it != contexts.end())
  227. {
  228. contexts.erase(it);
  229. return true;
  230. }
  231. return false;
  232. }
  233. Context* GetContext(const String& name)
  234. {
  235. ContextMap::iterator i = contexts.find(name);
  236. if (i == contexts.end())
  237. return nullptr;
  238. return i->second.get();
  239. }
  240. Context* GetContext(int index)
  241. {
  242. ContextMap::iterator i = contexts.begin();
  243. int count = 0;
  244. if (index < 0 || index >= GetNumContexts())
  245. return nullptr;
  246. while (count < index)
  247. {
  248. ++i;
  249. ++count;
  250. }
  251. if (i == contexts.end())
  252. return nullptr;
  253. return i->second.get();
  254. }
  255. int GetNumContexts()
  256. {
  257. return (int)contexts.size();
  258. }
  259. bool LoadFontFace(const String& file_path, bool fallback_face, Style::FontWeight weight)
  260. {
  261. return font_interface->LoadFontFace(file_path, fallback_face, weight);
  262. }
  263. bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  264. {
  265. return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
  266. }
  267. void RegisterPlugin(Plugin* plugin)
  268. {
  269. if (initialised)
  270. plugin->OnInitialise();
  271. PluginRegistry::RegisterPlugin(plugin);
  272. }
  273. void UnregisterPlugin(Plugin* plugin)
  274. {
  275. PluginRegistry::UnregisterPlugin(plugin);
  276. if (initialised)
  277. plugin->OnShutdown();
  278. }
  279. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  280. {
  281. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  282. }
  283. StringList GetTextureSourceList()
  284. {
  285. StringList result;
  286. if (!render_managers)
  287. return result;
  288. for (const auto& render_manager : *render_managers)
  289. {
  290. RenderManagerAccess::GetTextureSourceList(render_manager.second.get(), result);
  291. }
  292. return result;
  293. }
  294. void ReleaseTextures(RenderInterface* match_render_interface)
  295. {
  296. if (!render_managers)
  297. return;
  298. for (auto& render_manager : *render_managers)
  299. {
  300. if (!match_render_interface || render_manager.first == match_render_interface)
  301. RenderManagerAccess::ReleaseAllTextures(render_manager.second.get());
  302. }
  303. }
  304. void ReleaseCompiledGeometry(RenderInterface* match_render_interface)
  305. {
  306. if (!render_managers)
  307. return;
  308. for (auto& render_manager : *render_managers)
  309. {
  310. if (!match_render_interface || render_manager.first == match_render_interface)
  311. RenderManagerAccess::ReleaseAllCompiledGeometry(render_manager.second.get());
  312. }
  313. }
  314. void ReleaseFontResources()
  315. {
  316. if (font_interface)
  317. {
  318. for (const auto& name_context : contexts)
  319. name_context.second->GetRootElement()->DirtyFontFaceRecursive();
  320. font_interface->ReleaseFontResources();
  321. for (const auto& name_context : contexts)
  322. name_context.second->Update();
  323. }
  324. }
  325. void ReleaseMemoryPools()
  326. {
  327. if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
  328. {
  329. delete observerPtrBlockPool;
  330. observerPtrBlockPool = nullptr;
  331. }
  332. }
  333. // Functions that need to be accessible within the Core library, but not publicly.
  334. namespace CoreInternal {
  335. bool HasRenderManager(RenderInterface* match_render_interface)
  336. {
  337. return render_managers && (*render_managers).find(match_render_interface) != render_managers->end();
  338. }
  339. } // namespace CoreInternal
  340. } // namespace Rml