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