Make sure that global variables are initialized lazily. 55/285155/3
authorhuayong.xu <huayong.xu@samsung.com>
Wed, 7 Dec 2022 02:30:56 +0000 (10:30 +0800)
committerhuayong.xu <huayong.xu@samsung.com>
Tue, 7 Feb 2023 10:39:51 +0000 (18:39 +0800)
Global variables are initialized before main function or
when dali-core so is loaded firstly.
This would lengthen time of loading dali in some cases.
This patch is to make the variables be initialized lazily.

Change-Id: I2a8e27619d52ca60e28484bb4c5a8e74f1d20c27

dali/internal/event/actors/actor-sizer.cpp
dali/internal/render/common/render-item.cpp
dali/internal/update/animation/scene-graph-animation.cpp
dali/internal/update/nodes/node.cpp
dali/internal/update/render-tasks/scene-graph-camera.cpp
dali/internal/update/render-tasks/scene-graph-render-task-list.cpp
dali/internal/update/rendering/scene-graph-renderer.cpp
dali/internal/update/rendering/scene-graph-texture-set.cpp

index 93c5216..d3bbfa4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
@@ -66,8 +66,11 @@ constexpr float GetDimensionValue(const Dali::Vector2& values, const Dali::Dimen
 /**
  * @brief Keep a static recursionstack vector to avoid creating temporary vectors every Relayout().
  */
-static Dali::Internal::ActorSizer::ActorDimensionStack gRecursionStack{};
-
+Dali::Internal::ActorSizer::ActorDimensionStack& GetRecursionStack()
+{
+  static Dali::Internal::ActorSizer::ActorDimensionStack gRecursionStack{};
+  return gRecursionStack;
+}
 } // namespace
 
 namespace Dali::Internal
@@ -121,7 +124,7 @@ void ActorSizer::SetSizeInternal(const Vector3& size)
   if(mTargetSize != size || mTargetSizeDirtyFlag)
   {
     mTargetSizeDirtyFlag = false;
-    mTargetSize = size;
+    mTargetSize          = size;
 
     // Update the preferred size after relayoutting
     // It should be used in the next relayoutting
@@ -837,14 +840,14 @@ void ActorSizer::NegotiateDimension(Dimension::Type dimension, const Vector2& al
 void ActorSizer::NegotiateDimensions(const Vector2& allocatedSize)
 {
   // Negotiate all dimensions that require it
-  gRecursionStack.clear();
+  GetRecursionStack().clear();
 
   for(uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i)
   {
     const Dimension::Type dimension = static_cast<Dimension::Type>(1 << i);
 
     // Negotiate
-    NegotiateDimension(dimension, allocatedSize, gRecursionStack);
+    NegotiateDimension(dimension, allocatedSize, GetRecursionStack());
   }
 }
 
index 0197443..0ba37c3 100644 (file)
 namespace
 {
 //Memory pool used to allocate new RenderItems. Memory used by this pool will be released when shutting down DALi
-Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem> gRenderItemPool;
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem>& GetRenderItemPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem> gRenderItemPool;
+  return gRenderItemPool;
+}
 } // namespace
 
 namespace Dali
@@ -37,13 +41,13 @@ namespace SceneGraph
 {
 RenderItem* RenderItem::New()
 {
-  return new(gRenderItemPool.AllocateRaw()) RenderItem();
+  return new(GetRenderItemPool().AllocateRaw()) RenderItem();
 }
 
 RenderItemKey RenderItem::NewKey()
 {
-  void* ptr = gRenderItemPool.AllocateRaw();
-  auto  key = gRenderItemPool.GetKeyFromPtr(static_cast<RenderItem*>(ptr));
+  void* ptr = GetRenderItemPool().AllocateRaw();
+  auto  key = GetRenderItemPool().GetKeyFromPtr(static_cast<RenderItem*>(ptr));
   new(ptr) RenderItem();
   return RenderItemKey(key);
 }
@@ -65,17 +69,17 @@ RenderItem::~RenderItem() = default;
 
 RenderItem* RenderItem::Get(RenderItemKey::KeyType key)
 {
-  return gRenderItemPool.GetPtrFromKey(key);
+  return GetRenderItemPool().GetPtrFromKey(key);
 }
 
 RenderItemKey RenderItem::GetKey(const RenderItem& renderItem)
 {
-  return RenderItemKey(gRenderItemPool.GetKeyFromPtr(const_cast<RenderItem*>(&renderItem)));
+  return RenderItemKey(GetRenderItemPool().GetKeyFromPtr(const_cast<RenderItem*>(&renderItem)));
 }
 
 RenderItemKey RenderItem::GetKey(RenderItem* renderItem)
 {
-  return RenderItemKey(gRenderItemPool.GetKeyFromPtr(renderItem));
+  return RenderItemKey(GetRenderItemPool().GetKeyFromPtr(renderItem));
 }
 
 ClippingBox RenderItem::CalculateTransformSpaceAABB(const Matrix& transformMatrix, const Vector3& position, const Vector3& size)
@@ -188,12 +192,12 @@ ClippingBox RenderItem::CalculateViewportSpaceAABB(const Matrix& modelViewMatrix
 
 void RenderItem::operator delete(void* ptr)
 {
-  gRenderItemPool.Free(static_cast<RenderItem*>(ptr));
+  GetRenderItemPool().Free(static_cast<RenderItem*>(ptr));
 }
 
 uint32_t RenderItem::GetMemoryPoolCapacity()
 {
-  return gRenderItemPool.GetCapacity();
+  return GetRenderItemPool().GetCapacity();
 }
 
 } // namespace SceneGraph
index d75199a..659547f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
 namespace //Unnamed namespace
 {
 //Memory pool used to allocate new animations. Memory used by this pool will be released when shutting down DALi
-Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Animation> gAnimationMemoryPool;
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Animation>& GetAnimationMemoryPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Animation> gAnimationMemoryPool;
+  return gAnimationMemoryPool;
+}
 
 inline void WrapInPlayRange(float& elapsed, const float& playRangeStartSeconds, const float& playRangeEndSeconds)
 {
@@ -58,7 +62,7 @@ namespace SceneGraph
 {
 Animation* Animation::New(float durationSeconds, float speedFactor, const Vector2& playRange, int32_t loopCount, EndAction endAction, EndAction disconnectAction)
 {
-  return new(gAnimationMemoryPool.AllocateRawThreadSafe()) Animation(durationSeconds, speedFactor, playRange, loopCount, endAction, disconnectAction);
+  return new(GetAnimationMemoryPool().AllocateRawThreadSafe()) Animation(durationSeconds, speedFactor, playRange, loopCount, endAction, disconnectAction);
 }
 
 Animation::Animation(float durationSeconds, float speedFactor, const Vector2& playRange, int32_t loopCount, Dali::Animation::EndAction endAction, Dali::Animation::EndAction disconnectAction)
@@ -84,7 +88,7 @@ Animation::~Animation() = default;
 
 void Animation::operator delete(void* ptr)
 {
-  gAnimationMemoryPool.FreeThreadSafe(static_cast<Animation*>(ptr));
+  GetAnimationMemoryPool().FreeThreadSafe(static_cast<Animation*>(ptr));
 }
 
 void Animation::SetDuration(float durationSeconds)
@@ -486,7 +490,7 @@ void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake, bool animati
 
 uint32_t Animation::GetMemoryPoolCapacity()
 {
-  return gAnimationMemoryPool.GetCapacity();
+  return GetAnimationMemoryPool().GetCapacity();
 }
 
 } // namespace SceneGraph
index e428837..7a7183b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
@@ -29,7 +29,11 @@ namespace
 {
 // Memory pool used to allocate new nodes. Memory used by this pool will be released when process dies
 //  or DALI library is unloaded
-Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node>& GetNodeMemoryPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Node> gNodeMemoryPool;
+  return gNodeMemoryPool;
+}
 #ifdef DEBUG_ENABLED
 // keep track of nodes alive, to ensure we have 0 when the process exits or DALi library is unloaded
 int32_t gNodeCount = 0;
@@ -54,7 +58,7 @@ uint32_t Node::mNodeCounter = 0; ///< A counter to provide unique node ids, up-t
 
 Node* Node::New()
 {
-  return new(gNodeMemoryPool.AllocateRawThreadSafe()) Node;
+  return new(GetNodeMemoryPool().AllocateRawThreadSafe()) Node;
 }
 
 void Node::Delete(Node* node)
@@ -66,7 +70,7 @@ void Node::Delete(Node* node)
     node->~Node();
 
     // Mark the memory it used as free in the memory pool
-    gNodeMemoryPool.FreeThreadSafe(node);
+    GetNodeMemoryPool().FreeThreadSafe(node);
   }
   else
   {
@@ -336,7 +340,7 @@ void Node::RecursiveDisconnectFromSceneGraph(BufferIndex updateBufferIndex)
 
 uint32_t Node::GetMemoryPoolCapacity()
 {
-  return gNodeMemoryPool.GetCapacity();
+  return GetNodeMemoryPool().GetCapacity();
 }
 
 } // namespace SceneGraph
index 586d7cc..2322c4a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
@@ -48,7 +48,11 @@ namespace SceneGraph
 namespace
 {
 //Memory pool used to allocate new camera. Memory used by this pool will be released when shutting down DALi
-MemoryPoolObjectAllocator<Camera> gCameraMemoryPool;
+MemoryPoolObjectAllocator<Camera>& GetCameraMemoryPool()
+{
+  static MemoryPoolObjectAllocator<Camera> gCameraMemoryPool;
+  return gCameraMemoryPool;
+}
 
 template<typename T>
 T Sign(T value)
@@ -205,14 +209,14 @@ Camera::Camera()
 
 Camera* Camera::New()
 {
-  return new(gCameraMemoryPool.AllocateRawThreadSafe()) Camera();
+  return new(GetCameraMemoryPool().AllocateRawThreadSafe()) Camera();
 }
 
 Camera::~Camera() = default;
 
 void Camera::operator delete(void* ptr)
 {
-  gCameraMemoryPool.FreeThreadSafe(static_cast<Camera*>(ptr));
+  GetCameraMemoryPool().FreeThreadSafe(static_cast<Camera*>(ptr));
 }
 
 void Camera::SetType(Dali::Camera::Type type)
index edbbe4a..080e2f0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
 namespace //Unnamed namespace
 {
 //Memory pool used to allocate new RenderTaskLists. Memory used by this pool will be released when shutting down DALi
-Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderTaskList> gRenderTaskListMemoryPool;
-
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderTaskList>& GetRenderTaskListMemoryPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderTaskList> gRenderTaskListMemoryPool;
+  return gRenderTaskListMemoryPool;
+}
 } // unnamed namespace
 
 namespace Dali
@@ -36,7 +39,7 @@ namespace SceneGraph
 {
 RenderTaskList* RenderTaskList::New()
 {
-  return new(gRenderTaskListMemoryPool.AllocateRawThreadSafe()) RenderTaskList();
+  return new(GetRenderTaskListMemoryPool().AllocateRawThreadSafe()) RenderTaskList();
 }
 
 RenderTaskList::RenderTaskList()
@@ -50,7 +53,7 @@ RenderTaskList::~RenderTaskList() = default;
 
 void RenderTaskList::operator delete(void* ptr)
 {
-  gRenderTaskListMemoryPool.FreeThreadSafe(static_cast<RenderTaskList*>(ptr));
+  GetRenderTaskListMemoryPool().FreeThreadSafe(static_cast<RenderTaskList*>(ptr));
 }
 
 void RenderTaskList::SetRenderMessageDispatcher(RenderMessageDispatcher* renderMessageDispatcher)
@@ -123,7 +126,7 @@ CompleteNotificationInterface* RenderTaskList::GetCompleteNotificationInterface(
 
 uint32_t RenderTaskList::GetMemoryPoolCapacity()
 {
-  return gRenderTaskListMemoryPool.GetCapacity();
+  return GetRenderTaskListMemoryPool().GetCapacity();
 }
 
 } // namespace SceneGraph
index 476afdf..752a56d 100644 (file)
@@ -47,7 +47,11 @@ Debug::Filter* gSceneGraphRendererLogFilter = Debug::Filter::New(Debug::NoLoggin
 #endif
 
 // Memory pool used to allocate new renderers. Memory used by this pool will be released when shutting down DALi
-MemoryPoolObjectAllocator<Renderer> gRendererMemoryPool;
+MemoryPoolObjectAllocator<Renderer>& GetRendererMemoryPool()
+{
+  static MemoryPoolObjectAllocator<Renderer> gRendererMemoryPool;
+  return gRendererMemoryPool;
+}
 
 // Flags for re-sending data to renderer.
 enum Flags
@@ -80,8 +84,8 @@ enum Flags
 
 RendererKey Renderer::NewKey()
 {
-  void* ptr = gRendererMemoryPool.AllocateRawThreadSafe();
-  auto  key = gRendererMemoryPool.GetKeyFromPtr(static_cast<Renderer*>(ptr));
+  void* ptr = GetRendererMemoryPool().AllocateRawThreadSafe();
+  auto  key = GetRendererMemoryPool().GetKeyFromPtr(static_cast<Renderer*>(ptr));
   new(ptr) Renderer();
   return RendererKey(key);
 }
@@ -119,22 +123,22 @@ Renderer::~Renderer()
 
 void Renderer::operator delete(void* ptr)
 {
-  gRendererMemoryPool.FreeThreadSafe(static_cast<Renderer*>(ptr));
+  GetRendererMemoryPool().FreeThreadSafe(static_cast<Renderer*>(ptr));
 }
 
 Renderer* Renderer::Get(RendererKey::KeyType rendererKey)
 {
-  return gRendererMemoryPool.GetPtrFromKey(rendererKey);
+  return GetRendererMemoryPool().GetPtrFromKey(rendererKey);
 }
 
 RendererKey Renderer::GetKey(const Renderer& renderer)
 {
-  return RendererKey(gRendererMemoryPool.GetKeyFromPtr(const_cast<Renderer*>(&renderer)));
+  return RendererKey(GetRendererMemoryPool().GetKeyFromPtr(const_cast<Renderer*>(&renderer)));
 }
 
 RendererKey Renderer::GetKey(Renderer* renderer)
 {
-  return RendererKey(gRendererMemoryPool.GetKeyFromPtr(renderer));
+  return RendererKey(GetRendererMemoryPool().GetKeyFromPtr(renderer));
 }
 
 bool Renderer::PrepareRender(BufferIndex updateBufferIndex)
@@ -769,7 +773,7 @@ void Renderer::ResetDirtyFlag()
 
 uint32_t Renderer::GetMemoryPoolCapacity()
 {
-  return gRendererMemoryPool.GetCapacity();
+  return GetRendererMemoryPool().GetCapacity();
 }
 
 void Renderer::OnMappingChanged()
index 2008805..106b711 100644 (file)
 namespace //Unnamed namespace
 {
 //Memory pool used to allocate new texture sets. Memory used by this pool will be released when shutting down DALi
-Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet>& GetTextureSetMemoryPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::TextureSet> gTextureSetMemoryPool;
+  return gTextureSetMemoryPool;
+}
 } // namespace
 
 namespace Dali
@@ -37,7 +41,7 @@ namespace SceneGraph
 {
 TextureSet* TextureSet::New()
 {
-  return new(gTextureSetMemoryPool.AllocateRawThreadSafe()) TextureSet();
+  return new(GetTextureSetMemoryPool().AllocateRawThreadSafe()) TextureSet();
 }
 
 TextureSet::TextureSet()
@@ -52,7 +56,7 @@ TextureSet::~TextureSet()
 
 void TextureSet::operator delete(void* ptr)
 {
-  gTextureSetMemoryPool.FreeThreadSafe(static_cast<TextureSet*>(ptr));
+  GetTextureSetMemoryPool().FreeThreadSafe(static_cast<TextureSet*>(ptr));
 }
 
 void TextureSet::SetSampler(uint32_t index, Render::Sampler* sampler)
@@ -115,7 +119,7 @@ bool TextureSet::HasAlpha() const
 
 uint32_t TextureSet::GetMemoryPoolCapacity()
 {
-  return gTextureSetMemoryPool.GetCapacity();
+  return GetTextureSetMemoryPool().GetCapacity();
 }
 
 } // namespace SceneGraph