1c62119ae979ae49ce07f51b39cc1e75296dcde8
[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) 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 #include <dali/graphics-api/graphics-controller.h>
21 #include "test-gl-abstraction.h"
22 #include "test-gl-context-helper-abstraction.h"
23 #include "test-gl-sync-abstraction.h"
24 #include "test-graphics-command-buffer.h"
25 #include "test-graphics-program.h"
26 #include "test-graphics-reflection.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 class TestGraphicsController : public Dali::Graphics::Controller
89 {
90 public:
91   TestGraphicsController();
92
93   virtual ~TestGraphicsController() = default;
94
95   void Initialize()
96   {
97     mGl.Initialize();
98   }
99
100   Integration::GlAbstraction& GetGlAbstraction() override
101   {
102     return mGl;
103   }
104
105   Integration::GlSyncAbstraction& GetGlSyncAbstraction() override
106   {
107     return mGlSyncAbstraction;
108   }
109
110   Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override
111   {
112     return mGlContextHelperAbstraction;
113   }
114
115   void SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo) override;
116
117   /**
118    * @brief Presents render target
119    * @param renderTarget render target to present
120    */
121   void PresentRenderTarget(Graphics::RenderTarget* renderTarget) override;
122
123   /**
124    * @brief Waits until the GPU is idle
125    */
126   void WaitIdle() override;
127
128   /**
129    * @brief Lifecycle pause event
130    */
131   void Pause() override;
132
133   /**
134    * @brief Lifecycle resume event
135    */
136   void Resume() override;
137
138   /**
139    * @brief Lifecycle shutdown event
140    */
141   void Shutdown() override;
142
143   /**
144    * @brief Lifecycle destroy event
145    */
146   void Destroy() override;
147
148   /**
149    * @brief Executes batch update of textures
150    *
151    * This function may perform full or partial update of many textures.
152    * The data source may come from:
153    * - CPU memory (client side)
154    * - GPU memory (another Texture or Buffer)
155    *
156    * UpdateTextures() is the only way to update unmappable Texture objects.
157    * It is recommended to batch updates as it may help with optimizing
158    * memory transfers based on dependencies.
159    *
160    */
161   void UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>&       updateInfoList,
162                       const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList) override;
163
164   /**
165    * Auto generates mipmaps for the texture
166    * @param[in] texture The texture
167    */
168   void GenerateTextureMipmaps(const Graphics::Texture& texture) override;
169
170   /**
171    * TBD: do we need those functions in the new implementation?
172    */
173   bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override;
174
175   void RunGarbageCollector(size_t numberOfDiscardedRenderers) override;
176
177   void DiscardUnusedResources() override;
178
179   bool IsDiscardQueueEmpty() override;
180
181   /**
182    * @brief Test if the graphics subsystem has resumed & should force a draw
183    *
184    * @return true if the graphics subsystem requires a re-draw
185    */
186   bool IsDrawOnResumeRequired() override;
187
188   /**
189    * @brief Creates new Buffer object
190    *
191    * The Buffer object is created with underlying memory. The Buffer
192    * specification is immutable. Based on the BufferCreateInfo::usage,
193    * the memory may be client-side mappable or not.
194    *
195    * The old buffer may be passed as BufferCreateInfo::oldbuffer, however,
196    * it's up to the implementation whether the object will be reused or
197    * discarded and replaced by the new one.
198    *
199    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
200    * @return pointer to the Buffer object
201    */
202   Graphics::UniquePtr<Graphics::Buffer> CreateBuffer(const Graphics::BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer) override;
203
204   /**
205    * @brief Creates new CommandBuffer object
206    *
207    * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
208    * @return pointer to the CommandBuffer object
209    */
210   Graphics::UniquePtr<Graphics::CommandBuffer> CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer) override;
211
212   /**
213    * @brief Creates new RenderPass object
214    *
215    * @param[in] renderPassCreateInfo The valid RenderPassCreateInfo structure
216    * @return pointer to the RenderPass object
217    */
218   Graphics::UniquePtr<Graphics::RenderPass> CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass) override;
219
220   /**
221    * @brief Creates new Texture object
222    *
223    * @param[in] textureCreateInfo The valid TextureCreateInfo structure
224    * @return pointer to the TextureCreateInfo object
225    */
226   Graphics::UniquePtr<Graphics::Texture> CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture) override;
227
228   /**
229    * @brief Creates new Framebuffer object
230    *
231    * @param[in] framebufferCreateInfo The valid FramebufferCreateInfo structure
232    * @return pointer to the Framebuffer object
233    */
234   Graphics::UniquePtr<Graphics::Framebuffer> CreateFramebuffer(const Graphics::FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer) override;
235
236   /**
237    * @brief Creates new Pipeline object
238    *
239    * @param[in] pipelineCreateInfo The valid PipelineCreateInfo structure
240    * @return pointer to the Pipeline object
241    */
242   Graphics::UniquePtr<Graphics::Pipeline> CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline) override;
243
244   /**
245    * @brief Creates new Program object
246    *
247    * @param[in] programCreateInfo The valid ProgramCreateInfo structure
248    * @return pointer to the Program object
249    */
250   Graphics::UniquePtr<Graphics::Program> CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram) override;
251
252   /**
253    * @brief Creates new Shader object
254    *
255    * @param[in] shaderCreateInfo The valid ShaderCreateInfo structure
256    * @return pointer to the Shader object
257    */
258   Graphics::UniquePtr<Graphics::Shader> CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader) override;
259
260   /**
261    * @brief Creates new Sampler object
262    *
263    * @param[in] samplerCreateInfo The valid SamplerCreateInfo structure
264    * @return pointer to the Sampler object
265    */
266   Graphics::UniquePtr<Graphics::Sampler> CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler) override;
267
268   /**
269    * @brief Creates new RenderTarget object
270    *
271    * @param[in] renderTargetCreateInfo The valid RenderTargetCreateInfo structure
272    * @return pointer to the RenderTarget object
273    */
274   Graphics::UniquePtr<Graphics::RenderTarget> CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget) override;
275
276   /**
277    * @brief Maps memory associated with Buffer object
278    *
279    * @param[in] mapInfo Filled details of mapped resource
280    *
281    * @return Returns pointer to Memory object or Graphicsnullptr on error
282    */
283   Graphics::UniquePtr<Graphics::Memory> MapBufferRange(const Graphics::MapBufferInfo& mapInfo) override;
284
285   /**
286    * @brief Maps memory associated with the texture.
287    *
288    * Only Texture objects that are backed with linear memory (staging memory) can be mapped.
289    * Example:
290    * 1) GLES implementation may create PBO object as staging memory and couple it
291    * with the texture. Texture can be mapped and the memory can be read/write on demand.
292    *
293    * 2) Vulkan implementation may allocate DeviceMemory and use linear layout.
294    *
295    * @param[in] mapInfo Filled details of mapped resource
296    *
297    * @return Valid Memory object or nullptr on error
298    */
299   Graphics::UniquePtr<Graphics::Memory> MapTextureRange(const Graphics::MapTextureInfo& mapInfo) override;
300
301   /**
302    * @brief Unmaps memory and discards Memory object
303    *
304    * This function automatically removes lock if Memory has been
305    * previously locked.
306    *
307    * @param[in] memory Valid and previously mapped Memory object
308    */
309   void UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory) override;
310
311   /**
312    * @brief Returns memory requirements of the Texture object.
313    *
314    * Call this function whenever it's necessary to know how much memory
315    * is needed to store all the texture data and what memory alignment
316    * the data should follow.
317    *
318    * @return Returns memory requirements of Texture
319    */
320   Graphics::MemoryRequirements GetTextureMemoryRequirements(Graphics::Texture& texture) const override;
321
322   /**
323    * @brief Returns memory requirements of the Buffer object.
324    *
325    * Call this function whenever it's necessary to know how much memory
326    * is needed to store all the buffer data and what memory alignment
327    * the data should follow.
328    *
329    * @return Returns memory requirements of Buffer
330    */
331   Graphics::MemoryRequirements GetBufferMemoryRequirements(Graphics::Buffer& buffer) const override;
332
333   /**
334    * @brief Returns specification of the Texture object
335    *
336    * Function obtains specification of the Texture object. It may retrieve
337    * implementation dependent details like ie. whether the texture is
338    * emulated (for example, RGB emulated on RGBA), compressed etc.
339    *
340    * @return Returns the TextureProperties object
341    */
342   const Graphics::TextureProperties& GetTextureProperties(const Graphics::Texture& texture) override;
343
344   /**
345    * @brief Returns the reflection of the given program
346    *
347    * @param[in] program The program
348    * @return The reflection of the program
349    */
350   const Graphics::Reflection& GetProgramReflection(const Graphics::Program& program) override;
351
352   /**
353    * @brief Tests whether two Pipelines are the same.
354    *
355    * On the higher level, this function may help wit creating pipeline cache.
356    *
357    * @return true if pipeline objects match
358    */
359   bool PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const override;
360
361 public: // Test Functions
362   void SetVertexFormats(Property::Array& vfs)
363   {
364     mVertexFormats = vfs;
365   }
366
367   void AddCustomUniforms(std::vector<UniformData>& customUniforms)
368   {
369     mCustomUniforms = customUniforms;
370   }
371
372   void ClearSubmitStack()
373   {
374     mSubmitStack.clear();
375   }
376
377   /**
378    * @brief Retrieves program parameters
379    *
380    * This function can be used to retrieve data from internal implementation
381    *
382    * @param[in] program Valid program object
383    * @param[in] parameterId Integer parameter id
384    * @param[out] outData Pointer to output memory
385    * @return True on success
386    */
387   bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
388
389   void ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer);
390
391   void BindPipeline(TestGraphicsPipeline* pipeline);
392
393 public:
394   mutable TraceCallStack                    mCallStack;
395   mutable TraceCallStack                    mCommandBufferCallStack;
396   mutable TraceCallStack                    mFrameBufferCallStack;
397   mutable std::vector<Graphics::SubmitInfo> mSubmitStack;
398
399   TestGlAbstraction              mGl;
400   TestGlSyncAbstraction          mGlSyncAbstraction;
401   TestGlContextHelperAbstraction mGlContextHelperAbstraction;
402
403   bool isDiscardQueueEmptyResult{true};
404   bool isDrawOnResumeRequiredResult{true};
405
406   Property::Array mVertexFormats;
407
408   struct ProgramCache
409   {
410     std::map<Graphics::PipelineStage, std::vector<uint8_t>> shaders;
411     TestGraphicsProgramImpl*                                programImpl;
412   };
413   std::vector<ProgramCache> mProgramCache;
414
415   struct PipelineCache
416   {
417   };
418
419   std::vector<UniformData> mCustomUniforms;
420 };
421
422 } // namespace Dali
423
424 #endif //TEST_GRAPHICS_CONTROLLER_H