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