1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
5 * Copyright (c) 2021 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/integration-api/context-notifier.h>
26 #include <dali/integration-api/core-enumerations.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/math/rect.h>
50 class PlatformAbstraction;
52 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.
82 : keepUpdating(false),
83 needsNotification(false),
84 surfaceRectChanged(false),
85 secondsFromLastFrame(0.0f)
91 * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
92 * @return A bitmask of KeepUpdating values
94 uint32_t KeepUpdating()
100 * Query whether the Core requires an Notification event.
101 * This should be sent through the same mechanism (e.g. event loop) as input events.
102 * @return True if an Notification event should be sent.
104 bool NeedsNotification()
106 return needsNotification;
110 * Query wheter the default surface rect is changed or not.
111 * @return true if the default surface rect is changed.
113 bool SurfaceRectChanged()
115 return surfaceRectChanged;
119 * This method is provided so that FPS can be easily calculated with a release version
121 * @return the seconds from last frame as float
123 float SecondsFromLastFrame()
125 return secondsFromLastFrame;
129 uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
130 bool needsNotification;
131 bool surfaceRectChanged;
132 float secondsFromLastFrame;
136 * The status of the Core::Render operation.
145 : needsUpdate(false),
146 needsPostRender(false)
151 * Set whether update needs to run following a render.
152 * @param[in] updateRequired Set to true if an update is required to be run
154 void SetNeedsUpdate(bool updateRequired)
156 needsUpdate = updateRequired;
160 * Query the update status following rendering of a frame.
161 * @return True if update is required to be run
163 bool NeedsUpdate() const
169 * Sets if a post-render should be run.
170 * If nothing is rendered this frame, we can skip post-render.
171 * @param[in] postRenderRequired Set to True if post-render is required to be run
173 void SetNeedsPostRender(bool postRenderRequired)
175 needsPostRender = postRenderRequired;
179 * Queries if a post-render should be run.
180 * @return True if post-render is required to be run
182 bool NeedsPostRender() const
184 return needsPostRender;
188 bool needsUpdate : 1; ///< True if update is required to be run
189 bool needsPostRender : 1; ///< True if post-render is required to be run.
193 * Integration::Core is used for integration with the native windowing system.
194 * The following integration tasks must be completed:
196 * 1) Handle GL context creation, and notify the Core when this occurs.
198 * 2) Provide suspend/resume behaviour (see below for more details).
200 * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
201 * Notification events should be sent after a frame is updated (see UpdateStatus).
203 * 4) Run a rendering loop, instructing the Core to render each frame.
204 * A separate rendering thread is recommended; see multi-threading options below.
206 * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
208 * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
210 * Multi-threading notes:
212 * The Dali API methods are not reentrant. If you access the API from multiple threads simultaneously, then the results
213 * are undefined. This means that your application might segfault, or behave unpredictably.
215 * Rendering strategies:
217 * 1) Single-threaded. Call every Core method from the same thread. Event handling and rendering will occur in the same thread.
218 * This is not recommended, since processing input (slowly) can affect the smooth flow of animations.
220 * 2) Multi-threaded. The Core update & render operations can be processed in separate threads.
221 * See the method descriptions in Core to see which thread they should be called from.
222 * This is the recommended option, so that input processing will not affect the smoothness of animations.
223 * Note that the rendering thread must be halted, before destroying the GL context.
225 class DALI_CORE_API Core
230 * This object is used for integration with the native windowing system.
231 * @param[in] renderController The interface to an object which controls rendering.
232 * @param[in] platformAbstraction The interface providing platform specific services.
233 * @param[in] graphicsController The interface providing graphics services
234 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
235 * @param[in] depthBufferAvailable Whether the depth buffer is available
236 * @param[in] stencilBufferAvailable Whether the stencil buffer is available
237 * @param[in] partialUpdateAvailable Whether the partial update is available
238 * @return A newly allocated Core.
240 static Core* New(RenderController& renderController,
241 PlatformAbstraction& platformAbstraction,
242 Graphics::Controller& graphicsController,
243 RenderToFrameBuffer renderToFboEnabled,
244 DepthBufferAvailable depthBufferAvailable,
245 StencilBufferAvailable stencilBufferAvailable,
246 PartialUpdateAvailable partialUpdateAvailable);
249 * Non-virtual destructor. Core is not intended as a base class.
254 * Initialize the core
258 // GL Context Lifecycle
261 * Get the object that will notify the application/toolkit when context is lost/regained
263 ContextNotifierInterface* GetContextNotifier();
266 * Notify the Core that the GL context has been created.
267 * The context must be created before the Core can render.
268 * Multi-threading note: this method should be called from the rendering thread only
269 * @post The Core is aware of the GL context.
271 void ContextCreated();
274 * Notify the Core that that GL context is about to be destroyed.
275 * The Core will free any previously allocated GL resources.
276 * Multi-threading note: this method should be called from the rendering thread only
277 * @post The Core is unaware of any GL context.
279 void ContextDestroyed();
282 * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
285 * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
286 * been called on the render thread before this is called on the event thread.
288 * Multi-threading note: this method should be called from the main thread
290 void RecoverFromContextLoss();
295 * Notify Core that the scene has been created.
300 * Queue an event with Core.
301 * 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.
302 * Multi-threading note: this method should be called from the main thread.
303 * @param[in] event The new event.
305 void QueueEvent(const Event& event);
308 * Process the events queued with QueueEvent().
309 * Multi-threading note: this method should be called from the main thread.
310 * @pre ProcessEvents should not be called during ProcessEvents.
312 void ProcessEvents();
315 * The Core::Update() method prepares a frame for rendering. This method determines how many frames
316 * may be prepared, ahead of the rendering.
317 * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
318 * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
319 * the Core::Render() method for frame N has returned.
320 * @return The maximum update count (>= 1).
322 uint32_t GetMaximumUpdateCount() const;
325 * Update the scene for the next frame. This method must be called before each frame is rendered.
326 * Multi-threading notes: this method should be called from a dedicated update-thread.
327 * The update for frame N+1 may be processed whilst frame N is being rendered.
328 * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
329 * After this method returns, messages may be queued internally for the main thread.
330 * In order to process these messages, a notification is sent via the main thread's event loop.
331 * @param[in] elapsedSeconds Number of seconds since the last call
332 * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
333 * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
334 * @param[out] status showing whether further updates are required. This also shows
335 * whether a Notification event should be sent, regardless of whether the multi-threading is used.
336 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
337 * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object.
339 void Update(float elapsedSeconds,
340 uint32_t lastVSyncTimeMilliseconds,
341 uint32_t nextVSyncTimeMilliseconds,
342 UpdateStatus& status,
343 bool renderToFboEnabled,
344 bool isRenderingToFbo);
347 * This is called before rendering any scene in the next frame. This method should be preceded
348 * by a call up Update.
349 * Multi-threading note: this method should be called from a dedicated rendering thread.
350 * @pre The GL context must have been created, and made current.
351 * @param[out] status showing whether update is required to run.
352 * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
353 * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
355 void PreRender(RenderStatus& status, bool forceClear, bool uploadOnly);
358 * This is called before rendering any scene in the next frame. This method should be preceded
359 * by a call up Update.
360 * Multi-threading note: this method should be called from a dedicated rendering thread.
361 * @pre The GL context must have been created, and made current.
362 * @param[in] scene The scene to be rendered.
363 * @param[out] damagedRects containing damaged render items rects for this pass.
365 void PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects);
368 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
369 * This method should be called twice. The first pass to render off-screen frame buffers if any,
370 * and the second pass to render the surface.
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 Contains the rendering flags.
374 * @param[in] scene The scene to be rendered.
375 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
377 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo);
380 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
381 * This method should be called twice. The first pass to render off-screen frame buffers if any,
382 * and the second pass to render the surface.
383 * Multi-threading note: this method should be called from a dedicated rendering thread.
384 * @pre The GL context must have been created, and made current.
385 * @param[out] status Contains the rendering flags.
386 * @param[in] scene The scene to be rendered.
387 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
388 * @param[in] clippingRect The rect to clip rendered scene.
390 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect);
393 * This is called after rendering all the scenes in the next frame. This method should be
394 * followed by a call up RenderScene.
395 * Multi-threading note: this method should be called from a dedicated rendering thread.
396 * @pre The GL context must have been created, and made current.
397 * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
399 void PostRender(bool uploadOnly);
402 * @brief Register a processor
404 * Note, Core does not take ownership of this processor.
405 * @param[in] processor The process to register
407 void RegisterProcessor(Processor& processor);
410 * @brief Unregister a processor
411 * @param[in] processor The process to unregister
413 void UnregisterProcessor(Processor& processor);
416 * @brief Gets the Object registry.
417 * @return The object registry
419 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);
442 Internal::Core* mImpl;
445 } // namespace Integration
449 #endif // DALI_INTEGRATION_CORE_H