Merge "Make do not caching pixelBuffer in texture-manager." into 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) 2020 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 <string>
22 #include <dali/public-api/rendering/texture-set.h>
23 #include <dali/devel-api/common/owner-container.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/visuals/texture-manager-impl.h>
28 #include <dali-toolkit/devel-api/utility/npatch-utilities.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 /**
40  * The manager for loading Npatch textures.
41  * It caches them internally for better performance; i.e. to avoid loading and
42  * parsing the files over and over.
43  *
44  * Cache is not cleaned during app lifecycle as N patches take considerably
45  * small space and there's not usually a lot of them. Usually N patches are specified in
46  * toolkit default style and there is 1-2 per control that are shared across the whole application.
47  */
48 class NPatchLoader
49 {
50 public:
51
52   enum
53   {
54     UNINITIALIZED_ID = 0 ///< uninitialised id, use to initialize ids
55   };
56
57   struct Data
58   {
59     Data()
60     : url(),
61       textureSet(),
62       hash( 0 ),
63       croppedWidth( 0 ),
64       croppedHeight( 0 ),
65       border( 0, 0, 0, 0 ),
66       loadCompleted( false ),
67       renderingMap{ nullptr }
68     {}
69
70     ~Data();
71
72     using ObserverListType = Dali::Vector< TextureUploadObserver* >;
73
74     ObserverListType observerList;                 ///< Container used to store all observer clients of this Texture
75     std::string url;                               ///< Url of the N-Patch
76     TextureSet textureSet;                         ///< Texture containing the cropped image
77     NPatchUtility::StretchRanges stretchPixelsX;   ///< X stretch pixels
78     NPatchUtility::StretchRanges stretchPixelsY;   ///< Y stretch pixels
79     std::size_t hash;                              ///< Hash code for the Url
80     uint32_t croppedWidth;                         ///< Width of the cropped middle part of N-patch
81     uint32_t croppedHeight;                        ///< Height of the cropped middle part of N-patch
82     Rect< int > border;                            ///< The size of the border
83     bool loadCompleted;                            ///< True if the data loading is completed
84     void* renderingMap;                            ///< NPatch rendering data
85   };
86
87 public:
88
89   /**
90    * Constructor
91    */
92   NPatchLoader();
93
94   /**
95    * Destructor, non-virtual as not a base class
96    */
97   ~NPatchLoader();
98
99   /**
100    * @brief Retrieve a texture matching the n-patch url.
101    *
102    * @param [in] textureManager that will be used to loading image
103    * @param [in] textureObserver The NPatchVisual that requested loading.
104    * @param [in] url to retrieve
105    * @param [in] border The border size of the image
106    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
107    *                                   image has no alpha channel
108    * @param [in] synchronousLoading True if the image will be loaded in synchronous time.
109    * @return id of the texture.
110    */
111   std::size_t Load( TextureManager& textureManager, TextureUploadObserver* textureObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading );
112
113   /**
114    * @brief Set loaded PixelBuffer and its information
115    *
116    * @param [in] loadSuccess True if the texture load was successful (i.e. the resource is available). If false, then the resource failed to load.
117    * @param [in] id cache data id
118    * @param [in] pixelBuffer of loaded image
119    * @param [in] url           The url address of the loaded image.
120    * @param [in] preMultiplied True if the image had pre-multiplied alpha applied
121    */
122   void SetNPatchData( bool loadSuccess, std::size_t id, Devel::PixelBuffer& pixelBuffer, const Internal::VisualUrl& url, bool preMultiplied );
123
124   /**
125    * @brief Retrieve N patch data matching to an id
126    * @param [in] id of data
127    * @param [out] data const pointer to the data
128    * @return true if data matching to id was really found
129    */
130   bool GetNPatchData( std::size_t id, const Data*& data );
131
132 protected:
133
134   /**
135    * Undefined copy constructor.
136    */
137   NPatchLoader(const NPatchLoader&);
138
139   /**
140    * Undefined assignment operator.
141    */
142   NPatchLoader& operator=(const NPatchLoader& rhs);
143
144 private:
145
146   OwnerContainer< Data* > mCache;
147
148 };
149
150 } // name Internal
151
152 } // namespace Toolkit
153
154 } // namespace Dali
155
156 #endif // DALI_TOOLKIT_NPATCH_LOADER_H