From 73a52536910edf1cbb6ec462217e09a9cbd0449d Mon Sep 17 00:00:00 2001 From: "huayong.xu" Date: Wed, 7 Dec 2022 10:30:56 +0800 Subject: [PATCH] Make sure that global variables are initialized lazily. 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 | 15 +++++++++------ dali/internal/render/common/render-item.cpp | 22 +++++++++++++--------- .../update/animation/scene-graph-animation.cpp | 14 +++++++++----- dali/internal/update/nodes/node.cpp | 14 +++++++++----- .../update/render-tasks/scene-graph-camera.cpp | 12 ++++++++---- .../render-tasks/scene-graph-render-task-list.cpp | 15 +++++++++------ .../update/rendering/scene-graph-renderer.cpp | 20 ++++++++++++-------- .../update/rendering/scene-graph-texture-set.cpp | 12 ++++++++---- 8 files changed, 77 insertions(+), 47 deletions(-) diff --git a/dali/internal/event/actors/actor-sizer.cpp b/dali/internal/event/actors/actor-sizer.cpp index 93c5216..d3bbfa4 100644 --- a/dali/internal/event/actors/actor-sizer.cpp +++ b/dali/internal/event/actors/actor-sizer.cpp @@ -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(1 << i); // Negotiate - NegotiateDimension(dimension, allocatedSize, gRecursionStack); + NegotiateDimension(dimension, allocatedSize, GetRecursionStack()); } } diff --git a/dali/internal/render/common/render-item.cpp b/dali/internal/render/common/render-item.cpp index 0197443..0ba37c3 100644 --- a/dali/internal/render/common/render-item.cpp +++ b/dali/internal/render/common/render-item.cpp @@ -26,7 +26,11 @@ namespace { //Memory pool used to allocate new RenderItems. Memory used by this pool will be released when shutting down DALi -Dali::Internal::MemoryPoolObjectAllocator gRenderItemPool; +Dali::Internal::MemoryPoolObjectAllocator& GetRenderItemPool() +{ + static Dali::Internal::MemoryPoolObjectAllocator 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(ptr)); + void* ptr = GetRenderItemPool().AllocateRaw(); + auto key = GetRenderItemPool().GetKeyFromPtr(static_cast(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))); + return RenderItemKey(GetRenderItemPool().GetKeyFromPtr(const_cast(&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(ptr)); + GetRenderItemPool().Free(static_cast(ptr)); } uint32_t RenderItem::GetMemoryPoolCapacity() { - return gRenderItemPool.GetCapacity(); + return GetRenderItemPool().GetCapacity(); } } // namespace SceneGraph diff --git a/dali/internal/update/animation/scene-graph-animation.cpp b/dali/internal/update/animation/scene-graph-animation.cpp index d75199a..659547f 100644 --- a/dali/internal/update/animation/scene-graph-animation.cpp +++ b/dali/internal/update/animation/scene-graph-animation.cpp @@ -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. @@ -28,7 +28,11 @@ 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 gAnimationMemoryPool; +Dali::Internal::MemoryPoolObjectAllocator& GetAnimationMemoryPool() +{ + static Dali::Internal::MemoryPoolObjectAllocator 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(ptr)); + GetAnimationMemoryPool().FreeThreadSafe(static_cast(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 diff --git a/dali/internal/update/nodes/node.cpp b/dali/internal/update/nodes/node.cpp index e428837..7a7183b 100644 --- a/dali/internal/update/nodes/node.cpp +++ b/dali/internal/update/nodes/node.cpp @@ -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 gNodeMemoryPool; +Dali::Internal::MemoryPoolObjectAllocator& GetNodeMemoryPool() +{ + static Dali::Internal::MemoryPoolObjectAllocator 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 diff --git a/dali/internal/update/render-tasks/scene-graph-camera.cpp b/dali/internal/update/render-tasks/scene-graph-camera.cpp index 586d7cc..2322c4a 100644 --- a/dali/internal/update/render-tasks/scene-graph-camera.cpp +++ b/dali/internal/update/render-tasks/scene-graph-camera.cpp @@ -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 gCameraMemoryPool; +MemoryPoolObjectAllocator& GetCameraMemoryPool() +{ + static MemoryPoolObjectAllocator gCameraMemoryPool; + return gCameraMemoryPool; +} template 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(ptr)); + GetCameraMemoryPool().FreeThreadSafe(static_cast(ptr)); } void Camera::SetType(Dali::Camera::Type type) diff --git a/dali/internal/update/render-tasks/scene-graph-render-task-list.cpp b/dali/internal/update/render-tasks/scene-graph-render-task-list.cpp index edbbe4a..080e2f0 100644 --- a/dali/internal/update/render-tasks/scene-graph-render-task-list.cpp +++ b/dali/internal/update/render-tasks/scene-graph-render-task-list.cpp @@ -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. @@ -24,8 +24,11 @@ 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 gRenderTaskListMemoryPool; - +Dali::Internal::MemoryPoolObjectAllocator& GetRenderTaskListMemoryPool() +{ + static Dali::Internal::MemoryPoolObjectAllocator 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(ptr)); + GetRenderTaskListMemoryPool().FreeThreadSafe(static_cast(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 diff --git a/dali/internal/update/rendering/scene-graph-renderer.cpp b/dali/internal/update/rendering/scene-graph-renderer.cpp index 476afdf..752a56d 100644 --- a/dali/internal/update/rendering/scene-graph-renderer.cpp +++ b/dali/internal/update/rendering/scene-graph-renderer.cpp @@ -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 gRendererMemoryPool; +MemoryPoolObjectAllocator& GetRendererMemoryPool() +{ + static MemoryPoolObjectAllocator 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(ptr)); + void* ptr = GetRendererMemoryPool().AllocateRawThreadSafe(); + auto key = GetRendererMemoryPool().GetKeyFromPtr(static_cast(ptr)); new(ptr) Renderer(); return RendererKey(key); } @@ -119,22 +123,22 @@ Renderer::~Renderer() void Renderer::operator delete(void* ptr) { - gRendererMemoryPool.FreeThreadSafe(static_cast(ptr)); + GetRendererMemoryPool().FreeThreadSafe(static_cast(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))); + return RendererKey(GetRendererMemoryPool().GetKeyFromPtr(const_cast(&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() diff --git a/dali/internal/update/rendering/scene-graph-texture-set.cpp b/dali/internal/update/rendering/scene-graph-texture-set.cpp index 2008805..106b711 100644 --- a/dali/internal/update/rendering/scene-graph-texture-set.cpp +++ b/dali/internal/update/rendering/scene-graph-texture-set.cpp @@ -26,7 +26,11 @@ 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 gTextureSetMemoryPool; +Dali::Internal::MemoryPoolObjectAllocator& GetTextureSetMemoryPool() +{ + static Dali::Internal::MemoryPoolObjectAllocator 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(ptr)); + GetTextureSetMemoryPool().FreeThreadSafe(static_cast(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 -- 2.7.4