Send PixelData instead of raw buffer
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.cpp
index 0d97e69..f016af6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -22,6 +22,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
+#include <dali/internal/common/memory-pool-object-allocator.h>
 
 namespace Dali
 {
@@ -35,6 +36,9 @@ namespace
 Debug::Filter* gTextureFilter = Debug::Filter::New(Debug::Concise, false, "LOG_TEXTURE");
 #endif
 
+// Memory pool used to allocate new textures. Memory used by this pool will be released when shutting down DALi
+MemoryPoolObjectAllocator<Texture> gTextureMemoryPool;
+
 /**
  * Converts DALi pixel format to Graphics::Format
  * @param format
@@ -205,6 +209,24 @@ constexpr Graphics::TextureType ConvertType(Texture::Type type)
 
 } //Unnamed namespace
 
+TextureKey Texture::NewKey(Type type, Pixel::Format format, ImageDimensions size)
+{
+  void* ptr = gTextureMemoryPool.AllocateRawThreadSafe();
+  auto  key = gTextureMemoryPool.GetKeyFromPtr(static_cast<Texture*>(ptr));
+  new(ptr) Texture(type, format, size);
+
+  return TextureKey(key);
+}
+
+TextureKey Texture::NewKey(NativeImageInterfacePtr nativeImageInterface)
+{
+  void* ptr = gTextureMemoryPool.AllocateRawThreadSafe();
+  auto  key = gTextureMemoryPool.GetKeyFromPtr(static_cast<Texture*>(ptr));
+  new(ptr) Texture(nativeImageInterface);
+
+  return TextureKey(key);
+}
+
 Texture::Texture(Type type, Pixel::Format format, ImageDimensions size)
 : mGraphicsController(nullptr),
   mGraphicsTexture(nullptr),
@@ -235,6 +257,16 @@ Texture::Texture(NativeImageInterfacePtr nativeImageInterface)
 
 Texture::~Texture() = default;
 
+void Texture::operator delete(void* ptr)
+{
+  gTextureMemoryPool.FreeThreadSafe(static_cast<Texture*>(ptr));
+}
+
+Render::Texture* Texture::Get(TextureKey::KeyType key)
+{
+  return gTextureMemoryPool.GetPtrFromKey(key);
+}
+
 void Texture::Initialize(Graphics::Controller& graphicsController)
 {
   mGraphicsController = &graphicsController;
@@ -289,10 +321,9 @@ void Texture::Upload(PixelDataPtr pixelData, const Internal::Texture::UploadPara
 
   Graphics::TextureUpdateInfo info{};
 
-  const uint32_t bytePerPixel = Pixel::GetBytesPerPixel(pixelData->GetPixelFormat());
-  const uint32_t srcStride    = pixelData->GetStride();
-  uint32_t       srcOffset    = 0u;
-  uint32_t       srcSize      = pixelData->GetBufferSize();
+  const uint32_t srcStride = pixelData->GetStride();
+  uint32_t       srcOffset = 0u;
+  uint32_t       srcSize   = pixelData->GetBufferSize();
 
   const bool requiredSubPixelData = (!Pixel::IsCompressed(pixelData->GetPixelFormat())) &&
                                     ((params.dataXOffset != 0) ||
@@ -323,6 +354,7 @@ void Texture::Upload(PixelDataPtr pixelData, const Internal::Texture::UploadPara
      * srcOffset = A).offsetByte;
      * srcSize = ( C).offsetByte - A).offsetByte );
      */
+    const uint32_t bytePerPixel    = Pixel::GetBytesPerPixel(pixelData->GetPixelFormat());
     const uint32_t dataStrideByte  = (srcStride ? srcStride : static_cast<uint32_t>(params.dataWidth)) * bytePerPixel;
     const uint32_t dataXOffsetByte = params.dataXOffset * bytePerPixel;
     const uint32_t dataWidthByte   = static_cast<uint32_t>(params.dataWidth) * bytePerPixel;
@@ -343,8 +375,8 @@ void Texture::Upload(PixelDataPtr pixelData, const Internal::Texture::UploadPara
   info.srcFormat    = ConvertPixelFormat(pixelData->GetPixelFormat());
 
   Graphics::TextureUpdateSourceInfo updateSourceInfo{};
-  updateSourceInfo.sourceType          = Graphics::TextureUpdateSourceInfo::Type::MEMORY;
-  updateSourceInfo.memorySource.memory = pixelData->GetBuffer();
+  updateSourceInfo.sourceType                = Graphics::TextureUpdateSourceInfo::Type::PIXEL_DATA;
+  updateSourceInfo.pixelDataSource.pixelData = Dali::PixelData(pixelData.Get());
 
   mGraphicsController->UpdateTextures({info}, {updateSourceInfo});