Merge branch 'devel/master' into tizen
[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) 2019 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/integration-api/context-notifier.h>
27 #include <dali/integration-api/core-enumerations.h>
28 #include <dali/integration-api/resource-policies.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35 class Core;
36 }
37
38 namespace Integration
39 {
40 class Core;
41 class GestureManager;
42 class GlAbstraction;
43 class GlSyncAbstraction;
44 class PlatformAbstraction;
45 class Processor;
46 class RenderController;
47 struct Event;
48 struct TouchData;
49
50
51 /**
52  * The reasons why further updates are required.
53  */
54 namespace KeepUpdating
55 {
56 enum Reasons
57 {
58   NOT_REQUESTED           = 0,    ///< Zero means that no further updates are required
59   STAGE_KEEP_RENDERING    = 1<<1, ///<  - Stage::KeepRendering() is being used
60   ANIMATIONS_RUNNING      = 1<<2, ///< - Animations are ongoing
61   MONITORING_PERFORMANCE  = 1<<3, ///< - The --enable-performance-monitor option is being used
62   RENDER_TASK_SYNC        = 1<<4  ///< - A render task is waiting for render sync
63 };
64 }
65
66 /**
67  * The status of the Core::Update operation.
68  */
69 class UpdateStatus
70 {
71 public:
72
73   /**
74    * Constructor
75    */
76   UpdateStatus()
77   : keepUpdating(false),
78     needsNotification(false),
79     surfaceRectChanged(false),
80     secondsFromLastFrame( 0.0f )
81   {
82   }
83
84 public:
85
86   /**
87    * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
88    * @return A bitmask of KeepUpdating values
89    */
90   uint32_t KeepUpdating() { return keepUpdating; }
91
92   /**
93    * Query whether the Core requires an Notification event.
94    * This should be sent through the same mechanism (e.g. event loop) as input events.
95    * @return True if an Notification event should be sent.
96    */
97   bool NeedsNotification() { return needsNotification; }
98
99   /**
100    * Query wheter the default surface rect is changed or not.
101    * @return true if the default surface rect is changed.
102    */
103   bool SurfaceRectChanged() { return surfaceRectChanged; }
104
105   /**
106    * This method is provided so that FPS can be easily calculated with a release version
107    * of Core.
108    * @return the seconds from last frame as float
109    */
110   float SecondsFromLastFrame() { return secondsFromLastFrame; }
111
112 public:
113
114   uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
115   bool needsNotification;
116   bool surfaceRectChanged;
117   float secondsFromLastFrame;
118 };
119
120 /**
121  * The status of the Core::Render operation.
122  */
123 class RenderStatus
124 {
125 public:
126
127   /**
128    * Constructor
129    */
130   RenderStatus()
131   : needsUpdate( false ),
132     needsPostRender( false )
133   {
134   }
135
136   /**
137    * Set whether update needs to run following a render.
138    * @param[in] updateRequired Set to true if an update is required to be run
139    */
140   void SetNeedsUpdate( bool updateRequired )
141   {
142     needsUpdate = updateRequired;
143   }
144
145   /**
146    * Query the update status following rendering of a frame.
147    * @return True if update is required to be run
148    */
149   bool NeedsUpdate() const
150   {
151     return needsUpdate;
152   }
153
154   /**
155    * Sets if a post-render should be run.
156    * If nothing is rendered this frame, we can skip post-render.
157    * @param[in] postRenderRequired Set to True if post-render is required to be run
158    */
159   void SetNeedsPostRender( bool postRenderRequired )
160   {
161     needsPostRender = postRenderRequired;
162   }
163
164   /**
165    * Queries if a post-render should be run.
166    * @return True if post-render is required to be run
167    */
168   bool NeedsPostRender() const
169   {
170     return needsPostRender;
171   }
172
173 private:
174
175   bool needsUpdate      :1;  ///< True if update is required to be run
176   bool needsPostRender  :1;  ///< True if post-render is required to be run.
177 };
178
179
180 /**
181  * Integration::Core is used for integration with the native windowing system.
182  * The following integration tasks must be completed:
183  *
184  * 1) Handle GL context creation, and notify the Core when this occurs.
185  *
186  * 2) Provide suspend/resume behaviour (see below for more details).
187  *
188  * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
189  * Notification events should be sent after a frame is updated (see UpdateStatus).
190  *
191  * 4) Run a rendering loop, instructing the Core to render each frame.
192  * A separate rendering thread is recommended; see multi-threading options below.
193  *
194  * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
195  *
196  * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
197  *
198  * 7) Provide an implementation of the GestureManager interface, used to register gestures provided by the platform.
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   /**
220    * Create a new Core.
221    * This object is used for integration with the native windowing system.
222    * @param[in] renderController The interface to an object which controls rendering.
223    * @param[in] platformAbstraction The interface providing platform specific services.
224    * @param[in] glAbstraction The interface providing OpenGL services.
225    * @param[in] glSyncAbstraction The interface providing OpenGL sync objects.
226    * @param[in] gestureManager The interface providing gesture manager services.
227    * @param[in] policy The data retention policy. This depends on application setting
228    * and platform support. Dali should honour this policy when deciding to discard
229    * intermediate resource data.
230    * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
231    * @param[in] depthBufferAvailable Whether the depth buffer is available
232    * @param[in] stencilBufferAvailable Whether the stencil buffer is available
233    * @return A newly allocated Core.
234    */
235   static Core* New( RenderController& renderController,
236                     PlatformAbstraction& platformAbstraction,
237                     GlAbstraction& glAbstraction,
238                     GlSyncAbstraction& glSyncAbstraction,
239                     GestureManager& gestureManager,
240                     ResourcePolicy::DataRetention policy,
241                     RenderToFrameBuffer renderToFboEnabled,
242                     DepthBufferAvailable depthBufferAvailable,
243                     StencilBufferAvailable stencilBufferAvailable );
244
245   /**
246    * Non-virtual destructor. Core is not intended as a base class.
247    */
248   ~Core();
249
250   // GL Context Lifecycle
251
252   /**
253    * Get the object that will notify the application/toolkit when context is lost/regained
254    */
255   ContextNotifierInterface* GetContextNotifier();
256
257   /**
258    * Notify the Core that the GL context has been created.
259    * The context must be created before the Core can render.
260    * Multi-threading note: this method should be called from the rendering thread only
261    * @post The Core is aware of the GL context.
262    */
263   void ContextCreated();
264
265   /**
266    * Notify the Core that that GL context is about to be destroyed.
267    * The Core will free any previously allocated GL resources.
268    * Multi-threading note: this method should be called from the rendering thread only
269    * @post The Core is unaware of any GL context.
270    */
271   void ContextDestroyed();
272
273   /**
274    * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
275    * or Context loss.
276    *
277    * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
278    * been called on the render thread before this is called on the event thread.
279    *
280    * Multi-threading note: this method should be called from the main thread
281    */
282   void RecoverFromContextLoss();
283
284   /**
285    * Notify the Core that the GL surface has been resized.
286    * This should be done at least once i.e. after the first call to ContextCreated().
287    * The Core will use the surface size for camera calculations, and to set the GL viewport.
288    * Multi-threading note: this method should be called from the main thread
289    * @param[in] width The new surface width.
290    * @param[in] height The new surface height.
291    */
292   void SurfaceResized( uint32_t width, uint32_t height );
293
294   /**
295    * Notify the Core about the top margin size.
296    * Available stage size is reduced by this size.
297    * The stage is located below the size at the top of the display
298    * It is mainly useful for indicator in mobile device
299    * @param[in] margin margin size
300    */
301   void SetTopMargin( uint32_t margin );
302
303   // Core setters
304
305   /**
306    * Notify the Core about the display's DPI values.
307    * This should be done after the display is initialized and a Core instance is created.
308    * The Core will use the DPI values for font rendering.
309    * Multi-threading note: this method should be called from the main thread
310    * @param[in] dpiHorizontal Horizontal DPI value.
311    * @param[in] dpiVertical   Vertical DPI value.
312    */
313   void SetDpi( uint32_t dpiHorizontal, uint32_t dpiVertical );
314
315   // Core Lifecycle
316
317   /**
318    * Notify Core that the scene has been created.
319    */
320   void SceneCreated();
321
322   /**
323    * Queue an event with Core.
324    * 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.
325    * Multi-threading note: this method should be called from the main thread.
326    * @param[in] event The new event.
327    */
328   void QueueEvent(const Event& event);
329
330   /**
331    * Process the events queued with QueueEvent().
332    * Multi-threading note: this method should be called from the main thread.
333    * @pre ProcessEvents should not be called during ProcessEvents.
334    */
335   void ProcessEvents();
336
337   /**
338    * The Core::Update() method prepares a frame for rendering. This method determines how many frames
339    * may be prepared, ahead of the rendering.
340    * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
341    * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
342    * the Core::Render() method for frame N has returned.
343    * @return The maximum update count (>= 1).
344    */
345   uint32_t GetMaximumUpdateCount() const;
346
347   /**
348    * Update the scene for the next frame. This method must be called before each frame is rendered.
349    * Multi-threading notes: this method should be called from a dedicated update-thread.
350    * The update for frame N+1 may be processed whilst frame N is being rendered.
351    * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
352    * After this method returns, messages may be queued internally for the main thread.
353    * In order to process these messages, a notification is sent via the main thread's event loop.
354    * @param[in] elapsedSeconds Number of seconds since the last call
355    * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
356    * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
357    * @param[out] status showing whether further updates are required. This also shows
358    * whether a Notification event should be sent, regardless of whether the multi-threading is used.
359    * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
360    * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object.
361    */
362   void Update( float elapsedSeconds,
363                uint32_t lastVSyncTimeMilliseconds,
364                uint32_t nextVSyncTimeMilliseconds,
365                UpdateStatus& status,
366                bool renderToFboEnabled,
367                bool isRenderingToFbo );
368
369   /**
370    * Render the next frame. This method should be preceded by a call up Update.
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 showing whether update is required to run.
374    * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
375    */
376   void Render( RenderStatus& status, bool forceClear );
377
378   /**
379    * @brief Register a processor
380    *
381    * Note, Core does not take ownership of this processor.
382    * @param[in] processor The process to register
383    */
384   void RegisterProcessor( Processor& processor );
385
386   /**
387    * @brief Unregister a processor
388    * @param[in] processor The process to unregister
389    */
390   void UnregisterProcessor( Processor& processor );
391
392 private:
393
394   /**
395    * Private constructor; see also Core::New()
396    */
397   Core();
398
399   /**
400    * Undefined copy-constructor.
401    * This avoids accidental calls to a default copy-constructor.
402    * @param[in] core A reference to the object to copy.
403    */
404   Core(const Core& core);
405
406   /**
407    * Undefined assignment operator.
408    * This avoids accidental calls to a default assignment operator.
409    * @param[in] rhs A reference to the object to copy.
410    */
411   Core& operator=(const Core& rhs);
412
413 private:
414
415   Internal::Core* mImpl;
416
417 };
418
419 } // namespace Integration
420
421 } // namespace Dali
422
423 #endif // DALI_INTEGRATION_CORE_H