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