Memory Pool Logging
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-command-buffer.cpp
index 7cc24f8..bc463ab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,9 +31,9 @@ namespace Dali::Graphics::GLES
 {
 class CommandPool
 {
-  static const uint32_t COMMAND_POOL_DEFAULT_INCREMENT = 1024 * 32; // 32kb banks
-  static const uint32_t MEMORY_POOL_DEFAULT_INCREMENT  = 1024;      // 4kb memory pool increment
-  static const uint32_t MEMORY_POOL_DEFAULT_ALIGNMENT  = 64;        // 64bytes alignment
+  static constexpr uint32_t COMMAND_POOL_DEFAULT_INCREMENT = 1024 * 32 / sizeof(Command); // 32kb banks
+  static const uint32_t     MEMORY_POOL_DEFAULT_INCREMENT  = 1024;                        // 1kb memory pool increment
+  static const uint32_t     MEMORY_POOL_DEFAULT_ALIGNMENT  = 64;                          // 64bytes alignment
 
   template<class T>
   struct Block
@@ -65,7 +65,7 @@ class CommandPool
     inline void resize(int newSize)
     {
       ptr      = reinterpret_cast<T*>(realloc(ptr, newSize * sizeof(T)));
-      capacity = newSize;
+      capacity = newSize * sizeof(T);
       dataSize = newSize;
     }
 
@@ -94,17 +94,25 @@ class CommandPool
 
     IndirectPtr<T> Allocate(uint32_t count)
     {
-      // set fixed capacity
-      if(fixedCapacity && data.size() != fixedCapacity)
+      // Set fixed capacity
+      if(fixedCapacity)
       {
-        data.resize(fixedCapacity);
-        totalCapacity = data.size();
+        // resize data size when capacity is not setuped.
+        // Note if totalCapacity is bigger than fixedCapacity,
+        // just skip here and resize dynamically
+        if(DALI_UNLIKELY(totalCapacity < fixedCapacity))
+        {
+          data.resize(fixedCapacity);
+          totalCapacity = data.size();
+        }
       }
 
-      // or resize dynamically
-      else if(data.size() <= offset + count)
+      // Resize dynamically
+      if(DALI_UNLIKELY(totalCapacity < offset + count))
       {
-        data.resize(data.size() + Increment);
+        // Resize the memory size as ceil((offset + count - totalCapacity)) / Increment) * Increment
+        // So the incremented size of data is always multiplied of the value Increment.
+        data.resize(data.size() + ((offset + count - totalCapacity - 1) / Increment + 1) * Increment);
 
         // update base pointer, required for address translation
         totalCapacity = data.size();
@@ -216,6 +224,11 @@ public:
     size = commandPool.size;
     return commandPool.data.ptr;
   }
+
+  std::size_t GetTotalCapacity() const
+  {
+    return commandPool.data.capacity + memoryPool.data.capacity;
+  }
 };
 
 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
@@ -226,12 +239,12 @@ CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo
 
 CommandBuffer::~CommandBuffer() = default;
 
-void CommandBuffer::BindVertexBuffers(uint32_t                             firstBinding,
-                                      std::vector<const Graphics::Buffer*> buffers,
-                                      std::vector<uint32_t>                offsets)
+void CommandBuffer::BindVertexBuffers(uint32_t                                    firstBinding,
+                                      const std::vector<const Graphics::Buffer*>& buffers,
+                                      const std::vector<uint32_t>&                offsets)
 {
   auto command                                         = mCommandPool->AllocateCommand(CommandType::BIND_VERTEX_BUFFERS);
-  command->bindVertexBuffers.vertexBufferBindingsCount = firstBinding + buffers.size();
+  command->bindVertexBuffers.vertexBufferBindingsCount = firstBinding + static_cast<uint32_t>(buffers.size());
   auto pBindings                                       = mCommandPool->Allocate<GLES::VertexBufferBindingDescriptor>(firstBinding + buffers.size());
 
   command->bindVertexBuffers.vertexBufferBindings = pBindings;
@@ -306,7 +319,7 @@ void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
   command->bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
 }
 
-void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
+void CommandBuffer::BindTextures(const std::vector<TextureBinding>& textureBindings)
 {
   auto  command                        = mCommandPool->AllocateCommand(CommandType::BIND_TEXTURES);
   auto& bindTexturesCmd                = command->bindTextures;
@@ -315,7 +328,7 @@ void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
   memcpy(bindTexturesCmd.textureBindings.Ptr(), textureBindings.data(), sizeof(TextureBinding) * textureBindings.size());
 }
 
-void CommandBuffer::BindSamplers(std::vector<SamplerBinding>& samplerBindings)
+void CommandBuffer::BindSamplers(const std::vector<SamplerBinding>& samplerBindings)
 {
   auto  command                        = mCommandPool->AllocateCommand(CommandType::BIND_SAMPLERS);
   auto& bindSamplersCmd                = command->bindSamplers;
@@ -341,10 +354,10 @@ void CommandBuffer::BindIndexBuffer(const Graphics::Buffer& buffer,
 }
 
 void CommandBuffer::BeginRenderPass(
-  Graphics::RenderPass*   renderPass,
-  Graphics::RenderTarget* renderTarget,
-  Rect2D                  renderArea,
-  std::vector<ClearValue> clearValues)
+  Graphics::RenderPass*          renderPass,
+  Graphics::RenderTarget*        renderTarget,
+  Rect2D                         renderArea,
+  const std::vector<ClearValue>& clearValues)
 {
   auto  command                    = mCommandPool->AllocateCommand(CommandType::BEGIN_RENDERPASS);
   auto& cmd                        = *command;
@@ -422,6 +435,13 @@ void CommandBuffer::DrawIndexedIndirect(
   cmd.drawIndexedIndirect.stride    = stride;
 }
 
+void CommandBuffer::DrawNative(const DrawNativeInfo* drawNativeInfo)
+{
+  auto  command = mCommandPool->AllocateCommand(CommandType::DRAW_NATIVE);
+  auto& cmd     = command->drawNative;
+  memcpy(&cmd.drawNativeInfo, drawNativeInfo, sizeof(DrawNativeInfo));
+}
+
 void CommandBuffer::Reset()
 {
   mCommandPool->Rollback(false);
@@ -543,4 +563,14 @@ void CommandBuffer::DiscardResource()
   GetController().DiscardResource(this);
 }
 
+std::size_t CommandBuffer::GetCapacity()
+{
+  std::size_t total{0u};
+  if(mCommandPool)
+  {
+    total = mCommandPool->GetTotalCapacity();
+  }
+  return total;
+}
+
 } // namespace Dali::Graphics::GLES