Merge "Fix svace issues" into 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     * Uses an window surface to render to.
58     * @param [in] positionSize the position and size of the surface
59     * @param [in] surface can be a window or pixmap.
60     * @param [in] isTransparent if it is true, surface has 32 bit color depth, otherwise, 24 bit
61     */
62   WindowRenderSurface(Dali::PositionSize positionSize, Any surface, bool isTransparent = false);
63
64   /**
65    * @brief Destructor
66    */
67   virtual ~WindowRenderSurface();
68
69 public: // API
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] positionSize A new position and size of the surface
103    */
104   void RequestRotation(int angle, PositionSize positionSize);
105
106   /**
107    * @brief Gets the window base object
108    * @return The window base object
109    */
110   WindowBase* GetWindowBase();
111
112   /**
113    * @brief Intiailize Ime Surface for Ime window rendering.
114    *
115    * It sets one flag and callback function for Ime window rendering
116    * This callback function calls one special native window function for ready to commit buffer.
117    * The special function notify to display server.
118    * It is only used for Ime window.
119    */
120   void InitializeImeSurface();
121
122   /**
123    * @brief Sets the necessary for window rotation acknowledge.
124    * After this function called, SendRotationCompletedAcknowledgement() should be called to complete window rotation.
125    *
126    * More detail description is written in DevelWindow::SetNeedsRotationCompletedAcknowledgement().
127    *
128    * @param[in] window The window instance.
129    * @param[in] needAcknowledgement the flag is true if window rotation acknowledgement is sent.
130    */
131   void SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement);
132
133   /**
134    * @brief Updates window surface's position and size.
135    * It is just to update the local variable in window surface.
136    * This function is only called when window's position or size is changed by display server.
137    *
138    * @param[in] positionSize The updated window surface's position and size.
139    */
140   void UpdatePositionSize(Dali::PositionSize positionSize);
141
142   /**
143    * @brief This signal is emitted when the output is transformed.
144    */
145   OutputSignalType& OutputTransformedSignal();
146
147   /**
148    * @brief This signal is emitted when a rotation job is finished.
149    */
150   RotationFinishedSignalType& RotationFinishedSignal();
151
152 public: // from Dali::RenderSurfaceInterface
153   /**
154    * @copydoc Dali::RenderSurfaceInterface::GetPositionSize()
155    */
156   PositionSize GetPositionSize() const override;
157
158   /**
159    */
160   void GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical) override;
161
162   /**
163    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceOrientation()
164    */
165   int GetSurfaceOrientation() const override;
166
167   /**
168    * @copydoc Dali::RenderSurfaceInterface::GetScreenOrientation()
169    */
170   int GetScreenOrientation() const override;
171
172   /**
173    * @copydoc Dali::RenderSurfaceInterface::InitializeGraphics()
174    */
175   void InitializeGraphics() override;
176
177   /**
178    * @copydoc Dali::RenderSurfaceInterface::CreateSurface()
179    */
180   void CreateSurface() override;
181
182   /**
183    * @copydoc Dali::RenderSurfaceInterface::DestroySurface()
184    */
185   void DestroySurface() override;
186
187   /**
188    * @copydoc Dali::RenderSurfaceInterface::ReplaceGraphicsSurface()
189    */
190   bool ReplaceGraphicsSurface() override;
191
192   /**
193    * @copydoc Dali::RenderSurfaceInterface::MoveResize()
194    */
195   void MoveResize(Dali::PositionSize positionSize) override;
196
197   /**
198    * @copydoc Dali::RenderSurfaceInterface::StartRender()
199    */
200   void StartRender() override;
201
202   /**
203    * @copydoc Dali::RenderSurfaceInterface::PreRender()
204    */
205   bool PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect) override;
206
207   /**
208    * @copydoc Dali::RenderSurfaceInterface::PostRender()
209    */
210   void PostRender() override;
211
212   /**
213    * @copydoc Dali::RenderSurfaceInterface::StopRender()
214    */
215   void StopRender() override;
216
217   /**
218    * @copydoc Dali::RenderSurfaceInterface::SetThreadSynchronization
219    */
220   void SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization) override;
221
222   /**
223    * @copydoc Dali::RenderSurfaceInterface::ReleaseLock()
224    */
225   void ReleaseLock() override;
226
227   /**
228    * @copydoc Dali::RenderSurfaceInterface::GetSurfaceType()
229    */
230   Dali::RenderSurfaceInterface::Type GetSurfaceType() override;
231
232   /**
233    * @copydoc Dali::RenderSurfaceInterface::MakeContextCurrent()
234    */
235   void MakeContextCurrent() override;
236
237   /**
238    * @copydoc Dali::RenderSurfaceInterface::GetDepthBufferRequired()
239    */
240   Integration::DepthBufferAvailable GetDepthBufferRequired() override;
241
242   /**
243    * @copydoc Dali::RenderSurfaceInterface::GetStencilBufferRequired()
244    */
245   Integration::StencilBufferAvailable GetStencilBufferRequired() override;
246
247 private:
248   /**
249    * @brief Second stage construction
250    */
251   void Initialize(Any surface);
252
253   /**
254    * Notify output is transformed.
255    */
256   void OutputTransformed();
257
258   /**
259    * @brief Used as the callback for the post render.
260    * It is used both window rotation and supporting Ime window
261    */
262   void ProcessPostRender();
263
264   /**
265    * @brief Used as the callback for the frame rendered / presented.
266    */
267   void ProcessFrameCallback();
268
269   /**
270    * @brief Called when our event file descriptor has been written to.
271    * @param[in] eventBitMask bit mask of events that occured on the file descriptor
272    * @param[in] fileDescriptor The file descriptor
273    */
274   void OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor);
275
276   /**
277    * @brief Set the buffer damage rects.
278    * @param[in] damagedRects List of damaged rects
279    * @param[in] clippingRect The rect to clip rendered scene
280    */
281   void SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect);
282
283   /**
284    * @brief Swap buffers.
285    * @param[in] damagedRects List of damaged rects
286    */
287   void SwapBuffers(const std::vector<Rect<int>>& damagedRects);
288
289 protected:
290   // Undefined
291   WindowRenderSurface(const WindowRenderSurface&) = delete;
292
293   // Undefined
294   WindowRenderSurface& operator=(const WindowRenderSurface& rhs) = delete;
295
296 private:
297   struct FrameCallbackInfo
298   {
299     FrameCallbackInfo(Dali::Integration::Scene::FrameCallbackContainer& callbackList, int fd)
300     : callbacks(),
301       fileDescriptorMonitor(),
302       fileDescriptor(fd)
303     {
304       // Transfer owership of the CallbackBase
305       for(auto&& iter : callbackList)
306       {
307         callbacks.push_back(std::make_pair(std::move(iter.first), iter.second));
308       }
309     }
310
311     ~FrameCallbackInfo()
312     {
313       // Delete FileDescriptorMonitor before close fd.
314       fileDescriptorMonitor.release();
315       close(fileDescriptor);
316     }
317
318     Dali::Integration::Scene::FrameCallbackContainer callbacks;
319     std::unique_ptr<FileDescriptorMonitor>           fileDescriptorMonitor;
320     int                                              fileDescriptor;
321   };
322
323   using FrameCallbackInfoContainer = std::vector<std::unique_ptr<FrameCallbackInfo>>;
324
325 private: // Data
326   EglInterface*                          mEGL;
327   Dali::DisplayConnection*               mDisplayConnection;
328   PositionSize                           mPositionSize; ///< Position
329   std::unique_ptr<WindowBase>            mWindowBase;
330   ThreadSynchronizationInterface*        mThreadSynchronization;
331   TriggerEventInterface*                 mRenderNotification; ///< Render notification trigger
332   std::unique_ptr<TriggerEventInterface> mPostRenderTrigger;  ///< Post render callback function
333   std::unique_ptr<TriggerEventInterface> mFrameRenderedTrigger;
334   GraphicsInterface*                     mGraphics; ///< Graphics interface
335   EGLSurface                             mEGLSurface;
336   EGLContext                             mEGLContext;
337   ColorDepth                             mColorDepth; ///< Color depth of surface (32 bit or 24 bit)
338   OutputSignalType                       mOutputTransformedSignal;       ///< The signal of screen rotation occurs
339   RotationFinishedSignalType             mWindowRotationFinishedSignal;  ///< The signal of window rotation's finished
340   FrameCallbackInfoContainer             mFrameCallbackInfoContainer;
341   DamagedRectsContainer                  mBufferDamagedRects;
342   Dali::Mutex                            mMutex;
343   int                                    mWindowRotationAngle;
344   int                                    mScreenRotationAngle;
345   uint32_t                               mDpiHorizontal;
346   uint32_t                               mDpiVertical;
347   std::vector<Rect<int>>                 mDamagedRects{}; ///< Keeps collected damaged render items rects for one render pass. These rects are rotated by scene orientation.
348   bool                                   mOwnSurface;     ///< Whether we own the surface (responsible for deleting it)
349   bool                                   mIsImeWindowSurface;
350   bool                                   mNeedWindowRotationAcknowledgement;
351   bool                                   mIsWindowOrientationChanging;
352
353 }; // class WindowRenderSurface
354
355 } // namespace Adaptor
356
357 } // namespace Internal
358
359 } // namespace Dali
360
361 #endif // DALI_INTERNAL_WINDOWSYSTEM_COMMON_WINDOW_RENDER_SURFACE_H