(Scene) Add frame rendered/presented callback
[platform/core/uifw/dali-core.git] / dali / internal / update / common / scene-graph-scene.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_SCENE_H
2 #define DALI_INTERNAL_SCENE_GRAPH_SCENE_H
3
4 /*
5  * Copyright (c) 2020 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 // INTERNAL INCLUDES
21 #include <dali/integration-api/scene.h>
22 #include <dali/integration-api/gl-defines.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/render/gl-resources/context.h>
26 #include <dali/internal/render/common/render-instruction-container.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 class Context;
36
37 namespace SceneGraph
38 {
39 class RenderInstructionContainer;
40
41 class Scene
42 {
43 public:
44
45   /**
46    * Constructor
47    * @param[in] surface The render surface
48    */
49   Scene();
50
51   /**
52    * Destructor
53    */
54   virtual ~Scene();
55
56   /**
57    * Creates a scene object in the GPU.
58    * @param[in] context The GL context
59    */
60   void Initialize( Context& context );
61
62   /**
63    * Called by RenderManager to inform the scene that the context has been destroyed
64    */
65   void GlContextDestroyed();
66
67   /**
68    * Gets the context holding the GL state of rendering for the scene
69    * @return the context
70    */
71   Context* GetContext();
72
73   /**
74    * Gets the render instructions for the scene
75    * @return the render instructions
76    */
77   RenderInstructionContainer& GetRenderInstructions();
78
79   /**
80    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
81    *
82    * @param[in] callback The function to call
83    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
84    *
85    * @note A callback of the following type may be used:
86    * @code
87    *   void MyFunction( int frameId );
88    * @endcode
89    * This callback will be deleted once it is called.
90    *
91    * @note Ownership of the callback is passed onto this class.
92    */
93   void AddFrameRenderedCallback( CallbackBase* callback, int32_t frameId );
94
95   /**
96    * @brief Adds a callback that is called when the frame is displayed on the display.
97    *
98    * @param[in] callback The function to call
99    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
100    *
101    * @note A callback of the following type may be used:
102    * @code
103    *   void MyFunction( int frameId );
104    * @endcode
105    * This callback will be deleted once it is called.
106    *
107    * @note Ownership of the callback is passed onto this class.
108    */
109   void AddFramePresentedCallback( CallbackBase* callback, int32_t frameId );
110
111   /**
112    * @brief Gets the callback list that is called when the frame rendering is done by the graphics driver.
113    *
114    * @param[out] callbacks The callback list
115    */
116   void GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks );
117
118   /**
119    * @brief Gets the callback list that is called when the frame is displayed on the display.
120    *
121    * @param[out] callbacks The callback list
122    */
123   void GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks );
124
125 private:
126
127   Context*                    mContext;   ///< The context holding the GL state of rendering for the scene
128
129   // Render instructions describe what should be rendered during RenderManager::RenderScene()
130   // Update manager updates instructions for the next frame while we render the current one
131
132   RenderInstructionContainer  mInstructions;   ///< Render instructions for the scene
133
134   Dali::Integration::Scene::FrameCallbackContainer mFrameRenderedCallbacks;   ///< Frame rendered callbacks
135   Dali::Integration::Scene::FrameCallbackContainer mFramePresentedCallbacks;  ///< Frame presented callbacks
136 };
137
138 /// Messages
139 inline void AddFrameRenderedCallbackMessage( EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId )
140 {
141   typedef MessageValue2< Scene, CallbackBase*, int32_t > LocalType;
142
143   // Reserve some memory inside the message queue
144   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
145
146   // Construct message in the message queue memory; note that delete should not be called on the return value
147   new (slot) LocalType( &scene, &Scene::AddFrameRenderedCallback, const_cast< CallbackBase* >( callback ), frameId );
148 }
149
150 inline void AddFramePresentedCallbackMessage( EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId )
151 {
152   typedef MessageValue2< Scene, CallbackBase*, int32_t > LocalType;
153
154   // Reserve some memory inside the message queue
155   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
156
157   // Construct message in the message queue memory; note that delete should not be called on the return value
158   new (slot) LocalType( &scene, &Scene::AddFramePresentedCallback, const_cast< CallbackBase* >( callback ), frameId );
159 }
160
161 } // namespace SceneGraph
162
163 } // namespace Internal
164
165 } // namespace Dali
166
167
168 #endif // DALI_INTERNAL_SCENE_GRAPH_SCENE_H