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 secondsFromLastFrame(0.0f)
90 * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
91 * @return A bitmask of KeepUpdating values
93 uint32_t 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()
105 return needsNotification;
109 * This method is provided so that FPS can be easily calculated with a release version
111 * @return the seconds from last frame as float
113 float SecondsFromLastFrame()
115 return secondsFromLastFrame;
119 uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
120 bool needsNotification;
121 float secondsFromLastFrame;
125 * The status of the Core::Render operation.
134 : needsUpdate(false),
135 needsPostRender(false)
140 * Set whether update needs to run following a render.
141 * @param[in] updateRequired Set to true if an update is required to be run
143 void SetNeedsUpdate(bool updateRequired)
145 needsUpdate = updateRequired;
149 * Query the update status following rendering of a frame.
150 * @return True if update is required to be run
152 bool NeedsUpdate() const
158 * Sets if a post-render should be run.
159 * If nothing is rendered this frame, we can skip post-render.
160 * @param[in] postRenderRequired Set to True if post-render is required to be run
162 void SetNeedsPostRender(bool postRenderRequired)
164 needsPostRender = postRenderRequired;
168 * Queries if a post-render should be run.
169 * @return True if post-render is required to be run
171 bool NeedsPostRender() const
173 return needsPostRender;
177 bool needsUpdate : 1; ///< True if update is required to be run
178 bool needsPostRender : 1; ///< True if post-render is required to be run.
182 * Integration::Core is used for integration with the native windowing system.
183 * The following integration tasks must be completed:
185 * 1) Handle GL context creation, and notify the Core when this occurs.
187 * 2) Provide suspend/resume behaviour (see below for more details).
189 * 3) Run an event loop, for passing events to the Core e.g. multi-touch input events.
190 * Notification events should be sent after a frame is updated (see UpdateStatus).
192 * 4) Run a rendering loop, instructing the Core to render each frame.
193 * A separate rendering thread is recommended; see multi-threading options below.
195 * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
197 * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
199 * Multi-threading notes:
201 * The Dali API methods are not reentrant. If you access the API from multiple threads simultaneously, then the results
202 * are undefined. This means that your application might segfault, or behave unpredictably.
204 * Rendering strategies:
206 * 1) Single-threaded. Call every Core method from the same thread. Event handling and rendering will occur in the same thread.
207 * This is not recommended, since processing input (slowly) can affect the smooth flow of animations.
209 * 2) Multi-threaded. The Core update & render operations can be processed in separate threads.
210 * See the method descriptions in Core to see which thread they should be called from.
211 * This is the recommended option, so that input processing will not affect the smoothness of animations.
212 * Note that the rendering thread must be halted, before destroying the GL context.
214 class DALI_CORE_API Core
219 * This object is used for integration with the native windowing system.
220 * @param[in] renderController The interface to an object which controls rendering.
221 * @param[in] platformAbstraction The interface providing platform specific services.
222 * @param[in] graphicsController The interface providing graphics services
223 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
224 * @param[in] depthBufferAvailable Whether the depth buffer is available
225 * @param[in] stencilBufferAvailable Whether the stencil buffer is available
226 * @param[in] partialUpdateAvailable Whether the partial update is available
227 * @return A newly allocated Core.
229 static Core* New(RenderController& renderController,
230 PlatformAbstraction& platformAbstraction,
231 Graphics::Controller& graphicsController,
232 RenderToFrameBuffer renderToFboEnabled,
233 DepthBufferAvailable depthBufferAvailable,
234 StencilBufferAvailable stencilBufferAvailable,
235 PartialUpdateAvailable partialUpdateAvailable);
238 * Non-virtual destructor. Core is not intended as a base class.
243 * Initialize the core
247 // GL Context Lifecycle
250 * Get the object that will notify the application/toolkit when context is lost/regained
252 ContextNotifierInterface* GetContextNotifier();
255 * Notify the Core that the GL context has been created.
256 * The context must be created before the Core can render.
257 * Multi-threading note: this method should be called from the rendering thread only
258 * @post The Core is aware of the GL context.
260 void ContextCreated();
263 * Notify the Core that that GL context is about to be destroyed.
264 * The Core will free any previously allocated GL resources.
265 * Multi-threading note: this method should be called from the rendering thread only
266 * @post The Core is unaware of any GL context.
268 void ContextDestroyed();
271 * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
274 * In the case of ReplaceSurface, both ContextToBeDestroyed() and ContextCreated() will have
275 * been called on the render thread before this is called on the event thread.
277 * Multi-threading note: this method should be called from the main thread
279 void RecoverFromContextLoss();
284 * Notify Core that the scene has been created.
289 * Queue an event with Core.
290 * 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.
291 * Multi-threading note: this method should be called from the main thread.
292 * @param[in] event The new event.
294 void QueueEvent(const Event& event);
297 * Process the events queued with QueueEvent().
298 * Multi-threading note: this method should be called from the main thread.
299 * @pre ProcessEvents should not be called during ProcessEvents.
301 void ProcessEvents();
304 * The Core::Update() method prepares a frame for rendering. This method determines how many frames
305 * may be prepared, ahead of the rendering.
306 * For example if the maximum update count is 2, then Core::Update() for frame N+1 may be processed
307 * whilst frame N is being rendered. However the Core::Update() for frame N+2 may not be called, until
308 * the Core::Render() method for frame N has returned.
309 * @return The maximum update count (>= 1).
311 uint32_t GetMaximumUpdateCount() const;
314 * Update the scene for the next frame. This method must be called before each frame is rendered.
315 * Multi-threading notes: this method should be called from a dedicated update-thread.
316 * The update for frame N+1 may be processed whilst frame N is being rendered.
317 * However the update-thread must wait until frame N has been rendered, before processing frame N+2.
318 * After this method returns, messages may be queued internally for the main thread.
319 * In order to process these messages, a notification is sent via the main thread's event loop.
320 * @param[in] elapsedSeconds Number of seconds since the last call
321 * @param[in] lastVSyncTimeMilliseconds The last vsync time in milliseconds
322 * @param[in] nextVSyncTimeMilliseconds The time of the next predicted VSync in milliseconds
323 * @param[out] status showing whether further updates are required. This also shows
324 * whether a Notification event should be sent, regardless of whether the multi-threading is used.
325 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled.
326 * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object.
327 * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
329 void Update(float elapsedSeconds,
330 uint32_t lastVSyncTimeMilliseconds,
331 uint32_t nextVSyncTimeMilliseconds,
332 UpdateStatus& status,
333 bool renderToFboEnabled,
334 bool isRenderingToFbo,
338 * This is called before rendering any scene in the next frame. This method should be preceded
339 * by a call up Update.
340 * Multi-threading note: this method should be called from a dedicated rendering thread.
341 * @pre The GL context must have been created, and made current.
342 * @param[out] status showing whether update is required to run.
343 * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
345 void PreRender(RenderStatus& status, bool forceClear);
348 * This is called before rendering any scene in the next frame. This method should be preceded
349 * by a call up Update.
350 * Multi-threading note: this method should be called from a dedicated rendering thread.
351 * @pre The GL context must have been created, and made current.
352 * @param[in] scene The scene to be rendered.
353 * @param[out] damagedRects containing damaged render items rects for this pass.
355 void PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects);
358 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
359 * This method should be called twice. The first pass to render off-screen frame buffers if any,
360 * and the second pass to render the surface.
361 * Multi-threading note: this method should be called from a dedicated rendering thread.
362 * @pre The GL context must have been created, and made current.
363 * @param[out] status Contains the rendering flags.
364 * @param[in] scene The scene to be rendered.
365 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
367 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo);
370 * Render a scene in the next frame. This method should be preceded by a call up PreRender.
371 * This method should be called twice. The first pass to render off-screen frame buffers if any,
372 * and the second pass to render the surface.
373 * Multi-threading note: this method should be called from a dedicated rendering thread.
374 * @pre The GL context must have been created, and made current.
375 * @param[out] status Contains the rendering flags.
376 * @param[in] scene The scene to be rendered.
377 * @param[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
378 * @param[in] clippingRect The rect to clip rendered scene.
380 void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect);
383 * This is called after rendering all the scenes in the next frame. This method should be
384 * followed by a call up RenderScene.
385 * Multi-threading note: this method should be called from a dedicated rendering thread.
386 * @pre The GL context must have been created, and made current.
391 * @brief Register a processor
393 * Note, Core does not take ownership of this processor.
394 * @param[in] processor The process to register
395 * @param[in] postProcessor set this processor required to be called after size negotiation. Default is false.
397 void RegisterProcessor(Processor& processor, bool postProcessor = false);
400 * @brief Unregister a processor
401 * @param[in] processor The process to unregister
402 * @param[in] postProcessor True if the processor to be unregister is for post processor.
404 void UnregisterProcessor(Processor& processor, bool postProcessor = false);
407 * @brief Gets the Object registry.
408 * @return The object registry
410 ObjectRegistry GetObjectRegistry() const;
414 * Private constructor; see also Core::New()
419 * Undefined copy-constructor.
420 * This avoids accidental calls to a default copy-constructor.
421 * @param[in] core A reference to the object to copy.
423 Core(const Core& core);
426 * Undefined assignment operator.
427 * This avoids accidental calls to a default assignment operator.
428 * @param[in] rhs A reference to the object to copy.
430 Core& operator=(const Core& rhs);
433 Internal::Core* mImpl;
436 } // namespace Integration
440 #endif // DALI_INTEGRATION_CORE_H