[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / integration-api / core.h
1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
3
4 /*
5  * Copyright (c) 2024 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 <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/context-notifier.h>
26 #include <dali/integration-api/core-enumerations.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/math/rect.h>
30
31 namespace Dali
32 {
33 class Layer;
34 class ObjectRegistry;
35 class RenderTaskList;
36
37 namespace Graphics
38 {
39 class Controller;
40 }
41
42 namespace Internal
43 {
44 class Core;
45 }
46
47 namespace Integration
48 {
49 class Core;
50 class PlatformAbstraction;
51 class Processor;
52 class RenderController;
53 class Scene;
54 struct Event;
55 struct TouchEvent;
56
57 /**
58  * The reasons why further updates are required.
59  */
60 namespace KeepUpdating
61 {
62 enum Reasons
63 {
64   NOT_REQUESTED          = 0,      ///< Zero means that no further updates are required
65   STAGE_KEEP_RENDERING   = 1 << 1, ///<  - Stage::KeepRendering() is being used
66   ANIMATIONS_RUNNING     = 1 << 2, ///< - Animations are ongoing
67   MONITORING_PERFORMANCE = 1 << 3, ///< - The --enable-performance-monitor option is being used
68   RENDER_TASK_SYNC       = 1 << 4, ///< - A render task is waiting for render sync
69   FRAME_UPDATE_CALLBACK  = 1 << 5, ///< - Some FrameUpdateCallback return true
70   RENDERER_CONTINUOUSLY  = 1 << 6  ///< - Some Renderer renderinb behaviour is continuously
71 };
72 }
73
74 /**
75  * The status of the Core::Update operation.
76  */
77 class UpdateStatus
78 {
79 public:
80   /**
81    * Constructor
82    */
83   UpdateStatus()
84   : keepUpdating(false),
85     needsNotification(false),
86     secondsFromLastFrame(0.0f)
87   {
88   }
89
90 public:
91   /**
92    * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
93    * @return A bitmask of KeepUpdating values
94    */
95   uint32_t KeepUpdating()
96   {
97     return keepUpdating;
98   }
99
100   /**
101    * Query whether the Core requires an Notification event.
102    * This should be sent through the same mechanism (e.g. event loop) as input events.
103    * @return True if an Notification event should be sent.
104    */
105   bool NeedsNotification()
106   {
107     return needsNotification;
108   }
109
110   /**
111    * This method is provided so that FPS can be easily calculated with a release version
112    * of Core.
113    * @return the seconds from last frame as float
114    */
115   float SecondsFromLastFrame()
116   {
117     return secondsFromLastFrame;
118   }
119
120 public:
121   uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
122   bool     needsNotification;
123   float    secondsFromLastFrame;
124 };
125
126 /**
127  * The status of the Core::Render operation.
128  */
129 class RenderStatus
130 {
131 public:
132   /**
133    * Constructor
134    */
135   RenderStatus()
136   : needsUpdate(false),
137     needsPostRender(false)
138   {
139   }
140
141   /**
142    * Set whether update needs to run following a render.
143    * @param[in] updateRequired Set to true if an update is required to be run
144    */
145   void SetNeedsUpdate(bool updateRequired)
146   {
147     needsUpdate = updateRequired;
148   }
149
150   /**
151    * Query the update status following rendering of a frame.
152    * @return True if update is required to be run
153    */
154   bool NeedsUpdate() const
155   {
156     return needsUpdate;
157   }
158
159   /**
160    * Sets if a post-render should be run.
161    * If nothing is rendered this frame, we can skip post-render.
162    * @param[in] postRenderRequired Set to True if post-render is required to be run
163    */
164   void SetNeedsPostRender(bool postRenderRequired)
165   {
166     needsPostRender = postRenderRequired;
167   }
168
169   /**
170    * Queries if a post-render should be run.
171    * @return True if post-render is required to be run
172    */
173   bool NeedsPostRender() const
174   {
175     return needsPostRender;
176   }
177
178 private:
179   bool needsUpdate : 1;     ///< True if update is required to be run
180   bool needsPostRender : 1; ///< True if post-render is required to be run.
181 };
182
183 /**
184  * Integration::Core is used for integration with the native windowing system.
185  * The following integration tasks must be completed:
186  *
187  * 1) Handle Graphics context creation, and notify the Core when this occurs.
188  *
189  * 2) Provide suspend/resume behaviour (see below for more details).
190  *
191  * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
192  * Notification events should be sent after a frame is updated (see UpdateStatus).
193  *
194  * 4) Run a rendering loop, instructing the Core to render each frame.
195  * A separate rendering thread is recommended; see multi-threading options below.
196  *
197  * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
198  *
199  *
200  * Multi-threading notes:
201  *
202  * The Dali API methods are not reentrant.  If you access the API from multiple threads simultaneously, then the results
203  * are undefined. This means that your application might segfault, or behave unpredictably.
204  *
205  * Rendering strategies:
206  *
207  * 1) Single-threaded. Call every Core method from the same thread. Event handling and rendering will occur in the same thread.
208  * This is not recommended, since processing input (slowly) can affect the smooth flow of animations.
209  *
210  * 2) Multi-threaded. The Core update & render operations can be processed in separate threads.
211  * See the method descriptions in Core to see which thread they should be called from.
212  * This is the recommended option, so that input processing will not affect the smoothness of animations.
213  * Note that the rendering thread must be halted, before destroying the GL context.
214  */
215 class DALI_CORE_API Core
216 {
217 public:
218   /**
219    * Create a new Core.
220    * This object is used for integration with the native windowing system.
221    * @param[in] renderController The interface to an object which controls rendering.
222    * @param[in] platformAbstraction The interface providing platform specific services.
223    * @param[in] graphicsController The interface providing graphics services
224    * @param[in] corePolicy Flag list of update / rendering policies.
225    * @return A newly allocated Core.
226    */
227   static Core* New(RenderController&     renderController,
228                    PlatformAbstraction&  platformAbstraction,
229                    Graphics::Controller& graphicsController,
230                    CorePolicyFlags       corePolicy);
231
232   /**
233    * Non-virtual destructor. Core is not intended as a base class.
234    */
235   ~Core();
236
237   /**
238    * Initialize the core
239    */
240   void Initialize();
241
242   // GL Context Lifecycle
243
244   /**
245    * Get the object that will notify the application/toolkit when context is lost/regained
246    */
247   ContextNotifierInterface* GetContextNotifier();
248
249   /**
250    * Notify the Core that the GL context has been created.
251    * The context must be created before the Core can render.
252    * Multi-threading note: this method should be called from the rendering thread only
253    * @post The Core is aware of the GL context.
254    */
255   void ContextCreated();
256
257   /**
258    * Notify the Core that that GL context is about to be destroyed.
259    * The Core will free any previously allocated GL resources.
260    * Multi-threading note: this method should be called from the rendering thread only
261    * @post The Core is unaware of any GL context.
262    */
263   void ContextDestroyed();
264
265   /**
266    * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
267    * or Context loss.
268    *
269    * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
270    * been called on the render thread before this is called on the event thread.
271    *
272    * Multi-threading note: this method should be called from the main thread
273    */
274   void RecoverFromContextLoss();
275
276   // Core Lifecycle
277
278   /**
279    * Notify Core that the scene has been created.
280    */
281   void SceneCreated();
282
283   /**
284    * Queue an event with Core.
285    * Pre-processing of events may be beneficial e.g. a series of motion events could be throttled, so that only the last event is queued.
286    * Multi-threading note: this method should be called from the main thread.
287    * @param[in] event The new event.
288    */
289   void QueueEvent(const Event& event);
290
291   /**
292    * Relayout and flush the messages forcibly.
293    * Multi-threading note: this method should be called from the main thread.
294    * @pre ForceRelayout should not be called during ProcessEvents or ForceRelayout.
295    */
296   void ForceRelayout();
297
298   /**
299    * Process the events queued with QueueEvent().
300    * Multi-threading note: this method should be called from the main thread.
301    * @pre ProcessEvents should not be called during ProcessEvents.
302    */
303   void ProcessEvents();
304
305   /**
306    * The Core::Update() method prepares a frame for rendering. This method determines how many frames
307    * may be prepared, ahead of the rendering.
308    * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
309    * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
310    * the Core::Render() method for frame N has returned.
311    * @return The maximum update count (>= 1).
312    */
313   uint32_t GetMaximumUpdateCount() const;
314
315   /**
316    * Update the scene for the next frame. This method must be called before each frame is rendered.
317    * Multi-threading notes: this method should be called from a dedicated update-thread.
318    * The update for frame N+1 may be processed whilst frame N is being rendered.
319    * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
320    * After this method returns, messages may be queued internally for the main thread.
321    * In order to process these messages, a notification is sent via the main thread's event loop.
322    * @param[in] elapsedSeconds Number of seconds since the last call
323    * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
324    * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
325    * @param[out] status showing whether further updates are required. This also shows
326    * whether a Notification event should be sent, regardless of whether the multi-threading is used.
327    * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
328    * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object.
329    * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
330    */
331   void Update(float         elapsedSeconds,
332               uint32_t      lastVSyncTimeMilliseconds,
333               uint32_t      nextVSyncTimeMilliseconds,
334               UpdateStatus& status,
335               bool          renderToFboEnabled,
336               bool          isRenderingToFbo,
337               bool          uploadOnly);
338
339   /**
340    * This is called before rendering any scene in the next frame. This method should be preceded
341    * by a call up Update.
342    * Multi-threading note: this method should be called from a dedicated rendering thread.
343    * @pre The GL context must have been created, and made current.
344    * @param[out] status showing whether update is required to run.
345    * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
346    */
347   void PreRender(RenderStatus& status, bool forceClear);
348
349   /**
350    * This is called before rendering any scene in the next frame. This method should be preceded
351    * by a call up Update.
352    * Multi-threading note: this method should be called from a dedicated rendering thread.
353    * @pre The GL context must have been created, and made current.
354    * @param[in] scene The scene to be rendered.
355    * @param[out] damagedRects containing damaged render items rects for this pass.
356    */
357   void PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects);
358
359   /**
360    * Render a scene in the next frame. This method should be preceded by a call up PreRender.
361    * This method should be called twice. The first pass to render off-screen frame buffers if any,
362    * and the second pass to render the surface.
363    * Multi-threading note: this method should be called from a dedicated rendering thread.
364    * @pre The GL context must have been created, and made current.
365    * @param[out] status Contains the rendering flags.
366    * @param[in] scene The scene to be rendered.
367    * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
368    */
369   void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo);
370
371   /**
372    * Render a scene in the next frame. This method should be preceded by a call up PreRender.
373    * This method should be called twice. The first pass to render off-screen frame buffers if any,
374    * and the second pass to render the surface.
375    * Multi-threading note: this method should be called from a dedicated rendering thread.
376    * @pre The GL context must have been created, and made current.
377    * @param[out] status Contains the rendering flags.
378    * @param[in] scene The scene to be rendered.
379    * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
380    * @param[in] clippingRect The rect to clip rendered scene.
381    */
382   void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect);
383
384   /**
385    * This is called after rendering all the scenes in the next frame. This method should be
386    * followed by a call up RenderScene.
387    * Multi-threading note: this method should be called from a dedicated rendering thread.
388    * @pre The GL context must have been created, and made current.
389    */
390   void PostRender();
391
392   /**
393    * @brief Register a processor
394    *
395    * Note, Core does not take ownership of this processor.
396    * @param[in] processor The process to register
397    * @param[in] postProcessor set this processor required to be called after size negotiation. Default is false.
398    */
399   void RegisterProcessor(Processor& processor, bool postProcessor = false);
400
401   /**
402    * @brief Unregister a processor
403    * @param[in] processor The process to unregister
404    * @param[in] postProcessor True if the processor to be unregister is for post processor.
405    */
406   void UnregisterProcessor(Processor& processor, bool postProcessor = false);
407
408   /**
409    * @brief Unregister all processors and post processors what we registered before.
410    */
411   void UnregisterProcessors();
412
413   /**
414    * @brief Gets the Object registry.
415    * @return The object registry
416    */
417   ObjectRegistry GetObjectRegistry() const;
418
419   /**
420    * Log core's memory pool's capacities
421    */
422   void LogMemoryPools() const;
423
424 private:
425   /**
426    * Private constructor; see also Core::New()
427    */
428   Core();
429
430   /**
431    * Undefined copy-constructor.
432    * This avoids accidental calls to a default copy-constructor.
433    * @param[in] core A reference to the object to copy.
434    */
435   Core(const Core& core);
436
437   /**
438    * Undefined assignment operator.
439    * This avoids accidental calls to a default assignment operator.
440    * @param[in] rhs A reference to the object to copy.
441    */
442   Core& operator=(const Core& rhs);
443
444 private:
445   Internal::Core* mImpl;
446 };
447
448 } // namespace Integration
449
450 } // namespace Dali
451
452 #endif // DALI_INTEGRATION_CORE_H