[dali_2.3.22] Merge branch 'devel/master'
[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) 2022 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/devel-api/threading/mutex.h>
23 #include <dali/integration-api/scene.h>
24 #include <dali/public-api/signals/connection-tracker.h>
25 #include <dali/public-api/signals/dali-signal.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 class TriggerEventInterface;
37
38 namespace Internal
39 {
40 namespace Adaptor
41 {
42 class WindowBase;
43 class AdaptorInternalServices;
44
45 /**
46  * Window interface of render surface.
47  */
48 class WindowRenderSurface : public Dali::RenderSurfaceInterface, public ConnectionTracker
49 {
50 public:
51
52   using RotationFinishedSignalType = Signal<void()>        ; ///<The signal of window rotation's finished.
53   using OutputSignalType           = Signal<void()>;
54   using DamagedRectsContainer      = std::vector<Rect<int>>;
55
56   /**
57     * @brief Uses an window surface to render to.
58     *
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    * @brief Get the native window handle
73    *
74    * @return The native window handle
75    */
76   Any GetNativeWindow();
77
78   /**
79    * @brief Get the native window id
80    *
81    * @return The native window id
82    */
83   int GetNativeWindowId();
84
85   /**
86    * @brief Map window
87    */
88   void Map();
89
90   /**
91    * @brief Sets the render notification trigger to call when render thread is completed a frame
92    *
93    * @param renderNotification to use
94    */
95   void SetRenderNotification(TriggerEventInterface* renderNotification);
96
97   /**
98    * @brief Sets whether the surface is transparent or not.
99    *
100    * @param[in] transparent Whether the surface is transparent
101    */
102   void SetTransparency(bool transparent);
103
104   /**
105    * @brief Request surface rotation
106    *
107    * @param[in] angle A new angle of the surface
108    * @param[in] positionSize A new position and size of the surface
109    */
110   void RequestRotation(int angle, PositionSize positionSize);
111
112   /**
113    * @brief Gets the window base object
114    *
115    * @return The window base object
116    */
117   WindowBase* GetWindowBase();
118
119   /**
120    * @brief Intiailize Ime Surface for Ime window rendering.
121    * It sets one flag and callback function for Ime window rendering
122    * This callback function calls one special native window function for ready to commit buffer.
123    * The special function notify to display server.
124    * It is only used for Ime window.
125    */
126   void InitializeImeSurface();
127
128   /**
129    * @brief Sets the necessary for window rotation acknowledge.
130    * After this function called, SendRotationCompletedAcknowledgement() should be called to complete window rotation.
131    * More detail description is written in DevelWindow::SetNeedsRotationCompletedAcknowledgement().
132    *
133    * @param[in] window The window instance.
134    * @param[in] needAcknowledgement the flag is true if window rotation acknowledgement is sent.
135    */
136   void SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement);
137
138   /**
139    * @brief Updates window surface's position and size.
140    * It is just to update the local variable in window surface.
141    * This function is only called when window's position or size is changed by display server.
142    *
143    * @param[in] positionSize The updated window surface's position and size.
144    */
145   void UpdatePositionSize(Dali::PositionSize positionSize);
146
147   /**
148    * @brief Moves the window to the specified position.
149    *
150    * @param positionSize The new position of the window.
151    */
152   void Move(Dali::PositionSize positionSize);
153
154   /**
155    * @brief This signal is emitted when the output is transformed.
156    */
157   OutputSignalType& OutputTransformedSignal();
158
159   /**
160    * @brief This signal is emitted when a rotation job is finished.
161    */
162   RotationFinishedSignalType& RotationFinishedSignal();
163
164   /**
165    * @brief Sets front buffer rendering flag
166    *
167    * @param[in] enable The flag for front buffer rendering
168    */
169   void SetFrontBufferRendering(bool enable);
170
171 public: // from Dali::RenderSurfaceInterface
172   /**
173    * @copydoc Dali::RenderSurfaceInterface::GetPositionSize()
174    */
175   PositionSize GetPositionSize() const override;
176
177   /**
178    * @copydoc Dali::RenderSurfaceInterface::GetDpi()
179    */
180   void GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical) override;
181
182   /**
183    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceOrientation()
184    */
185   int GetSurfaceOrientation() const override;
186
187   /**
188    * @copydoc Dali::RenderSurfaceInterface::GetScreenOrientation()
189    */
190   int GetScreenOrientation() const override;
191
192   /**
193    * @copydoc Dali::RenderSurfaceInterface::InitializeGraphics()
194    */
195   void InitializeGraphics() override;
196
197   /**
198    * @copydoc Dali::RenderSurfaceInterface::CreateSurface()
199    */
200   void CreateSurface() override;
201
202   /**
203    * @copydoc Dali::RenderSurfaceInterface::DestroySurface()
204    */
205   void DestroySurface() override;
206
207   /**
208    * @copydoc Dali::RenderSurfaceInterface::ReplaceGraphicsSurface()
209    */
210   bool ReplaceGraphicsSurface() override;
211
212   /**
213    * @copydoc Dali::RenderSurfaceInterface::MoveResize()
214    */
215   void MoveResize(Dali::PositionSize positionSize) override;
216
217   /**
218    * @copydoc Dali::RenderSurfaceInterface::StartRender()
219    */
220   void StartRender() override;
221
222   /**
223    * @copydoc Dali::RenderSurfaceInterface::PreRender()
224    */
225   bool PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect) override;
226
227   /**
228    * @copydoc Dali::RenderSurfaceInterface::PostRender()
229    */
230   void PostRender() override;
231
232   /**
233    * @copydoc Dali::RenderSurfaceInterface::StopRender()
234    */
235   void StopRender() override;
236
237   /**
238    * @copydoc Dali::RenderSurfaceInterface::SetThreadSynchronization
239    */
240   void SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization) override;
241
242   /**
243    * @copydoc Dali::RenderSurfaceInterface::ReleaseLock()
244    */
245   void ReleaseLock() override;
246
247   /**
248    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceType()
249    */
250   Dali::RenderSurfaceInterface::Type GetSurfaceType() override;
251
252   /**
253    * @copydoc Dali::RenderSurfaceInterface::MakeContextCurrent()
254    */
255   void MakeContextCurrent() override;
256
257   /**
258    * @copydoc Dali::RenderSurfaceInterface::GetDepthBufferRequired()
259    */
260   Integration::DepthBufferAvailable GetDepthBufferRequired() override;
261
262   /**
263    * @copydoc Dali::RenderSurfaceInterface::GetStencilBufferRequired()
264    */
265   Integration::StencilBufferAvailable GetStencilBufferRequired() override;
266
267 private:
268   /**
269    * @brief Second stage construction
270    */
271   void Initialize(Any surface);
272
273   /**
274    * @brief Notify output is transformed.
275    */
276   void OutputTransformed();
277
278   /**
279    * @brief Used as the callback for the post render.
280    * It is used both window rotation and supporting Ime window
281    */
282   void ProcessPostRender();
283
284   /**
285    * @brief Used as the callback for the frame rendered / presented.
286    */
287   void ProcessFrameCallback();
288
289   /**
290    * @brief Called when our event file descriptor has been written to.
291    *
292    * @param[in] eventBitMask bit mask of events that occured on the file descriptor
293    * @param[in] fileDescriptor The file descriptor
294    */
295   void OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor);
296
297   /**
298    * @brief Set the buffer damage rects.
299    *
300    * @param[in] damagedRects List of damaged rects
301    * @param[in] clippingRect The rect to clip rendered scene
302    */
303   void SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect);
304
305   /**
306    * @brief Swap buffers.
307    *
308    * @param[in] damagedRects List of damaged rects
309    */
310   void SwapBuffers(const std::vector<Rect<int>>& damagedRects);
311
312 protected:
313   // Undefined
314   WindowRenderSurface(const WindowRenderSurface&) = delete;
315
316   // Undefined
317   WindowRenderSurface& operator=(const WindowRenderSurface& rhs) = delete;
318
319 private:
320   struct FrameCallbackInfo
321   {
322     FrameCallbackInfo(Dali::Integration::Scene::FrameCallbackContainer& callbackList, int fd)
323     : callbacks(),
324       fileDescriptorMonitor(),
325       fileDescriptor(fd)
326     {
327       // Transfer owership of the CallbackBase
328       for(auto&& iter : callbackList)
329       {
330         callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
331       }
332     }
333
334     ~FrameCallbackInfo()
335     {
336       // Delete FileDescriptorMonitor before close fd.
337       fileDescriptorMonitor.reset();
338       close(fileDescriptor);
339     }
340
341     Dali::Integration::Scene::FrameCallbackContainer callbacks;
342     std::unique_ptr<FileDescriptorMonitor>           fileDescriptorMonitor;
343     int                                              fileDescriptor;
344   };
345
346   using FrameCallbackInfoContainer = std::vector<std::unique_ptr<FrameCallbackInfo>>;
347
348 private: // Data
349   EglInterface*                          mEGL;
350   Dali::DisplayConnection*               mDisplayConnection;
351   PositionSize                           mPositionSize; ///< Position
352   std::unique_ptr<WindowBase>            mWindowBase;
353   ThreadSynchronizationInterface*        mThreadSynchronization;
354   TriggerEventInterface*                 mRenderNotification; ///< Render notification trigger
355   std::unique_ptr<TriggerEventInterface> mPostRenderTrigger;  ///< Post render callback function
356   std::unique_ptr<TriggerEventInterface> mFrameRenderedTrigger;
357   GraphicsInterface*                     mGraphics; ///< Graphics interface
358   EGLSurface                             mEGLSurface;
359   EGLContext                             mEGLContext;
360   ColorDepth                             mColorDepth; ///< Color depth of surface (32 bit or 24 bit)
361   OutputSignalType                       mOutputTransformedSignal;       ///< The signal of screen rotation occurs
362   RotationFinishedSignalType             mWindowRotationFinishedSignal;  ///< The signal of window rotation's finished
363   FrameCallbackInfoContainer             mFrameCallbackInfoContainer;
364   DamagedRectsContainer                  mBufferDamagedRects;
365   Dali::Mutex                            mMutex;
366   int                                    mWindowRotationAngle;
367   int                                    mScreenRotationAngle;
368   uint32_t                               mDpiHorizontal;
369   uint32_t                               mDpiVertical;
370   std::vector<Rect<int>>                 mDamagedRects{}; ///< Keeps collected damaged render items rects for one render pass. These rects are rotated by scene orientation.
371   bool                                   mOwnSurface;     ///< Whether we own the surface (responsible for deleting it)
372   bool                                   mIsImeWindowSurface;
373   bool                                   mNeedWindowRotationAcknowledgement;
374   bool                                   mIsWindowOrientationChanging;
375   bool                                   mIsFrontBufferRendering;
376   bool                                   mIsFrontBufferRenderingChanged;
377
378 }; // class WindowRenderSurface
379
380 } // namespace Adaptor
381
382 } // namespace Internal
383
384 } // namespace Dali
385
386 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H