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