Fix thread contention issues in messages and pan gestures.
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDER_MANAGER_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDER_MANAGER_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include <dali/devel-api/common/mutex.h>
23 #include <dali/public-api/math/rect.h>
24 #include <dali/internal/common/shader-saver.h>
25 #include <dali/internal/render/common/post-process-resource-dispatcher.h>
26 #include <dali/internal/update/resources/resource-manager-declarations.h>
27 #include <dali/internal/render/gl-resources/gpu-buffer.h>
28
29 namespace Dali
30 {
31
32 namespace Integration
33 {
34 class GlAbstraction;
35 class RenderStatus;
36 }
37
38 struct Vector4;
39
40 namespace Internal
41 {
42 class Context;
43 class ProgramCache;
44 class ShaderSaver;
45
46 namespace SceneGraph
47 {
48 class Renderer;
49 class RenderQueue;
50 class TextureCache;
51 class RenderInstruction;
52 class RenderInstructionContainer;
53 class RenderTracker;
54 class Shader;
55 class RenderGeometry;
56 class PropertyBufferDataProvider;
57
58 /**
59  * RenderManager is responsible for rendering the result of the previous "update", which
60  * is provided in a RenderCommand during UpdateManager::Update().
61  */
62 class RenderManager : public PostProcessResourceDispatcher
63 {
64 public:
65
66   /**
67    * Construct a new RenderManager.
68    * @param[in]  glAbstraction The GL abstraction used for rendering.
69    * @param[out] resourcePostProcessQueue A queue for sending rendered texture ids to the update-thread.
70    */
71   static RenderManager* New( Dali::Integration::GlAbstraction& glAbstraction, ResourcePostProcessList& resourcePostProcessQueue );
72
73   /**
74    * Non-virtual destructor; not intended as a base class
75    */
76   ~RenderManager();
77
78   /**
79    * Retrieve the RenderQueue. Messages should only be added to this from the update-thread.
80    * @return The render queue.
81    */
82   RenderQueue& GetRenderQueue();
83
84   /**
85    * Retrieve the texture cache. Messages should only be sent to this from the update thread,
86    * accessor methods should only be used from the render thread.
87    * @return The texture cache
88    */
89   TextureCache& GetTextureCache();
90
91   /**
92    * @copydoc Dali::Integration::Core::ContextCreated()
93    */
94   void ContextCreated();
95
96   /**
97    * @copydoc Dali::Integration::Core::ContextToBeDestroyed()
98    */
99   void ContextDestroyed();
100
101   /**
102    * Dispatch requests onto the postProcessResourcesQueue
103    * @param[in] request The request to dispatch
104    */
105   virtual void DispatchPostProcessRequest( ResourcePostProcessRequest& request );
106
107   /**
108    * Set the upstream interface for compiled shader binaries to be sent back to for eventual
109    * caching and saving.
110    * @param[in] upstream The abstract interface to send any received ShaderDatas onwards to..
111    * @note This should be called during core initialisation if shader binaries are to be used.
112    */
113   void SetShaderSaver( ShaderSaver& upstream );
114
115   /**
116    * Retrieve the render instructions; these should be set during each "update" traversal.
117    * @return The render instruction container.
118    */
119   RenderInstructionContainer& GetRenderInstructionContainer();
120
121   // The following methods should be called via RenderQueue messages
122
123   /**
124    * Set the background color i.e. the glClear color used at the beginning of each frame.
125    * @param[in] color The new background color.
126    */
127   void SetBackgroundColor( const Vector4& color );
128
129   /*
130    * Set the frame time delta (time elapsed since the last frame.
131    * @param[in] deltaTime the delta time
132    */
133   void SetFrameDeltaTime( float deltaTime );
134
135   /**
136    * Returns the rectangle for the default surface (probably the application window).
137    * @return Rectangle for the surface.
138    */
139   void SetDefaultSurfaceRect( const Rect<int>& rect );
140
141   /**
142    * Add a Renderer to the render manager.
143    * @param[in] renderer The renderer to add.
144    * @post renderer is owned by RenderManager
145    */
146   void AddRenderer( Renderer* renderer );
147
148   /**
149    * Remove a Renderer from the render manager.
150    * @param[in] renderer The renderer to remove.
151    * @post renderer is destroyed.
152    */
153   void RemoveRenderer( Renderer* renderer );
154
155   /**
156    * Add a geometry to the render manager.
157    * @param[in] geometry The geometry to add.
158    * @post geometry is owned by RenderManager
159    */
160   void AddGeometry( RenderGeometry* geometry );
161
162   /**
163    * Remove a geometry from the render manager.
164    * @param[in] geometry The geometry to remove.
165    * @post geometry is destroyed.
166    */
167   void RemoveGeometry( RenderGeometry* geometry );
168
169   /**
170    * Adds a property buffer to a RenderGeometry from the render manager.
171    * @param[in] geometry The geometry
172    * @param[in] propertyBuffer The property buffer to remove.
173    * @param[in] target Specifies the type of the buffer
174    * @param[in] usage Specifies how will the buffer be used
175    */
176   void AddPropertyBuffer( RenderGeometry* renderGeometry, PropertyBufferDataProvider* propertyBuffer, const GpuBuffer::Target& target, const GpuBuffer::Usage& usage );
177
178   /**
179    * Remove a property buffer from a RenderGeometry from the render manager.
180    * @param[in] geometry The geometry
181    * @param[in] propertyBuffer The property buffer to remove.
182    * @post property buffer is destroyed.
183    */
184   void RemovePropertyBuffer( RenderGeometry* renderGeometry, PropertyBufferDataProvider* propertyBuffer );
185
186   /**
187    * Adds a render tracker to the RenderManager. RenderManager takes ownership of the
188    * tracker. The lifetime of the tracker is related to the lifetime of the tracked
189    * object, usually an offscreen render task.
190    * @param[in] renderTracker The render tracker
191    */
192   void AddRenderTracker( RenderTracker* renderTracker );
193
194   /**
195    * Removes a render tracker from the RenderManager.
196    * @param[in] renderTracker The render tracker to remove.
197    */
198   void RemoveRenderTracker( RenderTracker* renderTracker );
199
200   /**
201    * Set the default shader that is to be used in absence of custom shader
202    * @param[in] shader that is the default one
203    */
204   void SetDefaultShader( Shader* shader );
205
206   /**
207    * returns the Program controller for sending program messages
208    * @return the ProgramController
209    */
210   ProgramCache* GetProgramCache();
211
212   // This method should be called from Core::Render()
213
214   /**
215    * Renders the results of the previous "update" traversal.
216    * @param[out] status contains the flag that indicates if render instructions exist
217    * @return true if a further update is required
218    */
219   bool Render( Integration::RenderStatus& status );
220
221 private:
222
223   /**
224    * Helper to process a single RenderInstruction.
225    * @param[in] instruction A description of the rendering operation.
226    * @param[in] defaultShader default shader to use.
227    * @param[in] elapsedTime from previous render.
228    */
229   void DoRender( RenderInstruction& instruction, Shader& defaultShader, float elapsedTime );
230
231 private:
232
233   /**
234    * Construct a new RenderManager.
235    */
236   RenderManager();
237
238   // Undefined
239   RenderManager( const RenderManager& );
240
241   // Undefined
242   RenderManager& operator=( const RenderManager& rhs );
243
244   // Set the last frame time while locking access
245   void SetLastFrameTime();
246
247 private:
248
249   struct Impl;
250   Impl* mImpl;
251   Dali::Mutex mMutex;
252
253 };
254
255 } // namespace SceneGraph
256
257 } // namespace Internal
258
259 } // namespace Dali
260
261 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDER_MANAGER_H__