Support a frame rendered / presented callback
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-render-surface.h
1 #ifndef DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H
2 #define DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/signals/connection-tracker.h>
23 #include <dali/public-api/signals/dali-signal.h>
24 #include <dali/integration-api/scene.h>
25 #include <unistd.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/integration-api/adaptor-framework/egl-interface.h>
29 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
30 #include <dali/internal/graphics/common/graphics-interface.h>
31 #include <dali/internal/system/common/file-descriptor-monitor.h>
32
33 namespace Dali
34 {
35
36 class TriggerEventInterface;
37
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42
43 class WindowBase;
44 class AdaptorInternalServices;
45
46 /**
47  * Window interface of render surface.
48  */
49 class WindowRenderSurface : public Dali::RenderSurfaceInterface, public ConnectionTracker
50 {
51 public:
52
53   typedef Signal< void ( ) > OutputSignalType;
54
55   /**
56     * Uses an window surface to render to.
57     * @param [in] positionSize the position and size of the surface
58     * @param [in] surface can be a window or pixmap.
59     * @param [in] isTransparent if it is true, surface has 32 bit color depth, otherwise, 24 bit
60     */
61   WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent = false );
62
63   /**
64    * @brief Destructor
65    */
66   virtual ~WindowRenderSurface();
67
68 public: // API
69
70   /**
71    * @brief Get the native window handle
72    * @return The native window handle
73    */
74   Any GetNativeWindow();
75
76   /**
77    * @brief Get the native window id
78    * @return The native window id
79    */
80   int GetNativeWindowId();
81
82   /**
83    * @brief Map window
84    */
85   void Map();
86
87   /**
88    * @brief Sets the render notification trigger to call when render thread is completed a frame
89    * @param renderNotification to use
90    */
91   void SetRenderNotification( TriggerEventInterface* renderNotification );
92
93   /**
94    * @brief Sets whether the surface is transparent or not.
95    * @param[in] transparent Whether the surface is transparent
96    */
97   void SetTransparency( bool transparent );
98
99   /**
100    * Request surface rotation
101    * @param[in] angle A new angle of the surface
102    * @param[in] width A new width of the surface
103    * @param[in] height A new height of the surface
104    */
105   void RequestRotation( int angle, int width, int height );
106
107   /**
108    * @brief Gets the window base object
109    * @return The window base object
110    */
111   WindowBase* GetWindowBase();
112
113   /**
114    * @brief This signal is emitted when the output is transformed.
115    */
116   OutputSignalType& OutputTransformedSignal();
117
118 public: // from Dali::RenderSurfaceInterface
119
120   /**
121    * @copydoc Dali::RenderSurfaceInterface::GetPositionSize()
122    */
123   virtual PositionSize GetPositionSize() const override;
124
125   /**
126    */
127   virtual void GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) override;
128
129   /**
130    * @copydoc Dali::RenderSurfaceInterface::InitializeGraphics()
131    */
132   virtual void InitializeGraphics() override;
133
134   /**
135    * @copydoc Dali::RenderSurfaceInterface::CreateSurface()
136    */
137   virtual void CreateSurface() override;
138
139   /**
140    * @copydoc Dali::RenderSurfaceInterface::DestroySurface()
141    */
142   virtual void DestroySurface() override;
143
144   /**
145    * @copydoc Dali::RenderSurfaceInterface::ReplaceGraphicsSurface()
146    */
147   virtual bool ReplaceGraphicsSurface() override;
148
149   /**
150    * @copydoc Dali::RenderSurfaceInterface::MoveResize()
151    */
152   virtual void MoveResize( Dali::PositionSize positionSize) override;
153
154   /**
155    * @copydoc Dali::RenderSurfaceInterface::StartRender()
156    */
157   virtual void StartRender() override;
158
159   /**
160    * @copydoc Dali::RenderSurfaceInterface::PreRender()
161    */
162   virtual bool PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect ) override;
163
164   /**
165    * @copydoc Dali::RenderSurfaceInterface::PostRender()
166    */
167   virtual void PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects ) override;
168
169   /**
170    * @copydoc Dali::RenderSurfaceInterface::StopRender()
171    */
172   virtual void StopRender() override;
173
174   /**
175    * @copydoc Dali::RenderSurfaceInterface::SetThreadSynchronization
176    */
177   virtual void SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization ) override;
178
179   /**
180    * @copydoc Dali::RenderSurfaceInterface::ReleaseLock()
181    */
182   virtual void ReleaseLock() override;
183
184   /**
185    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceType()
186    */
187   virtual Dali::RenderSurfaceInterface::Type GetSurfaceType() override;
188
189   /**
190    * @copydoc Dali::RenderSurfaceInterface::MakeContextCurrent()
191    */
192   virtual void MakeContextCurrent() override;
193
194   /**
195    * @copydoc Dali::RenderSurfaceInterface::GetDepthBufferRequired()
196    */
197   virtual Integration::DepthBufferAvailable GetDepthBufferRequired() override;
198
199   /**
200    * @copydoc Dali::RenderSurfaceInterface::GetStencilBufferRequired()
201    */
202   virtual Integration::StencilBufferAvailable GetStencilBufferRequired() override;
203
204 private:
205
206   /**
207    * @brief Second stage construction
208    */
209   void Initialize( Any surface );
210
211   /**
212    * Notify output is transformed.
213    */
214   void OutputTransformed();
215
216   /**
217    * @brief Used as the callback for the rotation-trigger.
218    */
219   void ProcessRotationRequest();
220
221   /**
222    * @brief Used as the callback for the frame rendered / presented.
223    */
224   void ProcessFrameCallback();
225
226   /**
227    * @brief Called when our event file descriptor has been written to.
228    * @param[in] eventBitMask bit mask of events that occured on the file descriptor
229    * @param[in] fileDescriptor The file descriptor
230    */
231   void OnFileDescriptorEventDispatched( FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor );
232
233 protected:
234
235   // Undefined
236   WindowRenderSurface(const WindowRenderSurface&) = delete;
237
238   // Undefined
239   WindowRenderSurface& operator=(const WindowRenderSurface& rhs) = delete;
240
241 private:
242
243   struct FrameCallbackInfo
244   {
245     FrameCallbackInfo( Dali::Integration::Scene::FrameCallbackContainer& callbackList, int fd )
246     : callbacks(),
247       fileDescriptorMonitor(),
248       fileDescriptor( fd )
249     {
250       // Transfer owership of the CallbackBase
251       for( auto&& iter : callbackList )
252       {
253         callbacks.push_back( std::make_pair( std::move( iter.first ), iter.second ) );
254       }
255     }
256
257     ~FrameCallbackInfo()
258     {
259       // Delete FileDescriptorMonitor before close fd.
260       fileDescriptorMonitor.release();
261       close( fileDescriptor );
262     }
263
264     Dali::Integration::Scene::FrameCallbackContainer callbacks;
265     std::unique_ptr< FileDescriptorMonitor > fileDescriptorMonitor;
266     int fileDescriptor;
267   };
268
269   using FrameCallbackInfoContainer = std::vector< std::unique_ptr< FrameCallbackInfo > >;
270
271 private: // Data
272
273   EglInterface*                   mEGL;
274   Dali::DisplayConnection*        mDisplayConnection;
275   PositionSize                    mPositionSize;       ///< Position
276   std::unique_ptr< WindowBase >   mWindowBase;
277   ThreadSynchronizationInterface* mThreadSynchronization;
278   TriggerEventInterface*          mRenderNotification; ///< Render notification trigger
279   TriggerEventInterface*          mRotationTrigger;
280   std::unique_ptr< TriggerEventInterface > mFrameRenderedTrigger;
281   GraphicsInterface*              mGraphics;           ///< Graphics interface
282   EGLSurface                      mEGLSurface;
283   EGLContext                      mEGLContext;
284   ColorDepth                      mColorDepth;         ///< Color depth of surface (32 bit or 24 bit)
285   OutputSignalType                mOutputTransformedSignal;
286   FrameCallbackInfoContainer      mFrameCallbackInfoContainer;
287   int                             mRotationAngle;
288   int                             mScreenRotationAngle;
289   bool                            mOwnSurface;         ///< Whether we own the surface (responsible for deleting it)
290   bool                            mRotationSupported;
291   bool                            mRotationFinished;
292   bool                            mScreenRotationFinished;
293   bool                            mResizeFinished;
294
295   uint32_t                        mDpiHorizontal;
296   uint32_t                        mDpiVertical;
297
298 }; // class WindowRenderSurface
299
300 } // namespace Adaptor
301
302 } // namespace internal
303
304 } // namespace Dali
305
306 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H