Core.cpp 13 KB

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