Core.cpp 11 KB

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