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