Request load texture again if the observer is destroyed during 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) 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    * Observer class to inform the npatch image is loaded.
53    */
54   class NPatchLoadObserver: public Dali::Toolkit::TextureUploadObserver
55   {
56   public:
57     /**
58      * Informs observer when the npatch image is loaded.
59      * @param[in] preMultiplied True if the image had pre-multiplied alpha applied
60      */
61     virtual void NPatchLoadComplete ( bool preMultiplied ) = 0;
62   };
63
64   enum
65   {
66     UNINITIALIZED_ID = 0 ///< uninitialised id, use to initialize ids
67   };
68
69   struct Data
70   {
71     Data()
72     : url(),
73       textureSet(),
74       hash( 0 ),
75       croppedWidth( 0 ),
76       croppedHeight( 0 ),
77       border( 0, 0, 0, 0 ),
78       loadCompleted( false ),
79       renderingMap{ nullptr }
80     {}
81
82     ~Data();
83
84     using ObserverListType = Dali::Vector< NPatchLoadObserver* >;
85
86     TextureUploadObserver* textureObserver;              ///< TextureUploadObserver that requests to load texture.
87     ObserverListType observerList;                       ///< Container used to store all observer clients of this Texture
88     std::string url;                                     ///< Url of the N-Patch
89     TextureSet textureSet;                               ///< Texture containing the cropped image
90     NPatchUtility::StretchRanges stretchPixelsX;         ///< X stretch pixels
91     NPatchUtility::StretchRanges stretchPixelsY;         ///< Y stretch pixels
92     std::size_t hash;                                    ///< Hash code for the Url
93     uint32_t croppedWidth;                               ///< Width of the cropped middle part of N-patch
94     uint32_t croppedHeight;                              ///< Height of the cropped middle part of N-patch
95     Rect< int > border;                                  ///< The size of the border
96     bool preMultiplyOnLoad;                              ///< Whether to multiply alpha into color channels on load 
97     bool loadCompleted;                                  ///< True if the data loading is completed
98     void* renderingMap;                                  ///< NPatch rendering data
99   };
100
101 public:
102
103   /**
104    * Constructor
105    */
106   NPatchLoader();
107
108   /**
109    * Destructor, non-virtual as not a base class
110    */
111   ~NPatchLoader();
112
113   /**
114    * @brief Retrieve a texture matching the n-patch url.
115    *
116    * @param [in] textureManager that will be used to loading image
117    * @param [in] nPatchObserver The NPatchVisual that requested loading.
118    * @param [in] url to retrieve
119    * @param [in] border The border size of the image
120    * @param [in,out] preMultiplyOnLoad True if the image color should be multiplied by it's alpha. Set to false if the
121    *                                   image has no alpha channel
122    * @param [in] synchronousLoading True if the image will be loaded in synchronous time.
123    * @return id of the texture.
124    */
125   std::size_t Load( TextureManager& textureManager, NPatchLoadObserver* nPatchObserver, const std::string& url, const Rect< int >& border, bool& preMultiplyOnLoad, bool synchronousLoading );
126
127   /**
128    * @brief Set loaded PixelBuffer and its information
129    *
130    * @param [in] id cache data id
131    * @param [in] pixelBuffer of loaded image
132    * @param [in] preMultiplied True if the image had pre-multiplied alpha applied
133    */
134   void SetNPatchData( std::size_t id, Devel::PixelBuffer& pixelBuffer, bool preMultiplied );
135
136   /**
137    * @brief Retrieve N patch data matching to an id
138    * @param [in] id of data
139    * @param [out] data const pointer to the data
140    * @return true if data matching to id was really found
141    */
142   bool GetNPatchData( std::size_t id, const Data*& data );
143
144   void Remove( std::size_t id, TextureManager& textureManager, NPatchLoadObserver* nPatchObserver, bool synchronousLoading );
145
146 protected:
147
148   /**
149    * Undefined copy constructor.
150    */
151   NPatchLoader(const NPatchLoader&);
152
153   /**
154    * Undefined assignment operator.
155    */
156   NPatchLoader& operator=(const NPatchLoader& rhs);
157
158 private:
159
160   OwnerContainer< Data* > mCache;
161
162 };
163
164 } // name Internal
165
166 } // namespace Toolkit
167
168 } // namespace Dali
169
170 #endif // DALI_TOOLKIT_NPATCH_LOADER_H