Replace POSIX unnamed semaphores by dali-core primitives
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.h
index cfcb25b..8c2da95 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_COMBINED_UPDATE_RENDER_CONTROLLER_H
 
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
  */
 
 // EXTERNAL INCLUDES
+#include <pthread.h>
 #include <semaphore.h>
+#include <atomic>
 #include <stdint.h>
-#include <dali/integration-api/core.h>
 #include <dali/devel-api/threading/conditional-wait.h>
+#include <dali/devel-api/threading/semaphore.h>
+#include <dali/integration-api/core.h>
 
 // INTERNAL INCLUDES
-#include <dali/integration-api/thread-synchronization-interface.h>
-#include <dali/internal/system/common/performance-interface.h>
-#include <dali/internal/system/common/fps-tracker.h>
+#include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
 #include <dali/internal/adaptor/common/thread-controller-interface.h>
+#include <dali/internal/system/common/fps-tracker.h>
+#include <dali/internal/system/common/performance-interface.h>
 #include <dali/internal/system/common/update-status-logger.h>
 #include <dali/internal/window-system/common/display-connection.h>
 
-
 namespace Dali
 {
 
@@ -79,7 +81,7 @@ public:
   /**
    * Constructor
    */
-  CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions );
+  CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions, ThreadMode threadMode );
 
   /**
    * Non virtual destructor. Not intended as base class.
@@ -89,63 +91,73 @@ public:
   /**
    * @copydoc ThreadControllerInterface::Initialize()
    */
-  virtual void Initialize();
+  void Initialize() override;
 
   /**
    * @copydoc ThreadControllerInterface::Start()
    */
-  virtual void Start();
+  void Start() override;
 
   /**
    * @copydoc ThreadControllerInterface::Pause()
    */
-  virtual void Pause();
+  void Pause() override;
 
   /**
    * @copydoc ThreadControllerInterface::Resume()
    */
-  virtual void Resume();
+  void Resume() override;
 
   /**
    * @copydoc ThreadControllerInterface::Stop()
    */
-  virtual void Stop();
+  void Stop() override;
 
   /**
    * @copydoc ThreadControllerInterface::RequestUpdate()
    */
-  virtual void RequestUpdate();
+  void RequestUpdate() override;
 
   /**
    * @copydoc ThreadControllerInterface::RequestUpdateOnce()
    */
-  virtual void RequestUpdateOnce( UpdateMode updateMode );
+  void RequestUpdateOnce( UpdateMode updateMode ) override;
 
   /**
    * @copydoc ThreadControllerInterface::ReplaceSurface()
    */
-  virtual void ReplaceSurface( Dali::RenderSurfaceInterface* surface );
+  void ReplaceSurface( Dali::RenderSurfaceInterface* surface ) override;
 
   /**
    * @copydoc ThreadControllerInterface::DeleteSurface()
    */
-  virtual void DeleteSurface( Dali::RenderSurfaceInterface* surface );
+  void DeleteSurface( Dali::RenderSurfaceInterface* surface ) override;
 
   /**
    * @copydoc ThreadControllerInterface::ResizeSurface()
    */
-  virtual void ResizeSurface();
+  void ResizeSurface() override;
+
+  /**
+   * @copydoc ThreadControllerInterface::WaitForGraphicsInitialization()
+   */
+  void WaitForGraphicsInitialization() override;
 
   /**
    * @copydoc ThreadControllerInterface::SetRenderRefreshRate()
    */
-  virtual void SetRenderRefreshRate( unsigned int numberOfFramesPerRender );
+  void SetRenderRefreshRate( unsigned int numberOfFramesPerRender ) override;
 
   /**
    * @copydoc ThreadControllerInterface::SetPreRenderCallback
    */
   void SetPreRenderCallback( CallbackBase* callback ) override;
 
+  /**
+   * @copydoc ThreadControllerInterface::AddSurface()
+   */
+  void AddSurface( Dali::RenderSurfaceInterface* surface ) override;
+
 private:
 
   // Undefined copy constructor.
@@ -229,7 +241,7 @@ private:
    *
    * @return Pointer to the new surface, NULL otherwise
    */
-  Integration::RenderSurface* ShouldSurfaceBeReplaced();
+  Dali::RenderSurfaceInterface* ShouldSurfaceBeReplaced();
 
   /**
    * Called by the Update/Render thread after a surface has been replaced.
@@ -244,7 +256,7 @@ private:
    *
    * @return Pointer to the deleted surface, nullptr otherwise
    */
-  Integration::RenderSurface* ShouldSurfaceBeDeleted();
+  Dali::RenderSurfaceInterface* ShouldSurfaceBeDeleted();
 
   /**
    * Called by the Update/Render thread after a surface has been deleted.
@@ -290,6 +302,11 @@ private:
   void NotifyThreadInitialised();
 
   /**
+   * Called by the update-render thread when graphics has been initialised.
+   */
+  void NotifyGraphicsInitialised();
+
+  /**
    * Helper to add a performance marker to the performance server (if it's active)
    * @param[in]  type  performance marker type
    */
@@ -306,7 +323,7 @@ private:
   /**
    * @copydoc ThreadSynchronizationInterface::PostRenderComplete()
    */
-  virtual void PostRenderComplete();
+  void PostRenderComplete() override;
 
   /////////////////////////////////////////////////////////////////////////////////////////////////
   //// Called by the Render Thread if post-rendering is required
@@ -315,19 +332,21 @@ private:
   /**
    * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
    */
-  virtual void PostRenderStarted();
+  void PostRenderStarted() override;
 
   /**
    * @copydoc ThreadSynchronizationInterface::PostRenderStarted()
    */
-  virtual void PostRenderWaitForCompletion();
+  void PostRenderWaitForCompletion() override;
 
 private:
 
   FpsTracker                        mFpsTracker;                       ///< Object that tracks the FPS
   UpdateStatusLogger                mUpdateStatusLogger;               ///< Object that logs the update-status as required.
 
-  sem_t                             mEventThreadSemaphore;             ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
+  Semaphore<>                       mEventThreadSemaphore;             ///< Used by the event thread to ensure all threads have been initialised, and when replacing the surface.
+  ConditionalWait                   mGraphicsInitializeWait;           ///< Used by the render thread to ensure the graphics has been initialised.
+  Semaphore<>                       mSurfaceSemaphore;                 ///< Used by the event thread to ensure the surface has been deleted or replaced.
 
   ConditionalWait                   mUpdateRenderThreadWaitCondition;  ///< The wait condition for the update-render-thread.
 
@@ -350,6 +369,8 @@ private:
   unsigned int                      mUpdateRequestCount;               ///< Count of update-requests we have received to ensure we do not go to sleep too early.
   unsigned int                      mRunning;                          ///< Read and set on the event-thread only to state whether we are running.
 
+  ThreadMode                        mThreadMode;                       ///< Whether the thread runs continuously or runs when it is requested.
+
   //
   // NOTE: cannot use booleans as these are used from multiple threads, must use variable with machine word size for atomic read/write
   //
@@ -362,8 +383,8 @@ private:
 
   volatile unsigned int             mUseElapsedTimeAfterWait;          ///< Whether we should use the elapsed time after waiting (set by the event-thread, read by the update-render-thread).
 
-  Integration::RenderSurface* volatile mNewSurface;                    ///< Will be set to the new-surface if requested (set by the event-thread, read & cleared by the update-render thread).
-  Integration::RenderSurface* volatile mDeletedSurface;                ///< Will be set to the deleted surface if requested (set by the event-thread, read & cleared by the update-render thread).
+  Dali::RenderSurfaceInterface* volatile mNewSurface;                  ///< Will be set to the new-surface if requested (set by the event-thread, read & cleared by the update-render thread).
+  Dali::RenderSurfaceInterface* volatile mDeletedSurface;              ///< Will be set to the deleted surface if requested (set by the event-thread, read & cleared by the update-render thread).
 
   volatile unsigned int             mPostRendering;                    ///< Whether post-rendering is taking place (set by the event & render threads, read by the render-thread).
   volatile unsigned int             mSurfaceResized;                   ///< Will be set to resize the surface (set by the event-thread, read & cleared by the update-render thread).
@@ -372,6 +393,8 @@ private:
   volatile unsigned int             mUploadWithoutRendering;           ///< Will be set to upload the resource only (with no rendering)
 
   volatile unsigned int             mFirstFrameAfterResume;            ///< Will be set to check the first frame after resume (for log)
+
+  std::vector<Rect<int>>            mDamagedRects;                     ///< Keeps collected damaged render items rects for one render pass
 };
 
 } // namespace Adaptor