1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
5 * Copyright (c) 2020 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/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>
47 class GlSyncAbstraction;
48 class GlContextHelperAbstraction;
49 class PlatformAbstraction;
51 class RenderController;
58 * The reasons why further updates are required.
60 namespace KeepUpdating
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
73 * The status of the Core::Update operation.
83 : keepUpdating(false),
84 needsNotification(false),
85 surfaceRectChanged(false),
86 secondsFromLastFrame( 0.0f )
93 * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
94 * @return A bitmask of KeepUpdating values
96 uint32_t KeepUpdating() { return keepUpdating; }
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.
103 bool NeedsNotification() { return needsNotification; }
106 * Query wheter the default surface rect is changed or not.
107 * @return true if the default surface rect is changed.
109 bool SurfaceRectChanged() { return surfaceRectChanged; }
112 * This method is provided so that FPS can be easily calculated with a release version
114 * @return the seconds from last frame as float
116 float SecondsFromLastFrame() { return secondsFromLastFrame; }
120 uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
121 bool needsNotification;
122 bool surfaceRectChanged;
123 float secondsFromLastFrame;
127 * The status of the Core::Render operation.
137 : needsUpdate( false ),
138 needsPostRender( false )
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
146 void SetNeedsUpdate( bool updateRequired )
148 needsUpdate = updateRequired;
152 * Query the update status following rendering of a frame.
153 * @return True if update is required to be run
155 bool NeedsUpdate() const
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
165 void SetNeedsPostRender( bool postRenderRequired )
167 needsPostRender = postRenderRequired;
171 * Queries if a post-render should be run.
172 * @return True if post-render is required to be run
174 bool NeedsPostRender() const
176 return needsPostRender;
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.
187 * Integration::Core is used for integration with the native windowing system.
188 * The following integration tasks must be completed:
190 * 1) Handle GL context creation, and notify the Core when this occurs.
192 * 2) Provide suspend/resume behaviour (see below for more details).
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).
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.
200 * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
202 * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
204 * Multi-threading notes:
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.
209 * Rendering strategies:
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.
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.
219 class DALI_CORE_API 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.
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);
248 * Non-virtual destructor. Core is not intended as a base class.
253 * Initialize the core
257 // GL Context Lifecycle
260 * Get the object that will notify the application/toolkit when context is lost/regained
262 ContextNotifierInterface* GetContextNotifier();
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.
270 void ContextCreated();
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.
278 void ContextDestroyed();
281 * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
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.
287 * Multi-threading note: this method should be called from the main thread
289 void RecoverFromContextLoss();
294 * Notify Core that the scene has been created.
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.
304 void QueueEvent(const Event& event);
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.
311 void ProcessEvents();
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).
321 uint32_t GetMaximumUpdateCount() const;
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.
338 void Update( float elapsedSeconds,
339 uint32_t lastVSyncTimeMilliseconds,
340 uint32_t nextVSyncTimeMilliseconds,
341 UpdateStatus& status,
342 bool renderToFboEnabled,
343 bool isRenderingToFbo );
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.
354 void PreRender( RenderStatus& status, bool forceClear, bool uploadOnly );
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.
364 void PreRender( Integration::Scene& scene, std::vector<Rect<int>>& damagedRects );
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.
376 void RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo );
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.
389 void RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect );
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.
398 void PostRender( bool uploadOnly );
401 * @brief Register a processor
403 * Note, Core does not take ownership of this processor.
404 * @param[in] processor The process to register
406 void RegisterProcessor( Processor& processor );
409 * @brief Unregister a processor
410 * @param[in] processor The process to unregister
412 void UnregisterProcessor( Processor& processor );
415 * @brief Gets the Object registry.
416 * @return The object registry
418 ObjectRegistry GetObjectRegistry() const;
423 * Private constructor; see also Core::New()
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.
432 Core(const Core& core);
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.
439 Core& operator=(const Core& rhs);
443 Internal::Core* mImpl;
447 } // namespace Integration
451 #endif // DALI_INTEGRATION_CORE_H