Add SetFrameRenderedCallback() in OffscreenWindow
[platform/core/uifw/dali-adaptor.git] / dali / internal / offscreen / common / offscreen-window-impl.cpp
index ab818ad..c55fadb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 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.
 
 // INTERNAL INCLUDES
 #include <dali/integration-api/adaptor-framework/adaptor.h>
-#include <dali/integration-api/adaptor-framework/native-render-surface.h>
 #include <dali/integration-api/adaptor-framework/native-render-surface-factory.h>
+#include <dali/integration-api/adaptor-framework/native-render-surface.h>
 #include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
+#include <dali/integration-api/debug.h>
 #include <dali/internal/offscreen/common/offscreen-application-impl.h>
 
 namespace Dali
 {
-
 namespace Internal
 {
-
-OffscreenWindow* OffscreenWindow::New( uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent )
+OffscreenWindow* OffscreenWindow::New(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent)
 {
-  OffscreenWindow* window = new OffscreenWindow( width, height, surface, isTranslucent );
+  OffscreenWindow* window = new OffscreenWindow(width, height, surface, isTranslucent);
   return window;
 }
 
-OffscreenWindow::OffscreenWindow( uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent )
+OffscreenWindow::OffscreenWindow(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent)
 : mRenderNotification()
 {
   // Create surface
-  mSurface = std::unique_ptr< RenderSurfaceInterface >( CreateNativeSurface( SurfaceSize( width, height ), surface, isTranslucent ) );
+  mSurface = std::unique_ptr<RenderSurfaceInterface>(CreateNativeSurface(SurfaceSize(width, height), surface, isTranslucent));
 }
 
-void OffscreenWindow::Initialize( bool isDefaultWindow )
+void OffscreenWindow::Initialize(bool isDefaultWindow)
 {
-  if( isDefaultWindow )
+  if(isDefaultWindow)
   {
-    Initialize();
     return;
   }
 
-  Dali::Integration::SceneHolder sceneHolderHandler = Dali::Integration::SceneHolder( this );
-  Dali::Adaptor::Get().AddWindow( sceneHolderHandler );
-
-  Initialize();
-}
-
-void OffscreenWindow::Initialize()
-{
-  // Connect callback to be notified when the surface is rendered
-  TriggerEventFactory triggerEventFactory;
-
-  mRenderNotification = std::unique_ptr< TriggerEventInterface >( triggerEventFactory.CreateTriggerEvent( MakeCallback( this, &OffscreenWindow::OnPostRender ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER ) );
-
-  NativeRenderSurface* surface = GetNativeRenderSurface();
-
-  if( !surface )
-  {
-    return;
-  }
-
-  surface->SetRenderNotification( mRenderNotification.get() );
+  Dali::Integration::SceneHolder sceneHolderHandler = Dali::Integration::SceneHolder(this);
+  Dali::Adaptor::Get().AddWindow(sceneHolderHandler);
 }
 
 OffscreenWindow::~OffscreenWindow()
 {
   NativeRenderSurface* surface = GetNativeRenderSurface();
 
-  if( surface )
+  if(surface)
   {
     // To prevent notification triggering in NativeRenderSurface::PostRender while deleting SceneHolder
-    surface->SetRenderNotification( nullptr );
+    surface->SetRenderNotification(nullptr);
   }
 }
 
@@ -94,40 +73,80 @@ uint32_t OffscreenWindow::GetLayerCount() const
   return mScene.GetLayerCount();
 }
 
-Dali::Layer OffscreenWindow::GetLayer( uint32_t depth ) const
+Dali::Layer OffscreenWindow::GetLayer(uint32_t depth) const
 {
-  return mScene.GetLayer( depth );
+  return mScene.GetLayer(depth);
 }
 
 OffscreenWindow::WindowSize OffscreenWindow::GetSize() const
 {
   Size size = mScene.GetSize();
 
-  return OffscreenWindow::WindowSize( static_cast<uint16_t>( size.width ), static_cast<uint16_t>( size.height ) );
+  return OffscreenWindow::WindowSize(static_cast<uint16_t>(size.width), static_cast<uint16_t>(size.height));
 }
 
 Dali::Any OffscreenWindow::GetNativeHandle() const
 {
   NativeRenderSurface* surface = GetNativeRenderSurface();
-  DALI_ASSERT_ALWAYS( surface && "surface handle is empty" );
+  DALI_ASSERT_ALWAYS(surface && "surface handle is empty");
 
   return surface->GetNativeRenderable();
 }
 
-NativeRenderSurface* OffscreenWindow::GetNativeRenderSurface() const
+void OffscreenWindow::SetPostRenderCallback(CallbackBase* callback)
+{
+  // Connect callback to be notified when the surface is rendered
+  mPostRenderCallback = std::unique_ptr<CallbackBase>(callback);
+  TriggerEventFactory triggerEventFactory;
+
+  if(!mRenderNotification)
+  {
+    mRenderNotification = std::unique_ptr<TriggerEventInterface>(triggerEventFactory.CreateTriggerEvent(MakeCallback(this, &OffscreenWindow::OnPostRender), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
+  }
+
+  NativeRenderSurface* surface = GetNativeRenderSurface();
+
+  if(!surface)
+  {
+    DALI_LOG_ERROR("NativeRenderSurface is null.");
+    return;
+  }
+
+  surface->SetRenderNotification(mRenderNotification.get());
+}
+
+void OffscreenWindow::SetFrameRenderedCallback(CallbackBase* callback)
 {
-  return dynamic_cast< NativeRenderSurface* >( mSurface.get() );
+  NativeRenderSurface* surface = GetNativeRenderSurface();
+
+  if(!surface)
+  {
+    DALI_LOG_ERROR("NativeRenderSurface is null.");
+    return;
+  }
+
+  surface->SetFrameRenderedCallback(callback);
 }
 
-void OffscreenWindow::OnPostRender()
+NativeRenderSurface* OffscreenWindow::GetNativeRenderSurface() const
 {
-  Dali::OffscreenWindow handle( this );
-  mPostRenderSignal.Emit( handle, GetNativeHandle() );
+  return dynamic_cast<NativeRenderSurface*>(mSurface.get());
 }
 
-OffscreenWindow::PostRenderSignalType& OffscreenWindow::PostRenderSignal()
+void OffscreenWindow::OnPostRender()
 {
-  return mPostRenderSignal;
+  NativeRenderSurface* surface = GetNativeRenderSurface();
+
+  if(!surface)
+  {
+    DALI_LOG_ERROR("NativeRenderSurface is null.");
+    return;
+  }
+
+  Dali::OffscreenWindow handle(this);
+  CallbackBase::Execute(*mPostRenderCallback, handle, surface->GetNativeRenderable());
+
+  surface->ReleaseLock();
 }
 
 } // namespace Internal