9b899591341950f96bd90ebf821ef3fed404a288
[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) 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
21 // INTERNAL INCLUDES
22 #include <dali/public-api/math/rect.h>
23 #include <dali/integration-api/core-enumerations.h>
24 #include <dali/internal/common/shader-saver.h>
25 #include <dali/internal/render/gl-resources/gpu-buffer.h>
26 #include <dali/internal/render/renderers/render-property-buffer.h>
27 #include <dali/internal/event/rendering/texture-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Integration
33 {
34 class GlAbstraction;
35 class GlSyncAbstraction;
36 class GlContextHelperAbstraction;
37 class RenderStatus;
38 class Scene;
39 }
40
41 struct Vector4;
42
43 namespace Internal
44 {
45 class Context;
46 class ProgramCache;
47 class ShaderSaver;
48
49 namespace Render
50 {
51 class FrameBuffer;
52 class Renderer;
53 struct Sampler;
54 class RenderTracker;
55 class Geometry;
56 class Texture;
57 }
58
59 namespace SceneGraph
60 {
61 class RenderQueue;
62 class RenderInstruction;
63 class RenderInstructionContainer;
64 class Shader;
65 class PropertyBufferDataProvider;
66 class Scene;
67
68 /**
69  * RenderManager is responsible for rendering the result of the previous "update", which
70  * is provided in a RenderCommand during UpdateManager::Update().
71  */
72 class RenderManager
73 {
74 public:
75
76   /**
77    * Construct a new RenderManager.
78    * @param[in]  glAbstraction              The GL abstraction used for rendering.
79    * @param[in]  glSyncAbstraction          The GL sync abstraction used fence sync creation/deletion.
80    * @param[in]  glContextHelperAbstraction The GL context helper abstraction for accessing GL context.
81    * @param[in]  depthBufferAvailable       Whether the depth buffer is available
82    * @param[in]  stencilBufferAvailable     Whether the stencil buffer is available
83    */
84   static RenderManager* New( Integration::GlAbstraction& glAbstraction,
85                              Integration::GlSyncAbstraction& glSyncAbstraction,
86                              Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
87                              Integration::DepthBufferAvailable depthBufferAvailable,
88                              Integration::StencilBufferAvailable stencilBufferAvailable );
89
90   /**
91    * Non-virtual destructor; not intended as a base class
92    */
93   ~RenderManager();
94
95   /**
96    * Retrieve the RenderQueue. Messages should only be added to this from the update-thread.
97    * @return The render queue.
98    */
99   RenderQueue& GetRenderQueue();
100
101   /**
102    * @copydoc Dali::Integration::Core::ContextCreated()
103    */
104   void ContextCreated();
105
106   /**
107    * @copydoc Dali::Integration::Core::ContextToBeDestroyed()
108    */
109   void ContextDestroyed();
110
111   /**
112    * Set the upstream interface for compiled shader binaries to be sent back to for eventual
113    * caching and saving.
114    * @param[in] upstream The abstract interface to send any received ShaderDatas onwards to..
115    * @note This should be called during core initialisation if shader binaries are to be used.
116    */
117   void SetShaderSaver( ShaderSaver& upstream );
118
119   // The following methods should be called via RenderQueue messages
120
121   /*
122    * Set the frame time delta (time elapsed since the last frame.
123    * @param[in] deltaTime the delta time
124    */
125   void SetFrameDeltaTime( float deltaTime );
126
127   /**
128    * Returns the rectangle for the default surface (probably the application window).
129    * @return Rectangle for the surface.
130    */
131   void SetDefaultSurfaceRect( const Rect<int>& rect );
132
133   /**
134    * Add a Renderer to the render manager.
135    * @param[in] renderer The renderer to add.
136    * @post renderer is owned by RenderManager
137    */
138   void AddRenderer( OwnerPointer< Render::Renderer >& renderer );
139
140   /**
141    * Remove a Renderer from the render manager.
142    * @param[in] renderer The renderer to remove.
143    * @post renderer is destroyed.
144    */
145   void RemoveRenderer( Render::Renderer* renderer );
146
147   /**
148    * Add a sampler to the render manager.
149    * @param[in] sampler The sampler to add.
150    * @post sampler is owned by RenderManager
151    */
152   void AddSampler( OwnerPointer< Render::Sampler >& sampler );
153
154   /**
155    * Remove a sampler from the render manager.
156    * @param[in] sampler The sampler to remove.
157    * @post sampler is destroyed.
158    */
159   void RemoveSampler( Render::Sampler* sampler );
160
161   /**
162    * Set minification and magnification filter modes for a sampler
163    * @param[in] minFilterMode Filter mode to use when the texture is minificated
164    * @param[in] magFilterMode Filter mode to use when the texture is magnified
165    */
166   void SetFilterMode( Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode );
167
168   /**
169    * Set wrapping mode for a sampler
170    * @param[in] rWrapMode Wrap mode in the z direction
171    * @param[in] uWrapMode Wrap mode in the x direction
172    * @param[in] vWrapMode Wrap mode in the y direction
173    */
174   void SetWrapMode( Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode );
175
176   /**
177    * Add a property buffer to the render manager.
178    * @param[in] propertyBuffer The property buffer to add.
179    * @post propertBuffer is owned by RenderManager
180    */
181   void AddPropertyBuffer( OwnerPointer< Render::PropertyBuffer >& propertyBuffer );
182
183   /**
184    * Remove a property buffer from the render manager.
185    * @param[in] propertyBuffer The property buffer to remove.
186    * @post propertyBuffer is destroyed.
187    */
188   void RemovePropertyBuffer( Render::PropertyBuffer* propertyBuffer );
189
190   /**
191    * Add a geometry to the render manager.
192    * @param[in] geometry The geometry to add.
193    * @post geometry is owned by RenderManager
194    */
195   void AddGeometry( OwnerPointer< Render::Geometry >& geometry );
196
197   /**
198    * Remove a geometry from the render manager.
199    * @param[in] geometry The geometry to remove.
200    * @post geometry is destroyed.
201    */
202   void RemoveGeometry( Render::Geometry* geometry );
203
204   /**
205    * Adds a property buffer to a geometry from the render manager.
206    * @param[in] geometry The geometry
207    * @param[in] propertyBuffer The property buffer to remove.
208    */
209   void AttachVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer );
210
211   /**
212    * Remove a property buffer from a Render::Geometry from the render manager.
213    * @param[in] geometry The geometry
214    * @param[in] propertyBuffer The property buffer to remove.
215    * @post property buffer is destroyed.
216    */
217   void RemoveVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer );
218
219   /**
220    * Sets the format of an existing property buffer
221    * @param[in] propertyBuffer The property buffer.
222    * @param[in] format The new format of the buffer
223    */
224   void SetPropertyBufferFormat( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Render::PropertyBuffer::Format>& format );
225
226   /**
227    * Sets the data of an existing property buffer
228    * @param[in] propertyBuffer The property buffer.
229    * @param[in] data The new data of the buffer
230    * @param[in] size The new size of the buffer
231    */
232   void SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Vector<uint8_t> >& data, uint32_t size );
233
234   /**
235    * Sets the data for the index buffer of an existing geometry
236    * @param[in] geometry The geometry
237    * @param[in] data A vector containing the indices
238    */
239   void SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<uint16_t>& data );
240
241   /**
242    * Set the geometry type of an existing render geometry
243    * @param[in] geometry The render geometry
244    * @param[in] geometryType The new geometry type
245    */
246   void SetGeometryType( Render::Geometry* geometry, uint32_t geometryType );
247
248   /**
249    * Adds a texture to the render manager
250    * @param[in] texture The texture to add
251    */
252   void AddTexture( OwnerPointer< Render::Texture >& texture );
253
254   /**
255    * Removes a texture from the render manager
256    * @param[in] texture The texture to remove
257    */
258   void RemoveTexture( Render::Texture* texture );
259
260   /**
261    * Uploads data to an existing texture
262    * @param[in] texture The texture
263    * @param[in] pixelData The pixel data object
264    * @param[in] params The parameters for the upload
265    */
266   void UploadTexture( Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params );
267
268   /**
269    * Generates mipmaps for a given texture
270    * @param[in] texture The texture
271    */
272   void GenerateMipmaps( Render::Texture* texture );
273
274   /**
275    * Adds a framebuffer to the render manager
276    * @param[in] frameBuffer The framebuffer to add
277    */
278   void AddFrameBuffer( OwnerPointer< Render::FrameBuffer >& frameBuffer );
279
280   /**
281    * Removes a framebuffer from the render manager
282    * @param[in] frameBuffer The framebuffer to remove
283    */
284   void RemoveFrameBuffer( Render::FrameBuffer* frameBuffer );
285
286   /**
287    * Attach a texture as color output to an existing FrameBuffer
288    * @param[in] frameBuffer The FrameBuffer
289    * @param[in] texture The texture that will be used as output when rendering
290    * @param[in] mipmapLevel The mipmap of the texture to be attached
291    * @param[in] layer Indicates which layer of a cube map or array texture to attach. Unused for 2D textures
292    */
293   void AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer );
294
295   /**
296    * Initializes a Scene to the render manager
297    * @param[in] scene The Scene to initialize
298    */
299   void InitializeScene( SceneGraph::Scene* scene );
300
301   /**
302    * Uninitializes a Scene to the render manager
303    * @param[in] scene The Scene to uninitialize
304    */
305   void UninitializeScene( SceneGraph::Scene* scene );
306
307   /**
308    * This is called when the surface of the scene has been replaced.
309    * @param[in] scene The scene.
310    */
311   void SurfaceReplaced( SceneGraph::Scene* scene );
312
313   /**
314    * Adds a render tracker to the RenderManager. RenderManager takes ownership of the
315    * tracker. The lifetime of the tracker is related to the lifetime of the tracked
316    * object, usually an offscreen render task.
317    * @param[in] renderTracker The render tracker
318    */
319   void AddRenderTracker( Render::RenderTracker* renderTracker );
320
321   /**
322    * Removes a render tracker from the RenderManager.
323    * @param[in] renderTracker The render tracker to remove.
324    */
325   void RemoveRenderTracker( Render::RenderTracker* renderTracker );
326
327   /**
328    * returns the Program controller for sending program messages
329    * @return the ProgramController
330    */
331   ProgramCache* GetProgramCache();
332
333   // This method should be called from Core::PreRender()
334
335   /**
336    * This is called before rendering any scene in the next frame. This method should be preceded
337    * by a call up Update.
338    * Multi-threading note: this method should be called from a dedicated rendering thread.
339    * @pre The GL context must have been created, and made current.
340    * @param[out] status showing whether update is required to run.
341    * @param[in] forceClear force the Clear on the framebuffer even if nothing is rendered.
342    * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
343    */
344   void PreRender( Integration::RenderStatus& status, bool forceClear, bool uploadOnly );
345
346   // This method should be called from Core::RenderScene()
347
348   /**
349    * Render a scene in the next frame. This method should be preceded by a call up PreRender.
350    * This method should be called twice. The first pass to render off-screen frame buffers if any,
351    * and the second pass to render the surface.
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[in] renderToFbo True to render off-screen frame buffers only if any, and False to render the surface only.
356    */
357   void RenderScene( Integration::Scene& scene, bool renderToFbo );
358
359   // This method should be called from Core::PostRender()
360
361   /**
362    * This is called after rendering all the scenes in the next frame. This method should be
363    * followed by a call up RenderScene.
364    * Multi-threading note: this method should be called from a dedicated rendering thread.
365    * @pre The GL context must have been created, and made current.
366    * @param[in] uploadOnly uploadOnly Upload the resource only without rendering.
367    */
368   void PostRender( bool uploadOnly );
369
370
371 private:
372
373 private:
374
375   /**
376    * Construct a new RenderManager.
377    */
378   RenderManager();
379
380   // Undefined
381   RenderManager( const RenderManager& );
382
383   // Undefined
384   RenderManager& operator=( const RenderManager& rhs );
385
386 private:
387
388   struct Impl;
389   Impl* mImpl;
390
391 };
392
393 } // namespace SceneGraph
394
395 } // namespace Internal
396
397 } // namespace Dali
398
399 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_MANAGER_H