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