Merge "(Partial Update) Refactorize PartialRenderingData check logic" into devel...
[platform/core/uifw/dali-core.git] / dali / internal / event / size-negotiation / memory-pool-relayout-container.h
1 #ifndef DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H
2 #define DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H
3
4 /*
5  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <memory> // for std::unique_ptr
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/actors/actor.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/size-negotiation/relayout-container.h>
28
29 #include <dali/integration-api/ordered-set.h>
30 #include <dali/internal/common/memory-pool-object-allocator.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 /**
37  * @brief Container to encapsulate information required for relayout.
38  *
39  * Uses a memory pool to manage data allocations.
40  */
41 class MemoryPoolRelayoutContainer : public RelayoutContainer
42 {
43 public:
44   /**
45    * Struct to store the relayout information
46    */
47   struct RelayoutInfo
48   {
49     Dali::Actor actor; ///< The actor to relayout
50     Vector2     size;  ///< The desired size of the actor
51 #if defined(LOW_SPEC_MEMORY_MANAGEMENT_ENABLED)
52     struct RelayoutInfoCompareLess
53     {
54       bool operator()(const RelayoutInfo* lhs, const RelayoutInfo* rhs) const noexcept
55       {
56         return lhs->actor < rhs->actor;
57       }
58     };
59 #else
60     struct RelayoutInfoHash
61     {
62       std::size_t operator()(const RelayoutInfo* x) const noexcept
63       {
64         return reinterpret_cast<std::size_t>(x->actor.GetObjectPtr());
65       }
66     };
67     struct RelayoutInfoCompare
68     {
69       bool operator()(const RelayoutInfo* lhs, const RelayoutInfo* rhs) const noexcept
70       {
71         return lhs->actor == rhs->actor;
72       }
73     };
74 #endif
75   };
76
77   /**
78    * @brief Default constructor
79    *
80    * @param objectAllocator A memory pool that can allocate memory for RelayoutInfos
81    */
82   MemoryPoolRelayoutContainer(MemoryPoolObjectAllocator<RelayoutInfo>& objectAllocator);
83
84   /**
85    * Virtual destructor
86    */
87   ~MemoryPoolRelayoutContainer() override;
88
89   /**
90    * @brief Add relayout information to the container if it does'nt already exist
91    *
92    * @param actor The actor to relayout
93    * @param size The size to relayout
94    */
95   void Add(const Dali::Actor& actor, const Vector2& size) override;
96
97   /**
98    * @brief Remove information from the container
99    */
100   void PopBack();
101
102   /**
103    * @brief Retrieve relayout information for the latest added
104    *
105    * @param[out] actorOut Latest added actor
106    * @param[out] sizeOt Latest added size
107    */
108   void GetBack(Dali::Actor& actorOut, Vector2& sizeOut) const;
109
110   /**
111    * @brief The count of information in the container
112    */
113   size_t Size() const;
114
115   /**
116    * @brief Set the capacity of the container
117    *
118    * @param capacity The new capacity for the container
119    */
120   void Reserve(size_t capacity);
121
122   /**
123    * @brief Reset the container, freeing all memory
124    */
125   void Clear();
126
127 private:
128   /**
129    * @brief Returns if the container contains the actor or not
130    *
131    * @param actor The actor to search for
132    * @return Return if the actor was found or not
133    */
134   bool Contains(const Dali::Actor& actor);
135
136 private:
137 #if defined(LOW_SPEC_MEMORY_MANAGEMENT_ENABLED)
138   using RelayoutInfoContainer = Dali::Integration::OrderedSet<RelayoutInfo, false, RelayoutInfo::RelayoutInfoCompareLess>;
139 #else
140   using RelayoutInfoContainer = Dali::Integration::OrderedSet<RelayoutInfo, false, RelayoutInfo::RelayoutInfoHash, RelayoutInfo::RelayoutInfoCompare>;
141 #endif
142
143   RelayoutInfoContainer mRelayoutInfos; ///< The list of relayout infos
144
145   MemoryPoolObjectAllocator<RelayoutInfo>& mAllocator; ///< The memory pool from which the infos are allocated
146
147   std::unique_ptr<RelayoutInfo> mDummyRelayoutInfo; ///< Dummy pointer that will be used to compare relayout infors what we already holded.
148 };
149
150 } // namespace Internal
151
152 } // namespace Dali
153
154 #endif // DALI_INTERNAL_MEMORY_POOL_RELAYOUT_CONTAINER_H