updated test files to match dali-core : buffer impl
[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 "dali-test-suite-utils.h"
20 #include "test-graphics-buffer.h"
21 #include "test-graphics-command-buffer.h"
22 #include "test-graphics-sampler.h"
23 #include "test-graphics-texture.h"
24
25 #include <dali/integration-api/gl-defines.h>
26 #include <iostream>
27 #include <sstream>
28
29 namespace Dali
30 {
31 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo)
32 {
33   return o << "usage:" << std::hex << bufferCreateInfo.usage << ", size:" << std::dec << bufferCreateInfo.size;
34 }
35
36 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo)
37 {
38   return o << "level:" << (commandBufferCreateInfo.level == Graphics::CommandBufferLevel::PRIMARY ? "PRIMARY" : "SECONDARY")
39            << ", fixedCapacity:" << std::dec << commandBufferCreateInfo.fixedCapacity;
40 }
41
42 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType)
43 {
44   switch(textureType)
45   {
46     case Graphics::TextureType::TEXTURE_2D:
47       o << "TEXTURE_2D";
48       break;
49     case Graphics::TextureType::TEXTURE_3D:
50       o << "TEXTURE_3D";
51       break;
52     case Graphics::TextureType::TEXTURE_CUBEMAP:
53       o << "TEXTURE_CUBEMAP";
54       break;
55   }
56   return o;
57 }
58
59 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent)
60 {
61   o << "width:" << extent.width << ", height:" << extent.height;
62   return o;
63 }
64
65 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo)
66 {
67   o << "textureType:" << createInfo.textureType
68     << " size:" << createInfo.size
69     << " format:" << static_cast<uint32_t>(createInfo.format)
70     << " mipMapFlag:" << createInfo.mipMapFlag
71     << " layout:" << (createInfo.layout == Graphics::TextureLayout::LINEAR ? "LINEAR" : "OPTIMAL")
72     << " usageFlags:" << std::hex << createInfo.usageFlags
73     << " data:" << std::hex << createInfo.data
74     << " dataSize:" << std::dec << createInfo.dataSize
75     << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr;
76   return o;
77 }
78
79 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode)
80 {
81   switch(addressMode)
82   {
83     case Graphics::SamplerAddressMode::REPEAT:
84       o << "REPEAT";
85       break;
86     case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
87       o << "MIRRORED_REPEAT";
88       break;
89     case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
90       o << "CLAMP_TO_EDGE";
91       break;
92     case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
93       o << "CLAMP_TO_BORDER";
94       break;
95     case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
96       o << "MIRROR_CLAMP_TO_EDGE";
97       break;
98   }
99   return o;
100 }
101
102 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode)
103 {
104   switch(filterMode)
105   {
106     case Graphics::SamplerFilter::LINEAR:
107       o << "LINEAR";
108       break;
109     case Graphics::SamplerFilter::NEAREST:
110       o << "NEAREST";
111       break;
112   }
113   return o;
114 }
115
116 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode)
117 {
118   switch(mipmapMode)
119   {
120     case Graphics::SamplerMipmapMode::NONE:
121       o << "NONE";
122       break;
123     case Graphics::SamplerMipmapMode::LINEAR:
124       o << "LINEAR";
125       break;
126     case Graphics::SamplerMipmapMode::NEAREST:
127       o << "NEAREST";
128       break;
129   }
130   return o;
131 }
132
133 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo)
134 {
135   o << "minFilter:" << createInfo.minFilter
136     << " magFilter:" << createInfo.magFilter
137     << " wrapModeU:" << createInfo.addressModeU
138     << " wrapModeV:" << createInfo.addressModeV
139     << " wrapModeW:" << createInfo.addressModeW
140     << " mipMapMode:" << createInfo.mipMapMode;
141   return o;
142 }
143
144 class TestGraphicsMemory : public Graphics::Memory
145 {
146 public:
147   TestGraphicsMemory(TraceCallStack& callStack, TestGraphicsBuffer& buffer, uint32_t mappedOffset, uint32_t mappedSize)
148   : mCallStack(callStack),
149     mBuffer(buffer),
150     mMappedOffset(mappedOffset),
151     mMappedSize(mappedSize)
152   {
153   }
154
155   void* LockRegion(uint32_t offset, uint32_t size) override
156   {
157     std::ostringstream o;
158     o << offset << ", " << size;
159     mCallStack.PushCall("Memory::LockRegion", o.str());
160
161     if(offset > mMappedOffset + mMappedSize ||
162        size + offset > mMappedOffset + mMappedSize)
163     {
164       tet_infoline("TestGraphics.Memory::LockRegion() Out of bounds");
165       mBuffer.memory.resize(mMappedOffset + offset + size); // Grow to prevent memcpy from crashing
166     }
167     mLockedOffset = offset;
168     mLockedSize   = size;
169     return &mBuffer.memory[mMappedOffset + offset];
170   }
171
172   void Unlock(bool flush) override
173   {
174     mCallStack.PushCall("Memory::Unlock", (flush ? "Flush" : "NoFlush"));
175     if(flush)
176     {
177       Flush();
178     }
179   }
180
181   void Flush() override
182   {
183     mCallStack.PushCall("Memory::Flush", "");
184     mBuffer.Bind();
185     mBuffer.Upload(mMappedOffset + mLockedOffset, mLockedSize);
186     mBuffer.Unbind();
187   }
188
189   TraceCallStack&     mCallStack;
190   TestGraphicsBuffer& mBuffer;
191   uint32_t            mMappedOffset;
192   uint32_t            mMappedSize;
193   uint32_t            mLockedOffset;
194   uint32_t            mLockedSize;
195 };
196
197 TestGraphicsController::TestGraphicsController()
198 {
199   mCallStack.Enable(true);
200   mCallStack.EnableLogging(true);
201   mCommandBufferCallStack.Enable(true);
202   mCommandBufferCallStack.EnableLogging(true);
203   auto& trace = mGlAbstraction.GetTextureTrace();
204   trace.Enable(true);
205   trace.EnableLogging(true);
206 }
207
208 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
209 {
210   std::ostringstream          out;
211   TraceCallStack::NamedParams namedParams;
212   out << "cmdBuffer[" << submitInfo.cmdBuffer.size() << "], flags:" << std::hex << submitInfo.flags;
213   namedParams["submitInfo"] = out.str();
214
215   mCallStack.PushCall("Controller::SubmitCommandBuffers", "", namedParams);
216
217   for(auto& commandBuffer : submitInfo.cmdBuffer)
218   {
219     for(auto& binding : (static_cast<TestGraphicsCommandBuffer*>(commandBuffer))->mTextureBindings)
220     {
221       if(binding.texture)
222       {
223         auto texture = const_cast<TestGraphicsTexture*>(static_cast<const TestGraphicsTexture*>(binding.texture));
224
225         texture->Bind(binding.binding);
226
227         if(binding.sampler)
228         {
229           auto sampler = const_cast<TestGraphicsSampler*>(static_cast<const TestGraphicsSampler*>(binding.sampler));
230           if(sampler)
231           {
232             sampler->Apply(texture->GetTarget());
233           }
234         }
235
236         texture->Prepare(); // Ensure native texture is ready
237       }
238     }
239   }
240 }
241
242 /**
243  * @brief Presents render target
244  * @param renderTarget render target to present
245  */
246 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
247 {
248   std::ostringstream          out;
249   TraceCallStack::NamedParams namedParams;
250   out << std::hex << renderTarget;
251   namedParams["renderTarget"] = out.str();
252   mCallStack.PushCall("Controller::PresentRenderTarget", "", namedParams);
253 }
254
255 /**
256  * @brief Waits until the GPU is idle
257  */
258 void TestGraphicsController::WaitIdle()
259 {
260   mCallStack.PushCall("Controller::WaitIdle", "");
261 }
262
263 /**
264  * @brief Lifecycle pause event
265  */
266 void TestGraphicsController::Pause()
267 {
268   mCallStack.PushCall("Controller::Pause", "");
269 }
270
271 /**
272  * @brief Lifecycle resume event
273  */
274 void TestGraphicsController::Resume()
275 {
276   mCallStack.PushCall("Controller::Resume", "");
277 }
278
279 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>&       updateInfoList,
280                                             const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
281 {
282   std::ostringstream          out;
283   TraceCallStack::NamedParams namedParams;
284   out << "[" << updateInfoList.size() << "]:";
285   namedParams["updateInfoList"] = out.str();
286   out.str("");
287   out << "[" << sourceList.size() << "]:";
288   namedParams["sourceList"] = out.str();
289
290   mCallStack.PushCall("Controller::UpdateTextures", "", namedParams);
291
292   // Call either TexImage2D or TexSubImage2D
293   for(unsigned int i = 0; i < updateInfoList.size(); ++i)
294   {
295     auto& updateInfo = updateInfoList[i];
296     auto& source     = sourceList[i];
297
298     auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
299     texture->Bind(0); // Use first texture unit during resource update
300     texture->Update(updateInfo, source);
301   }
302 }
303
304 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
305 {
306   TraceCallStack::NamedParams namedParams;
307   namedParams["enableDepth"]   = enableDepth ? "T" : "F";
308   namedParams["enableStencil"] = enableStencil ? "T" : "F";
309   mCallStack.PushCall("Controller::EnableDepthStencilBuffer", "", namedParams);
310   return false;
311 }
312
313 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
314 {
315   std::ostringstream out;
316   out << numberOfDiscardedRenderers;
317   TraceCallStack::NamedParams namedParams;
318   namedParams["numberOfDiscardedrenderers"] = out.str();
319   mCallStack.PushCall("Controller::RunGarbageCollector", "", namedParams);
320 }
321
322 void TestGraphicsController::DiscardUnusedResources()
323 {
324   mCallStack.PushCall("Controller::DiscardUnusedResources", "");
325 }
326
327 bool TestGraphicsController::IsDiscardQueueEmpty()
328 {
329   mCallStack.PushCall("Controller::IsDiscardQueueEmpty", "");
330   return isDiscardQueueEmptyResult;
331 }
332
333 /**
334  * @brief Test if the graphics subsystem has resumed & should force a draw
335  *
336  * @return true if the graphics subsystem requires a re-draw
337  */
338 bool TestGraphicsController::IsDrawOnResumeRequired()
339 {
340   mCallStack.PushCall("Controller::IsDrawOnResumeRequired", "");
341   return isDrawOnResumeRequiredResult;
342 }
343
344 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& createInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
345 {
346   std::ostringstream oss;
347   oss << "bufferCreateInfo:" << createInfo;
348   mCallStack.PushCall("Controller::CreateBuffer", oss.str());
349   return Graphics::MakeUnique<TestGraphicsBuffer>(mCallStack, mGlAbstraction, createInfo.size, createInfo.usage);
350 }
351
352 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
353 {
354   std::ostringstream oss;
355   oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
356   mCallStack.PushCall("Controller::CreateCommandBuffer", oss.str());
357   return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGlAbstraction);
358 }
359
360 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
361 {
362   mCallStack.PushCall("Controller::CreateRenderPass", "");
363   return nullptr;
364 }
365
366 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
367 {
368   std::ostringstream params, oss;
369   params << "textureCreateInfo:" << textureCreateInfo;
370   TraceCallStack::NamedParams namedParams;
371   oss << textureCreateInfo;
372   namedParams["textureCreateInfo"] = oss.str();
373   mCallStack.PushCall("Controller::CreateTexture", params.str(), namedParams);
374
375   return Graphics::MakeUnique<TestGraphicsTexture>(mGlAbstraction, textureCreateInfo);
376 }
377
378 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(const Graphics::FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
379 {
380   mCallStack.PushCall("Controller::CreateFramebuffer", "");
381   return nullptr;
382 }
383
384 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
385 {
386   mCallStack.PushCall("Controller::CreatePipeline", "");
387   return nullptr;
388 }
389
390 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
391 {
392   mCallStack.PushCall("Controller::CreateShader", "");
393   return nullptr;
394 }
395
396 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
397 {
398   std::ostringstream params, oss;
399   params << "samplerCreateInfo:" << samplerCreateInfo;
400   TraceCallStack::NamedParams namedParams;
401   oss << samplerCreateInfo;
402   namedParams["samplerCreateInfo"] = oss.str();
403   mCallStack.PushCall("Controller::CreateSampler", params.str(), namedParams);
404
405   return Graphics::MakeUnique<TestGraphicsSampler>(mGlAbstraction, samplerCreateInfo);
406 }
407
408 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
409 {
410   mCallStack.PushCall("Controller::CreateRenderTarget", "");
411   return nullptr;
412 }
413
414 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
415 {
416   mCallStack.PushCall("Controller::MapBufferRange", "");
417
418   auto buffer = static_cast<TestGraphicsBuffer*>(mapInfo.buffer);
419   buffer->memory.resize(mapInfo.offset + mapInfo.size); // For initial testing, allow writes past capacity
420
421   return std::make_unique<TestGraphicsMemory>(mCallStack, *buffer, mapInfo.offset, mapInfo.size);
422 }
423
424 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
425 {
426   mCallStack.PushCall("Controller::MapTextureRange", "");
427   return nullptr;
428 }
429
430 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
431 {
432   mCallStack.PushCall("Controller::UnmapMemory", "");
433 }
434
435 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
436 {
437   mCallStack.PushCall("Controller::GetTextureMemoryRequirements", "");
438   return Graphics::MemoryRequirements{};
439 }
440
441 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
442 {
443   mCallStack.PushCall("Controller::GetBufferMemoryRequirements", "");
444   return Graphics::MemoryRequirements{};
445 }
446
447 const Graphics::TextureProperties& TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
448 {
449   static Graphics::TextureProperties textureProperties{};
450   mCallStack.PushCall("Controller::GetTextureProperties", "");
451
452   return textureProperties;
453 }
454
455 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
456 {
457   mCallStack.PushCall("Controller::PipelineEquals", "");
458   return false;
459 }
460
461 } // namespace Dali