Memory Pool Logging
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-command-buffer.h
index cad69bd..c2ae566 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
 
 /*
- * 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.
 // INTERNAL INCLUDES
 #include "gles-graphics-resource.h"
 #include "gles-graphics-types.h"
+#include "gles-sync-object.h"
+
+#include <cstring>
 
 namespace Dali::Graphics::GLES
 {
-class Texture;
 class Pipeline;
-
+class RenderPass;
+class Framebuffer;
+class CommandBuffer;
+class CommandPool;
 enum class CommandType
 {
   FLUSH,
@@ -46,7 +51,22 @@ enum class CommandType
   DRAW_INDEXED_INDIRECT,
   SET_SCISSOR,
   SET_SCISSOR_TEST,
-  SET_VIEWPORT
+  SET_VIEWPORT,
+  BEGIN_RENDERPASS,
+  END_RENDERPASS,
+  EXECUTE_COMMAND_BUFFERS,
+  PRESENT_RENDER_TARGET,
+  SET_COLOR_MASK,
+  CLEAR_STENCIL_BUFFER,
+  CLEAR_DEPTH_BUFFER,
+  SET_STENCIL_TEST_ENABLE,
+  SET_STENCIL_WRITE_MASK,
+  SET_STENCIL_OP,
+  SET_STENCIL_FUNC,
+  SET_DEPTH_COMPARE_OP,
+  SET_DEPTH_TEST_ENABLE,
+  SET_DEPTH_WRITE_ENABLE,
+  DRAW_NATIVE,
 };
 
 /**
@@ -63,212 +83,25 @@ static void InvokeDestructor(T& object)
  */
 struct Command
 {
-  Command() = delete;
-
-  Command(CommandType commandType)
+  Command()
   {
-    type = commandType;
-    switch(type)
-    {
-      case CommandType::BIND_VERTEX_BUFFERS:
-      {
-        new(&bindVertexBuffers) decltype(bindVertexBuffers);
-        break;
-      }
-      case CommandType::BIND_TEXTURES:
-      {
-        new(&bindTextures) decltype(bindTextures);
-        break;
-      }
-      default:
-      {
-      }
-    }
   }
 
-  ~Command()
+  explicit Command(CommandType commandType)
   {
-    switch(type)
-    {
-      case CommandType::BIND_VERTEX_BUFFERS:
-      {
-        InvokeDestructor(bindVertexBuffers);
-        break;
-      }
-      case CommandType::BIND_TEXTURES:
-      {
-        InvokeDestructor(bindTextures);
-        break;
-      }
-      default:
-      {
-      }
-    }
+    type = commandType;
   }
 
-  /**
-   * @brief Copy constructor
-   * @param[in] rhs Command
-   */
-  Command(const Command& rhs)
-  {
-    switch(rhs.type)
-    {
-      case CommandType::BIND_VERTEX_BUFFERS:
-      {
-        new(&bindVertexBuffers) decltype(bindVertexBuffers);
-        bindVertexBuffers = rhs.bindVertexBuffers;
-        break;
-      }
-      case CommandType::BIND_INDEX_BUFFER:
-      {
-        bindIndexBuffer = rhs.bindIndexBuffer;
-        break;
-      }
-      case CommandType::BIND_SAMPLERS:
-      {
-        bindSamplers = rhs.bindSamplers;
-        break;
-      }
-      case CommandType::BIND_TEXTURES:
-      {
-        new(&bindTextures) decltype(bindTextures);
-        bindTextures = rhs.bindTextures;
-        break;
-      }
-      case CommandType::BIND_PIPELINE:
-      {
-        bindPipeline = rhs.bindPipeline;
-        break;
-      }
-      case CommandType::BIND_UNIFORM_BUFFER:
-      {
-        bindUniformBuffers = rhs.bindUniformBuffers;
-        break;
-      }
-      case CommandType::DRAW:
-      {
-        draw.type = rhs.draw.type;
-        draw.draw = rhs.draw.draw;
-        break;
-      }
-      case CommandType::DRAW_INDEXED:
-      {
-        draw.type        = rhs.draw.type;
-        draw.drawIndexed = rhs.draw.drawIndexed;
-        break;
-      }
-      case CommandType::DRAW_INDEXED_INDIRECT:
-      {
-        draw.type                = rhs.draw.type;
-        draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
-        break;
-      }
-      case CommandType::FLUSH:
-      {
-        // Nothing to do
-        break;
-      }
-      case CommandType::SET_SCISSOR:
-      {
-        scissor.region = rhs.scissor.region;
-        break;
-      }
-      case CommandType::SET_SCISSOR_TEST:
-      {
-        scissorTest.enable = rhs.scissorTest.enable;
-        break;
-      }
-      case CommandType::SET_VIEWPORT:
-      {
-        viewport.region = rhs.viewport.region;
-        break;
-      }
-    }
-    type = rhs.type;
-  }
+  ~Command() = default;
 
   /**
    * @brief Copy constructor
    * @param[in] rhs Command
    */
-  Command(Command&& rhs) noexcept
-  {
-    switch(rhs.type)
-    {
-      case CommandType::BIND_VERTEX_BUFFERS:
-      {
-        new(&bindVertexBuffers) decltype(bindVertexBuffers);
-        bindVertexBuffers = std::move(rhs.bindVertexBuffers);
-        break;
-      }
-      case CommandType::BIND_INDEX_BUFFER:
-      {
-        bindIndexBuffer = rhs.bindIndexBuffer;
-        break;
-      }
-      case CommandType::BIND_UNIFORM_BUFFER:
-      {
-        bindUniformBuffers = std::move(rhs.bindUniformBuffers);
-        break;
-      }
-      case CommandType::BIND_SAMPLERS:
-      {
-        bindSamplers = std::move(rhs.bindSamplers);
-        break;
-      }
-      case CommandType::BIND_TEXTURES:
-      {
-        new(&bindTextures) decltype(bindTextures);
-        bindTextures = std::move(rhs.bindTextures);
-        break;
-      }
-      case CommandType::BIND_PIPELINE:
-      {
-        bindPipeline = rhs.bindPipeline;
-        break;
-      }
-      case CommandType::DRAW:
-      {
-        draw.type = rhs.draw.type;
-        draw.draw = rhs.draw.draw;
-        break;
-      }
-      case CommandType::DRAW_INDEXED:
-      {
-        draw.type        = rhs.draw.type;
-        draw.drawIndexed = rhs.draw.drawIndexed;
-        break;
-      }
-      case CommandType::DRAW_INDEXED_INDIRECT:
-      {
-        draw.type                = rhs.draw.type;
-        draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
-        break;
-      }
-      case CommandType::FLUSH:
-      {
-        // Nothing to do
-        break;
-      }
-      case CommandType::SET_SCISSOR:
-      {
-        scissor.region = rhs.scissor.region;
-        break;
-      }
-      case CommandType::SET_SCISSOR_TEST:
-      {
-        scissorTest.enable = rhs.scissorTest.enable;
-        break;
-      }
-      case CommandType::SET_VIEWPORT:
-      {
-        viewport.region = rhs.viewport.region;
-        break;
-      }
-    }
-    type = rhs.type;
-  }
+  Command(const Command& rhs) = default;
+  Command& operator=(const Command& rhs) = default;
+  Command(Command&& rhs) noexcept        = delete;
+  Command& operator=(Command&& rhs) = delete;
 
   CommandType type{CommandType::FLUSH}; ///< Type of command
 
@@ -276,19 +109,22 @@ struct Command
   {
     struct
     {
-      std::vector<Graphics::TextureBinding> textureBindings;
+      IndirectPtr<Graphics::TextureBinding> textureBindings;
+      uint32_t                              textureBindingsCount;
     } bindTextures{};
 
     // BindSampler command
     struct
     {
-      std::vector<Graphics::SamplerBinding> samplerBindings;
+      IndirectPtr<Graphics::SamplerBinding> samplerBindings;
+      uint32_t                              samplerBindingsCount;
     } bindSamplers;
 
     struct
     {
       using Binding = GLES::VertexBufferBindingDescriptor;
-      std::vector<Binding> vertexBufferBindings;
+      IndirectPtr<Binding> vertexBufferBindings;
+      uint32_t             vertexBufferBindingsCount;
     } bindVertexBuffers;
 
     struct : public IndexBufferBindingDescriptor
@@ -297,7 +133,8 @@ struct Command
 
     struct
     {
-      std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
+      IndirectPtr<UniformBufferBindingDescriptor> uniformBufferBindings;
+      uint32_t                                    uniformBufferBindingsCount;
       UniformBufferBindingDescriptor              standaloneUniformsBufferBinding{};
     } bindUniformBuffers;
 
@@ -324,6 +161,66 @@ struct Command
     {
       Graphics::Viewport region;
     } viewport;
+
+    struct BeginRenderPassDescriptor
+      beginRenderPass;
+
+    struct
+    {
+      Graphics::SyncObject* syncObject;
+    } endRenderPass;
+
+    struct
+    {
+      IndirectPtr<const GLES::CommandBuffer*> buffers;
+      uint32_t                                buffersCount;
+    } executeCommandBuffers;
+
+    struct
+    {
+      GLES::RenderTarget* targetToPresent;
+    } presentRenderTarget;
+
+    struct
+    {
+      Graphics::CompareOp compareOp;
+      bool                testEnabled;
+      bool                writeEnabled;
+    } depth;
+
+    struct
+    {
+      Graphics::StencilOp failOp;
+      Graphics::StencilOp passOp;
+      Graphics::StencilOp depthFailOp;
+    } stencilOp;
+
+    struct
+    {
+      uint32_t mask;
+    } stencilWriteMask;
+
+    struct
+    {
+      uint32_t            compareMask;
+      Graphics::CompareOp compareOp;
+      uint32_t            reference;
+    } stencilFunc;
+
+    struct
+    {
+      bool enabled;
+    } stencilTest;
+
+    struct
+    {
+      bool enabled;
+    } colorMask;
+
+    struct
+    {
+      DrawNativeInfo drawNativeInfo;
+    } drawNative;
   };
 };
 
@@ -336,49 +233,78 @@ public:
 
   ~CommandBuffer() override;
 
-  void BindVertexBuffers(uint32_t                             firstBinding,
-                         std::vector<const Graphics::Buffer*> buffers,
-                         std::vector<uint32_t>                offsets) override;
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindVertexBuffers
+   */
+  void BindVertexBuffers(uint32_t                                    firstBinding,
+                         const std::vector<const Graphics::Buffer*>& buffers,
+                         const std::vector<uint32_t>&                offsets) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindUniformBuffers
+   */
   void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindPipeline
+   */
   void BindPipeline(const Graphics::Pipeline& pipeline) override;
 
-  void BindTextures(std::vector<TextureBinding>& textureBindings) override;
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindTextures
+   */
+  void BindTextures(const std::vector<TextureBinding>& textureBindings) override;
 
-  void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override;
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindSamplers
+   */
+  void BindSamplers(const std::vector<SamplerBinding>& samplerBindings) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindPushConstants
+   */
   void BindPushConstants(void*    data,
                          uint32_t size,
                          uint32_t binding) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BindIndexBuffer
+   */
   void BindIndexBuffer(const Graphics::Buffer& buffer,
                        uint32_t                offset,
                        Format                  format) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::BeginRenderPass
+   */
   void BeginRenderPass(
-    Graphics::RenderPass&   renderPass,
-    Graphics::RenderTarget& renderTarget,
-    Extent2D                renderArea,
-    std::vector<ClearValue> clearValues) override;
+    Graphics::RenderPass*          renderPass,
+    Graphics::RenderTarget*        renderTarget,
+    Rect2D                         renderArea,
+    const std::vector<ClearValue>& clearValues) override;
 
   /**
-   * @brief Ends current render pass
-   *
-   * This command must be issued in order to finalize the render pass.
-   * It's up to the implementation whether anything has to be done but
-   * the Controller may use end RP marker in order to resolve resource
-   * dependencies (for example, to know when target texture is ready
-   * before passing it to another render pass).
+   * @copydoc Dali::Graphics::CommandBuffer::EndRenderPass
    */
-  void EndRenderPass() override;
+  void EndRenderPass(Graphics::SyncObject* syncObject) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::ExecuteCommandBuffers
+   */
+  void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::Draw
+   */
   void Draw(
     uint32_t vertexCount,
     uint32_t instanceCount,
     uint32_t firstVertex,
     uint32_t firstInstance) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::DrawIndexed
+   */
   void DrawIndexed(
     uint32_t indexCount,
     uint32_t instanceCount,
@@ -386,31 +312,137 @@ public:
     int32_t  vertexOffset,
     uint32_t firstInstance) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::DrawIndexedIndirect
+   */
   void DrawIndexedIndirect(
     Graphics::Buffer& buffer,
     uint32_t          offset,
     uint32_t          drawCount,
     uint32_t          stride) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::DrawNative
+   */
+  void DrawNative(const DrawNativeInfo* drawNativeInfo) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::Reset
+   */
   void Reset() override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetScissor
+   */
   void SetScissor(Graphics::Rect2D value) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetScissorTestEnable
+   */
   void SetScissorTestEnable(bool value) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetViewport
+   */
   void SetViewport(Viewport value) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetViewportEnable
+   */
   void SetViewportEnable(bool value) override;
 
-  [[nodiscard]] const std::vector<Command>& GetCommands() const;
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetColorMask
+   */
+  void SetColorMask(bool enabled) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::ClearStencilBuffer
+   */
+  void ClearStencilBuffer() override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetStencilTestEnable
+   */
+  void SetStencilTestEnable(bool stencilEnable) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetStencilWriteMask
+   */
+  void SetStencilWriteMask(uint32_t writeMask) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetStencilOp
+   */
+  void SetStencilOp(Graphics::StencilOp failOp,
+                    Graphics::StencilOp passOp,
+                    Graphics::StencilOp depthFailOp) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetStencilFunc
+   */
+  void SetStencilFunc(Graphics::CompareOp compareOp,
+                      uint32_t            reference,
+                      uint32_t            compareMask) override;
 
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetDepthCompareOp
+   */
+  void SetDepthCompareOp(Graphics::CompareOp compareOp) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetDepthTestEnable
+   */
+  void SetDepthTestEnable(bool depthTestEnable) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::SetDepthWriteEnable
+   */
+  void SetDepthWriteEnable(bool depthWriteEnable) override;
+
+  /**
+   * @copydoc Dali::Graphics::CommandBuffer::ClearDepthBuffer
+   */
+  void ClearDepthBuffer() override;
+
+  /**
+   * @brief Presents specified render target
+   *
+   * @param[in] renderTarget Valid pointer to a RenderTarget
+   *
+   * It's internal command that schedules presentation of
+   * specified render target.
+   */
+  void PresentRenderTarget(GLES::RenderTarget* renderTarget);
+
+  /**
+   * @brief Returns pointer to the list of command and size of the list
+   *
+   * @param[out] size Size of the list
+   * @return Valid pointer to the list of commands
+   */
+  [[nodiscard]] const Command* GetCommands(uint32_t& size) const;
+
+  /**
+   * @brief Destroy the associated resources
+   */
   void DestroyResource() override;
+
+  /**
+   * @brief Initialize associated resources
+   */
   bool InitializeResource() override;
 
+  /**
+   * @brief Add this resource to the discard queue
+   */
   void DiscardResource() override;
 
+  // Get the total memory usage of this command buffer
+  std::size_t GetCapacity();
+
 private:
-  std::vector<Command> mCommands;
+  std::unique_ptr<CommandPool> mCommandPool; ///< Pool of commands and transient memory
 };
 } // namespace Dali::Graphics::GLES