[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / image-loader / image-atlas-impl.h
1 #ifndef DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H
2 #define DALI_TOOLKIT_IMAGE_ATLAS_IMPL_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/common/owner-container.h>
22 #include <dali/public-api/adaptor-framework/encoded-image-buffer.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/signals/connection-tracker.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
29 #include <dali-toolkit/internal/image-loader/atlas-packer.h>
30 #include <dali-toolkit/internal/visuals/visual-url.h>
31 #include <dali-toolkit/public-api/image-loader/async-image-loader.h>
32
33 namespace Dali
34 {
35 class EventThreadCallback;
36
37 namespace Toolkit
38 {
39 namespace Internal
40 {
41 class ImageAtlas : public BaseObject, public ConnectionTracker
42 {
43 public:
44   typedef Toolkit::ImageAtlas::SizeType SizeType;
45
46   /**
47    * @copydoc ImageAtlas::PackToAtlas( const std::vector<PixelData>&, Dali::Vector<Vector4>& )
48    */
49   static Texture PackToAtlas(const std::vector<PixelData>& pixelData, Dali::Vector<Vector4>& textureRects);
50
51   /**
52    * Constructor
53    * @param [in] width          The atlas width in pixels.
54    * @param [in] height         The atlas height in pixels.
55    * @param [in] pixelFormat    The pixel format.
56    */
57   ImageAtlas(SizeType width, SizeType height, Pixel::Format pixelFormat);
58
59   /**
60    * @copydoc Toolkit::ImageAtlas::New
61    */
62   static IntrusivePtr<ImageAtlas> New(SizeType width, SizeType height, Pixel::Format pixelFormat);
63
64   /**
65    * @copydoc Toolkit::ImageAtlas::GetAtlas
66    */
67   Texture GetAtlas();
68
69   /**
70    * @copydoc Toolkit::ImageAtlas::GetOccupancyRate
71    */
72   float GetOccupancyRate() const;
73
74   /**
75    * @copydoc Toolkit::ImageAtlas::SetBrokenImage
76    */
77   void SetBrokenImage(const std::string& brokenImageUrl);
78
79   /**
80    * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, const std::string&, ImageDimensions,FittingMode::Type, bool )
81    */
82   bool Upload(Vector4&             textureRect,
83               const VisualUrl&     url,
84               ImageDimensions      size,
85               FittingMode::Type    fittingMode,
86               bool                 orientationCorrection,
87               AtlasUploadObserver* atlasUploadObserver);
88
89   /**
90    * @brief Upload a resource image to the atlas by encoded buffer.
91    *
92    * @note To make the atlasing efficient, a valid size should be provided.
93    *       If size is not provided, then SegFault occured.
94    *       Do not set a size that is bigger than the actual image size, as the up-scaling is not available,
95    *       the content of the area not covered by actual image is undefined, it will not be cleared.
96    *
97    * SamplingMode::BOX_THEN_LINEAR is used to sampling pixels from the input image while fitting it to desired size.
98    *
99    * @param [out] textureRect The texture area of the resource image in the atlas.
100    * @param [in] encodedImageBuffer The encoded raw buffer of the resource image file to use.
101    * @param [in] size The width and height to fit the loaded image to.
102    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
103    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
104    * @param [in] atlasUploadObserver The observer to observe the upload state inside the ImageAtlas.
105    * @return True if there is enough space to fit this image in,false otherwise.
106    * @note The valid callback function here is required to have the signature of void( void ).
107    */
108   bool Upload(Vector4&                  textureRect,
109               const EncodedImageBuffer& encodedImageBuffer,
110               ImageDimensions           size,
111               FittingMode::Type         fittingMode,
112               bool                      orientationCorrection,
113               AtlasUploadObserver*      atlasUploadObserver);
114
115   /**
116    * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, PixelData )
117    */
118   bool Upload(Vector4& textureRect, PixelData pixelData);
119
120   /**
121    * @copydoc Toolkit::ImageAtlas::Remove
122    */
123   void Remove(const Vector4& textureRect);
124
125   /**
126    * Resets the destroying observer pointer so that we know not to call methods of this object any more.
127    */
128   void ObserverDestroyed(AtlasUploadObserver* observer);
129
130 protected:
131   /**
132    * Destructor
133    */
134   ~ImageAtlas();
135
136 private:
137   /**
138    * @copydoc PixelDataRequester::ProcessPixels
139    */
140   void UploadToAtlas(uint32_t id, PixelData pixelData);
141
142   /**
143    * Upload broken image
144    *
145    * @param[in] area The pixel area for uploading.
146    */
147   void UploadBrokenImage(const Rect<uint32_t>& area);
148
149   // Undefined
150   ImageAtlas(const ImageAtlas& imageAtlas);
151
152   // Undefined
153   ImageAtlas& operator=(const ImageAtlas& imageAtlas);
154
155 private:
156   /**
157    * Each loading task( identified with an ID ) is associated with a rect region for packing the loaded pixel data into the atlas,
158    * and an AtlasUploadObserver whose UploadCompleted method should get executed once the sub texture is ready.
159    */
160   struct LoadingTaskInfo
161   {
162     LoadingTaskInfo(uint32_t             loadTaskId,
163                     uint32_t             packPositionX,
164                     uint32_t             packPositionY,
165                     uint32_t             width,
166                     uint32_t             height,
167                     AtlasUploadObserver* observer)
168     : loadTaskId(loadTaskId),
169       packRect(packPositionX, packPositionY, width, height),
170       observer(observer)
171     {
172     }
173
174     uint32_t             loadTaskId;
175     Rect<uint32_t>       packRect;
176     AtlasUploadObserver* observer;
177   };
178
179   OwnerContainer<LoadingTaskInfo*> mLoadingTaskInfoContainer;
180
181   Texture                   mAtlas;
182   AtlasPacker               mPacker;
183   Toolkit::AsyncImageLoader mAsyncLoader;
184   std::string               mBrokenImageUrl;
185   ImageDimensions           mBrokenImageSize;
186   float                     mWidth;
187   float                     mHeight;
188   Pixel::Format             mPixelFormat;
189 };
190
191 } // namespace Internal
192
193 inline const Internal::ImageAtlas& GetImplementation(const Toolkit::ImageAtlas& imageAtlas)
194 {
195   DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
196
197   const BaseObject& handle = imageAtlas.GetBaseObject();
198
199   return static_cast<const Internal::ImageAtlas&>(handle);
200 }
201
202 inline Internal::ImageAtlas& GetImplementation(Toolkit::ImageAtlas& imageAtlas)
203 {
204   DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
205
206   BaseObject& handle = imageAtlas.GetBaseObject();
207
208   return static_cast<Internal::ImageAtlas&>(handle);
209 }
210
211 } // namespace Toolkit
212
213 } // namespace Dali
214
215 #endif // DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H