[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-controller.h
1 #ifndef TEST_GRAPHICS_CONTROLLER_H
2 #define TEST_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 #include <dali/graphics-api/graphics-controller.h>
21 #include <unordered_map>
22 #include "test-gl-abstraction.h"
23 #include "test-graphics-command-buffer.h"
24 #include "test-graphics-program.h"
25 #include "test-graphics-reflection.h"
26 #include "test-graphics-sync-impl.h"
27
28 namespace Dali
29 {
30 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo);
31 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo);
32 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType);
33 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent);
34 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo);
35 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode);
36 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode);
37 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode);
38 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo);
39
40 template<typename T>
41 T* Uncast(const Graphics::CommandBuffer* object)
42 {
43   return const_cast<T*>(static_cast<const T*>(object));
44 }
45
46 template<typename T>
47 T* Uncast(const Graphics::Texture* object)
48 {
49   return const_cast<T*>(static_cast<const T*>(object));
50 }
51
52 template<typename T>
53 T* Uncast(const Graphics::Sampler* object)
54 {
55   return const_cast<T*>(static_cast<const T*>(object));
56 }
57
58 template<typename T>
59 T* Uncast(const Graphics::Buffer* object)
60 {
61   return const_cast<T*>(static_cast<const T*>(object));
62 }
63
64 template<typename T>
65 T* Uncast(const Graphics::Shader* object)
66 {
67   return const_cast<T*>(static_cast<const T*>(object));
68 }
69
70 template<typename T>
71 T* Uncast(const Graphics::Framebuffer* object)
72 {
73   return const_cast<T*>(static_cast<const T*>(object));
74 }
75
76 template<typename T>
77 T* Uncast(const Graphics::Pipeline* object)
78 {
79   return const_cast<T*>(static_cast<const T*>(object));
80 }
81
82 template<typename T>
83 T* Uncast(const Graphics::RenderTarget* object)
84 {
85   return const_cast<T*>(static_cast<const T*>(object));
86 }
87
88 template<typename T>
89 T* Uncast(const Graphics::SyncObject* object)
90 {
91   return const_cast<T*>(static_cast<const T*>(object));
92 }
93
94 class TestGraphicsController : public Dali::Graphics::Controller
95 {
96 public:
97   TestGraphicsController();
98
99   virtual ~TestGraphicsController() = default;
100
101   void Initialize()
102   {
103     mGl.Initialize();
104   }
105
106   Integration::GlAbstraction& GetGlAbstraction()
107   {
108     return mGl;
109   }
110
111   Integration::GraphicsConfig& GetGraphicsConfig()
112   {
113     return mGl;
114   }
115
116   TestGraphicsSyncImplementation& GetGraphicsSyncImpl()
117   {
118     return mGraphicsSyncImpl;
119   }
120
121   void SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo) override;
122
123   /**
124    * @brief Presents render target
125    * @param renderTarget render target to present
126    */
127   void PresentRenderTarget(Graphics::RenderTarget* renderTarget) override;
128
129   /**
130    * @brief Waits until the GPU is idle
131    */
132   void WaitIdle() override;
133
134   /**
135    * @brief Lifecycle pause event
136    */
137   void Pause() override;
138
139   /**
140    * @brief Lifecycle resume event
141    */
142   void Resume() override;
143
144   /**
145    * @brief Lifecycle shutdown event
146    */
147   void Shutdown() override;
148
149   /**
150    * @brief Lifecycle destroy event
151    */
152   void Destroy() override;
153
154   /**
155    * @brief Executes batch update of textures
156    *
157    * This function may perform full or partial update of many textures.
158    * The data source may come from:
159    * - CPU memory (client side)
160    * - GPU memory (another Texture or Buffer)
161    *
162    * UpdateTextures() is the only way to update unmappable Texture objects.
163    * It is recommended to batch updates as it may help with optimizing
164    * memory transfers based on dependencies.
165    *
166    */
167   void UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>&       updateInfoList,
168                       const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList) override;
169
170   /**
171    * Auto generates mipmaps for the texture
172    * @param[in] texture The texture
173    */
174   void GenerateTextureMipmaps(const Graphics::Texture& texture) override;
175
176   /**
177    * TBD: do we need those functions in the new implementation?
178    */
179   bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override;
180
181   void RunGarbageCollector(size_t numberOfDiscardedRenderers) override;
182
183   void DiscardUnusedResources() override;
184
185   bool IsDiscardQueueEmpty() override;
186
187   /**
188    * @brief Test if the graphics subsystem has resumed & should force a draw
189    *
190    * @return true if the graphics subsystem requires a re-draw
191    */
192   bool IsDrawOnResumeRequired() override;
193
194   /**
195    * @brief Creates new Buffer object
196    *
197    * The Buffer object is created with underlying memory. The Buffer
198    * specification is immutable. Based on the BufferCreateInfo::usage,
199    * the memory may be client-side mappable or not.
200    *
201    * The old buffer may be passed as BufferCreateInfo::oldbuffer, however,
202    * it's up to the implementation whether the object will be reused or
203    * discarded and replaced by the new one.
204    *
205    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
206    * @return pointer to the Buffer object
207    */
208   Graphics::UniquePtr<Graphics::Buffer> CreateBuffer(const Graphics::BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer) override;
209
210   void DiscardBuffer(TestGraphicsBuffer* buffer);
211
212   /**
213    * @brief Creates new CommandBuffer object
214    *
215    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
216    * @return pointer to the CommandBuffer object
217    */
218   Graphics::UniquePtr<Graphics::CommandBuffer> CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer) override;
219
220   /**
221    * @brief Creates new RenderPass object
222    *
223    * @param[in] renderPassCreateInfo The valid RenderPassCreateInfo structure
224    * @return pointer to the RenderPass object
225    */
226   Graphics::UniquePtr<Graphics::RenderPass> CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass) override;
227
228   /**
229    * @brief Creates new Texture object
230    *
231    * @param[in] textureCreateInfo The valid TextureCreateInfo structure
232    * @return pointer to the TextureCreateInfo object
233    */
234   Graphics::UniquePtr<Graphics::Texture> CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture) override;
235
236   /**
237    * @brief Creates new Framebuffer object
238    *
239    * @param[in] framebufferCreateInfo The valid FramebufferCreateInfo structure
240    * @return pointer to the Framebuffer object
241    */
242   Graphics::UniquePtr<Graphics::Framebuffer> CreateFramebuffer(const Graphics::FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer) override;
243
244   /**
245    * @brief Creates new Pipeline object
246    *
247    * @param[in] pipelineCreateInfo The valid PipelineCreateInfo structure
248    * @return pointer to the Pipeline object
249    */
250   Graphics::UniquePtr<Graphics::Pipeline> CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline) override;
251
252   /**
253    * @brief Creates new Program object
254    *
255    * @param[in] programCreateInfo The valid ProgramCreateInfo structure
256    * @return pointer to the Program object
257    */
258   Graphics::UniquePtr<Graphics::Program> CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram) override;
259
260   /**
261    * @brief Creates new Shader object
262    *
263    * @param[in] shaderCreateInfo The valid ShaderCreateInfo structure
264    * @return pointer to the Shader object
265    */
266   Graphics::UniquePtr<Graphics::Shader> CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader) override;
267
268   /**
269    * @brief Creates new Sampler object
270    *
271    * @param[in] samplerCreateInfo The valid SamplerCreateInfo structure
272    * @return pointer to the Sampler object
273    */
274   Graphics::UniquePtr<Graphics::Sampler> CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler) override;
275
276   /**
277    * @brief Creates new RenderTarget object
278    *
279    * @param[in] renderTargetCreateInfo The valid RenderTargetCreateInfo structure
280    * @return pointer to the RenderTarget object
281    */
282   Graphics::UniquePtr<Graphics::RenderTarget> CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget) override;
283
284   /**
285    * @brief Creates new sync object
286    * Could add timeout etc to createinfo... but nah.
287    *
288    * @return pointer to the SyncObject
289    */
290   Graphics::UniquePtr<Graphics::SyncObject> CreateSyncObject(const Graphics::SyncObjectCreateInfo&       syncObjectCreateInfo,
291                                                              Graphics::UniquePtr<Graphics::SyncObject>&& oldSyncObject) override;
292
293   /**
294    * @brief Maps memory associated with Buffer object
295    *
296    * @param[in] mapInfo Filled details of mapped resource
297    *
298    * @return Returns pointer to Memory object or nullptr on error
299    */
300   Graphics::UniquePtr<Graphics::Memory> MapBufferRange(const Graphics::MapBufferInfo& mapInfo) override;
301
302   /**
303    * @brief Maps memory associated with the texture.
304    *
305    * Only Texture objects that are backed with linear memory (staging memory) can be mapped.
306    * Example:
307    * 1) GLES implementation may create PBO object as staging memory and couple it
308    * with the texture. Texture can be mapped and the memory can be read/write on demand.
309    *
310    * 2) Vulkan implementation may allocate DeviceMemory and use linear layout.
311    *
312    * @param[in] mapInfo Filled details of mapped resource
313    *
314    * @return Valid Memory object or nullptr on error
315    */
316   Graphics::UniquePtr<Graphics::Memory> MapTextureRange(const Graphics::MapTextureInfo& mapInfo) override;
317
318   /**
319    * @brief Unmaps memory and discards Memory object
320    *
321    * This function automatically removes lock if Memory has been
322    * previously locked.
323    *
324    * @param[in] memory Valid and previously mapped Memory object
325    */
326   void UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory) override;
327
328   /**
329    * @brief Returns memory requirements of the Texture object.
330    *
331    * Call this function whenever it's necessary to know how much memory
332    * is needed to store all the texture data and what memory alignment
333    * the data should follow.
334    *
335    * @return Returns memory requirements of Texture
336    */
337   Graphics::MemoryRequirements GetTextureMemoryRequirements(Graphics::Texture& texture) const override;
338
339   /**
340    * @brief Returns memory requirements of the Buffer object.
341    *
342    * Call this function whenever it's necessary to know how much memory
343    * is needed to store all the buffer data and what memory alignment
344    * the data should follow.
345    *
346    * @return Returns memory requirements of Buffer
347    */
348   Graphics::MemoryRequirements GetBufferMemoryRequirements(Graphics::Buffer& buffer) const override;
349
350   /**
351    * @brief Returns specification of the Texture object
352    *
353    * Function obtains specification of the Texture object. It may retrieve
354    * implementation dependent details like ie. whether the texture is
355    * emulated (for example, RGB emulated on RGBA), compressed etc.
356    *
357    * @return Returns the TextureProperties object
358    */
359   Graphics::TextureProperties GetTextureProperties(const Graphics::Texture& texture) override;
360
361   /**
362    * @brief Returns the reflection of the given program
363    *
364    * @param[in] program The program
365    * @return The reflection of the program
366    */
367   const Graphics::Reflection& GetProgramReflection(const Graphics::Program& program) override;
368
369   /**
370    * @brief Tests whether two Pipelines are the same.
371    *
372    * On the higher level, this function may help wit creating pipeline cache.
373    *
374    * @return true if pipeline objects match
375    */
376   bool PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const override;
377
378   /**
379    * @brief Retrieves program parameters
380    *
381    * This function can be used to retrieve data from internal implementation
382    *
383    * @param[in] program Valid program object
384    * @param[in] parameterId Integer parameter id
385    * @param[out] outData Pointer to output memory
386    * @return True on success
387    */
388   bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
389
390 public: // ResourceId relative API.
391   /**
392    * @brief Create Graphics::Texture as resourceId.
393    * The ownership of Graphics::Texture will be hold on this controller.
394    * @note If some Graphics::Texture already created before, assert.
395    * @post DiscardTextureFromResourceId() or ReleaseTextureFromResourceId() should be called when we don't use resourceId texture anymore.
396    *
397    * @param[in] resourceId The unique id of resouces.
398    * @return Pointer of Graphics::Texture, or nullptr if we fail to create.
399    */
400   Graphics::Texture* CreateTextureByResourceId(uint32_t resourceId, const Graphics::TextureCreateInfo& createInfo) override;
401
402   /**
403    * @brief Discard Graphics::Texture as resourceId.
404    *
405    * @param[in] resourceId The unique id of resouces.
406    */
407   void DiscardTextureFromResourceId(uint32_t resourceId) override;
408
409   /**
410    * @brief Get the Graphics::Texture as resourceId.
411    *
412    * @param[in] resourceId The unique id of resouces.
413    * @return Pointer of Graphics::Texture, or nullptr if there is no valid objects.
414    */
415   Graphics::Texture* GetTextureFromResourceId(uint32_t resourceId) override;
416
417   /**
418    * @brief Get the ownership of Graphics::Texture as resourceId.
419    *
420    * @param[in] resourceId The unique id of resouces.
421    * @return Pointer of Graphics::Texture.
422    */
423   Graphics::UniquePtr<Graphics::Texture> ReleaseTextureFromResourceId(uint32_t resourceId) override;
424
425 public: // Test Functions
426   void SetAutoAttrCreation(bool v)
427   {
428     mAutoAttrCreation = v;
429   }
430   bool AutoAttrCreation()
431   {
432     return mAutoAttrCreation;
433   }
434
435   void SetVertexFormats(Property::Array& vfs)
436   {
437     mVertexFormats = vfs;
438   }
439
440   void AddCustomUniforms(std::vector<UniformData>& customUniforms)
441   {
442     mCustomUniforms = customUniforms;
443   }
444
445   void AddCustomUniformBlock(const TestGraphicsReflection::TestUniformBlockInfo& blockInfo)
446   {
447     mCustomUniformBlocks.push_back(blockInfo);
448   }
449
450   void ClearSubmitStack()
451   {
452     mSubmitStack.clear();
453   }
454
455   void ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer);
456
457   void BindPipeline(TestGraphicsPipeline* pipeline);
458
459 public:
460   mutable TraceCallStack                    mCallStack;
461   mutable TraceCallStack                    mCommandBufferCallStack;
462   mutable TraceCallStack                    mFrameBufferCallStack;
463   mutable std::vector<Graphics::SubmitInfo> mSubmitStack;
464
465   TestGlAbstraction              mGl;
466   TestGraphicsSyncImplementation mGraphicsSyncImpl;
467
468   bool            isDiscardQueueEmptyResult{true};
469   bool            isDrawOnResumeRequiredResult{true};
470   bool            mAutoAttrCreation{true};
471   Property::Array mVertexFormats;
472
473   struct ProgramCache
474   {
475     std::map<Graphics::PipelineStage, std::vector<uint8_t>> shaders;
476     TestGraphicsProgramImpl*                                programImpl;
477   };
478   std::vector<ProgramCache> mProgramCache;
479
480   std::vector<TestGraphicsBuffer*> mAllocatedBuffers;
481
482   std::unordered_map<uint32_t, Graphics::UniquePtr<Graphics::Texture>> mTextureUploadBindMapper;
483
484   struct PipelineCache
485   {
486   };
487
488   std::vector<UniformData>                                  mCustomUniforms;
489   std::vector<TestGraphicsReflection::TestUniformBlockInfo> mCustomUniformBlocks;
490 };
491
492 } // namespace Dali
493
494 #endif //TEST_GRAPHICS_CONTROLLER_H