Fix to do not update state of render task in case of uploadOnly
[platform/core/uifw/dali-core.git] / dali / integration-api / core.h
1 #ifndef DALI_INTEGRATION_CORE_H
2 #define DALI_INTEGRATION_CORE_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33 class Layer;
34 class ObjectRegistry;
35 class RenderTaskList;
36
37 namespace Graphics
38 {
39 class Controller;
40 }
41
42 namespace Internal
43 {
44 class Core;
45 }
46
47 namespace Integration
48 {
49 class Core;
50 class PlatformAbstraction;
51 class Processor;
52 class RenderController;
53 class Scene;
54 struct Event;
55 struct TouchEvent;
56
57 /**
58  * The reasons why further updates are required.
59  */
60 namespace KeepUpdating
61 {
62 enum Reasons
63 {
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 };
70 }
71
72 /**
73  * The status of the Core::Update operation.
74  */
75 class UpdateStatus
76 {
77 public:
78   /**
79    * Constructor
80    */
81   UpdateStatus()
82   : keepUpdating(false),
83     needsNotification(false),
84     secondsFromLastFrame(0.0f)
85   {
86   }
87
88 public:
89   /**
90    * Query whether the Core has further frames to update & render e.g. when animations are ongoing.
91    * @return A bitmask of KeepUpdating values
92    */
93   uint32_t KeepUpdating()
94   {
95     return keepUpdating;
96   }
97
98   /**
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.
102    */
103   bool NeedsNotification()
104   {
105     return needsNotification;
106   }
107
108   /**
109    * This method is provided so that FPS can be easily calculated with a release version
110    * of Core.
111    * @return the seconds from last frame as float
112    */
113   float SecondsFromLastFrame()
114   {
115     return secondsFromLastFrame;
116   }
117
118 public:
119   uint32_t keepUpdating; ///< A bitmask of KeepUpdating values
120   bool     needsNotification;
121   float    secondsFromLastFrame;
122 };
123
124 /**
125  * The status of the Core::Render operation.
126  */
127 class RenderStatus
128 {
129 public:
130   /**
131    * Constructor
132    */
133   RenderStatus()
134   : needsUpdate(false),
135     needsPostRender(false)
136   {
137   }
138
139   /**
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
142    */
143   void SetNeedsUpdate(bool updateRequired)
144   {
145     needsUpdate = updateRequired;
146   }
147
148   /**
149    * Query the update status following rendering of a frame.
150    * @return True if update is required to be run
151    */
152   bool NeedsUpdate() const
153   {
154     return needsUpdate;
155   }
156
157   /**
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
161    */
162   void SetNeedsPostRender(bool postRenderRequired)
163   {
164     needsPostRender = postRenderRequired;
165   }
166
167   /**
168    * Queries if a post-render should be run.
169    * @return True if post-render is required to be run
170    */
171   bool NeedsPostRender() const
172   {
173     return needsPostRender;
174   }
175
176 private:
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.
179 };
180
181 /**
182  * Integration::Core is used for integration with the native windowing system.
183  * The following integration tasks must be completed:
184  *
185  * 1) Handle GL context creation, and notify the Core when this occurs.
186  *
187  * 2) Provide suspend/resume behaviour (see below for more details).
188  *
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).
191  *
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.
194  *
195  * 5) Provide an implementation of the PlatformAbstraction interface, used to access platform specific services.
196  *
197  * 6) Provide an implementation of the GlAbstraction interface, used to access OpenGL services.
198  *
199  * Multi-threading notes:
200  *
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.
203  *
204  * Rendering strategies:
205  *
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.
208  *
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.
213  */
214 class DALI_CORE_API Core
215 {
216 public:
217   /**
218    * Create a new 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.
228    */
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);
236
237   /**
238    * Non-virtual destructor. Core is not intended as a base class.
239    */
240   ~Core();
241
242   /**
243    * Initialize the core
244    */
245   void Initialize();
246
247   // GL Context Lifecycle
248
249   /**
250    * Get the object that will notify the application/toolkit when context is lost/regained
251    */
252   ContextNotifierInterface* GetContextNotifier();
253
254   /**
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.
259    */
260   void ContextCreated();
261
262   /**
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.
267    */
268   void ContextDestroyed();
269
270   /**
271    * Notify the Core that the GL context has been re-created, e.g. after ReplaceSurface
272    * or Context loss.
273    *
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.
276    *
277    * Multi-threading note: this method should be called from the main thread
278    */
279   void RecoverFromContextLoss();
280
281   // Core Lifecycle
282
283   /**
284    * Notify Core that the scene has been created.
285    */
286   void SceneCreated();
287
288   /**
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.
293    */
294   void QueueEvent(const Event& event);
295
296   /**
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.
300    */
301   void ProcessEvents();
302
303   /**
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).
310    */
311   uint32_t GetMaximumUpdateCount() const;
312
313   /**
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.
328    */
329   void Update(float         elapsedSeconds,
330               uint32_t      lastVSyncTimeMilliseconds,
331               uint32_t      nextVSyncTimeMilliseconds,
332               UpdateStatus& status,
333               bool          renderToFboEnabled,
334               bool          isRenderingToFbo,
335               bool          uploadOnly);
336
337   /**
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.
344    */
345   void PreRender(RenderStatus& status, bool forceClear);
346
347   /**
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.
354    */
355   void PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects);
356
357   /**
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.
366    */
367   void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo);
368
369   /**
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.
379    */
380   void RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect);
381
382   /**
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.
387    */
388   void PostRender();
389
390   /**
391    * @brief Register a processor
392    *
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.
396    */
397   void RegisterProcessor(Processor& processor, bool postProcessor = false);
398
399   /**
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.
403    */
404   void UnregisterProcessor(Processor& processor, bool postProcessor = false);
405
406   /**
407    * @brief Gets the Object registry.
408    * @return The object registry
409    */
410   ObjectRegistry GetObjectRegistry() const;
411
412 private:
413   /**
414    * Private constructor; see also Core::New()
415    */
416   Core();
417
418   /**
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.
422    */
423   Core(const Core& core);
424
425   /**
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.
429    */
430   Core& operator=(const Core& rhs);
431
432 private:
433   Internal::Core* mImpl;
434 };
435
436 } // namespace Integration
437
438 } // namespace Dali
439
440 #endif // DALI_INTEGRATION_CORE_H