[dali_2.3.19] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch-loader.h
1 #ifndef DALI_TOOLKIT_NPATCH_LOADER_H
2 #define DALI_TOOLKIT_NPATCH_LOADER_H
3
4 /*
5  * Copyright (c) 2022 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 // EXTERNAL INCLUDES
21 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
22 #include <dali/public-api/rendering/texture-set.h>
23 #include <memory> // for std::unique_ptr
24 #include <string>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
28 #include <dali-toolkit/internal/texture-manager/texture-manager-impl.h>
29 #include <dali-toolkit/internal/visuals/npatch-data.h>
30 #include <dali-toolkit/internal/visuals/visual-url.h>
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Internal
37 {
38 /**
39  * The manager for loading Npatch textures.
40  * It caches them internally for better performance; i.e. to avoid loading and
41  * parsing the files over and over.
42  *
43  * Cache is not cleaned during app lifecycle as N patches take considerably
44  * small space and there's not usually a lot of them. Usually N patches are specified in
45  * toolkit default style and there is 1-2 per control that are shared across the whole application.
46  */
47 class NPatchLoader
48 {
49 public:
50   /**
51    * Constructor
52    */
53   NPatchLoader();
54
55   /**
56    * Destructor, non-virtual as not a base class
57    */
58   ~NPatchLoader();
59
60   /**
61    * @brief Retrieve a texture matching the n-patch url.
62    *
63    * @param [in] textureManager that will be used to loading image
64    * @param [in] textureObserver The NPatchVisual that requested loading.
65    * @param [in] url to retrieve
66    * @param [in] border The border size of the image
67    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
68    *                                   image has no alpha channel
69    * @param [in] synchronousLoading True if the image will be loaded in synchronous time.
70    * @return id of the texture.
71    */
72   std::size_t Load(TextureManager& textureManager, TextureUploadObserver* textureObserver, const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad, bool synchronousLoading);
73
74   /**
75    * @brief Set loaded PixelBuffer and its information
76    *
77    * @param [in] id cache data id
78    * @param [in] pixelBuffer of loaded image
79    * @param [in] preMultiplied True if the image had pre-multiplied alpha applied
80    */
81   void SetNPatchData(std::size_t id, Devel::PixelBuffer& pixelBuffer, bool preMultiplied);
82
83   /**
84    * @brief Retrieve N patch data matching to an id
85    * @param [in] id of data
86    * @param [out] data const pointer to the NPatchData
87    * @return true if data matching to id was really found
88    */
89   bool GetNPatchData(const NPatchData::NPatchDataId id, const NPatchData*& data);
90
91   /**
92    * @brief Remove a texture matching id.
93    * Erase the observer from the observer list of cache if we need.
94    * This API decrease cached NPatchInfo reference.
95    * If the NPatchInfo reference become 0, the textureSet will be reset.
96    *
97    * @param [in] id cache data id
98    * @param [in] textureObserver The NPatchVisual that requested loading.
99    */
100   void Remove(std::size_t id, TextureUploadObserver* textureObserver);
101
102 private:
103   NPatchData::NPatchDataId GenerateUniqueNPatchDataId();
104
105   int32_t GetCacheIndexFromId(const NPatchData::NPatchDataId id);
106
107 private:
108   /**
109    * @brief Information of NPatchData
110    * It also hold ownership of NPatchData memory.
111    */
112   struct NPatchInfo
113   {
114     NPatchInfo(NPatchData* data)
115     : mData(data),
116       mReferenceCount(1u)
117     {
118     }
119     ~NPatchInfo()
120     {
121     }
122     NPatchInfo(NPatchInfo&& info) noexcept // move constructor
123     {
124       mData                = std::move(info.mData);
125       mReferenceCount      = info.mReferenceCount;
126       info.mReferenceCount = 0u;
127     }
128     NPatchInfo& operator=(NPatchInfo&& info) noexcept // move operator
129     {
130       mData                = std::move(info.mData);
131       mReferenceCount      = info.mReferenceCount;
132       info.mReferenceCount = 0u;
133       return *this;
134     }
135
136     NPatchInfo()                       = delete;            // Do not use default constructor
137     NPatchInfo(const NPatchInfo& info) = delete;            // Do not use copy constructor
138     NPatchInfo& operator=(const NPatchInfo& info) = delete; // Do not use copy assign
139
140     std::unique_ptr<NPatchData> mData;
141     std::int16_t                mReferenceCount; ///< The number of N-patch visuals that use this data.
142   };
143
144   /**
145    * @brief Get cached NPatchData by inputed url and border. If there is no cached data, create new one.
146    * @note This API increase cached NPatchInfo reference.
147    *
148    * @param [in] url to retrieve
149    * @param [in] border The border size of the image
150    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
151    *                                   image has no alpha channel
152    * @return NPatchData pointer that Load function will used.
153    */
154   NPatchData* GetNPatchData(const VisualUrl& url, const Rect<int>& border, bool& preMultiplyOnLoad);
155
156 protected:
157   /**
158    * Undefined copy constructor.
159    */
160   NPatchLoader(const NPatchLoader&);
161
162   /**
163    * Undefined assignment operator.
164    */
165   NPatchLoader& operator=(const NPatchLoader& rhs);
166
167 private:
168   NPatchData::NPatchDataId mCurrentNPatchDataId;
169   std::vector<NPatchInfo>  mCache;
170 };
171
172 } // namespace Internal
173
174 } // namespace Toolkit
175
176 } // namespace Dali
177
178 #endif // DALI_TOOLKIT_NPATCH_LOADER_H