(Vector) Use Processor to reduce delay when the resource is ready 34/216134/6
authorHeeyong Song <heeyong.song@samsung.com>
Mon, 21 Oct 2019 08:42:52 +0000 (17:42 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Tue, 5 Nov 2019 06:00:06 +0000 (15:00 +0900)
Change-Id: I15100403644245b444a512b662f12c8b3dd85dda

dali-extension/vector-animation-renderer/file.list
dali-extension/vector-animation-renderer/tizen-vector-animation-event-handler.h [new file with mode: 0755]
dali-extension/vector-animation-renderer/tizen-vector-animation-manager.cpp [new file with mode: 0755]
dali-extension/vector-animation-renderer/tizen-vector-animation-manager.h [new file with mode: 0755]
dali-extension/vector-animation-renderer/tizen-vector-animation-renderer.cpp
dali-extension/vector-animation-renderer/tizen-vector-animation-renderer.h

index 18142b1..ac36d00 100644 (file)
@@ -1,2 +1,4 @@
 vector_animation_renderer_plugin_src_files = \
-   $(extension_src_dir)/vector-animation-renderer/tizen-vector-animation-renderer.cpp
+   $(extension_src_dir)/vector-animation-renderer/tizen-vector-animation-renderer.cpp \
+   $(extension_src_dir)/vector-animation-renderer/tizen-vector-animation-manager.cpp
+
diff --git a/dali-extension/vector-animation-renderer/tizen-vector-animation-event-handler.h b/dali-extension/vector-animation-renderer/tizen-vector-animation-event-handler.h
new file mode 100755 (executable)
index 0000000..869d2fd
--- /dev/null
@@ -0,0 +1,68 @@
+#ifndef DALI_TIZEN_VECTOR_ANIMATION_EVENT_HANDLER_H
+#define DALI_TIZEN_VECTOR_ANIMATION_EVENT_HANDLER_H
+
+/*
+ * Copyright (c) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+
+namespace Dali
+{
+
+namespace Plugin
+{
+
+/**
+ * @brief Tizen vector animation event handler
+ */
+class TizenVectorAnimationEventHandler
+{
+public:
+
+  /**
+   * @brief Notify events
+   */
+  virtual void NotifyEvent() = 0;
+
+protected:
+
+  /**
+   * constructor
+   */
+  TizenVectorAnimationEventHandler()
+  {
+  }
+
+  /**
+   * virtual destructor
+   */
+  virtual ~TizenVectorAnimationEventHandler()
+  {
+  }
+
+  // Undefined copy constructor.
+  TizenVectorAnimationEventHandler( const TizenVectorAnimationEventHandler& ) = delete;
+
+  // Undefined assignment operator.
+  TizenVectorAnimationEventHandler& operator=( const TizenVectorAnimationEventHandler& ) = delete;
+};
+
+} // namespace Plugin
+
+} // namespace Dali;
+
+#endif // DALI_TIZEN_VECTOR_ANIMATION_EVENT_HANDLER_H
diff --git a/dali-extension/vector-animation-renderer/tizen-vector-animation-manager.cpp b/dali-extension/vector-animation-renderer/tizen-vector-animation-manager.cpp
new file mode 100755 (executable)
index 0000000..c9e163c
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// CLASS HEADER
+#include <dali-extension/vector-animation-renderer/tizen-vector-animation-manager.h>
+
+// EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+
+namespace Dali
+{
+
+namespace Plugin
+{
+
+TizenVectorAnimationManager& TizenVectorAnimationManager::Get()
+{
+  static TizenVectorAnimationManager animationManager;
+  return animationManager;
+}
+
+TizenVectorAnimationManager::TizenVectorAnimationManager()
+: mEventHandlers(),
+  mTriggeredHandlers(),
+  mMutex(),
+  mEventTrigger( new EventThreadCallback( MakeCallback( this, &TizenVectorAnimationManager::OnEventTriggered ) ) )
+{
+}
+
+TizenVectorAnimationManager::~TizenVectorAnimationManager()
+{
+}
+
+void TizenVectorAnimationManager::AddEventHandler( TizenVectorAnimationEventHandler& handler )
+{
+  if( mEventHandlers.end() == std::find( mEventHandlers.begin(), mEventHandlers.end(), &handler ) )
+  {
+    if( mEventHandlers.empty() )
+    {
+      Adaptor::Get().RegisterProcessor( *this );
+    }
+
+    mEventHandlers.push_back( &handler );
+  }
+}
+
+void TizenVectorAnimationManager::RemoveEventHandler( TizenVectorAnimationEventHandler& handler )
+{
+  auto iter = std::find( mEventHandlers.begin(), mEventHandlers.end(), &handler );
+  if( iter != mEventHandlers.end() )
+  {
+    mEventHandlers.erase( iter );
+
+    if( mEventHandlers.empty() )
+    {
+      if( Adaptor::IsAvailable() )
+      {
+        Adaptor::Get().UnregisterProcessor( *this );
+      }
+    }
+  }
+}
+
+void TizenVectorAnimationManager::TriggerEvent( TizenVectorAnimationEventHandler& handler )
+{
+  Dali::Mutex::ScopedLock lock( mMutex );
+
+  if( mTriggeredHandlers.end() == std::find( mTriggeredHandlers.begin(), mTriggeredHandlers.end(), &handler ) )
+  {
+    mTriggeredHandlers.push_back( &handler );
+
+    mEventTrigger->Trigger();
+  }
+}
+
+void TizenVectorAnimationManager::Process()
+{
+  OnEventTriggered();
+}
+
+void TizenVectorAnimationManager::OnEventTriggered()
+{
+  Dali::Mutex::ScopedLock lock( mMutex );
+
+  for( auto&& iter : mTriggeredHandlers )
+  {
+    iter->NotifyEvent();
+  }
+
+  mTriggeredHandlers.clear();
+}
+
+} // namespace Plugin
+
+} // namespace Dali;
diff --git a/dali-extension/vector-animation-renderer/tizen-vector-animation-manager.h b/dali-extension/vector-animation-renderer/tizen-vector-animation-manager.h
new file mode 100755 (executable)
index 0000000..aa6d63f
--- /dev/null
@@ -0,0 +1,114 @@
+#ifndef DALI_TIZEN_VECTOR_ANIMATION_MANAGER_H
+#define DALI_TIZEN_VECTOR_ANIMATION_MANAGER_H
+
+/*
+ * Copyright (c) 2019 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <dali/public-api/common/vector-wrapper.h>
+#include <dali/devel-api/threading/mutex.h>
+#include <dali/devel-api/adaptor-framework/event-thread-callback.h>
+#include <dali/integration-api/processor-interface.h>
+#include <memory>
+
+// INTERNAL INCLUDES
+#include <dali-extension/vector-animation-renderer/tizen-vector-animation-event-handler.h>
+
+namespace Dali
+{
+
+namespace Plugin
+{
+
+/**
+ * @brief Tizen vector animation manager
+ */
+class TizenVectorAnimationManager : public Integration::Processor
+{
+public:
+
+  /**
+   * @brief Create or retrieve TizenVectorAnimationManager singleton.
+   *
+   * @return A reference to the TizenVectorAnimationManager.
+   */
+  static TizenVectorAnimationManager& Get();
+
+  /**
+   * @brief Add the event handler.
+   *
+   * @param handler The event handler to add.
+   */
+  void AddEventHandler( TizenVectorAnimationEventHandler& handler );
+
+  /**
+   * @brief Remove the event handler.
+   *
+   * @param handler The event handler to remove.
+   */
+  void RemoveEventHandler( TizenVectorAnimationEventHandler& handler );
+
+  /**
+   * @brief Trigger the event.
+   */
+  void TriggerEvent( TizenVectorAnimationEventHandler& handler );
+
+protected: // Implementation of Processor
+
+  /**
+   * @copydoc Dali::Integration::Processor::Process()
+   */
+  void Process() override;
+
+private:
+
+  /**
+   * @brief Event callback to process events.
+   */
+  void OnEventTriggered();
+
+private:
+
+  /**
+   * @brief Constructor.
+   */
+  TizenVectorAnimationManager();
+
+  /**
+   * @brief Destructor.
+   */
+  virtual ~TizenVectorAnimationManager();
+
+  // Undefined
+  TizenVectorAnimationManager( const TizenVectorAnimationManager& ) = delete;
+
+  // Undefined
+  TizenVectorAnimationManager& operator=( const TizenVectorAnimationManager& ) = delete;
+
+private:
+
+  std::vector< TizenVectorAnimationEventHandler* > mEventHandlers;
+  std::vector< TizenVectorAnimationEventHandler* > mTriggeredHandlers;
+  Dali::Mutex                                      mMutex;
+  std::unique_ptr< EventThreadCallback >           mEventTrigger;
+};
+
+} // namespace Plugin
+
+} // namespace Dali;
+
+#endif // DALI_TIZEN_VECTOR_ANIMATION_MANAGER_H
index 416041d..70719c0 100755 (executable)
@@ -26,6 +26,9 @@
 #include <cstring> // for strlen()
 #include <tbm_surface_internal.h>
 
+// INTERNAL INCLUDES
+#include <dali-extension/vector-animation-renderer/tizen-vector-animation-manager.h>
+
 // The plugin factories
 extern "C" DALI_EXPORT_API Dali::VectorAnimationRendererPlugin* CreateVectorAnimationRendererPlugin( void )
 {
@@ -57,7 +60,6 @@ TizenVectorAnimationRenderer::TizenVectorAnimationRenderer()
   mRenderedTexture(),
   mTargetSurface(),
   mVectorRenderer(),
-  mResourceReadyTrigger( new EventThreadCallback( MakeCallback( this, &TizenVectorAnimationRenderer::OnResourceReady ) ) ),
   mUploadCompletedSignal(),
   mTbmQueue( NULL ),
   mTotalFrameNumber( 0 ),
@@ -67,7 +69,8 @@ TizenVectorAnimationRenderer::TizenVectorAnimationRenderer()
   mDefaultHeight( 0 ),
   mFrameRate( 60.0f ),
   mResourceReady( false ),
-  mShaderChanged( false )
+  mShaderChanged( false ),
+  mResourceReadyTriggered( false )
 {
 }
 
@@ -76,6 +79,8 @@ TizenVectorAnimationRenderer::~TizenVectorAnimationRenderer()
   Dali::Mutex::ScopedLock lock( mMutex );
 
   ResetBuffers();
+
+  TizenVectorAnimationManager::Get().RemoveEventHandler( *this );
 }
 
 bool TizenVectorAnimationRenderer::Initialize( const std::string& url )
@@ -97,6 +102,8 @@ bool TizenVectorAnimationRenderer::Initialize( const std::string& url )
   mDefaultWidth = static_cast< uint32_t >( w );
   mDefaultHeight = static_cast< uint32_t >( h );
 
+  TizenVectorAnimationManager::Get().AddEventHandler( *this );
+
   DALI_LOG_RELEASE_INFO( "TizenVectorAnimationRenderer::Initialize: file [%s] [%p]\n", url.c_str(), this );
 
   return true;
@@ -157,10 +164,10 @@ void TizenVectorAnimationRenderer::SetSize( uint32_t width, uint32_t height )
 
 bool TizenVectorAnimationRenderer::Render( uint32_t frameNumber )
 {
+  Dali::Mutex::ScopedLock lock( mMutex );
+
   if( tbm_surface_queue_can_dequeue( mTbmQueue, 0 ) )
   {
-    Dali::Mutex::ScopedLock lock( mMutex );
-
     tbm_surface_h tbmSurface;
 
     if( tbm_surface_queue_dequeue( mTbmQueue, &tbmSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
@@ -218,18 +225,17 @@ bool TizenVectorAnimationRenderer::Render( uint32_t frameNumber )
     {
       mRenderedTexture = mTexture;
       mResourceReady = true;
+      mResourceReadyTriggered = true;
 
-      mResourceReadyTrigger->Trigger();
+      TizenVectorAnimationManager::Get().TriggerEvent( *this );
 
       DALI_LOG_RELEASE_INFO( "TizenVectorAnimationRenderer::Render: Resource ready [current = %d] [%p]\n", frameNumber, this );
     }
-  }
-  else
-  {
-    return false;
+
+    return true;
   }
 
-  return true;
+  return false;
 }
 
 uint32_t TizenVectorAnimationRenderer::GetTotalFrameNumber() const
@@ -252,6 +258,8 @@ void TizenVectorAnimationRenderer::GetDefaultSize( uint32_t& width, uint32_t& he
 
 void TizenVectorAnimationRenderer::GetLayerInfo( Property::Map& map ) const
 {
+  Dali::Mutex::ScopedLock lock( mMutex );
+
   auto layerInfo = mVectorRenderer->layers();
 
   for( auto&& iter : layerInfo )
@@ -268,6 +276,27 @@ VectorAnimationRendererPlugin::UploadCompletedSignalType& TizenVectorAnimationRe
   return mUploadCompletedSignal;
 }
 
+void TizenVectorAnimationRenderer::NotifyEvent()
+{
+  Dali::Mutex::ScopedLock lock( mMutex );
+
+  if( mResourceReadyTriggered )
+  {
+    DALI_LOG_RELEASE_INFO( "TizenVectorAnimationRenderer::NotifyEvent: Set Texture [%p]\n", this );
+
+    // Set texture
+    if( mRenderer )
+    {
+      TextureSet textureSet = mRenderer.GetTextures();
+      textureSet.SetTexture( 0, mRenderedTexture );
+    }
+
+    mResourceReadyTriggered = false;
+
+    mUploadCompletedSignal.Emit();
+  }
+}
+
 void TizenVectorAnimationRenderer::SetShader()
 {
   if( mShaderChanged )
@@ -339,20 +368,6 @@ void TizenVectorAnimationRenderer::ResetBuffers()
   mBuffers.clear();
 }
 
-void TizenVectorAnimationRenderer::OnResourceReady()
-{
-  DALI_LOG_RELEASE_INFO( "TizenVectorAnimationRenderer::OnResourceReady: Set Texture [%p]\n", this );
-
-  // Set texture
-  if( mRenderer )
-  {
-    TextureSet textureSet = mRenderer.GetTextures();
-    textureSet.SetTexture( 0, mRenderedTexture );
-  }
-
-  mUploadCompletedSignal.Emit();
-}
-
 } // namespace Plugin
 
 } // namespace Dali;
index 9b61e01..dbb3765 100755 (executable)
  */
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/math/uint-16-pair.h>
 #include <dali/public-api/common/vector-wrapper.h>
 #include <dali/devel-api/threading/mutex.h>
-#include <dali/devel-api/adaptor-framework/event-thread-callback.h>
 #include <dali/devel-api/adaptor-framework/native-image-source-queue.h>
 #include <dali/devel-api/adaptor-framework/vector-animation-renderer-plugin.h>
 #include <memory>
@@ -30,6 +28,9 @@
 #include <tbm_surface.h>
 #include <tbm_surface_queue.h>
 
+// INTERNAL INCLUDES
+#include <dali-extension/vector-animation-renderer/tizen-vector-animation-event-handler.h>
+
 namespace Dali
 {
 
@@ -39,7 +40,7 @@ namespace Plugin
 /**
  * @brief Implementation of the Tizen vector animation renderer class which has Tizen platform dependency.
  */
-class TizenVectorAnimationRenderer : public Dali::VectorAnimationRendererPlugin
+class TizenVectorAnimationRenderer : public Dali::VectorAnimationRendererPlugin, public TizenVectorAnimationEventHandler
 {
 public:
 
@@ -98,6 +99,13 @@ public:
    */
   UploadCompletedSignalType& UploadCompletedSignal() override;
 
+protected: // Implementation of TizenVectorAnimationEventHandler
+
+  /**
+   * @copydoc Dali::Plugin::TizenVectorAnimationEventHandler::NotifyEvent()
+   */
+  void NotifyEvent() override;
+
 private:
 
   /**
@@ -110,24 +118,18 @@ private:
    */
   void ResetBuffers();
 
-  /**
-   * @brief Event callback from rasterize thread. This is called after the first frame is ready.
-   */
-  void OnResourceReady();
-
 private:
 
   using SurfacePair = std::pair< tbm_surface_h, rlottie::Surface >;
 
   std::string                            mUrl;                   ///< The content file path
   std::vector< SurfacePair >             mBuffers;               ///< EGL Image vector
-  Dali::Mutex                            mMutex;                 ///< Mutex
+  mutable Dali::Mutex                    mMutex;                 ///< Mutex
   Dali::Renderer                         mRenderer;              ///< Renderer
   Dali::Texture                          mTexture;               ///< Texture
   Dali::Texture                          mRenderedTexture;       ///< Rendered Texture
   NativeImageSourceQueuePtr              mTargetSurface;         ///< The target surface
   std::unique_ptr< rlottie::Animation >  mVectorRenderer;        ///< The vector animation renderer
-  std::unique_ptr< EventThreadCallback > mResourceReadyTrigger;  ///< Resource ready trigger
   UploadCompletedSignalType              mUploadCompletedSignal; ///< Upload completed signal
   tbm_surface_queue_h                    mTbmQueue;              ///< Tbm surface queue handle
   uint32_t                               mTotalFrameNumber;      ///< The total frame number
@@ -138,6 +140,7 @@ private:
   float                                  mFrameRate;             ///< The frame rate of the content
   bool                                   mResourceReady;         ///< Whether the resource is ready
   bool                                   mShaderChanged;         ///< Whether the shader is changed to support native image
+  bool                                   mResourceReadyTriggered;///< Whether the resource ready is triggered
 };
 
 } // namespace Plugin