1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
5 * Copyright (c) 2024 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
69 FRAME_UPDATE_CALLBACK = 1 << 5, ///< - Some FrameUpdateCallback return true
70 RENDERER_CONTINUOUSLY = 1 << 6 ///< - Some Renderer renderinb behaviour is continuously
75 * The status of the Core::Update operation.
84 : keepUpdating(false),
85 needsNotification(false),
86 secondsFromLastFrame(0.0f)
92 * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
93 * @return A bitmask of KeepUpdating values
95 uint32_t KeepUpdating()
101 * Query whether the Core requires an Notification event.
102 * This should be sent through the same mechanism (e.g. event loop) as input events.
103 * @return True if an Notification event should be sent.
105 bool NeedsNotification()
107 return needsNotification;
111 * This method is provided so that FPS can be easily calculated with a release version
113 * @return the seconds from last frame as float
115 float SecondsFromLastFrame()
117 return secondsFromLastFrame;
121 uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
122 bool needsNotification;
123 float secondsFromLastFrame;
127 * The status of the Core::Render operation.
136 : needsUpdate(false),
137 needsPostRender(false)
142 * Set whether update needs to run following a render.
143 * @param[in] updateRequired Set to true if an update is required to be run
145 void SetNeedsUpdate(bool updateRequired)
147 needsUpdate = updateRequired;
151 * Query the update status following rendering of a frame.
152 * @return True if update is required to be run
154 bool NeedsUpdate() const
160 * Sets if a post-render should be run.
161 * If nothing is rendered this frame, we can skip post-render.
162 * @param[in] postRenderRequired Set to True if post-render is required to be run
164 void SetNeedsPostRender(bool postRenderRequired)
166 needsPostRender = postRenderRequired;
170 * Queries if a post-render should be run.
171 * @return True if post-render is required to be run
173 bool NeedsPostRender() const
175 return needsPostRender;
179 bool needsUpdate : 1; ///< True if update is required to be run
180 bool needsPostRender : 1; ///< True if post-render is required to be run.
184 * Integration::Core is used for integration with the native windowing system.
185 * The following integration tasks must be completed:
187 * 1) Handle Graphics context creation, and notify the Core when this occurs.
189 * 2) Provide suspend/resume behaviour (see below for more details).
191 * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
192 * Notification events should be sent after a frame is updated (see UpdateStatus).
194 * 4) Run a rendering loop, instructing the Core to render each frame.
195 * A separate rendering thread is recommended; see multi-threading options below.
197 * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
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
220 * This object is used for integration with the native windowing system.
221 * @param[in] renderController The interface to an object which controls rendering.
222 * @param[in] platformAbstraction The interface providing platform specific services.
223 * @param[in] graphicsController The interface providing graphics services
224 * @param[in] corePolicy Flag list of update / rendering policies.
225 * @return A newly allocated Core.
227 static Core* New(RenderController& renderController,
228 PlatformAbstraction& platformAbstraction,
229 Graphics::Controller& graphicsController,
230 CorePolicyFlags corePolicy);
233 * Non-virtual destructor. Core is not intended as a base class.
238 * Initialize the core
242 // GL Context Lifecycle
245 * Get the object that will notify the application/toolkit when context is lost/regained
247 ContextNotifierInterface* GetContextNotifier();
250 * Notify the Core that the GL context has been created.
251 * The context must be created before the Core can render.
252 * Multi-threading note: this method should be called from the rendering thread only
253 * @post The Core is aware of the GL context.
255 void ContextCreated();
258 * Notify the Core that that GL context is about to be destroyed.
259 * The Core will free any previously allocated GL resources.
260 * Multi-threading note: this method should be called from the rendering thread only
261 * @post The Core is unaware of any GL context.
263 void ContextDestroyed();
266 * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
269 * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
270 * been called on the render thread before this is called on the event thread.
272 * Multi-threading note: this method should be called from the main thread
274 void RecoverFromContextLoss();
279 * Notify Core that the scene has been created.
284 * Queue an event with Core.
285 * 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.
286 * Multi-threading note: this method should be called from the main thread.
287 * @param[in] event The new event.
289 void QueueEvent(const Event& event);
292 * Relayout and flush the messages forcibly.
293 * Multi-threading note: this method should be called from the main thread.
294 * @pre ForceRelayout should not be called during ProcessEvents or ForceRelayout.
296 void ForceRelayout();
299 * Process the events queued with QueueEvent().
300 * Multi-threading note: this method should be called from the main thread.
301 * @pre ProcessEvents should not be called during ProcessEvents.
303 void ProcessEvents();
306 * The Core::Update() method prepares a frame for rendering. This method determines how many frames
307 * may be prepared, ahead of the rendering.
308 * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
309 * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
310 * the Core::Render() method for frame N has returned.
311 * @return The maximum update count (>= 1).
313 uint32_t GetMaximumUpdateCount() const;
316 * Update the scene for the next frame. This method must be called before each frame is rendered.
317 * Multi-threading notes: this method should be called from a dedicated update-thread.
318 * The update for frame N+1 may be processed whilst frame N is being rendered.
319 * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
320 * After this method returns, messages may be queued internally for the main thread.
321 * In order to process these messages, a notification is sent via the main thread's event loop.
322 * @param[in] elapsedSeconds Number of seconds since the last call
323 * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
324 * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
325 * @param[out] status showing whether further updates are required. This also shows
326 * whether a Notification event should be sent, regardless of whether the multi-threading is used.
327 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
328 * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object.
329 * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
331 void Update(float elapsedSeconds,
332 uint32_t lastVSyncTimeMilliseconds,
333 uint32_t nextVSyncTimeMilliseconds,
334 UpdateStatus& status,
335 bool renderToFboEnabled,
336 bool isRenderingToFbo,
340 * This is called before rendering any scene in the next frame. This method should be preceded
341 * by a call up Update.
342 * Multi-threading note: this method should be called from a dedicated rendering thread.
343 * @pre The GL context must have been created, and made current.
344 * @param[out] status showing whether update is required to run.
345 * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
347 void PreRender(RenderStatus& status, bool forceClear);
350 * This is called before rendering any scene in the next frame. This method should be preceded
351 * by a call up Update.
352 * Multi-threading note: this method should be called from a dedicated rendering thread.
353 * @pre The GL context must have been created, and made current.
354 * @param[in] scene The scene to be rendered.
355 * @param[out] damagedRects containing damaged render items rects for this pass.
357 void PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects);
360 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
361 * This method should be called twice. The first pass to render off-screen frame buffers if any,
362 * and the second pass to render the surface.
363 * Multi-threading note: this method should be called from a dedicated rendering thread.
364 * @pre The GL context must have been created, and made current.
365 * @param[out] status Contains the rendering flags.
366 * @param[in] scene The scene to be rendered.
367 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
369 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo);
372 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
373 * This method should be called twice. The first pass to render off-screen frame buffers if any,
374 * and the second pass to render the surface.
375 * Multi-threading note: this method should be called from a dedicated rendering thread.
376 * @pre The GL context must have been created, and made current.
377 * @param[out] status Contains the rendering flags.
378 * @param[in] scene The scene to be rendered.
379 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
380 * @param[in] clippingRect The rect to clip rendered scene.
382 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect);
385 * This is called after rendering all the scenes in the next frame. This method should be
386 * followed by a call up RenderScene.
387 * Multi-threading note: this method should be called from a dedicated rendering thread.
388 * @pre The GL context must have been created, and made current.
393 * @brief Register a processor
395 * Note, Core does not take ownership of this processor.
396 * @param[in] processor The process to register
397 * @param[in] postProcessor set this processor required to be called after size negotiation. Default is false.
399 void RegisterProcessor(Processor& processor, bool postProcessor = false);
402 * @brief Unregister a processor
403 * @param[in] processor The process to unregister
404 * @param[in] postProcessor True if the processor to be unregister is for post processor.
406 void UnregisterProcessor(Processor& processor, bool postProcessor = false);
409 * @brief Unregister all processors and post processors what we registered before.
411 void UnregisterProcessors();
414 * @brief Gets the Object registry.
415 * @return The object registry
417 ObjectRegistry GetObjectRegistry() const;
420 * Log core's memory pool's capacities
422 void LogMemoryPools() const;
426 * Private constructor; see also Core::New()
431 * Undefined copy-constructor.
432 * This avoids accidental calls to a default copy-constructor.
433 * @param[in] core A reference to the object to copy.
435 Core(const Core& core);
438 * Undefined assignment operator.
439 * This avoids accidental calls to a default assignment operator.
440 * @param[in] rhs A reference to the object to copy.
442 Core& operator=(const Core& rhs);
445 Internal::Core* mImpl;
448 } // namespace Integration
452 #endif // DALI_INTEGRATION_CORE_H