GLES2/3 support for memory mapping 09/253509/6
authoradam.b <adam.b@samsung.com>
Fri, 26 Mar 2021 12:09:25 +0000 (12:09 +0000)
committeradam.b <adam.b@samsung.com>
Fri, 26 Mar 2021 12:09:25 +0000 (12:09 +0000)
Change-Id: Ie352c452a5a5bbfbc4973178e35e3a1b613a9753

dali/internal/graphics/gles-impl/egl-graphics-controller.cpp
dali/internal/graphics/gles-impl/egl-graphics-controller.h
dali/internal/graphics/gles-impl/file.list
dali/internal/graphics/gles-impl/gles-graphics-types.h
dali/internal/graphics/gles-impl/gles2-graphics-memory.cpp [new file with mode: 0644]
dali/internal/graphics/gles-impl/gles2-graphics-memory.h [moved from dali/internal/graphics/gles-impl/gles-graphics-memory.h with 84% similarity]
dali/internal/graphics/gles-impl/gles3-graphics-memory.cpp [moved from dali/internal/graphics/gles-impl/gles-graphics-memory.cpp with 78% similarity]
dali/internal/graphics/gles-impl/gles3-graphics-memory.h [new file with mode: 0644]

index dc1e280..9746ac4 100644 (file)
@@ -26,6 +26,7 @@
 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
+#include <dali/internal/graphics/gles-impl/gles3-graphics-memory.h>
 #include <dali/public-api/common/dali-common.h>
 #include "gles-graphics-program.h"
 
@@ -411,7 +412,14 @@ Graphics::UniquePtr<Memory> EglGraphicsController::MapBufferRange(const MapBuffe
   // in case when the buffer is not there yet
   ProcessCreateQueues();
 
-  return Graphics::UniquePtr<Memory>(new GLES::Memory(mapInfo, *this));
+  if(GetGLESVersion() < GLES::GLESVersion::GLES_30)
+  {
+    return Graphics::UniquePtr<Memory>(new GLES::Memory2(mapInfo, *this));
+  }
+  else
+  {
+    return Graphics::UniquePtr<Memory>(new GLES::Memory3(mapInfo, *this));
+  }
 }
 
 bool EglGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
index 2ae3296..5bf2e0d 100644 (file)
 // INTERNAL INCLUDES
 #include "gles-context.h"
 #include "gles-graphics-buffer.h"
-#include "gles-graphics-memory.h"
 #include "gles-graphics-pipeline-cache.h"
 #include "gles-graphics-pipeline.h"
 #include "gles-graphics-reflection.h"
 #include "gles-graphics-sampler.h"
 #include "gles-graphics-texture.h"
+#include "gles-graphics-types.h"
+#include "gles2-graphics-memory.h"
 
 namespace Dali
 {
@@ -459,6 +460,18 @@ public:
    */
   [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
 
+  /**
+   * @brief Returns runtime supported GLES version
+   *
+   * @return GLES version enum
+   */
+  GLES::GLESVersion GetGLESVersion() const
+  {
+    // TODO: return proper version but for now we can
+    // test fallbacks
+    return GLES::GLESVersion::GLES_20;
+  }
+
 private:
   Integration::GlAbstraction*              mGlAbstraction{nullptr};
   Integration::GlSyncAbstraction*          mGlSyncAbstraction{nullptr};
index 6195634..09df141 100644 (file)
@@ -6,7 +6,8 @@ SET( adaptor_graphics_gles_src_files ${adaptor_graphics_gles_src_files}
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-command-buffer.cpp
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-debug.cpp
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-framebuffer.cpp
-    ${adaptor_graphics_dir}/gles-impl/gles-graphics-memory.cpp
+    ${adaptor_graphics_dir}/gles-impl/gles2-graphics-memory.cpp
+    ${adaptor_graphics_dir}/gles-impl/gles3-graphics-memory.cpp
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-pipeline.cpp
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-program.cpp
     ${adaptor_graphics_dir}/gles-impl/gles-graphics-reflection.cpp
index 31b9f6d..b8b6fcd 100644 (file)
@@ -1858,6 +1858,14 @@ struct GLTypeConversion
   GLType type{GLType::UNDEFINED};
 };
 
+enum class GLESVersion
+{
+  GLES_20 = 20,
+  GLES_30 = 30,
+  GLES_31 = 31,
+  GLES_32 = 32
+};
+
 } // namespace Dali::Graphics::GLES
 
 #endif //DALI_GRAPHICS_API_TYPES_H
diff --git a/dali/internal/graphics/gles-impl/gles2-graphics-memory.cpp b/dali/internal/graphics/gles-impl/gles2-graphics-memory.cpp
new file mode 100644 (file)
index 0000000..f92af32
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2021 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// CLASS HEADER
+#include "gles2-graphics-memory.h"
+
+// EXTERNAL INCLUDES
+#include <dali/integration-api/gl-abstraction.h>
+#include <dali/integration-api/gl-defines.h>
+
+// INTERNAL INCLUDES
+#include "egl-graphics-controller.h"
+#include "gles-graphics-buffer.h"
+
+namespace Dali::Graphics::GLES
+{
+Memory2::Memory2(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
+: mController(controller)
+{
+  mMapBufferInfo = mapInfo;
+  mMapObjectType = MapObjectType::BUFFER;
+}
+
+Memory2::Memory2(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
+: mController(controller)
+{
+  mMapTextureInfo = mapInfo;
+  mMapObjectType  = MapObjectType::TEXTURE;
+}
+
+Memory2::~Memory2()
+{
+  //Unlock(true);
+}
+
+void* Memory2::LockRegion(uint32_t offset, uint32_t size)
+{
+  // allocate temporary buffer (reading back may not be supported)
+  // emulated buffer is always mapped from beginning
+  if(mMapObjectType == MapObjectType::BUFFER)
+  {
+    auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
+    if(buffer->IsCPUAllocated())
+    {
+      using Ptr = char*;
+      mMappedPointer = Ptr(buffer->GetCPUAllocatedAddress()) + offset;
+      mIsAllocatedLocally = false;
+    }
+    else
+    {
+      auto retval    = malloc(size);
+      mMappedPointer = retval;
+      mIsAllocatedLocally = true;
+    }
+  }
+
+  return mMappedPointer;
+}
+
+void Memory2::Unlock(bool flush)
+{
+  auto gl = mController.GetGL();
+
+  // for buffer...
+  if(mMapObjectType == MapObjectType::BUFFER&& mMappedPointer)
+  {
+    auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
+    if(!buffer->IsCPUAllocated())
+    {
+      buffer->Bind(BufferUsage::VERTEX_BUFFER);
+      gl->BufferSubData(GL_ARRAY_BUFFER, mMapBufferInfo.offset, mMapBufferInfo.size, mMappedPointer);
+    }
+  }
+
+  if(mIsAllocatedLocally)
+  {
+    free(mMappedPointer);
+    mMappedPointer = nullptr;
+  }
+
+  if(flush)
+  {
+    Flush();
+  }
+}
+
+void Memory2::Flush()
+{
+  // TODO:
+}
+
+} // namespace Dali::Graphics::GLES
\ No newline at end of file
@@ -31,14 +31,14 @@ namespace Dali::Graphics
 class EglGraphicsController;
 namespace GLES
 {
-class Memory : public Dali::Graphics::Memory
+class Memory2 : public Dali::Graphics::Memory
 {
 public:
-  Memory(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller);
+  Memory2(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller);
 
-  Memory(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller);
+  Memory2(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller);
 
-  ~Memory() override;
+  ~Memory2() override;
 
   void* LockRegion(uint32_t offset, uint32_t size) override;
 
@@ -64,6 +64,7 @@ public:
   };
 
   void* mMappedPointer{nullptr};
+  bool mIsAllocatedLocally{false};
 };
 } // namespace GLES
 } // namespace Dali::Graphics
@@ -16,7 +16,7 @@
  */
 
 // CLASS HEADER
-#include "gles-graphics-memory.h"
+#include "gles3-graphics-memory.h"
 
 // EXTERNAL INCLUDES
 #include <dali/integration-api/gl-abstraction.h>
 
 namespace Dali::Graphics::GLES
 {
-Memory::Memory(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
+Memory3::Memory3(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller)
 : mController(controller)
 {
   mMapBufferInfo = mapInfo;
   mMapObjectType = MapObjectType::BUFFER;
 }
 
-Memory::Memory(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
+Memory3::Memory3(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller)
 : mController(controller)
 {
   mMapTextureInfo = mapInfo;
   mMapObjectType  = MapObjectType::TEXTURE;
 }
 
-Memory::~Memory()
+Memory3::~Memory3()
 {
   Unlock(true);
 }
 
-void* Memory::LockRegion(uint32_t offset, uint32_t size)
+void* Memory3::LockRegion(uint32_t offset, uint32_t size)
 {
   auto gl = mController.GetGL();
 
@@ -73,15 +73,18 @@ void* Memory::LockRegion(uint32_t offset, uint32_t size)
   return nullptr;
 }
 
-void Memory::Unlock(bool flush)
+void Memory3::Unlock(bool flush)
 {
   auto gl = mController.GetGL();
 
   if(mMapObjectType == MapObjectType::BUFFER && mMappedPointer)
   {
     auto buffer = static_cast<GLES::Buffer*>(mMapBufferInfo.buffer);
-    buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
-    gl->UnmapBuffer(GL_ARRAY_BUFFER);
+    if(!buffer->IsCPUAllocated())
+    {
+      buffer->Bind(Graphics::BufferUsage::VERTEX_BUFFER);
+      gl->UnmapBuffer(GL_ARRAY_BUFFER);
+    }
   }
 
   if(flush)
@@ -90,7 +93,7 @@ void Memory::Unlock(bool flush)
   }
 }
 
-void Memory::Flush()
+void Memory3::Flush()
 {
   // TODO:
 }
diff --git a/dali/internal/graphics/gles-impl/gles3-graphics-memory.h b/dali/internal/graphics/gles-impl/gles3-graphics-memory.h
new file mode 100644 (file)
index 0000000..657aecd
--- /dev/null
@@ -0,0 +1,71 @@
+#ifndef DALI_GRAPHICS_GLES3_MEMORY_H
+#define DALI_GRAPHICS_GLES3_MEMORY_H
+
+/*
+ * Copyright (c) 2021 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// EXTERNAL INCLUDES
+#include <dali/graphics-api/graphics-memory.h>
+#include <cstdint>
+
+// INTERNAL INCLUDES
+#include "gles-graphics-buffer.h"
+#include "gles-graphics-texture.h"
+
+namespace Dali::Graphics
+{
+class EglGraphicsController;
+namespace GLES
+{
+class Memory3 : public Dali::Graphics::Memory
+{
+public:
+  Memory3(const Graphics::MapBufferInfo& mapInfo, EglGraphicsController& controller);
+
+  Memory3(const Graphics::MapTextureInfo& mapInfo, EglGraphicsController& controller);
+
+  ~Memory3() override;
+
+  void* LockRegion(uint32_t offset, uint32_t size) override;
+
+  void Unlock(bool flush) override;
+
+  void Flush() override;
+
+  EglGraphicsController& mController;
+
+  enum class MapObjectType
+  {
+    NONE,
+    BUFFER,
+    TEXTURE
+  };
+
+  MapObjectType mMapObjectType{MapObjectType::NONE};
+
+  union
+  {
+    Graphics::MapTextureInfo mMapTextureInfo{};
+    Graphics::MapBufferInfo  mMapBufferInfo;
+  };
+
+  void* mMappedPointer{nullptr};
+};
+} // namespace GLES
+} // namespace Dali::Graphics
+
+#endif
\ No newline at end of file