[dali_2.3.30] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / graphics-api / graphics-controller.h
1 #ifndef DALI_GRAPHICS_CONTROLLER_H
2 #define DALI_GRAPHICS_CONTROLLER_H
3
4 /*
5  * Copyright (c) 2024 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 <dali/integration-api/graphics-config.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include "graphics-buffer-create-info.h"
28 #include "graphics-command-buffer-create-info.h"
29 #include "graphics-framebuffer-create-info.h"
30 #include "graphics-memory.h"
31 #include "graphics-pipeline-create-info.h"
32 #include "graphics-program-create-info.h"
33 #include "graphics-reflection.h"
34 #include "graphics-render-pass-create-info.h"
35 #include "graphics-render-target-create-info.h"
36 #include "graphics-sampler-create-info.h"
37 #include "graphics-shader-create-info.h"
38 #include "graphics-sync-object-create-info.h"
39 #include "graphics-texture-create-info.h"
40 #include "graphics-texture-upload-helper.h"
41
42 namespace Dali
43 {
44 namespace Graphics
45 {
46 class Command;
47 class CommandBuffer;
48 class Buffer;
49 class Framebuffer;
50 class Memory;
51 class Pipeline;
52 class RenderPass;
53 class RenderTarget;
54 class Sampler;
55 class Shader;
56 class SyncObject;
57 class Texture;
58
59 /**
60  * @brief Controller class controls render loop
61  *
62  * Controller class is responsible for executing render calls
63  * and controlling pipeline state.
64  */
65 class Controller
66 {
67 public:
68   /**
69    * Get graphics configuration for info about the graphics subsystem.
70    */
71   virtual Integration::GraphicsConfig& GetGraphicsConfig() = 0;
72
73   /**
74    * @brief Destroys controller
75    */
76   virtual ~Controller() = default;
77
78   /**
79    * @brief Submits array of command buffers
80    *
81    * Submits command buffers to the graphics pipeline. Submitted commands
82    * may be executed instantly or postponed.
83    *
84    * @param[in] submitInfo Valid SubmitInfo structure
85    */
86   virtual void SubmitCommandBuffers(const SubmitInfo& submitInfo) = 0;
87
88   /**
89    * @brief Presents render target
90    * @param renderTarget render target to present
91    */
92   virtual void PresentRenderTarget(RenderTarget* renderTarget) = 0;
93
94   /**
95    * @brief Waits until the GPU is idle
96    */
97   virtual void WaitIdle() = 0;
98
99   /**
100    * @brief Lifecycle pause event
101    */
102   virtual void Pause() = 0;
103
104   /**
105    * @brief Lifecycle resume event
106    */
107   virtual void Resume() = 0;
108
109   /**
110    * @brief Lifecycle shutdown event
111    */
112   virtual void Shutdown() = 0;
113
114   /**
115    * @brief Lifecycle destroy event
116    */
117   virtual void Destroy() = 0;
118
119   /**
120    * @brief Executes batch update of textures
121    *
122    * This function may perform full or partial update of many textures.
123    * The data source may come from:
124    * - CPU memory (client side)
125    * - GPU memory (another Texture or Buffer)
126    *
127    * UpdateTextures() is the only way to update unmappable Texture objects.
128    * It is recommended to batch updates as it may help with optimizing
129    * memory transfers based on dependencies.
130    *
131    */
132   virtual void UpdateTextures(const std::vector<TextureUpdateInfo>&       updateInfoList,
133                               const std::vector<TextureUpdateSourceInfo>& sourceList) = 0;
134
135   /**
136    * Auto generates mipmaps for the texture
137    * @param[in] texture The texture
138    */
139   virtual void GenerateTextureMipmaps(const Texture& texture) = 0;
140
141   /**
142    * @brief Enables depth/stencil buffer
143    *
144    * @param[in] enableDepth True to enable depth
145    * @param[in] enableStencil True to enable stencil
146    * @return True on success
147    */
148   virtual bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) = 0;
149
150   /**
151    * @brief Runs garbage collector (if supported)
152    *
153    * @param[in] numberOfDiscardedRenderers number of discarded renderers
154    */
155   virtual void RunGarbageCollector(size_t numberOfDiscardedRenderers) = 0;
156
157   /**
158    * @brief Discards unused resources
159    */
160   virtual void DiscardUnusedResources() = 0;
161
162   /**
163    * @brief Tests whether discard queue is empty
164    *
165    * @return True if empty
166    */
167   virtual bool IsDiscardQueueEmpty() = 0;
168
169   /**
170    * @brief Test if the graphics subsystem has resumed & should force a draw
171    *
172    * @return true if the graphics subsystem requires a re-draw
173    */
174   virtual bool IsDrawOnResumeRequired() = 0;
175
176   /**
177    * @brief Creates new Buffer object
178    *
179    * The Buffer object is created with underlying memory. The Buffer
180    * specification is immutable. Based on the BufferCreateInfo::usage,
181    * the memory may be client-side mappable or not.
182    *
183    * The old buffer may be passed as BufferCreateInfo::oldbuffer, however,
184    * it's up to the implementation whether the object will be reused or
185    * discarded and replaced by the new one.
186    *
187    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
188    * @param[in] oldBuffer The valid pointer to the old object or nullptr. The object will be reused or destroyed.
189    * @return pointer to the Buffer object
190    */
191   virtual UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, UniquePtr<Buffer>&& oldBuffer) = 0;
192
193   /**
194    * @brief Creates new CommandBuffer object
195    *
196    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
197    * @param[in] oldCommandBuffer The valid pointer to the old object or nullptr. The object will be reused or destroyed.
198    * @return pointer to the CommandBuffer object
199    */
200   virtual UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, UniquePtr<CommandBuffer>&& oldCommandBuffer) = 0;
201
202   /**
203    * @brief Creates new RenderPass object
204    *
205    * @param[in] renderPassCreateInfo The valid RenderPassCreateInfo structure
206    * @param[in] oldRenderPass The valid pointer to the old object or nullptr. The object will be reused or destroyed.
207    * @return pointer to the RenderPass object
208    */
209   virtual UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, UniquePtr<RenderPass>&& oldRenderPass) = 0;
210
211   /**
212    * @brief Creates new Texture object
213    *
214    * @param[in] textureCreateInfo The valid TextureCreateInfo structure
215    * @param[in] oldTexture The valid pointer to the old object or nullptr. The object will be reused or destroyed.
216    * @return pointer to the TextureCreateInfo object
217    */
218   virtual UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, UniquePtr<Texture>&& oldTexture) = 0;
219
220   /**
221    * @brief Creates new Framebuffer object
222    *
223    * @param[in] framebufferCreateInfo The valid FramebufferCreateInfo structure
224    * @param[in] oldFramebuffer The valid pointer to the old object or nullptr. The object will be reused or destroyed.
225    * @return pointer to the Framebuffer object
226    */
227   virtual UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, UniquePtr<Framebuffer>&& oldFramebuffer) = 0;
228
229   /**
230    * @brief Creates new Pipeline object
231    *
232    * @param[in] pipelineCreateInfo The valid PipelineCreateInfo structure
233    * @param[in] oldPipeline The valid pointer to the old object or nullptr. The object will be reused or destroyed.
234    * @return pointer to the Pipeline object
235    */
236   virtual UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, UniquePtr<Pipeline>&& oldPipeline) = 0;
237
238   /**
239    * @brief Creates new Program object
240    *
241    * @param[in] ProgramCreateInfo The valid ProgramCreateInfo structure
242    * @param[in] oldProgram The valid pointer to the old object or nullptr. The object will be reused or destroyed.
243    * @return pointer to the Program object
244    */
245   virtual UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) = 0;
246
247   /**
248    * @brief Creates new Shader object
249    *
250    * @param[in] shaderCreateInfo The valid ShaderCreateInfo structure
251    * @param[in] oldShader The valid pointer to the old object or nullptr. The object will be reused or destroyed.
252    * @return pointer to the Shader object
253    */
254   virtual UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, UniquePtr<Shader>&& oldShader) = 0;
255
256   /**
257    * @brief Creates new Sampler object
258    *
259    * @param[in] samplerCreateInfo The valid SamplerCreateInfo structure
260    * @param[in] oldSampler The valid pointer to the old object or nullptr. The object will be reused or destroyed.
261    * @return pointer to the Sampler object
262    */
263   virtual UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, UniquePtr<Sampler>&& oldSampler) = 0;
264
265   /**
266    * @brief Creates new RenderTarget object
267    *
268    * @param[in] renderTargetCreateInfo The valid RenderTargetCreateInfo structure
269    * @param[in] oldRenderTarget The valid pointer to the old object or nullptr. The object will be reused or destroyed.
270    * @return pointer to the RenderTarget object
271    */
272   virtual UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, UniquePtr<RenderTarget>&& oldRenderTarget) = 0;
273
274   /**
275    * Create a synchronisation object.
276    *
277    * @return A pointer to an opaque sync object
278    * @param[in] syncObjectCreateInfo The valid SyncObjectCreateInfo structure
279    * @param[in] oldSyncObject The valid pointer to the old object or nullptr. The object will be reused or destroyed.
280    */
281   virtual UniquePtr<SyncObject> CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
282                                                  UniquePtr<SyncObject>&&     oldSyncObject) = 0;
283
284   /**
285    * @brief Maps memory associated with Buffer object
286    *
287    * @param[in] mapInfo Filled details of mapped resource
288    * @return Returns pointer to Memory object or nullptr on error
289    */
290   virtual UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) = 0;
291
292   /**
293    * @brief Maps memory associated with the texture.
294    *
295    * Only Texture objects that are backed with linear memory (staging memory) can be mapped.
296    * Example:
297    * 1) GLES implementation may create PBO object as staging memory and couple it
298    * with the texture. Texture can be mapped and the memory can be read/write on demand.
299    *
300    * 2) Vulkan implementation may allocate DeviceMemory and use linear layout.
301    *
302    * @param[in] mapInfo Filled details of mapped resource
303    *
304    * @return Valid Memory object or nullptr on error
305    */
306   virtual UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) = 0;
307
308   /**
309    * @brief Unmaps memory and discards Memory object
310    *
311    * This function automatically removes lock if Memory has been
312    * previously locked.
313    *
314    * @param[in] memory Valid and previously mapped Memory object
315    */
316   virtual void UnmapMemory(UniquePtr<Memory> memory) = 0;
317
318   /**
319    * @brief Returns memory requirements of the Texture object.
320    *
321    * Call this function whenever it's necessary to know how much memory
322    * is needed to store all the texture data and what memory alignment
323    * the data should follow.
324    *
325    * @return Returns memory requirements of Texture
326    */
327   virtual MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const = 0;
328
329   /**
330    * @brief Returns memory requirements of the Buffer object.
331    *
332    * Call this function whenever it's necessary to know how much memory
333    * is needed to store all the buffer data and what memory alignment
334    * the data should follow.
335    *
336    * @return Returns memory requirements of Buffer
337    */
338   virtual MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const = 0;
339
340   /**
341    * @brief Returns specification of the Texture object
342    *
343    * Function obtains specification of the Texture object. It may retrieve
344    * implementation dependent details like ie. whether the texture is
345    * emulated (for example, RGB emulated on RGBA), compressed etc.
346    *
347    * @return Returns the TextureProperties object
348    */
349   virtual TextureProperties GetTextureProperties(const Texture& texture) = 0;
350
351   /**
352    * @brief Returns the reflection of the given program
353    *
354    * @param[in] program The program
355    * @return The reflection of the program
356    */
357   virtual const Reflection& GetProgramReflection(const Program& program) = 0;
358
359   /**
360    * @brief Tests whether two Pipelines are the same.
361    *
362    * On the higher level, this function may help wit creating pipeline cache.
363    *
364    * @return true if pipeline objects match
365    */
366   virtual bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const = 0;
367
368   /**
369    * @brief Retrieves program parameters
370    *
371    * This function can be used to retrieve data from internal implementation
372    *
373    * @param[in] program Valid program object
374    * @param[in] parameterId Integer parameter id
375    * @param[out] outData Pointer to output memory
376    * @return True on success
377    */
378   virtual bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) = 0;
379
380 public: // ResourceId relative API.
381   /**
382    * @brief Create Graphics::Texture as resourceId.
383    * The ownership of Graphics::Texture will be hold on this controller.
384    * @note If some Graphics::Texture already created before, assert.
385    * @post DiscardTextureFromResourceId() or ReleaseTextureFromResourceId() should be called when we don't use resourceId texture anymore.
386    *
387    * @param[in] resourceId The unique id of resouces.
388    * @return Pointer of Graphics::Texture, or nullptr if we fail to create.
389    */
390   virtual Graphics::Texture* CreateTextureByResourceId(uint32_t resourceId, const Graphics::TextureCreateInfo& createInfo) = 0;
391
392   /**
393    * @brief Discard Graphics::Texture as resourceId.
394    *
395    * @param[in] resourceId The unique id of resouces.
396    */
397   virtual void DiscardTextureFromResourceId(uint32_t resourceId) = 0;
398
399   /**
400    * @brief Get the Graphics::Texture as resourceId.
401    *
402    * @param[in] resourceId The unique id of resouces.
403    * @return Pointer of Graphics::Texture, or nullptr if there is no valid objects.
404    */
405   virtual Graphics::Texture* GetTextureFromResourceId(uint32_t resourceId) = 0;
406
407   /**
408    * @brief Get the ownership of Graphics::Texture as resourceId.
409    *
410    * @param[in] resourceId The unique id of resouces.
411    * @return Pointer of Graphics::Texture.
412    */
413   virtual UniquePtr<Graphics::Texture> ReleaseTextureFromResourceId(uint32_t resourceId) = 0;
414
415 protected:
416   /**
417    * Creates controller
418    */
419   Controller() = default;
420 };
421 } // namespace Graphics
422 } // namespace Dali
423
424 #endif