Core.cpp 12 KB

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