Merge "Updates to handle context loss and regain" 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) 2014 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/public-api/common/dali-common.h>
23 #include <dali/public-api/common/view-mode.h>
24 #include <dali/integration-api/context-notifier.h>
25 #include <dali/integration-api/resource-policies.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32 class Core;
33 }
34
35 namespace Integration
36 {
37
38 class Core;
39 class GestureManager;
40 class GlAbstraction;
41 class GlSyncAbstraction;
42 class PlatformAbstraction;
43 class RenderController;
44 class SystemOverlay;
45 struct Event;
46 struct TouchData;
47
48
49 /**
50  * The reasons why further updates are required.
51  */
52 namespace KeepUpdating DALI_IMPORT_API
53 {
54   extern const unsigned int NOT_REQUESTED; ///< Zero means that no further updates are required
55
56   // Bit-field values
57   extern const unsigned int STAGE_KEEP_RENDERING;   ///< 0x01 - Stage::KeepRendering() is being used
58   extern const unsigned int INCOMING_MESSAGES;      ///< 0x02 - Event-thread is sending messages to update-thread
59   extern const unsigned int ANIMATIONS_RUNNING;     ///< 0x04 - Animations are ongoing
60   extern const unsigned int DYNAMICS_CHANGED;       ///< 0x08 - A dynamics simulation is running
61   extern const unsigned int LOADING_RESOURCES;      ///< 0x10 - Resources are being loaded
62   extern const unsigned int MONITORING_PERFORMANCE; ///< 0x20 - The --enable-performance-monitor option is being used
63   extern const unsigned int RENDER_TASK_SYNC;       ///< 0x40 - A render task is waiting for render sync
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     secondsFromLastFrame( 0.0f )
80   {
81   }
82
83 public:
84
85   /**
86    * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
87    * @return A bitmask of KeepUpdating values
88    */
89   unsigned int KeepUpdating() { return keepUpdating; }
90
91   /**
92    * Query whether the Core requires an Notification event.
93    * This should be sent through the same mechanism (e.g. event loop) as input events.
94    * @return True if an Notification event should be sent.
95    */
96   bool NeedsNotification() { return needsNotification; }
97
98   /**
99    * This method is provided so that FPS can be easily calculated with a release version
100    * of Core.
101    * @return the seconds from last frame as float
102    */
103   float SecondsFromLastFrame() { return secondsFromLastFrame; }
104
105 public:
106
107   unsigned int keepUpdating; ///< A bitmask of KeepUpdating values
108   bool needsNotification;
109   float secondsFromLastFrame;
110 };
111
112 /**
113  * The status of the Core::Render operation.
114  */
115 class RenderStatus
116 {
117 public:
118
119   /**
120    * Constructor
121    */
122   RenderStatus()
123   : needsUpdate(false),
124     hasRendered(false)
125   {
126   }
127
128   /**
129    * Set whether update needs to run following a render.
130    * This might be because render has sent messages to update, or it has
131    * some textures to upload over several frames.
132    */
133   void SetNeedsUpdate(bool updateRequired) { needsUpdate = updateRequired; }
134
135   /**
136    * Query the update status following rendering of a frame.
137    * @return true if update should run.
138    */
139   bool NeedsUpdate() { return needsUpdate; }
140
141   /**
142    * Set whether there were new render instructions.
143    */
144   void SetHasRendered(bool rendered) { hasRendered = rendered; }
145
146   /**
147    * Query whether there were new render instructions.
148    * @return true if there were render instructions
149    */
150   bool HasRendered() { return hasRendered; }
151
152 private:
153
154   bool needsUpdate;
155   bool hasRendered;
156 };
157
158 /**
159  * Integration::Core is used for integration with the native windowing system.
160  * The following integration tasks must be completed:
161  *
162  * 1) Handle GL context creation, and notify the Core when this occurs.
163  *
164  * 2) Provide suspend/resume behaviour (see below for more details).
165  *
166  * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
167  * Notification events should be sent after a frame is updated (see UpdateStatus).
168  *
169  * 4) Run a rendering loop, instructing the Core to render each frame.
170  * A separate rendering thread is recommended; see multi-threading options below.
171  *
172  * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
173  *
174  * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
175  *
176  * 7) Provide an implementation of the GestureManager interface, used to register gestures provided by the platform.
177  *
178  * Suspend/Resume behaviour:
179  *
180  * The Core has no knowledge of the application lifecycle, but can be suspended.
181  * In the suspended state, input events will not be processed, and animations will not progress any further.
182  * The Core can still render in the suspended state; the same frame will be produced each time.
183  *
184  * Multi-threading notes:
185  *
186  * The Dali API methods are not reentrant.  If you access the API from multiple threads simultaneously, then the results
187  * are undefined. This means that your application might segfault, or behave unpredictably.
188  *
189  * Rendering strategies:
190  *
191  * 1) Single-threaded. Call every Core method from the same thread. Event handling and rendering will occur in the same thread.
192  * This is not recommended, since processing input (slowly) can affect the smooth flow of animations.
193  *
194  * 2) Multi-threaded. The Core update & render operations can be processed in separate threads.
195  * See the method descriptions in Core to see which thread they should be called from.
196  * This is the recommended option, so that input processing will not affect the smoothness of animations.
197  * Note that the rendering thread must be halted, before destroying the GL context.
198  */
199 class DALI_IMPORT_API Core
200 {
201 public:
202
203   /**
204    * Create a new Core.
205    * This object is used for integration with the native windowing system.
206    * @param[in] renderController The interface to an object which controls rendering.
207    * @param[in] platformAbstraction The interface providing platform specific services.
208    * @param[in] glAbstraction The interface providing OpenGL services.
209    * @param[in] glSyncAbstraction The interface providing OpenGL sync objects.
210    * @param[in] gestureManager The interface providing gesture manager services.
211    * @param[in] policy The data retention policy. This depends on application setting
212    * and platform support. Dali should honour this policy when deciding to discard
213    * intermediate resource data.
214    * @return A newly allocated Core.
215    */
216   static Core* New(RenderController& renderController,
217                    PlatformAbstraction& platformAbstraction,
218                    GlAbstraction& glAbstraction,
219                    GlSyncAbstraction& glSyncAbstraction,
220                    GestureManager& gestureManager,
221                    ResourcePolicy::DataRetention policy);
222
223   /**
224    * Non-virtual destructor. Core is not intended as a base class.
225    */
226   ~Core();
227
228   // GL Context Lifecycle
229
230   /**
231    * Get the object that will notify the application/toolkit when context is lost/regained
232    */
233   ContextNotifierInterface* GetContextNotifier();
234
235   /**
236    * Notify the Core that the GL context has been created.
237    * The context must be created before the Core can render.
238    * Multi-threading note: this method should be called from the rendering thread only
239    * @post The Core is aware of the GL context.
240    */
241   void ContextCreated();
242
243   /**
244    * Notify the Core that that GL context is about to be destroyed.
245    * The Core will free any previously allocated GL resources.
246    * Multi-threading note: this method should be called from the rendering thread only
247    * @post The Core is unaware of any GL context.
248    */
249   void ContextDestroyed();
250
251   /**
252    * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
253    * or Context loss.
254    *
255    * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
256    * been called on the render thread before this is called on the event thread.
257    *
258    * Multi-threading note: this method should be called from the main thread
259    */
260   void RecoverFromContextLoss();
261
262   /**
263    * Notify the Core that the GL surface has been resized.
264    * This should be done at least once i.e. after the first call to ContextCreated().
265    * The Core will use the surface size for camera calculations, and to set the GL viewport.
266    * Multi-threading note: this method should be called from the main thread
267    * @param[in] width The new surface width.
268    * @param[in] height The new surface height.
269    */
270   void SurfaceResized(unsigned int width, unsigned int height);
271
272   // Core setters
273
274   /**
275    * Notify the Core about the display's DPI values.
276    * This should be done after the display is initialized and a Core instance is created.
277    * The Core will use the DPI values for font rendering.
278    * Multi-threading note: this method should be called from the main thread
279    * @param[in] dpiHorizontal Horizontal DPI value.
280    * @param[in] dpiVertical   Vertical DPI value.
281    */
282   void SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical);
283
284   // Core Lifecycle
285
286   /**
287    * Put Core into the suspended state.
288    * Any ongoing event processing will be cancelled, for example multi-touch sequences.
289    * The core expects the system has suspended us. Animation time will continue during the suspended
290    * state.
291    * Multi-threading note: this method should be called from the main thread
292    * @post The Core is in the suspended state.
293    */
294   void Suspend();
295
296   /**
297    * Resume the Core from the suspended state.
298    * At the first update, the elapsed time passed to the animations will be equal to the time spent
299    * suspended.
300    * Multi-threading note: this method should be called from the main thread
301    * @post The Core is not in the suspended state.
302    */
303   void Resume();
304
305   /**
306    * Queue an event with Core.
307    * 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.
308    * Multi-threading note: this method should be called from the main thread.
309    * @param[in] event The new event.
310    */
311   void QueueEvent(const Event& event);
312
313   /**
314    * Process the events queued with QueueEvent().
315    * Multi-threading note: this method should be called from the main thread.
316    * @pre ProcessEvents should not be called during ProcessEvents.
317    */
318   void ProcessEvents();
319
320   /**
321    * Update external raw touch data in core.
322    * The core will use the touch data to generate Dali Touch/Gesture events for applications to use
323    * in the update thread.
324    * @param[in] touch The raw touch data.
325    * @note This can be called from either the event thread OR a dedicated touch thread.
326    */
327   void UpdateTouchData(const TouchData& touch);
328
329   /**
330    * The Core::Update() method prepares a frame for rendering. This method determines how many frames
331    * may be prepared, ahead of the rendering.
332    * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
333    * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
334    * the Core::Render() method for frame N has returned.
335    * @return The maximum update count (>= 1).
336    */
337   unsigned int GetMaximumUpdateCount() const;
338
339   /**
340    * Update the scene for the next frame. This method must be called before each frame is rendered.
341    * Multi-threading notes: this method should be called from a dedicated update-thread.
342    * The update for frame N+1 may be processed whilst frame N is being rendered.
343    * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
344    * After this method returns, messages may be queued internally for the main thread.
345    * In order to process these messages, a notification is sent via the main thread's event loop.
346    * @param[in] elapsedSeconds Number of seconds since the last call
347    * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
348    * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
349    * @param[out] status showing whether further updates are required. This also shows
350    * whether a Notification event should be sent, regardless of whether the multi-threading is used.
351    */
352   void Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, UpdateStatus& status );
353
354   /**
355    * Render the next frame. This method should be preceded by a call up Update.
356    * Multi-threading note: this method should be called from a dedicated rendering thread.
357    * @pre The GL context must have been created, and made current.
358    * @param[out] status showing whether update is required to run.
359    */
360   void Render( RenderStatus& status );
361
362   // System-level overlay
363
364   /**
365    * Use the SystemOverlay to draw content for system-level indicators, dialogs etc.
366    * @return The SystemOverlay.
367    */
368   SystemOverlay& GetSystemOverlay();
369
370   /**
371    * Set the stereoscopic 3D view mode
372    * @param[in] viewMode The new view mode
373    */
374   void SetViewMode( ViewMode viewMode );
375
376   /**
377    * Get the current view mode
378    * @return The current view mode
379    * @see SetViewMode.
380    */
381   ViewMode GetViewMode() const;
382
383   /**
384    * Set the stereo base (eye seperation) for stereoscopic 3D
385    * @param[in] stereoBase The stereo base (eye seperation) for stereoscopic 3D (mm)
386    */
387   void SetStereoBase( float stereoBase );
388
389   /**
390    * Get the stereo base (eye seperation) for stereoscopic 3D
391    * @return The stereo base (eye seperation) for stereoscopic 3D (mm)
392    */
393   float GetStereoBase() const;
394
395 private:
396
397   /**
398    * Private constructor; see also Core::New()
399    */
400   Core();
401
402   /**
403    * Undefined copy-constructor.
404    * This avoids accidental calls to a default copy-constructor.
405    * @param[in] core A reference to the object to copy.
406    */
407   Core(const Core& core);
408
409   /**
410    * Undefined assignment operator.
411    * This avoids accidental calls to a default assignment operator.
412    * @param[in] rhs A reference to the object to copy.
413    */
414   Core& operator=(const Core& rhs);
415
416 private:
417
418   Internal::Core* mImpl;
419
420 };
421
422 } // namespace Integration
423
424 } // namespace Dali
425
426 #endif // __DALI_INTEGRATION_CORE_H__