1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cstdint> // uint32_t
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>
43 class GlSyncAbstraction;
44 class PlatformAbstraction;
46 class RenderController;
52 * The reasons why further updates are required.
54 namespace KeepUpdating
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
67 * The status of the Core::Update operation.
77 : keepUpdating(false),
78 needsNotification(false),
79 surfaceRectChanged(false),
80 secondsFromLastFrame( 0.0f )
87 * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
88 * @return A bitmask of KeepUpdating values
90 uint32_t KeepUpdating() { return keepUpdating; }
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.
97 bool NeedsNotification() { return needsNotification; }
100 * Query wheter the default surface rect is changed or not.
101 * @return true if the default surface rect is changed.
103 bool SurfaceRectChanged() { return surfaceRectChanged; }
106 * This method is provided so that FPS can be easily calculated with a release version
108 * @return the seconds from last frame as float
110 float SecondsFromLastFrame() { return secondsFromLastFrame; }
114 uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
115 bool needsNotification;
116 bool surfaceRectChanged;
117 float secondsFromLastFrame;
121 * The status of the Core::Render operation.
131 : needsUpdate( false ),
132 needsPostRender( false )
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
140 void SetNeedsUpdate( bool updateRequired )
142 needsUpdate = updateRequired;
146 * Query the update status following rendering of a frame.
147 * @return True if update is required to be run
149 bool NeedsUpdate() const
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
159 void SetNeedsPostRender( bool postRenderRequired )
161 needsPostRender = postRenderRequired;
165 * Queries if a post-render should be run.
166 * @return True if post-render is required to be run
168 bool NeedsPostRender() const
170 return needsPostRender;
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.
181 * Integration::Core is used for integration with the native windowing system.
182 * The following integration tasks must be completed:
184 * 1) Handle GL context creation, and notify the Core when this occurs.
186 * 2) Provide suspend/resume behaviour (see below for more details).
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).
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.
194 * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
196 * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
198 * 7) Provide an implementation of the GestureManager interface, used to register gestures provided by the platform.
200 * Multi-threading notes:
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.
205 * Rendering strategies:
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.
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.
215 class DALI_CORE_API 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.
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 );
246 * Non-virtual destructor. Core is not intended as a base class.
250 // GL Context Lifecycle
253 * Get the object that will notify the application/toolkit when context is lost/regained
255 ContextNotifierInterface* GetContextNotifier();
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.
263 void ContextCreated();
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.
271 void ContextDestroyed();
274 * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
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.
280 * Multi-threading note: this method should be called from the main thread
282 void RecoverFromContextLoss();
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.
292 void SurfaceResized( uint32_t width, uint32_t height );
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
301 void SetTopMargin( uint32_t margin );
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.
313 void SetDpi( uint32_t dpiHorizontal, uint32_t dpiVertical );
318 * Notify Core that the scene has been created.
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.
328 void QueueEvent(const Event& event);
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.
335 void ProcessEvents();
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).
345 uint32_t GetMaximumUpdateCount() const;
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.
362 void Update( float elapsedSeconds,
363 uint32_t lastVSyncTimeMilliseconds,
364 uint32_t nextVSyncTimeMilliseconds,
365 UpdateStatus& status,
366 bool renderToFboEnabled,
367 bool isRenderingToFbo );
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.
376 void Render( RenderStatus& status, bool forceClear );
379 * @brief Register a processor
381 * Note, Core does not take ownership of this processor.
382 * @param[in] processor The process to register
384 void RegisterProcessor( Processor& processor );
387 * @brief Unregister a processor
388 * @param[in] processor The process to unregister
390 void UnregisterProcessor( Processor& processor );
395 * Private constructor; see also Core::New()
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.
404 Core(const Core& core);
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.
411 Core& operator=(const Core& rhs);
415 Internal::Core* mImpl;
419 } // namespace Integration
423 #endif // DALI_INTEGRATION_CORE_H