Merge branch 'devel/master' into tizen
[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/devel-api/threading/mutex.h>
25 #include <dali/integration-api/scene.h>
26 #include <unistd.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/integration-api/adaptor-framework/egl-interface.h>
30 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
31 #include <dali/internal/graphics/common/graphics-interface.h>
32 #include <dali/internal/system/common/file-descriptor-monitor.h>
33
34 namespace Dali
35 {
36
37 class TriggerEventInterface;
38
39 namespace Internal
40 {
41 namespace Adaptor
42 {
43
44 class WindowBase;
45 class AdaptorInternalServices;
46
47 /**
48  * Window interface of render surface.
49  */
50 class WindowRenderSurface : public Dali::RenderSurfaceInterface, public ConnectionTracker
51 {
52 public:
53
54   using OutputSignalType = Signal< void ( ) >;
55   using DamagedRectsContainer = std::list< std::vector< Rect< int > > >;
56
57   /**
58     * Uses an window surface to render to.
59     * @param [in] positionSize the position and size of the surface
60     * @param [in] surface can be a window or pixmap.
61     * @param [in] isTransparent if it is true, surface has 32 bit color depth, otherwise, 24 bit
62     */
63   WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent = false );
64
65   /**
66    * @brief Destructor
67    */
68   virtual ~WindowRenderSurface();
69
70 public: // API
71
72   /**
73    * @brief Get the native window handle
74    * @return The native window handle
75    */
76   Any GetNativeWindow();
77
78   /**
79    * @brief Get the native window id
80    * @return The native window id
81    */
82   int GetNativeWindowId();
83
84   /**
85    * @brief Map window
86    */
87   void Map();
88
89   /**
90    * @brief Sets the render notification trigger to call when render thread is completed a frame
91    * @param renderNotification to use
92    */
93   void SetRenderNotification( TriggerEventInterface* renderNotification );
94
95   /**
96    * @brief Sets whether the surface is transparent or not.
97    * @param[in] transparent Whether the surface is transparent
98    */
99   void SetTransparency( bool transparent );
100
101   /**
102    * Request surface rotation
103    * @param[in] angle A new angle of the surface
104    * @param[in] width A new width of the surface
105    * @param[in] height A new height of the surface
106    */
107   void RequestRotation( int angle, int width, int height );
108
109   /**
110    * @brief Gets the window base object
111    * @return The window base object
112    */
113   WindowBase* GetWindowBase();
114
115   /**
116    * @brief This signal is emitted when the output is transformed.
117    */
118   OutputSignalType& OutputTransformedSignal();
119
120 public: // from Dali::RenderSurfaceInterface
121
122   /**
123    * @copydoc Dali::RenderSurfaceInterface::GetPositionSize()
124    */
125   PositionSize GetPositionSize() const override;
126
127   /**
128    */
129   void GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical ) override;
130
131   /**
132    * @copydoc Dali::RenderSurfaceInterface::GetOrientation()
133    */
134   int GetOrientation() const override;
135
136   /**
137    * @copydoc Dali::RenderSurfaceInterface::InitializeGraphics()
138    */
139   void InitializeGraphics() override;
140
141   /**
142    * @copydoc Dali::RenderSurfaceInterface::CreateSurface()
143    */
144   void CreateSurface() override;
145
146   /**
147    * @copydoc Dali::RenderSurfaceInterface::DestroySurface()
148    */
149   void DestroySurface() override;
150
151   /**
152    * @copydoc Dali::RenderSurfaceInterface::ReplaceGraphicsSurface()
153    */
154   bool ReplaceGraphicsSurface() override;
155
156   /**
157    * @copydoc Dali::RenderSurfaceInterface::MoveResize()
158    */
159   void MoveResize( Dali::PositionSize positionSize) override;
160
161   /**
162    * @copydoc Dali::RenderSurfaceInterface::StartRender()
163    */
164   void StartRender() override;
165
166   /**
167    * @copydoc Dali::RenderSurfaceInterface::PreRender()
168    */
169   bool PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect ) override;
170
171   /**
172    * @copydoc Dali::RenderSurfaceInterface::PostRender()
173    */
174   void PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects ) override;
175
176   /**
177    * @copydoc Dali::RenderSurfaceInterface::StopRender()
178    */
179   void StopRender() override;
180
181   /**
182    * @copydoc Dali::RenderSurfaceInterface::SetThreadSynchronization
183    */
184   void SetThreadSynchronization( ThreadSynchronizationInterface& threadSynchronization ) override;
185
186   /**
187    * @copydoc Dali::RenderSurfaceInterface::ReleaseLock()
188    */
189   void ReleaseLock() override;
190
191   /**
192    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceType()
193    */
194   Dali::RenderSurfaceInterface::Type GetSurfaceType() override;
195
196   /**
197    * @copydoc Dali::RenderSurfaceInterface::MakeContextCurrent()
198    */
199   void MakeContextCurrent() override;
200
201   /**
202    * @copydoc Dali::RenderSurfaceInterface::GetDepthBufferRequired()
203    */
204   Integration::DepthBufferAvailable GetDepthBufferRequired() override;
205
206   /**
207    * @copydoc Dali::RenderSurfaceInterface::GetStencilBufferRequired()
208    */
209   Integration::StencilBufferAvailable GetStencilBufferRequired() override;
210
211 private:
212
213   /**
214    * @brief Second stage construction
215    */
216   void Initialize( Any surface );
217
218   /**
219    * Notify output is transformed.
220    */
221   void OutputTransformed();
222
223   /**
224    * @brief Used as the callback for the rotation-trigger.
225    */
226   void ProcessRotationRequest();
227
228   /**
229    * @brief Used as the callback for the frame rendered / presented.
230    */
231   void ProcessFrameCallback();
232
233   /**
234    * @brief Called when our event file descriptor has been written to.
235    * @param[in] eventBitMask bit mask of events that occured on the file descriptor
236    * @param[in] fileDescriptor The file descriptor
237    */
238   void OnFileDescriptorEventDispatched( FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor );
239
240   /**
241    * @brief Set the buffer damage rects.
242    * @param[in] damagedRects List of damaged rects
243    * @param[in] clippingRect The rect to clip rendered scene
244    */
245   void SetBufferDamagedRects( const std::vector< Rect< int > >& damagedRects, Rect< int >& clippingRect );
246
247   /**
248    * @brief Swap buffers.
249    * @param[in] damagedRects List of damaged rects
250    */
251   void SwapBuffers( const std::vector<Rect<int>>& damagedRects );
252
253 protected:
254
255   // Undefined
256   WindowRenderSurface(const WindowRenderSurface&) = delete;
257
258   // Undefined
259   WindowRenderSurface& operator=(const WindowRenderSurface& rhs) = delete;
260
261 private:
262
263   struct FrameCallbackInfo
264   {
265     FrameCallbackInfo( Dali::Integration::Scene::FrameCallbackContainer& callbackList, int fd )
266     : callbacks(),
267       fileDescriptorMonitor(),
268       fileDescriptor( fd )
269     {
270       // Transfer owership of the CallbackBase
271       for( auto&& iter : callbackList )
272       {
273         callbacks.push_back( std::make_pair( std::move( iter.first ), iter.second ) );
274       }
275     }
276
277     ~FrameCallbackInfo()
278     {
279       // Delete FileDescriptorMonitor before close fd.
280       fileDescriptorMonitor.release();
281       close( fileDescriptor );
282     }
283
284     Dali::Integration::Scene::FrameCallbackContainer callbacks;
285     std::unique_ptr< FileDescriptorMonitor > fileDescriptorMonitor;
286     int fileDescriptor;
287   };
288
289   using FrameCallbackInfoContainer = std::vector< std::unique_ptr< FrameCallbackInfo > >;
290
291 private: // Data
292
293   EglInterface*                   mEGL;
294   Dali::DisplayConnection*        mDisplayConnection;
295   PositionSize                    mPositionSize;       ///< Position
296   std::unique_ptr< WindowBase >   mWindowBase;
297   ThreadSynchronizationInterface* mThreadSynchronization;
298   TriggerEventInterface*          mRenderNotification; ///< Render notification trigger
299   TriggerEventInterface*          mRotationTrigger;
300   std::unique_ptr< TriggerEventInterface > mFrameRenderedTrigger;
301   GraphicsInterface*              mGraphics;           ///< Graphics interface
302   EGLSurface                      mEGLSurface;
303   EGLContext                      mEGLContext;
304   ColorDepth                      mColorDepth;         ///< Color depth of surface (32 bit or 24 bit)
305   OutputSignalType                mOutputTransformedSignal;
306   FrameCallbackInfoContainer      mFrameCallbackInfoContainer;
307   DamagedRectsContainer           mBufferDamagedRects;
308   Dali::Mutex                     mMutex;
309   int                             mWindowRotationAngle;
310   int                             mScreenRotationAngle;
311   uint32_t                        mDpiHorizontal;
312   uint32_t                        mDpiVertical;
313   bool                            mOwnSurface;         ///< Whether we own the surface (responsible for deleting it)
314   bool                            mWindowRotationFinished;
315   bool                            mScreenRotationFinished;
316   bool                            mResizeFinished;
317   bool                            mDefaultScreenRotationAvailable;
318
319 }; // class WindowRenderSurface
320
321 } // namespace Adaptor
322
323 } // namespace internal
324
325 } // namespace Dali
326
327 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H