Updated test files to match dali-core
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-controller.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "test-graphics-controller.h"
18
19 #include "test-graphics-buffer.h"
20 #include "test-graphics-command-buffer.h"
21 #include "test-graphics-sampler.h"
22 #include "test-graphics-texture.h"
23
24 #include <dali/integration-api/gl-defines.h>
25 #include <iostream>
26 #include <sstream>
27
28 namespace Dali
29 {
30 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo)
31 {
32   return o << "usage:" << std::hex << bufferCreateInfo.usage << ", size:" << std::dec << bufferCreateInfo.size;
33 }
34
35 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo)
36 {
37   return o << "level:" << (commandBufferCreateInfo.level == Graphics::CommandBufferLevel::PRIMARY ? "PRIMARY" : "SECONDARY")
38            << ", fixedCapacity:" << std::dec << commandBufferCreateInfo.fixedCapacity;
39 }
40
41 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType)
42 {
43   switch(textureType)
44   {
45     case Graphics::TextureType::TEXTURE_2D:
46       o << "TEXTURE_2D";
47       break;
48     case Graphics::TextureType::TEXTURE_3D:
49       o << "TEXTURE_3D";
50       break;
51     case Graphics::TextureType::TEXTURE_CUBEMAP:
52       o << "TEXTURE_CUBEMAP";
53       break;
54   }
55   return o;
56 }
57
58 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent)
59 {
60   o << "width:" << extent.width << ", height:" << extent.height;
61   return o;
62 }
63
64 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo)
65 {
66   o << "textureType:" << createInfo.textureType
67     << " size:" << createInfo.size
68     << " format:" << static_cast<uint32_t>(createInfo.format)
69     << " mipMapFlag:" << createInfo.mipMapFlag
70     << " layout:" << (createInfo.layout == Graphics::TextureLayout::LINEAR ? "LINEAR" : "OPTIMAL")
71     << " usageFlags:" << std::hex << createInfo.usageFlags
72     << " data:" << std::hex << createInfo.data
73     << " dataSize:" << std::dec << createInfo.dataSize
74     << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr;
75   return o;
76 }
77
78 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode)
79 {
80   switch(addressMode)
81   {
82     case Graphics::SamplerAddressMode::REPEAT:
83       o << "REPEAT";
84       break;
85     case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
86       o << "MIRRORED_REPEAT";
87       break;
88     case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
89       o << "CLAMP_TO_EDGE";
90       break;
91     case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
92       o << "CLAMP_TO_BORDER";
93       break;
94     case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
95       o << "MIRROR_CLAMP_TO_EDGE";
96       break;
97   }
98   return o;
99 }
100
101 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode)
102 {
103   switch(filterMode)
104   {
105     case Graphics::SamplerFilter::LINEAR:
106       o << "LINEAR";
107       break;
108     case Graphics::SamplerFilter::NEAREST:
109       o << "NEAREST";
110       break;
111   }
112   return o;
113 }
114
115 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode)
116 {
117   switch(mipmapMode)
118   {
119     case Graphics::SamplerMipmapMode::NONE:
120       o << "NONE";
121       break;
122     case Graphics::SamplerMipmapMode::LINEAR:
123       o << "LINEAR";
124       break;
125     case Graphics::SamplerMipmapMode::NEAREST:
126       o << "NEAREST";
127       break;
128   }
129   return o;
130 }
131
132 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo)
133 {
134   o << "minFilter:" << createInfo.minFilter
135     << " magFilter:" << createInfo.magFilter
136     << " wrapModeU:" << createInfo.addressModeU
137     << " wrapModeV:" << createInfo.addressModeV
138     << " wrapModeW:" << createInfo.addressModeW
139     << " mipMapMode:" << createInfo.mipMapMode;
140   return o;
141 }
142
143 TestGraphicsController::TestGraphicsController()
144 {
145   mCallStack.Enable(true);
146   mCallStack.EnableLogging(true);
147   mCommandBufferCallStack.Enable(true);
148   mCommandBufferCallStack.EnableLogging(true);
149   auto& trace = mGlAbstraction.GetTextureTrace();
150   trace.Enable(true);
151   trace.EnableLogging(true);
152 }
153
154 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
155 {
156   std::ostringstream          out;
157   TraceCallStack::NamedParams namedParams;
158   out << "cmdBuffer[" << submitInfo.cmdBuffer.size() << "], flags:" << std::hex << submitInfo.flags;
159   namedParams["submitInfo"] = out.str();
160
161   mCallStack.PushCall("SubmitCommandBuffers", "", namedParams);
162
163   for(auto& commandBuffer : submitInfo.cmdBuffer)
164   {
165     for(auto& binding : (static_cast<TestGraphicsCommandBuffer*>(commandBuffer))->mTextureBindings)
166     {
167       if(binding.texture)
168       {
169         auto texture = const_cast<TestGraphicsTexture*>(static_cast<const TestGraphicsTexture*>(binding.texture));
170
171         texture->Bind(binding.binding);
172
173         if(binding.sampler)
174         {
175           auto sampler = const_cast<TestGraphicsSampler*>(static_cast<const TestGraphicsSampler*>(binding.sampler));
176           if(sampler)
177           {
178             sampler->Apply(texture->GetTarget());
179           }
180         }
181
182         texture->Prepare(); // Ensure native texture is ready
183       }
184     }
185   }
186 }
187
188 /**
189  * @brief Presents render target
190  * @param renderTarget render target to present
191  */
192 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
193 {
194   std::ostringstream          out;
195   TraceCallStack::NamedParams namedParams;
196   out << std::hex << renderTarget;
197   namedParams["renderTarget"] = out.str();
198   mCallStack.PushCall("PresentRenderTarget", "", namedParams);
199 }
200
201 /**
202  * @brief Waits until the GPU is idle
203  */
204 void TestGraphicsController::WaitIdle()
205 {
206   mCallStack.PushCall("WaitIdle", "");
207 }
208
209 /**
210  * @brief Lifecycle pause event
211  */
212 void TestGraphicsController::Pause()
213 {
214   mCallStack.PushCall("Pause", "");
215 }
216
217 /**
218  * @brief Lifecycle resume event
219  */
220 void TestGraphicsController::Resume()
221 {
222   mCallStack.PushCall("Resume", "");
223 }
224
225 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>&       updateInfoList,
226                                             const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
227 {
228   std::ostringstream          out;
229   TraceCallStack::NamedParams namedParams;
230   out << "[" << updateInfoList.size() << "]:";
231   namedParams["updateInfoList"] = out.str();
232   out.str("");
233   out << "[" << sourceList.size() << "]:";
234   namedParams["sourceList"] = out.str();
235
236   mCallStack.PushCall("UpdateTextures", "", namedParams);
237
238   // Call either TexImage2D or TexSubImage2D
239   for(unsigned int i = 0; i < updateInfoList.size(); ++i)
240   {
241     auto& updateInfo = updateInfoList[i];
242     auto& source     = sourceList[i];
243
244     auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
245     texture->Bind(0); // Use first texture unit during resource update
246     texture->Update(updateInfo, source);
247   }
248 }
249
250 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
251 {
252   TraceCallStack::NamedParams namedParams;
253   namedParams["enableDepth"]   = enableDepth ? "T" : "F";
254   namedParams["enableStencil"] = enableStencil ? "T" : "F";
255   mCallStack.PushCall("EnableDepthStencilBuffer", "", namedParams);
256   return false;
257 }
258
259 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
260 {
261   std::ostringstream out;
262   out << numberOfDiscardedRenderers;
263   TraceCallStack::NamedParams namedParams;
264   namedParams["numberOfDiscardedrenderers"] = out.str();
265   mCallStack.PushCall("RunGarbageCollector", "", namedParams);
266 }
267
268 void TestGraphicsController::DiscardUnusedResources()
269 {
270   mCallStack.PushCall("DiscardUnusedResources", "");
271 }
272
273 bool TestGraphicsController::IsDiscardQueueEmpty()
274 {
275   mCallStack.PushCall("IsDiscardQueueEmpty", "");
276   return isDiscardQueueEmptyResult;
277 }
278
279 /**
280  * @brief Test if the graphics subsystem has resumed & should force a draw
281  *
282  * @return true if the graphics subsystem requires a re-draw
283  */
284 bool TestGraphicsController::IsDrawOnResumeRequired()
285 {
286   mCallStack.PushCall("IsDrawOnResumeRequired", "");
287   return isDrawOnResumeRequiredResult;
288 }
289
290 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
291 {
292   std::ostringstream oss;
293   oss << "bufferCreateInfo:" << bufferCreateInfo;
294   mCallStack.PushCall("CreateBuffer", oss.str());
295
296   return Graphics::MakeUnique<TestGraphicsBuffer>();
297 }
298
299 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
300 {
301   std::ostringstream oss;
302   oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
303   mCallStack.PushCall("CreateCommandBuffer", oss.str());
304   return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGlAbstraction);
305 }
306
307 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
308 {
309   mCallStack.PushCall("CreateRenderPass", "");
310   return nullptr;
311 }
312
313 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
314 {
315   std::ostringstream params, oss;
316   params << "textureCreateInfo:" << textureCreateInfo;
317   TraceCallStack::NamedParams namedParams;
318   oss << textureCreateInfo;
319   namedParams["textureCreateInfo"] = oss.str();
320   mCallStack.PushCall("CreateTexture", params.str(), namedParams);
321
322   return Graphics::MakeUnique<TestGraphicsTexture>(mGlAbstraction, textureCreateInfo);
323 }
324
325 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(const Graphics::FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
326 {
327   mCallStack.PushCall("CreateFramebuffer", "");
328   return nullptr;
329 }
330
331 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
332 {
333   mCallStack.PushCall("CreatePipeline", "");
334   return nullptr;
335 }
336
337 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
338 {
339   mCallStack.PushCall("CreateShader", "");
340   return nullptr;
341 }
342
343 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
344 {
345   std::ostringstream params, oss;
346   params << "samplerCreateInfo:" << samplerCreateInfo;
347   TraceCallStack::NamedParams namedParams;
348   oss << samplerCreateInfo;
349   namedParams["samplerCreateInfo"] = oss.str();
350   mCallStack.PushCall("CreateSampler", params.str(), namedParams);
351
352   return Graphics::MakeUnique<TestGraphicsSampler>(mGlAbstraction, samplerCreateInfo);
353 }
354
355 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
356 {
357   mCallStack.PushCall("CreateRenderTarget", "");
358   return nullptr;
359 }
360
361 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
362 {
363   mCallStack.PushCall("MapBufferRange", "");
364   return nullptr;
365 }
366
367 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
368 {
369   mCallStack.PushCall("MapTextureRange", "");
370   return nullptr;
371 }
372
373 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
374 {
375   mCallStack.PushCall("UnmapMemory", "");
376 }
377
378 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
379 {
380   mCallStack.PushCall("GetTextureMemoryRequirements", "");
381   return Graphics::MemoryRequirements{};
382 }
383
384 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
385 {
386   mCallStack.PushCall("GetBufferMemoryRequirements", "");
387   return Graphics::MemoryRequirements{};
388 }
389
390 const Graphics::TextureProperties& TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
391 {
392   static Graphics::TextureProperties textureProperties{};
393   mCallStack.PushCall("GetTextureProperties", "");
394
395   return textureProperties;
396 }
397
398 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
399 {
400   mCallStack.PushCall("PipelineEquals", "");
401   return false;
402 }
403
404 } // namespace Dali