2
0

Core.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 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/SystemInterface.h"
  37. #include "../../Include/RmlUi/Core/StyleSheetSpecification.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. #include "EventSpecification.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 bool initialised = false;
  71. using ContextMap = UnorderedMap< String, ContextPtr >;
  72. static ContextMap contexts;
  73. // The ObserverPtrBlock pool
  74. extern Pool<ObserverPtrBlock>* observerPtrBlockPool;
  75. #ifndef RMLUI_VERSION
  76. #define RMLUI_VERSION "custom"
  77. #endif
  78. bool Initialise()
  79. {
  80. RMLUI_ASSERTMSG(!initialised, "Rml::Initialise() called, but RmlUi is already initialised!");
  81. Log::Initialise();
  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. EventSpecificationInterface::Initialize();
  99. TextureDatabase::Initialise();
  100. if (!font_interface)
  101. {
  102. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  103. default_font_interface = MakeUnique<FontEngineInterfaceDefault>();
  104. font_interface = default_font_interface.get();
  105. #else
  106. Log::Message(Log::LT_ERROR, "No font interface set!");
  107. return false;
  108. #endif
  109. }
  110. StyleSheetSpecification::Initialise();
  111. StyleSheetParser::Initialise();
  112. StyleSheetFactory::Initialise();
  113. TemplateCache::Initialise();
  114. Factory::Initialise();
  115. // Initialise plugins integrated with Core.
  116. #ifdef RMLUI_ENABLE_LOTTIE_PLUGIN
  117. Lottie::Initialise();
  118. #endif
  119. #ifdef RMLUI_ENABLE_SVG_PLUGIN
  120. SVG::Initialise();
  121. #endif
  122. // Notify all plugins we're starting up.
  123. PluginRegistry::NotifyInitialise();
  124. initialised = true;
  125. return true;
  126. }
  127. void Shutdown()
  128. {
  129. RMLUI_ASSERTMSG(initialised, "Rml::Shutdown() called, but RmlUi is not initialised!");
  130. // Clear out all contexts, which should also clean up all attached elements.
  131. contexts.clear();
  132. // Notify all plugins we're being shutdown.
  133. PluginRegistry::NotifyShutdown();
  134. Factory::Shutdown();
  135. TemplateCache::Shutdown();
  136. StyleSheetFactory::Shutdown();
  137. StyleSheetParser::Shutdown();
  138. StyleSheetSpecification::Shutdown();
  139. font_interface = nullptr;
  140. default_font_interface.reset();
  141. TextureDatabase::Shutdown();
  142. initialised = false;
  143. render_interface = nullptr;
  144. file_interface = nullptr;
  145. system_interface = nullptr;
  146. default_file_interface.reset();
  147. Log::Shutdown();
  148. // Release any memory pools
  149. ReleaseMemoryPools();
  150. }
  151. // Returns the version of this RmlUi library.
  152. String GetVersion()
  153. {
  154. return RMLUI_VERSION;
  155. }
  156. // Sets the interface through which all RmlUi messages will be routed.
  157. void SetSystemInterface(SystemInterface* _system_interface)
  158. {
  159. system_interface = _system_interface;
  160. }
  161. // Returns RmlUi's system interface.
  162. SystemInterface* GetSystemInterface()
  163. {
  164. return system_interface;
  165. }
  166. // Sets the interface through which all rendering requests are made.
  167. void SetRenderInterface(RenderInterface* _render_interface)
  168. {
  169. render_interface = _render_interface;
  170. }
  171. // Returns RmlUi's render interface.
  172. RenderInterface* GetRenderInterface()
  173. {
  174. return render_interface;
  175. }
  176. // Sets the interface through which all file I/O requests are made.
  177. void SetFileInterface(FileInterface* _file_interface)
  178. {
  179. file_interface = _file_interface;
  180. }
  181. // Returns RmlUi's file interface.
  182. FileInterface* GetFileInterface()
  183. {
  184. return file_interface;
  185. }
  186. // Sets the interface through which all font requests are made.
  187. void SetFontEngineInterface(FontEngineInterface* _font_interface)
  188. {
  189. font_interface = _font_interface;
  190. }
  191. // Returns RmlUi's file interface.
  192. FontEngineInterface* GetFontEngineInterface()
  193. {
  194. return font_interface;
  195. }
  196. // Creates a new element context.
  197. Context* CreateContext(const String& name, const Vector2i dimensions, RenderInterface* custom_render_interface)
  198. {
  199. if (!initialised)
  200. return nullptr;
  201. if (!custom_render_interface && !render_interface)
  202. {
  203. Log::Message(Log::LT_WARNING, "Failed to create context '%s', no render interface specified and no default render interface exists.", name.c_str());
  204. return nullptr;
  205. }
  206. if (GetContext(name))
  207. {
  208. Log::Message(Log::LT_WARNING, "Failed to create context '%s', context already exists.", name.c_str());
  209. return nullptr;
  210. }
  211. ContextPtr new_context = Factory::InstanceContext(name);
  212. if (!new_context)
  213. {
  214. Log::Message(Log::LT_WARNING, "Failed to instance context '%s', instancer returned nullptr.", name.c_str());
  215. return nullptr;
  216. }
  217. // Set the render interface on the context, and add a reference onto it.
  218. if (custom_render_interface)
  219. new_context->render_interface = custom_render_interface;
  220. else
  221. new_context->render_interface = render_interface;
  222. new_context->SetDimensions(dimensions);
  223. Context* new_context_raw = new_context.get();
  224. contexts[name] = std::move(new_context);
  225. PluginRegistry::NotifyContextCreate(new_context_raw);
  226. return new_context_raw;
  227. }
  228. bool RemoveContext(const String& name)
  229. {
  230. auto it = contexts.find(name);
  231. if (it != contexts.end())
  232. {
  233. contexts.erase(it);
  234. return true;
  235. }
  236. return false;
  237. }
  238. // Fetches a previously constructed context by name.
  239. Context* GetContext(const String& name)
  240. {
  241. ContextMap::iterator i = contexts.find(name);
  242. if (i == contexts.end())
  243. return nullptr;
  244. return i->second.get();
  245. }
  246. // Fetches a context by index.
  247. Context* GetContext(int index)
  248. {
  249. ContextMap::iterator i = contexts.begin();
  250. int count = 0;
  251. if (index < 0 || index >= GetNumContexts())
  252. return nullptr;
  253. while (count < index)
  254. {
  255. ++i;
  256. ++count;
  257. }
  258. if (i == contexts.end())
  259. return nullptr;
  260. return i->second.get();
  261. }
  262. // Returns the number of active contexts.
  263. int GetNumContexts()
  264. {
  265. return (int) contexts.size();
  266. }
  267. bool LoadFontFace(const String& file_name, bool fallback_face, Style::FontWeight weight)
  268. {
  269. return font_interface->LoadFontFace(file_name, fallback_face, weight);
  270. }
  271. bool LoadFontFace(const byte* data, int data_size, const String& font_family, Style::FontStyle style, Style::FontWeight weight, bool fallback_face)
  272. {
  273. return font_interface->LoadFontFace(data, data_size, font_family, style, weight, fallback_face);
  274. }
  275. // Registers a generic rmlui plugin
  276. void RegisterPlugin(Plugin* plugin)
  277. {
  278. if (initialised)
  279. plugin->OnInitialise();
  280. PluginRegistry::RegisterPlugin(plugin);
  281. }
  282. // Unregisters a generic rmlui plugin
  283. void UnregisterPlugin(Plugin* plugin)
  284. {
  285. PluginRegistry::UnregisterPlugin(plugin);
  286. if(initialised)
  287. plugin->OnShutdown();
  288. }
  289. EventId RegisterEventType(const String& type, bool interruptible, bool bubbles, DefaultActionPhase default_action_phase)
  290. {
  291. return EventSpecificationInterface::InsertOrReplaceCustom(type, interruptible, bubbles, default_action_phase);
  292. }
  293. StringList GetTextureSourceList()
  294. {
  295. return TextureDatabase::GetSourceList();
  296. }
  297. void ReleaseTextures(RenderInterface* in_render_interface)
  298. {
  299. TextureDatabase::ReleaseTextures(in_render_interface);
  300. }
  301. void ReleaseCompiledGeometry()
  302. {
  303. return GeometryDatabase::ReleaseAll();
  304. }
  305. void ReleaseMemoryPools()
  306. {
  307. if (observerPtrBlockPool && observerPtrBlockPool->GetNumAllocatedObjects() <= 0)
  308. {
  309. delete observerPtrBlockPool;
  310. observerPtrBlockPool = nullptr;
  311. }
  312. }
  313. void ReleaseFontResources()
  314. {
  315. if (font_interface)
  316. {
  317. for (const auto& name_context : contexts)
  318. name_context.second->GetRootElement()->DirtyFontFaceRecursive();
  319. font_interface->ReleaseFontResources();
  320. for (const auto& name_context : contexts)
  321. name_context.second->Update();
  322. }
  323. }
  324. } // namespace Rml