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