Core.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/StyleSheetSpecification.h"
  37. #include "../../Include/RmlUi/Core/SystemInterface.h"
  38. #include "../../Include/RmlUi/Core/Types.h"
  39. #include "EventSpecification.h"
  40. #include "FileInterfaceDefault.h"
  41. #include "GeometryDatabase.h"
  42. #include "PluginRegistry.h"
  43. #include "StyleSheetFactory.h"
  44. #include "StyleSheetParser.h"
  45. #include "TemplateCache.h"
  46. #include "TextureDatabase.h"
  47. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  48. #include "FontEngineDefault/FontEngineInterfaceDefault.h"
  49. #endif
  50. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  51. #include "../Lottie/LottiePlugin.h"
  52. #endif
  53. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  54. #include "../SVG/SVGPlugin.h"
  55. #endif
  56. #include "Pool.h"
  57. namespace Rml {
  58. // RmlUi's renderer interface.
  59. static RenderInterface* render_interface = nullptr;
  60. /// RmlUi's system interface.
  61. static SystemInterface* system_interface = nullptr;
  62. // RmlUi's file I/O interface.
  63. static FileInterface* file_interface = nullptr;
  64. // RmlUi's font engine interface.
  65. static FontEngineInterface* font_interface = nullptr;
  66. // Default interfaces should be created and destroyed on Initialise and Shutdown, respectively.
  67. static UniquePtr<FileInterface> default_file_interface;
  68. static UniquePtr<FontEngineInterface> default_font_interface;
  69. static bool initialised = false;
  70. using ContextMap = UnorderedMap<String, ContextPtr>;
  71. static ContextMap contexts;
  72. // The ObserverPtrBlock pool
  73. extern Pool<ObserverPtrBlock>* observerPtrBlockPool;
  74. #ifndef RMLUI_VERSION
  75. #define RMLUI_VERSION "custom"
  76. #endif
  77. bool Initialise()
  78. {
  79. RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");
  80. Log::Initialise();
  81. // Check for valid interfaces, or install default interfaces as appropriate.
  82. if (!system_interface)
  83. {
  84. Log::Message(Log::LT_ERROR, "No system interface set!");
  85. return false;
  86. }
  87. if (!render_interface)
  88. {
  89. Log::Message(Log::LT_ERROR, "No render interface set!");
  90. return false;
  91. }
  92. if (!file_interface)
  93. {
  94. #ifndef RMLUI_NO_FILE_INTERFACE_DEFAULT
  95. default_file_interface = MakeUnique<FileInterfaceDefault>();
  96. file_interface = default_file_interface.get();
  97. #else
  98. Log::Message(Log::LT_ERROR, "No file interface set!");
  99. return false;
  100. #endif
  101. }
  102. EventSpecificationInterface::Initialize();
  103. TextureDatabase::Initialise();
  104. if (!font_interface)
  105. {
  106. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  107. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  108. font_interface = default_font_interface.get();
  109. #else
  110. Log::Message(Log::LT_ERROR, "No font engine interface set!");
  111. return false;
  112. #endif
  113. }
  114. StyleSheetSpecification::Initialise();
  115. StyleSheetParser::Initialise();
  116. StyleSheetFactory::Initialise();
  117. TemplateCache::Initialise();
  118. Factory::Initialise();
  119. // Initialise plugins integrated with Core.
  120. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  121. Lottie::Initialise();
  122. #endif
  123. #ifdef RMLUI_ENABLE_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 = nullptr;
  144. default_font_interface.reset();
  145. TextureDatabase::Shutdown();
  146. initialised = false;
  147. render_interface = nullptr;
  148. file_interface = nullptr;
  149. system_interface = nullptr;
  150. default_file_interface.reset();
  151. Log::Shutdown();
  152. // Release any memory pools
  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. if (initialised)
  170. {
  171. Log::Message(Log::LT_ERROR, "The render interface is not allowed to be set or changed after RmlUi has been initialised.");
  172. return;
  173. }
  174. render_interface = _render_interface;
  175. }
  176. RenderInterface* GetRenderInterface()
  177. {
  178. return render_interface;
  179. }
  180. void SetFileInterface(FileInterface* _file_interface)
  181. {
  182. file_interface = _file_interface;
  183. }
  184. FileInterface* GetFileInterface()
  185. {
  186. return file_interface;
  187. }
  188. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  189. {
  190. font_interface = _font_interface;
  191. }
  192. FontEngineInterface* GetFontEngineInterface()
  193. {
  194. return font_interface;
  195. }
  196. Context* CreateContext(const String& name, const Vector2i dimensions)
  197. {
  198. if (!initialised)
  199. return nullptr;
  200. if (GetContext(name))
  201. {
  202. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  203. return nullptr;
  204. }
  205. ContextPtr new_context = Factory::InstanceContext(name);
  206. if (!new_context)
  207. {
  208. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  209. return nullptr;
  210. }
  211. new_context->SetDimensions(dimensions);
  212. Context* new_context_raw = new_context.get();
  213. contexts[name] = std::move(new_context);
  214. PluginRegistry::NotifyContextCreate(new_context_raw);
  215. return new_context_raw;
  216. }
  217. bool RemoveContext(const String& name)
  218. {
  219. auto it = contexts.find(name);
  220. if (it != contexts.end())
  221. {
  222. contexts.erase(it);
  223. return true;
  224. }
  225. return false;
  226. }
  227. Context* GetContext(const String& name)
  228. {
  229. ContextMap::iterator i = contexts.find(name);
  230. if (i == contexts.end())
  231. return nullptr;
  232. return i->second.get();
  233. }
  234. Context* GetContext(int index)
  235. {
  236. ContextMap::iterator i = contexts.begin();
  237. int count = 0;
  238. if (index < 0 || index >= GetNumContexts())
  239. return nullptr;
  240. while (count < index)
  241. {
  242. ++i;
  243. ++count;
  244. }
  245. if (i == contexts.end())
  246. return nullptr;
  247. return i->second.get();
  248. }
  249. int GetNumContexts()
  250. {
  251. return (int)contexts.size();
  252. }
  253. bool LoadFontFace(const String& file_path, bool fallback_face, Style::FontWeight weight)
  254. {
  255. return font_interface->LoadFontFace(file_path, fallback_face, weight);
  256. }
  257. bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  258. {
  259. return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
  260. }
  261. void RegisterPlugin(Plugin* plugin)
  262. {
  263. if (initialised)
  264. plugin->OnInitialise();
  265. PluginRegistry::RegisterPlugin(plugin);
  266. }
  267. void UnregisterPlugin(Plugin* plugin)
  268. {
  269. PluginRegistry::UnregisterPlugin(plugin);
  270. if (initialised)
  271. plugin->OnShutdown();
  272. }
  273. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  274. {
  275. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  276. }
  277. StringList GetTextureSourceList()
  278. {
  279. return TextureDatabase::GetSourceList();
  280. }
  281. void ReleaseTextures()
  282. {
  283. TextureDatabase::ReleaseTextures();
  284. }
  285. void ReleaseCompiledGeometry()
  286. {
  287. return GeometryDatabase::ReleaseAll();
  288. }
  289. void ReleaseMemoryPools()
  290. {
  291. if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
  292. {
  293. delete observerPtrBlockPool;
  294. observerPtrBlockPool = nullptr;
  295. }
  296. }
  297. void ReleaseFontResources()
  298. {
  299. if (font_interface)
  300. {
  301. for (const auto& name_context : contexts)
  302. name_context.second->GetRootElement()->DirtyFontFaceRecursive();
  303. font_interface->ReleaseFontResources();
  304. for (const auto& name_context : contexts)
  305. name_context.second->Update();
  306. }
  307. }
  308. } // namespace Rml