Add RemoveSystemInformation to remove callback
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / adaptor-impl.cpp
index fabadaf..c64cbdc 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.
@@ -34,6 +34,7 @@
 #include <dali/integration-api/input-options.h>
 #include <dali/integration-api/processor-interface.h>
 #include <dali/integration-api/profiling.h>
+#include <dali/integration-api/trace.h>
 #include <dali/public-api/actors/layer.h>
 #include <dali/public-api/events/wheel-event.h>
 #include <dali/public-api/object/any.h>
 #include <dali/devel-api/text-abstraction/font-client.h>
 
 #include <dali/internal/accessibility/common/tts-player-impl.h>
-#include <dali/internal/clipboard/common/clipboard-impl.h>
 #include <dali/internal/graphics/common/egl-image-extensions.h>
 #include <dali/internal/graphics/gles/egl-sync-implementation.h>
 #include <dali/internal/graphics/gles/gl-implementation.h>
 #include <dali/internal/graphics/gles/gl-proxy-implementation.h>
 #include <dali/internal/system/common/callback-manager.h>
 #include <dali/internal/system/common/object-profiler.h>
+#include <dali/internal/system/common/system-factory.h>
 #include <dali/internal/window-system/common/display-connection.h>
 #include <dali/internal/window-system/common/display-utils.h> // For Utils::MakeUnique
 #include <dali/internal/window-system/common/event-handler.h>
 #include <dali/internal/window-system/common/window-impl.h>
 #include <dali/internal/window-system/common/window-render-surface.h>
+#include <dali/internal/window-system/common/window-system.h>
 
-#include <dali/devel-api/adaptor-framework/accessibility-impl.h>
+#include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
 #include <dali/internal/system/common/logging.h>
 
 #include <dali/internal/imaging/common/image-loader-plugin-proxy.h>
 #include <dali/internal/imaging/common/image-loader.h>
 #include <dali/internal/system/common/locale-utils.h>
 
+#include <dali/devel-api/adaptor-framework/environment-variable.h>
 #include <dali/internal/system/common/configuration-manager.h>
 #include <dali/internal/system/common/environment-variables.h>
 
@@ -91,6 +94,9 @@ namespace
 {
 thread_local Adaptor* gThreadLocalAdaptor = NULL; // raw thread specific pointer to allow Adaptor::Get
 
+DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
+
+const char* ENABLE_IMAGE_LOADER_PLUGIN_ENV = "DALI_ENABLE_IMAGE_LOADER_PLUGIN";
 } // unnamed namespace
 
 Dali::Adaptor* Adaptor::New(Dali::Integration::SceneHolder window, Dali::RenderSurfaceInterface* surface, EnvironmentOptions* environmentOptions, ThreadMode threadMode)
@@ -156,7 +162,7 @@ void Adaptor::Initialize(GraphicsFactory& graphicsFactory)
   mEnvironmentOptions->CreateTraceManager(mPerformanceInterface);
   mEnvironmentOptions->InstallTraceFunction(); // install tracing for main thread
 
-  mCallbackManager = CallbackManager::New();
+  mCallbackManager = Dali::Internal::Adaptor::GetSystemFactory()->CreateCallbackManager();
 
   Dali::Internal::Adaptor::SceneHolder* defaultWindow = mWindows.front();
 
@@ -175,6 +181,9 @@ void Adaptor::Initialize(GraphicsFactory& graphicsFactory)
                                  mGraphics->GetStencilBufferRequired(),
                                  mGraphics->GetPartialUpdateRequired());
 
+  // Create TextureUploadManager after mCore created
+  mTextureUploadManager = Dali::Devel::TextureUploadManager::Get();
+
   defaultWindow->SetAdaptor(Get());
 
   Dali::Integration::SceneHolder defaultSceneHolder(defaultWindow);
@@ -187,6 +196,14 @@ void Adaptor::Initialize(GraphicsFactory& graphicsFactory)
     mObjectProfiler = new ObjectProfiler(mCore->GetObjectRegistry(), timeInterval);
   }
 
+  const uint32_t poolTimeInterval = mEnvironmentOptions->GetMemoryPoolInterval();
+  if(0u < poolTimeInterval)
+  {
+    mMemoryPoolTimer = Dali::Timer::New(poolTimeInterval * 1000);
+    mMemoryPoolTimer.TickSignal().Connect(mMemoryPoolTimerSlotDelegate, &Adaptor::MemoryPoolTimeout);
+    mMemoryPoolTimer.Start();
+  }
+
   mNotificationTrigger = TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &Adaptor::ProcessCoreEvents), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER);
 
   mDisplayConnection = Dali::DisplayConnection::New(*mGraphics, defaultWindow->GetSurface()->GetSurfaceType());
@@ -295,7 +312,6 @@ void Adaptor::Initialize(GraphicsFactory& graphicsFactory)
     Integration::SetTapMaximumAllowedTime(mEnvironmentOptions->GetTapMaximumAllowedTime());
   }
 
-
   std::string systemCachePath = GetSystemCachePath();
   if(!systemCachePath.empty())
   {
@@ -303,17 +319,10 @@ void Adaptor::Initialize(GraphicsFactory& graphicsFactory)
     if(0 != dir_err && errno != EEXIST)
     {
       DALI_LOG_ERROR("Error creating system cache directory: %s!\n", systemCachePath.c_str());
-      exit(1);
     }
   }
 
   mConfigurationManager = Utils::MakeUnique<ConfigurationManager>(systemCachePath, mGraphics.get(), mThreadController);
-
-  auto appName = GetApplicationPackageName();
-  auto bridge  = Accessibility::Bridge::GetCurrentBridge();
-  bridge->SetApplicationName(appName);
-  bridge->Initialize();
-  Dali::Stage::GetCurrent().KeyEventSignal().Connect(&mAccessibilityObserver, &AccessibilityObserver::OnAccessibleKeyEvent);
 }
 
 void Adaptor::AccessibilityObserver::OnAccessibleKeyEvent(const Dali::KeyEvent& event)
@@ -359,7 +368,9 @@ Adaptor::~Adaptor()
 
   delete mDisplayConnection;
   delete mPlatformAbstraction;
-  delete mCallbackManager;
+
+  mCallbackManager.reset();
+
   delete mPerformanceInterface;
 
   mGraphics->Destroy();
@@ -389,16 +400,20 @@ void Adaptor::Start()
   // Start the callback manager
   mCallbackManager->Start();
 
+  // Initialize accessibility bridge after callback manager is started to use Idler callback
+  auto appName = GetApplicationPackageName();
+  auto bridge  = Accessibility::Bridge::GetCurrentBridge();
+  bridge->SetApplicationName(appName);
+  bridge->Initialize();
+  Dali::Stage::GetCurrent().KeyEventSignal().Connect(&mAccessibilityObserver, &AccessibilityObserver::OnAccessibleKeyEvent);
+
   Dali::Internal::Adaptor::SceneHolder* defaultWindow = mWindows.front();
 
   unsigned int dpiHor, dpiVer;
   dpiHor = dpiVer = 0;
 
   defaultWindow->GetSurface()->GetDpi(dpiHor, dpiVer);
-
-  // set the DPI value for font rendering
-  FontClient fontClient = FontClient::Get();
-  fontClient.SetDpi(dpiHor, dpiVer);
+  Dali::Internal::Adaptor::WindowSystem::SetDpi(dpiHor, dpiVer);
 
   // Initialize the thread controller
   mThreadController->Initialize();
@@ -420,7 +435,12 @@ void Adaptor::Start()
   ProcessCoreEvents(); // Ensure any startup messages are processed.
 
   // Initialize the image loader plugin
-  Internal::Adaptor::ImageLoaderPluginProxy::Initialize();
+  auto enablePluginString = Dali::EnvironmentVariable::GetEnvironmentVariable(ENABLE_IMAGE_LOADER_PLUGIN_ENV);
+  bool enablePlugin       = enablePluginString ? std::atoi(enablePluginString) : false;
+  if(enablePlugin)
+  {
+    Internal::Adaptor::ImageLoaderPluginProxy::Initialize();
+  }
 
   for(ObserverContainer::iterator iter = mObservers.begin(), endIter = mObservers.end(); iter != endIter; ++iter)
   {
@@ -539,7 +559,12 @@ void Adaptor::Stop()
     }
 
     // Destroy the image loader plugin
-    Internal::Adaptor::ImageLoaderPluginProxy::Destroy();
+    auto enablePluginString = Dali::EnvironmentVariable::GetEnvironmentVariable(ENABLE_IMAGE_LOADER_PLUGIN_ENV);
+    bool enablePlugin       = enablePluginString ? std::atoi(enablePluginString) : false;
+    if(enablePlugin)
+    {
+      Internal::Adaptor::ImageLoaderPluginProxy::Destroy();
+    }
 
     delete mNotificationTrigger;
     mNotificationTrigger = NULL;
@@ -548,6 +573,8 @@ void Adaptor::Stop()
 
     mState = STOPPED;
 
+    RemoveSystemInformation();
+
     DALI_LOG_RELEASE_INFO("Adaptor::Stop\n");
   }
 }
@@ -646,6 +673,12 @@ bool Adaptor::AddIdle(CallbackBase* callback, bool hasReturnValue, bool forceAdd
     idleAdded = mCallbackManager->AddIdleCallback(callback, hasReturnValue);
   }
 
+  if(!idleAdded)
+  {
+    // Delete callback
+    delete callback;
+  }
+
   return idleAdded;
 }
 
@@ -674,7 +707,10 @@ bool Adaptor::AddWindow(Dali::Integration::SceneHolder childWindow)
   windowImpl.GetRootLayer().SetProperty(Dali::Actor::Property::LAYOUT_DIRECTION, mRootLayoutDirection);
 
   // Add the new Window to the container - the order is not important
-  mWindows.push_back(&windowImpl);
+  {
+    Dali::Mutex::ScopedLock lock(mMutex);
+    mWindows.push_back(&windowImpl);
+  }
 
   Dali::RenderSurfaceInterface* surface = windowImpl.GetSurface();
 
@@ -692,6 +728,7 @@ bool Adaptor::RemoveWindow(Dali::Integration::SceneHolder* childWindow)
   {
     if(*iter == &windowImpl)
     {
+      Dali::Mutex::ScopedLock lock(mMutex);
       mWindows.erase(iter);
       return true;
     }
@@ -706,6 +743,7 @@ bool Adaptor::RemoveWindow(std::string childWindowName)
   {
     if((*iter)->GetName() == childWindowName)
     {
+      Dali::Mutex::ScopedLock lock(mMutex);
       mWindows.erase(iter);
       return true;
     }
@@ -720,6 +758,7 @@ bool Adaptor::RemoveWindow(Internal::Adaptor::SceneHolder* childWindow)
   {
     if((*iter)->GetId() == childWindow->GetId())
     {
+      Dali::Mutex::ScopedLock lock(mMutex);
       mWindows.erase(iter);
       return true;
     }
@@ -814,9 +853,15 @@ Integration::PlatformAbstraction& Adaptor::GetPlatformAbstraction() const
 
 void Adaptor::GetWindowContainerInterface(WindowContainer& windows)
 {
+  Dali::Mutex::ScopedLock lock(mMutex);
   windows = mWindows;
 }
 
+Devel::TextureUploadManager& Adaptor::GetTextureUploadManager()
+{
+  return mTextureUploadManager;
+}
+
 void Adaptor::DestroyTtsPlayer(Dali::TtsPlayer::Mode mode)
 {
   if(mTtsPlayers[mode])
@@ -897,10 +942,22 @@ void Adaptor::QueueCoreEvent(const Dali::Integration::Event& event)
   }
 }
 
+void Adaptor::FlushUpdateMessages()
+{
+  if(mCore)
+  {
+    DALI_TRACE_SCOPE(gTraceFilter, "DALI_FLUSH_UPDATE_MESSAGES");
+
+    mCore->ForceRelayout();
+  }
+}
+
 void Adaptor::ProcessCoreEvents()
 {
   if(mCore)
   {
+    DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_CORE_EVENTS");
+
     if(mPerformanceInterface)
     {
       mPerformanceInterface->AddMarker(PerformanceInterface::PROCESS_EVENTS_START);
@@ -915,7 +972,7 @@ void Adaptor::ProcessCoreEvents()
   }
 }
 
-void Adaptor::RequestUpdate(bool forceUpdate)
+void Adaptor::RequestUpdate()
 {
   switch(mState)
   {
@@ -927,11 +984,8 @@ void Adaptor::RequestUpdate(bool forceUpdate)
     case PAUSED:
     case PAUSED_WHILE_HIDDEN:
     {
-      if(forceUpdate)
-      {
-        // Update (and resource upload) without rendering
-        mThreadController->RequestUpdateOnce(UpdateMode::SKIP_RENDER);
-      }
+      // Update (and resource upload) without rendering
+      mThreadController->RequestUpdateOnce(UpdateMode::SKIP_RENDER);
       break;
     }
     default:
@@ -942,20 +996,27 @@ void Adaptor::RequestUpdate(bool forceUpdate)
   }
 }
 
-void Adaptor::RequestProcessEventsOnIdle(bool forceProcess)
+void Adaptor::RequestProcessEventsOnIdle()
 {
-  // Only request a notification if the Adaptor is actually running
-  // and we haven't installed the idle notification
-  if((!mNotificationOnIdleInstalled) && (RUNNING == mState || READY == mState || forceProcess))
+  // We want to run the processes even when paused
+  if(STOPPED != mState)
   {
-    mNotificationOnIdleInstalled = AddIdleEnterer(MakeCallback(this, &Adaptor::ProcessCoreEventsFromIdle), forceProcess);
+    if(!mNotificationOnIdleInstalled)
+    {
+      // If we haven't installed the idle notification, install it idle enterer.
+      mNotificationOnIdleInstalled = AddIdleEnterer(MakeCallback(this, &Adaptor::ProcessCoreEventsFromIdle), true);
+    }
+    else
+    {
+      // Request comes during ProcessCoreEventsFromIdle running.
+      // Mark as we need to call ProcessEvents in next idle events.
+      mRequiredIdleRepeat = true;
+    }
   }
 }
 
 void Adaptor::OnWindowShown()
 {
-  Dali::Accessibility::Bridge::GetCurrentBridge()->ApplicationShown();
-
   if(PAUSED_WHILE_HIDDEN == mState)
   {
     // Adaptor can now be resumed
@@ -986,8 +1047,6 @@ void Adaptor::OnWindowShown()
 
 void Adaptor::OnWindowHidden()
 {
-  Dali::Accessibility::Bridge::GetCurrentBridge()->ApplicationHidden();
-
   if(RUNNING == mState || READY == mState)
   {
     bool allWindowsHidden = true;
@@ -1032,7 +1091,7 @@ void Adaptor::OnWindowHidden()
 void Adaptor::OnDamaged(const DamageArea& area)
 {
   // This is needed for the case where Dali window is partially obscured
-  RequestUpdate(false);
+  RequestUpdate();
 }
 
 void Adaptor::SurfaceResizePrepare(Dali::RenderSurfaceInterface* surface, SurfaceSize surfaceSize)
@@ -1102,6 +1161,13 @@ void Adaptor::RenderOnce()
 
       ProcessCoreEvents();
     }
+
+    // Force rendering
+    for(auto&& iter : mWindows)
+    {
+      iter->GetSurface()->SetFullSwapNextFrame();
+    }
+
     mThreadController->RequestUpdateOnce(updateMode);
   }
 }
@@ -1111,6 +1177,11 @@ const LogFactoryInterface& Adaptor::GetLogFactory()
   return *mEnvironmentOptions;
 }
 
+const TraceFactoryInterface& Adaptor::GetTraceFactory()
+{
+  return *mEnvironmentOptions;
+}
+
 void Adaptor::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
 {
   GetCore().RegisterProcessor(processor, postProcessor);
@@ -1126,6 +1197,15 @@ bool Adaptor::IsMultipleWindowSupported() const
   return mConfigurationManager->IsMultipleWindowSupported();
 }
 
+int32_t Adaptor::GetRenderThreadId() const
+{
+  if(mThreadController)
+  {
+    return mThreadController->GetThreadId();
+  }
+  return 0;
+}
+
 void Adaptor::RequestUpdateOnce()
 {
   if(mThreadController)
@@ -1136,12 +1216,20 @@ void Adaptor::RequestUpdateOnce()
 
 bool Adaptor::ProcessCoreEventsFromIdle()
 {
+  // Reset repeat idler flag.
+  mRequiredIdleRepeat = false;
   ProcessCoreEvents();
 
-  // the idle handle automatically un-installs itself
-  mNotificationOnIdleInstalled = false;
+  // If someone request ProcessCoreEvents during above ProcessCoreEvents call, we might need to run idle one more times.
+  // Else, the idle handle automatically un-installs itself
+  mNotificationOnIdleInstalled = mRequiredIdleRepeat;
 
-  return false;
+  if(mRequiredIdleRepeat)
+  {
+    DALI_LOG_DEBUG_INFO("Required ProcessCoreEvents one more times\n");
+  }
+
+  return mRequiredIdleRepeat;
 }
 
 Dali::Internal::Adaptor::SceneHolder* Adaptor::GetWindow(Dali::Actor& actor)
@@ -1213,6 +1301,7 @@ Adaptor::Adaptor(Dali::Integration::SceneHolder window, Dali::Adaptor& adaptor,
   mPlatformAbstraction(nullptr),
   mCallbackManager(nullptr),
   mNotificationOnIdleInstalled(false),
+  mRequiredIdleRepeat(false),
   mNotificationTrigger(nullptr),
   mDaliFeedbackPlugin(),
   mFeedbackController(nullptr),
@@ -1222,8 +1311,11 @@ Adaptor::Adaptor(Dali::Integration::SceneHolder window, Dali::Adaptor& adaptor,
   mPerformanceInterface(nullptr),
   mKernelTracer(),
   mSystemTracer(),
+  mTextureUploadManager(),
   mObjectProfiler(nullptr),
+  mMemoryPoolTimerSlotDelegate(this),
   mSocketFactory(),
+  mMutex(),
   mThreadMode(threadMode),
   mEnvironmentOptionsOwned(environmentOptions ? false : true /* If not provided then we own the object */),
   mUseRemoteSurface(false),
@@ -1269,6 +1361,12 @@ void Adaptor::RemoveIdleEnterer(CallbackBase* callback)
   mCallbackManager->RemoveIdleEntererCallback(callback);
 }
 
+bool Adaptor::MemoryPoolTimeout()
+{
+  mCore->LogMemoryPools();
+  return true; // Keep logging forever
+}
+
 } // namespace Adaptor
 
 } // namespace Internal