Core.cpp 11 KB

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