Core.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. if (!font_interface)
  99. {
  100. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  101. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  102. font_interface = default_font_interface.get();
  103. #else
  104. Log::Message(Log::LT_ERROR, "No font engine interface set!");
  105. return false;
  106. #endif
  107. }
  108. EventSpecificationInterface::Initialize();
  109. render_managers = MakeUnique<SmallUnorderedMap<RenderInterface*, UniquePtr<RenderManager>>>();
  110. if (render_interface)
  111. (*render_managers)[render_interface] = MakeUnique<RenderManager>(render_interface);
  112. font_interface->Initialize();
  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->Shutdown();
  143. render_managers.reset();
  144. initialised = false;
  145. font_interface = nullptr;
  146. render_interface = nullptr;
  147. file_interface = nullptr;
  148. system_interface = nullptr;
  149. default_font_interface.reset();
  150. default_file_interface.reset();
  151. ReleaseMemoryPools();
  152. }
  153. String GetVersion()
  154. {
  155. return RMLUI_VERSION;
  156. }
  157. void SetSystemInterface(SystemInterface* _system_interface)
  158. {
  159. system_interface = _system_interface;
  160. }
  161. SystemInterface* GetSystemInterface()
  162. {
  163. return system_interface;
  164. }
  165. void SetRenderInterface(RenderInterface* _render_interface)
  166. {
  167. render_interface = _render_interface;
  168. }
  169. RenderInterface* GetRenderInterface()
  170. {
  171. return render_interface;
  172. }
  173. void SetFileInterface(FileInterface* _file_interface)
  174. {
  175. file_interface = _file_interface;
  176. }
  177. FileInterface* GetFileInterface()
  178. {
  179. return file_interface;
  180. }
  181. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  182. {
  183. font_interface = _font_interface;
  184. }
  185. FontEngineInterface* GetFontEngineInterface()
  186. {
  187. return font_interface;
  188. }
  189. Context* CreateContext(const String& name, const Vector2i dimensions, RenderInterface* render_interface_for_context)
  190. {
  191. if (!initialised)
  192. return nullptr;
  193. if (!render_interface_for_context)
  194. render_interface_for_context = render_interface;
  195. if (!render_interface_for_context)
  196. {
  197. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.",
  198. name.c_str());
  199. return nullptr;
  200. }
  201. if (GetContext(name))
  202. {
  203. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  204. return nullptr;
  205. }
  206. // Each unique render interface gets its own render manager.
  207. auto& render_manager = (*render_managers)[render_interface_for_context];
  208. if (!render_manager)
  209. render_manager = MakeUnique<RenderManager>(render_interface_for_context);
  210. ContextPtr new_context = Factory::InstanceContext(name, render_manager.get());
  211. if (!new_context)
  212. {
  213. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  214. return nullptr;
  215. }
  216. new_context->SetDimensions(dimensions);
  217. Context* new_context_raw = new_context.get();
  218. contexts[name] = std::move(new_context);
  219. PluginRegistry::NotifyContextCreate(new_context_raw);
  220. return new_context_raw;
  221. }
  222. bool RemoveContext(const String& name)
  223. {
  224. auto it = contexts.find(name);
  225. if (it != contexts.end())
  226. {
  227. contexts.erase(it);
  228. return true;
  229. }
  230. return false;
  231. }
  232. Context* GetContext(const String& name)
  233. {
  234. ContextMap::iterator i = contexts.find(name);
  235. if (i == contexts.end())
  236. return nullptr;
  237. return i->second.get();
  238. }
  239. Context* GetContext(int index)
  240. {
  241. ContextMap::iterator i = contexts.begin();
  242. int count = 0;
  243. if (index < 0 || index >= GetNumContexts())
  244. return nullptr;
  245. while (count < index)
  246. {
  247. ++i;
  248. ++count;
  249. }
  250. if (i == contexts.end())
  251. return nullptr;
  252. return i->second.get();
  253. }
  254. int GetNumContexts()
  255. {
  256. return (int)contexts.size();
  257. }
  258. bool LoadFontFace(const String& file_path, bool fallback_face, Style::FontWeight weight)
  259. {
  260. return font_interface->LoadFontFace(file_path, fallback_face, weight);
  261. }
  262. bool LoadFontFace(Span<const byte> data, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  263. {
  264. return font_interface->LoadFontFace(data, font_family, style, weight, fallback_face);
  265. }
  266. void RegisterPlugin(Plugin* plugin)
  267. {
  268. if (initialised)
  269. plugin->OnInitialise();
  270. PluginRegistry::RegisterPlugin(plugin);
  271. }
  272. void UnregisterPlugin(Plugin* plugin)
  273. {
  274. PluginRegistry::UnregisterPlugin(plugin);
  275. if (initialised)
  276. plugin->OnShutdown();
  277. }
  278. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  279. {
  280. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  281. }
  282. StringList GetTextureSourceList()
  283. {
  284. StringList result;
  285. if (!render_managers)
  286. return result;
  287. for (const auto& render_manager : *render_managers)
  288. {
  289. RenderManagerAccess::GetTextureSourceList(render_manager.second.get(), result);
  290. }
  291. return result;
  292. }
  293. void ReleaseTextures(RenderInterface* match_render_interface)
  294. {
  295. if (!render_managers)
  296. return;
  297. for (auto& render_manager : *render_managers)
  298. {
  299. if (!match_render_interface || render_manager.first == match_render_interface)
  300. RenderManagerAccess::ReleaseAllTextures(render_manager.second.get());
  301. }
  302. }
  303. bool ReleaseTexture(const String& source, RenderInterface* match_render_interface)
  304. {
  305. if (!render_managers)
  306. return false;
  307. bool result = false;
  308. for (auto& render_manager : *render_managers)
  309. {
  310. if (!match_render_interface || render_manager.first == match_render_interface)
  311. {
  312. if (RenderManagerAccess::ReleaseTexture(render_manager.second.get(), source))
  313. result = true;
  314. }
  315. }
  316. return result;
  317. }
  318. void ReleaseCompiledGeometry(RenderInterface* match_render_interface)
  319. {
  320. if (!render_managers)
  321. return;
  322. for (auto& render_manager : *render_managers)
  323. {
  324. if (!match_render_interface || render_manager.first == match_render_interface)
  325. RenderManagerAccess::ReleaseAllCompiledGeometry(render_manager.second.get());
  326. }
  327. }
  328. void ReleaseFontResources()
  329. {
  330. if (font_interface)
  331. {
  332. for (const auto& name_context : contexts)
  333. name_context.second->GetRootElement()->DirtyFontFaceRecursive();
  334. font_interface->ReleaseFontResources();
  335. for (const auto& name_context : contexts)
  336. name_context.second->Update();
  337. }
  338. }
  339. void ReleaseMemoryPools()
  340. {
  341. if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
  342. {
  343. delete observerPtrBlockPool;
  344. observerPtrBlockPool = nullptr;
  345. }
  346. }
  347. // Functions that need to be accessible within the Core library, but not publicly.
  348. namespace CoreInternal {
  349. bool HasRenderManager(RenderInterface* match_render_interface)
  350. {
  351. return render_managers && render_managers->find(match_render_interface) != render_managers->end();
  352. }
  353. } // namespace CoreInternal
  354. } // namespace Rml