Changed RenderItem* to RenderItemKey 72/286472/8
authorDavid Steele <david.steele@samsung.com>
Fri, 6 Jan 2023 16:41:53 +0000 (16:41 +0000)
committerDavid Steele <david.steele@samsung.com>
Fri, 20 Jan 2023 16:46:50 +0000 (16:46 +0000)
Change-Id: I255b92f4f7e35209cdbe28d10bd3f2bc991bf843
Signed-off-by: David Steele <david.steele@samsung.com>
automated-tests/src/dali-internal/utc-Dali-Internal-MemoryPoolObjectAllocator.cpp
dali/internal/render/common/render-item-key.h [new file with mode: 0644]
dali/internal/render/common/render-item.cpp
dali/internal/render/common/render-item.h
dali/internal/render/common/render-list.h
dali/internal/update/manager/render-instruction-processor.cpp
dali/internal/update/manager/render-instruction-processor.h

index 25d2838..af1d3e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 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.
@@ -102,7 +102,8 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
   Internal::MemoryPoolObjectAllocator<MemoryPoolObjectAllocatorTestObject> allocator;
 
   // Allocate an object
-  MemoryPoolObjectAllocatorTestObject* testObject1 = allocator.Allocate();
+  void*                                ptr         = allocator.AllocateRaw();
+  MemoryPoolObjectAllocatorTestObject* testObject1 = new(ptr) MemoryPoolObjectAllocatorTestObject();
   DALI_TEST_CHECK(testObject1);
 
   MemoryPoolObjectAllocatorTestObjectTracking tracking1;
@@ -120,7 +121,8 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocation(void)
   // Reset and allocate another object
   allocator.ResetMemoryPool();
 
-  MemoryPoolObjectAllocatorTestObject* testObject2 = allocator.Allocate();
+  ptr                                              = allocator.AllocateRaw();
+  MemoryPoolObjectAllocatorTestObject* testObject2 = new(ptr) MemoryPoolObjectAllocatorTestObject();
   DALI_TEST_CHECK(testObject2);
 
   MemoryPoolObjectAllocatorTestObjectTracking tracking2;
@@ -164,14 +166,16 @@ int UtcDaliMemoryPoolObjectAllocatorObjectAllocationPOD(void)
 {
   Internal::MemoryPoolObjectAllocator<bool> allocator;
 
-  bool* testObject1 = allocator.Allocate();
+  void* ptr         = allocator.AllocateRaw();
+  bool* testObject1 = new(ptr) bool();
   DALI_TEST_CHECK(testObject1);
 
   allocator.Destroy(testObject1);
 
   allocator.ResetMemoryPool();
 
-  bool* testObject2 = allocator.Allocate();
+  ptr               = allocator.AllocateRaw();
+  bool* testObject2 = new(ptr) bool();
   DALI_TEST_CHECK(testObject2);
 
   allocator.Destroy(testObject2);
diff --git a/dali/internal/render/common/render-item-key.h b/dali/internal/render/common/render-item-key.h
new file mode 100644 (file)
index 0000000..dd4360a
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_ITEM_KEY_H
+#define DALI_INTERNAL_SCENE_GRAPH_RENDER_ITEM_KEY_H
+
+/*
+ * 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.
+ * 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.
+ *
+ */
+
+#include <dali/internal/common/memory-pool-key.h>
+#include <dali/internal/common/type-abstraction-enums.h>
+
+namespace Dali
+{
+namespace Internal::SceneGraph
+{
+struct RenderItem;
+
+using RenderItemKey = MemoryPoolKey<RenderItem>;
+} // namespace Internal::SceneGraph
+
+// Ensure RenderItemKey can be used in Dali::Vector
+template<>
+struct TypeTraits<Internal::SceneGraph::RenderItemKey> : public BasicTypes<Internal::SceneGraph::RenderItemKey>
+{
+  enum
+  {
+    IS_TRIVIAL_TYPE = true
+  };
+};
+
+} // namespace Dali
+
+#endif //  DALI_INTERNAL_SCENE_GRAPH_RENDER_ITEM_KEY_H
index 1aafa37..9949611 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.
@@ -28,6 +28,7 @@ namespace
 //Memory pool used to allocate new RenderItems. Memory used by this pool will be released when shutting down DALi
 Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::RenderItem> gRenderItemPool;
 } // namespace
+
 namespace Dali
 {
 namespace Internal
@@ -39,6 +40,14 @@ RenderItem* RenderItem::New()
   return new(gRenderItemPool.AllocateRaw()) RenderItem();
 }
 
+RenderItemKey RenderItem::NewKey()
+{
+  void* ptr = gRenderItemPool.AllocateRaw();
+  auto  key = gRenderItemPool.GetKeyFromPtr(static_cast<RenderItem*>(ptr));
+  new(ptr) RenderItem();
+  return RenderItemKey(key);
+}
+
 RenderItem::RenderItem()
 : mModelMatrix(false),
   mModelViewMatrix(false),
@@ -54,6 +63,21 @@ RenderItem::RenderItem()
 
 RenderItem::~RenderItem() = default;
 
+RenderItem* RenderItem::Get(RenderItemKey::KeyType key)
+{
+  return gRenderItemPool.GetPtrFromKey(key);
+}
+
+RenderItemKey RenderItem::GetKey(const RenderItem& renderItem)
+{
+  return RenderItemKey(gRenderItemPool.GetKeyFromPtr(const_cast<RenderItem*>(&renderItem)));
+}
+
+RenderItemKey RenderItem::GetKey(RenderItem* renderItem)
+{
+  return RenderItemKey(gRenderItemPool.GetKeyFromPtr(renderItem));
+}
+
 ClippingBox RenderItem::CalculateTransformSpaceAABB(const Matrix& transformMatrix, const Vector3& position, const Vector3& size)
 {
   // Calculate extent vector of the AABB:
index 0a82320..cda4331 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_ITEM_H
 
 /*
- * 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.
@@ -19,6 +19,8 @@
  */
 
 // INTERNAL INCLUDES
+
+#include <dali/internal/render/common/render-item-key.h>
 #include <dali/internal/update/nodes/node.h>
 #include <dali/public-api/actors/layer.h>
 #include <dali/public-api/common/vector-wrapper.h>
@@ -47,11 +49,39 @@ struct RenderItem
   static RenderItem* New();
 
   /**
+   * Construct a new RenderItem
+   * @return A key to a new RenderItem
+   */
+  static RenderItemKey NewKey();
+
+  /**
    * Non-virtual destructor; RenderItem is not suitable as a base class.
    */
   ~RenderItem();
 
   /**
+   * Get a pointer to the given object in the associated memory pool.
+   * @param[in] key A key to the memory pool object
+   * @return a ptr to the given object, or nullptr if not found/invalid
+   */
+  static RenderItem* Get(RenderItemKey::KeyType key);
+
+  /**
+   * Get the key of the given renderer in the associated memory pool.
+   * @param[in] renderer the given renderer
+   * @return The key in the associated memory pool.
+   */
+  static RenderItemKey GetKey(const RenderItem& renderer);
+
+  /**
+   * Get the key of the given renderer in the associated memory pool.
+   * @param[in] renderer the given renderer
+   * @return The key in the associated memory pool, or -1 if not
+   * found.
+   */
+  static RenderItemKey GetKey(RenderItem* renderer);
+
+  /**
    * Produce a 2D AABB in transformed space
    * See below for caveats.
    *
@@ -111,8 +141,8 @@ private:
   RenderItem();
 
   // RenderItems should not be copied as they are heavy
-  RenderItem(const RenderItem& item);
-  RenderItem& operator=(const RenderItem& item);
+  RenderItem(const RenderItem& item) = delete;
+  RenderItem& operator=(const RenderItem& item) = delete;
 };
 
 } // namespace SceneGraph
index a694624..6621c8c 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_LIST_H
 
 /*
- * Copyright (c) 2021 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.
 
 // INTERNAL INCLUDES
 #include <dali/devel-api/common/owner-container.h>
+#include <dali/public-api/math/rect.h>
+
 #include <dali/graphics-api/graphics-controller.h>
 #include <dali/internal/render/common/render-item.h>
-#include <dali/public-api/math/rect.h>
+#include <dali/internal/update/manager/owner-key-container.h>
 
 namespace Dali
 {
@@ -42,13 +44,13 @@ namespace SceneGraph
 {
 class Layer;
 
-using RenderItemContainer = OwnerContainer<RenderItem*>;
+using RenderItemContainer = OwnerKeyContainer<RenderItem>;
 
 struct RenderList;
 using RenderListContainer = OwnerContainer<RenderList*>;
 
 /**
- * The RenderList structure provides the renderer with a list of renderers.
+ * The RenderList structure provides the renderer manager with a list of renderers.
  */
 struct RenderList
 {
@@ -118,11 +120,11 @@ public:
     // check if we have enough items, we can only be one behind at worst
     if(mItems.Count() <= mNextFree)
     {
-      mItems.PushBack(RenderItem::New()); // Push a new empty render item
+      mItems.PushBack(RenderItem::NewKey()); // Push a new empty render item
     }
     // get the item mNextFree points to and increase by one
-    RenderItem& item = *mItems[mNextFree++];
-    return item;
+    RenderItem* item = mItems[mNextFree++].Get();
+    return *item;
   }
 
   /**
@@ -131,7 +133,14 @@ public:
   RenderItem& GetItem(uint32_t index) const
   {
     DALI_ASSERT_DEBUG(index < GetCachedItemCount());
-    return *mItems[index];
+    RenderItem* item = mItems[index].Get();
+    return *item;
+  }
+
+  RenderItemKey GetItemKey(uint32_t index) const
+  {
+    DALI_ASSERT_DEBUG(index < GetCachedItemCount());
+    return mItems[index];
   }
 
   /**
@@ -277,7 +286,7 @@ public:
   }
 
 private:
-  RenderItemContainer mItems;    ///< Each item is a renderer and matrix pair
+  RenderItemContainer mItems;    ///< Container of render items
   uint32_t            mNextFree; ///< index for the next free item to use
 
   mutable Graphics::UniquePtr<Graphics::CommandBuffer> mGraphicsCommandBuffer{nullptr};
index 9486e91..cf81d88 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.
@@ -459,10 +459,8 @@ inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex,
 
   // List of zValue calculating functions.
   const Dali::Layer::SortFunctionType zValueFunctionFromVector3[] = {
-    [](const Vector3& position)
-    { return position.z; },
-    [](const Vector3& position)
-    { return position.LengthSquared(); },
+    [](const Vector3& position) { return position.z; },
+    [](const Vector3& position) { return position.LengthSquared(); },
     layer.GetSortFunction(),
   };
 
@@ -478,8 +476,8 @@ inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex,
 
   for(uint32_t index = 0; index < renderableCount; ++index)
   {
-    RenderItem& item = renderList.GetItem(index);
-
+    RenderItemKey itemKey = renderList.GetItemKey(index);
+    RenderItem&   item    = *itemKey.Get();
     if(DALI_LIKELY(item.mRenderer))
     {
       item.mRenderer->SetSortAttributes(mSortingHelper[index]);
@@ -491,7 +489,7 @@ inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex,
     mSortingHelper[index].zValue = zValueFunctionFromVector3[zValueFunctionIndex](item.mModelViewMatrix.GetTranslation3()) - static_cast<float>(item.mDepthIndex);
 
     // Keep the renderitem pointer in the helper so we can quickly reorder items after sort.
-    mSortingHelper[index].renderItem = &item;
+    mSortingHelper[index].renderItem = itemKey;
   }
 
   // Here we determine which comparitor (of the 3) to use.
index abaa466..138c3bd 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_SCENE_GRAPH_RENDER_INSTRUCTION_PROCESSOR_H
 
 /*
- * 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.
  */
 
 // INTERNAL INCLUDES
+#include <dali/public-api/common/dali-vector.h>
+
 #include <dali/internal/common/buffer-index.h>
+#include <dali/internal/render/common/render-item-key.h>
 #include <dali/internal/update/manager/sorted-layers.h>
-#include <dali/public-api/common/dali-vector.h>
 
 namespace Dali
 {
@@ -64,7 +66,7 @@ public:
   struct SortAttributes
   {
     SortAttributes()
-    : renderItem(nullptr),
+    : renderItem{},
       shader(nullptr),
       textureSet(nullptr),
       geometry(nullptr),
@@ -72,7 +74,7 @@ public:
     {
     }
 
-    RenderItem*             renderItem; ///< The render item that is being sorted (includes depth index)
+    RenderItemKey           renderItem; ///< The render item that is being sorted (includes depth index)
     const Shader*           shader;     ///< The shader instance
     const void*             textureSet; ///< The textureSet instance
     const Render::Geometry* geometry;   ///< The geometry instance