Make sure that global variables are initialized lazily. 45/288045/4
authorhuayong.xu <huayong.xu@samsung.com>
Thu, 9 Feb 2023 12:06:46 +0000 (20:06 +0800)
committerhuayong.xu <huayong.xu@samsung.com>
Mon, 13 Feb 2023 02:24:02 +0000 (10:24 +0800)
Global variables are initialized before main function or
when dali-core so is loaded firstly.
This patch is to reduce loading time of dali in theory.

Change-Id: If644ff61bdc27d268e913d9455bfa506bcea5892

dali/devel-api/adaptor-framework/accessibility.cpp
dali/internal/accessibility/bridge/dbus/dbus-tizen.cpp [changed mode: 0644->0755]
dali/internal/graphics/gles-impl/gles-graphics-pipeline-cache.cpp [changed mode: 0644->0755]
dali/internal/input/common/key-impl.cpp

index b058328..1139607 100644 (file)
@@ -366,7 +366,7 @@ public:
       return false;
     }
 
-    auto self = Self();
+    auto self                = Self();
     auto oldHighlightedActor = GetCurrentlyHighlightedActor();
     if(self == oldHighlightedActor)
     {
@@ -385,7 +385,7 @@ public:
 
     SetCurrentlyHighlightedActor(self);
 
-    auto window                                 = Dali::DevelWindow::Get(self);
+    auto                             window     = Dali::DevelWindow::Get(self);
     Dali::Internal::Adaptor::Window& windowImpl = Dali::GetImplementation(window);
     windowImpl.EmitAccessibilityHighlightSignal(true);
 
@@ -413,7 +413,7 @@ public:
 
     SetCurrentlyHighlightedActor({});
 
-    auto window                                 = Dali::DevelWindow::Get(self);
+    auto                             window     = Dali::DevelWindow::Get(self);
     Dali::Internal::Adaptor::Window& windowImpl = Dali::GetImplementation(window);
     windowImpl.EmitAccessibilityHighlightSignal(false);
 
@@ -453,7 +453,7 @@ public:
 
     if(mRoot)
     {
-      Dali::Window window                         = Dali::DevelWindow::Get(Self());
+      Dali::Window                     window     = Dali::DevelWindow::Get(Self());
       Dali::Internal::Adaptor::Window& windowImpl = Dali::GetImplementation(window);
       attributes["resID"]                         = windowImpl.GetNativeResourceId();
     }
@@ -479,7 +479,11 @@ public:
 using AdaptorAccessiblesType = std::unordered_map<const Dali::RefObject*, std::unique_ptr<AdaptorAccessible> >;
 
 // Save RefObject from an Actor in Accessible::Get()
-AdaptorAccessiblesType gAdaptorAccessibles;
+AdaptorAccessiblesType& GetAdaptorAccessibles()
+{
+  static AdaptorAccessiblesType gAdaptorAccessibles;
+  return gAdaptorAccessibles;
+}
 
 std::function<Accessible*(Dali::Actor)> convertingFunctor = [](Dali::Actor) -> Accessible* {
   return nullptr;
@@ -492,7 +496,7 @@ void Accessible::SetObjectRegistry(ObjectRegistry registry)
 {
   objectRegistry = registry;
   objectRegistry.ObjectDestroyedSignal().Connect([](const Dali::RefObject* obj) {
-    gAdaptorAccessibles.erase(obj);
+    GetAdaptorAccessibles().erase(obj);
   });
 }
 
@@ -511,11 +515,11 @@ Accessible* Accessible::Get(Dali::Actor actor)
   auto accessible = convertingFunctor(actor);
   if(!accessible)
   {
-    auto pair = gAdaptorAccessibles.emplace(&actor.GetBaseObject(), nullptr);
+    auto pair = GetAdaptorAccessibles().emplace(&actor.GetBaseObject(), nullptr);
     if(pair.second)
     {
-      bool isRoot                    = false;
-      Dali::Integration::Scene scene = Dali::Integration::Scene::Get(actor);
+      bool                     isRoot = false;
+      Dali::Integration::Scene scene  = Dali::Integration::Scene::Get(actor);
       if(scene)
       {
         isRoot = (actor == scene.GetRootLayer());
old mode 100644 (file)
new mode 100755 (executable)
index 52014da..baab45a
@@ -110,8 +110,7 @@ DBus::DBusClient::DBusClient(std::string busName, std::string pathName, std::str
   else
     connectionState->connection = conn;
 
-
-  if (!connectionState->connection)
+  if(!connectionState->connection)
   {
     DALI_LOG_ERROR("DBusClient connection is not ready\n");
     return;
@@ -525,7 +524,7 @@ struct DefaultDBusWrapper : public DBusWrapper
     }
 
     auto p = eldbus_connection_get(eldbusType);
-    if (!p)
+    if(!p)
     {
       DALI_LOG_ERROR("cannot get dbus connection\n");
       return NULL;
@@ -579,25 +578,51 @@ struct DefaultDBusWrapper : public DBusWrapper
     DBusWrapper::ConnectionWeakPtr connection;
   };
 
-  static std::unordered_map<const Eldbus_Service_Interface*, std::unique_ptr<Implementation>> globalEntries;
-  static std::mutex                                                                           globalEntriesMutex;
-
-#undef EINA_FALSE
-#undef EINA_TRUE
-#define EINA_FALSE static_cast<Eina_Bool>(0)
-#define EINA_TRUE static_cast<Eina_Bool>(1)
-
-  static Eina_Bool property_get_callback(const Eldbus_Service_Interface* iface, const char* propertyName, Eldbus_Message_Iter* iter, const Eldbus_Message* message, Eldbus_Message** error)
+  struct GlobalEntries
   {
-    Implementation* impl = nullptr;
+    static GlobalEntries& Get()
+    {
+      static GlobalEntries instance;
+      return instance;
+    }
+
+    Implementation* Find(const Eldbus_Service_Interface* iface)
     {
+      Implementation*             impl = nullptr;
       std::lock_guard<std::mutex> lock(globalEntriesMutex);
       auto                        it = globalEntries.find(iface);
       if(it != globalEntries.end())
       {
         impl = it->second.get();
       }
+      return impl;
     }
+
+    void Add(const Eldbus_Service_Interface* iface, std::unique_ptr<Implementation> impl)
+    {
+      std::lock_guard<std::mutex> lock(globalEntriesMutex);
+      globalEntries[iface] = std::move(impl);
+    }
+
+    void Erase(const Eldbus_Service_Interface* iface)
+    {
+      std::lock_guard<std::mutex> lock(globalEntriesMutex);
+      globalEntries.erase(iface);
+    }
+
+  private:
+    std::unordered_map<const Eldbus_Service_Interface*, std::unique_ptr<Implementation>> globalEntries;
+    std::mutex                                                                           globalEntriesMutex;
+  };
+
+#undef EINA_FALSE
+#undef EINA_TRUE
+#define EINA_FALSE static_cast<Eina_Bool>(0)
+#define EINA_TRUE static_cast<Eina_Bool>(1)
+
+  static Eina_Bool property_get_callback(const Eldbus_Service_Interface* iface, const char* propertyName, Eldbus_Message_Iter* iter, const Eldbus_Message* message, Eldbus_Message** error)
+  {
+    Implementation* impl = GlobalEntries::Get().Find(iface);
     if(!impl)
     {
       return EINA_FALSE;
@@ -631,15 +656,7 @@ struct DefaultDBusWrapper : public DBusWrapper
 
   static Eldbus_Message* property_set_callback(const Eldbus_Service_Interface* iface, const char* propertyName, Eldbus_Message_Iter* iter, const Eldbus_Message* message)
   {
-    Implementation* impl = nullptr;
-    {
-      std::lock_guard<std::mutex> lock(globalEntriesMutex);
-      auto                        it = globalEntries.find(iface);
-      if(it != globalEntries.end())
-      {
-        impl = it->second.get();
-      }
-    }
+    Implementation* impl = GlobalEntries::Get().Find(iface);
     if(!impl)
     {
       auto ret = eldbus_message_error_new(message, "org.freedesktop.DBus.Error.Failed", "Unknown interface");
@@ -675,13 +692,7 @@ struct DefaultDBusWrapper : public DBusWrapper
 
   static Eldbus_Message* method_callback(const Eldbus_Service_Interface* iface, const Eldbus_Message* message)
   {
-    Implementation* impl = nullptr;
-    {
-      std::lock_guard<std::mutex> lock(globalEntriesMutex);
-      auto                        it = globalEntries.find(iface);
-      if(it != globalEntries.end())
-        impl = it->second.get();
-    }
+    Implementation* impl = GlobalEntries::Get().Find(iface);
     if(!impl)
     {
       auto ret = eldbus_message_error_new(message, "org.freedesktop.DBus.Error.Failed", "Unknown interface");
@@ -793,21 +804,15 @@ struct DefaultDBusWrapper : public DBusWrapper
       std::move(signalsMap),
       connection});
 
-    {
-      std::lock_guard<std::mutex> lock(globalEntriesMutex);
-      auto                        v = fallback ? eldbus_service_interface_fallback_register(get(connection), pathName.c_str(), &impl->dsc) : eldbus_service_interface_register(get(connection), pathName.c_str(), &impl->dsc);
-      assert(v);
-      globalEntries[v] = std::move(impl);
-      DBUS_DEBUG("registering interface %p (%d)", v, fallback ? 1 : 0);
-      destructors.push_back([=]() {
-        DBUS_DEBUG("unregistering interface %p", v);
-        {
-          std::lock_guard<std::mutex> lock(globalEntriesMutex);
-          globalEntries.erase(v);
-        }
-        eldbus_service_interface_unregister(v);
-      });
-    }
+    auto v = fallback ? eldbus_service_interface_fallback_register(get(connection), pathName.c_str(), &impl->dsc) : eldbus_service_interface_register(get(connection), pathName.c_str(), &impl->dsc);
+    DALI_ASSERT_ALWAYS(v && "Eldbus register failed!");
+    GlobalEntries::Get().Add(v, std::move(impl));
+    DBUS_DEBUG("registering interface %p (%d)", v, fallback ? 1 : 0);
+    destructors.push_back([=]() {
+      DBUS_DEBUG("unregistering interface %p", v);
+      GlobalEntries::Get().Erase(v);
+      eldbus_service_interface_unregister(v);
+    });
   }
 
   static void listenerEventChangedCallback(void* data, Eldbus_Proxy* proxy EINA_UNUSED, void* event)
@@ -838,9 +843,6 @@ struct DefaultDBusWrapper : public DBusWrapper
   }
 };
 
-std::unordered_map<const Eldbus_Service_Interface*, std::unique_ptr<DefaultDBusWrapper::Implementation>> DefaultDBusWrapper::globalEntries;
-std::mutex                                                                                               DefaultDBusWrapper::globalEntriesMutex;
-
 DBusWrapper* DBusWrapper::Installed()
 {
   if(!InstalledWrapper)
old mode 100644 (file)
new mode 100755 (executable)
index f9a7d16..69f45b7
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -133,85 +133,88 @@ operator==(const Dali::Graphics::VertexInputState::Binding& lhs, const Dali::Gra
 using PipelineStateCompateFunctionType = bool(const Graphics::PipelineCreateInfo*,
                                               const Graphics::PipelineCreateInfo*);
 
-static std::vector<PipelineStateCompateFunctionType*> STATE_COMPARE_FUNC_TABLE{};
+static std::vector<PipelineStateCompateFunctionType*>& GetStateCompareFuncTable()
+{
+  static std::vector<PipelineStateCompateFunctionType*> stateCompareFuncTable{};
+  return stateCompareFuncTable;
+}
 
 /**
  * @brief Initialises compare function lookup table
  */
 void InitialiseStateCompareLookupTable()
 {
-  STATE_COMPARE_FUNC_TABLE = {
-    [](const auto* lhs, const auto* rhs) -> bool // colorBlendState
-    {
-      const auto& lcb = *lhs->colorBlendState;
-      const auto& rcb = *rhs->colorBlendState;
-      return lcb.logicOpEnable == rcb.logicOpEnable &&
-             lcb.logicOp == rcb.logicOp &&
-             cmpf(lcb.blendConstants[0], rcb.blendConstants[0]) &&
-             cmpf(lcb.blendConstants[1], rcb.blendConstants[1]) &&
-             cmpf(lcb.blendConstants[2], rcb.blendConstants[2]) &&
-             cmpf(lcb.blendConstants[3], rcb.blendConstants[3]) &&
-             lcb.blendEnable == rcb.blendEnable &&
-             lcb.srcColorBlendFactor == rcb.srcColorBlendFactor &&
-             lcb.dstColorBlendFactor == rcb.dstColorBlendFactor &&
-             lcb.colorBlendOp == rcb.colorBlendOp &&
-             lcb.srcAlphaBlendFactor == rcb.srcAlphaBlendFactor &&
-             lcb.dstAlphaBlendFactor == rcb.dstAlphaBlendFactor &&
-             lcb.alphaBlendOp == rcb.alphaBlendOp &&
-             lcb.colorComponentWriteBits == rcb.colorComponentWriteBits;
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // viewport state
-    {
-      const auto& lvp = *lhs->viewportState;
-      const auto& rvp = *rhs->viewportState;
-      return lvp.viewport == rvp.viewport &&
-             lvp.scissor == rvp.scissor &&
-             lvp.scissorTestEnable == rvp.scissorTestEnable;
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // basePipeline
-    {
-      return lhs->basePipeline == rhs->basePipeline;
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // depthStencilState
-    {
-      const auto& lds = *lhs->depthStencilState;
-      const auto& rds = *rhs->depthStencilState;
-      return lds.depthTestEnable == rds.depthTestEnable &&
-             lds.depthWriteEnable == rds.depthWriteEnable &&
-             lds.depthCompareOp == rds.depthCompareOp &&
-             lds.stencilTestEnable == rds.stencilTestEnable &&
-             lds.front == rds.front &&
-             lds.back == rds.back;
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // rasterizationState
-    {
-      const auto& lrs = *lhs->rasterizationState;
-      const auto& rrs = *rhs->rasterizationState;
-      return lrs.cullMode == rrs.cullMode &&
-             lrs.polygonMode == rrs.polygonMode &&
-             lrs.frontFace == rrs.frontFace;
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // vertexInputState
-    {
-      const auto& lvi = *lhs->vertexInputState;
-      const auto& rvi = *rhs->vertexInputState;
-      return lvi.bufferBindings.size() == rvi.bufferBindings.size() &&
-             lvi.attributes.size() == rvi.attributes.size() &&
-             std::equal(lvi.bufferBindings.begin(), lvi.bufferBindings.end(), rvi.bufferBindings.begin(), [](const auto& lhs, const auto& rhs) {
-               return operator==(lhs, rhs);
-             }) &&
-             std::equal(lvi.attributes.begin(), lvi.attributes.end(), rvi.attributes.begin(), [](const auto& lhs, const auto& rhs) {
-               return operator==(lhs, rhs);
-             });
-    },
-    [](const auto* lhs, const auto* rhs) -> bool // inputAssemblyState
-    {
-      const auto& lia = *lhs->inputAssemblyState;
-      const auto& ria = *rhs->inputAssemblyState;
-      return lia.topology == ria.topology &&
-             lia.primitiveRestartEnable == ria.primitiveRestartEnable;
-    },
-  };
+  GetStateCompareFuncTable().clear();
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // colorBlendState
+                                       {
+                                         const auto& lcb = *lhs->colorBlendState;
+                                         const auto& rcb = *rhs->colorBlendState;
+                                         return lcb.logicOpEnable == rcb.logicOpEnable &&
+                                                lcb.logicOp == rcb.logicOp &&
+                                                cmpf(lcb.blendConstants[0], rcb.blendConstants[0]) &&
+                                                cmpf(lcb.blendConstants[1], rcb.blendConstants[1]) &&
+                                                cmpf(lcb.blendConstants[2], rcb.blendConstants[2]) &&
+                                                cmpf(lcb.blendConstants[3], rcb.blendConstants[3]) &&
+                                                lcb.blendEnable == rcb.blendEnable &&
+                                                lcb.srcColorBlendFactor == rcb.srcColorBlendFactor &&
+                                                lcb.dstColorBlendFactor == rcb.dstColorBlendFactor &&
+                                                lcb.colorBlendOp == rcb.colorBlendOp &&
+                                                lcb.srcAlphaBlendFactor == rcb.srcAlphaBlendFactor &&
+                                                lcb.dstAlphaBlendFactor == rcb.dstAlphaBlendFactor &&
+                                                lcb.alphaBlendOp == rcb.alphaBlendOp &&
+                                                lcb.colorComponentWriteBits == rcb.colorComponentWriteBits;
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // viewport state
+                                       {
+                                         const auto& lvp = *lhs->viewportState;
+                                         const auto& rvp = *rhs->viewportState;
+                                         return lvp.viewport == rvp.viewport &&
+                                                lvp.scissor == rvp.scissor &&
+                                                lvp.scissorTestEnable == rvp.scissorTestEnable;
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // basePipeline
+                                       {
+                                         return lhs->basePipeline == rhs->basePipeline;
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // depthStencilState
+                                       {
+                                         const auto& lds = *lhs->depthStencilState;
+                                         const auto& rds = *rhs->depthStencilState;
+                                         return lds.depthTestEnable == rds.depthTestEnable &&
+                                                lds.depthWriteEnable == rds.depthWriteEnable &&
+                                                lds.depthCompareOp == rds.depthCompareOp &&
+                                                lds.stencilTestEnable == rds.stencilTestEnable &&
+                                                lds.front == rds.front &&
+                                                lds.back == rds.back;
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // rasterizationState
+                                       {
+                                         const auto& lrs = *lhs->rasterizationState;
+                                         const auto& rrs = *rhs->rasterizationState;
+                                         return lrs.cullMode == rrs.cullMode &&
+                                                lrs.polygonMode == rrs.polygonMode &&
+                                                lrs.frontFace == rrs.frontFace;
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // vertexInputState
+                                       {
+                                         const auto& lvi = *lhs->vertexInputState;
+                                         const auto& rvi = *rhs->vertexInputState;
+                                         return lvi.bufferBindings.size() == rvi.bufferBindings.size() &&
+                                                lvi.attributes.size() == rvi.attributes.size() &&
+                                                std::equal(lvi.bufferBindings.begin(), lvi.bufferBindings.end(), rvi.bufferBindings.begin(), [](const auto& lhs, const auto& rhs) {
+                                                  return operator==(lhs, rhs);
+                                                }) &&
+                                                std::equal(lvi.attributes.begin(), lvi.attributes.end(), rvi.attributes.begin(), [](const auto& lhs, const auto& rhs) {
+                                                  return operator==(lhs, rhs);
+                                                });
+                                       });
+  GetStateCompareFuncTable().push_back([](const auto* lhs, const auto* rhs) -> bool // inputAssemblyState
+                                       {
+                                         const auto& lia = *lhs->inputAssemblyState;
+                                         const auto& ria = *rhs->inputAssemblyState;
+                                         return lia.topology == ria.topology &&
+                                                lia.primitiveRestartEnable == ria.primitiveRestartEnable;
+                                       });
 }
 
 /**
@@ -341,7 +344,7 @@ PipelineImpl* PipelineCache::FindPipelineImpl(const PipelineCreateInfo& info)
         // Test only set states
         if((entry.stateBitmask & (1 << i)))
         {
-          if(!STATE_COMPARE_FUNC_TABLE[i](&info, &cacheInfo))
+          if(!(GetStateCompareFuncTable()[i](&info, &cacheInfo)))
           {
             break;
           }
@@ -483,4 +486,4 @@ void PipelineCache::FlushCache()
   //       killing the program (if program isn't in use anymore)
 }
 
-} // namespace Dali::Graphics::GLES
\ No newline at end of file
+} // namespace Dali::Graphics::GLES
index bcfd43c..623c689 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -271,29 +271,33 @@ private:
   bool   mIsLookupTableInitialized;          ///< flag for basic lookup table initialization
   bool   mIsExtensionLookupTableInitialized; ///< flag for extension lookup table initialization
 };
-KeyMap globalKeyLookup;
 
+KeyMap& GetKeyMap()
+{
+  static KeyMap globalKeyLookup;
+  return globalKeyLookup;
+}
 } // namespace
 
 bool IsKey(const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
 {
-  int key = globalKeyLookup.GetDaliKeyEnum(keyEvent.GetKeyName().c_str());
+  int key = GetKeyMap().GetDaliKeyEnum(keyEvent.GetKeyName().c_str());
   return daliKey == key;
 }
 
 bool IsDeviceButton(const char* keyName)
 {
-  return globalKeyLookup.IsDeviceButton(keyName);
+  return GetKeyMap().IsDeviceButton(keyName);
 }
 
 const char* GetKeyName(Dali::KEY daliKey)
 {
-  return globalKeyLookup.GetKeyName(daliKey);
+  return GetKeyMap().GetKeyName(daliKey);
 }
 
 int GetDaliKeyCode(const char* keyName)
 {
-  return globalKeyLookup.GetDaliKeyEnum(keyName);
+  return GetKeyMap().GetDaliKeyEnum(keyName);
 }
 
 } // namespace KeyLookup