Updated all header files to new format
[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) 2021 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/common/intrusive-ptr.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/signals/connection-tracker.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/image-loader/image-atlas.h>
28 #include <dali-toolkit/internal/image-loader/atlas-packer.h>
29 #include <dali-toolkit/public-api/image-loader/async-image-loader.h>
30
31 namespace Dali
32 {
33 class EventThreadCallback;
34
35 namespace Toolkit
36 {
37 namespace Internal
38 {
39 class ImageAtlas : public BaseObject, public ConnectionTracker
40 {
41 public:
42   typedef Toolkit::ImageAtlas::SizeType SizeType;
43
44   /**
45    * @copydoc ImageAtlas::PackToAtlas( const std::vector<PixelData>&, Dali::Vector<Vector4>& )
46    */
47   static Texture PackToAtlas(const std::vector<PixelData>& pixelData, Dali::Vector<Vector4>& textureRects);
48
49   /**
50    * Constructor
51    * @param [in] width          The atlas width in pixels.
52    * @param [in] height         The atlas height in pixels.
53    * @param [in] pixelFormat    The pixel format.
54    */
55   ImageAtlas(SizeType width, SizeType height, Pixel::Format pixelFormat);
56
57   /**
58    * @copydoc Toolkit::ImageAtlas::New
59    */
60   static IntrusivePtr<ImageAtlas> New(SizeType width, SizeType height, Pixel::Format pixelFormat);
61
62   /**
63    * @copydoc Toolkit::ImageAtlas::GetAtlas
64    */
65   Texture GetAtlas();
66
67   /**
68    * @copydoc Toolkit::ImageAtlas::GetOccupancyRate
69    */
70   float GetOccupancyRate() const;
71
72   /**
73    * @copydoc Toolkit::ImageAtlas::SetBrokenImage
74    */
75   void SetBrokenImage(const std::string& brokenImageUrl);
76
77   /**
78    * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, const std::string&, ImageDimensions,FittingMode::Type, bool )
79    */
80   bool Upload(Vector4&             textureRect,
81               const std::string&   url,
82               ImageDimensions      size,
83               FittingMode::Type    fittingMode,
84               bool                 orientationCorrection,
85               AtlasUploadObserver* atlasUploadObserver);
86
87   /**
88    * @copydoc Toolkit::ImageAtlas::Upload( Vector4&, PixelData )
89    */
90   bool Upload(Vector4& textureRect, PixelData pixelData);
91
92   /**
93    * @copydoc Toolkit::ImageAtlas::Remove
94    */
95   void Remove(const Vector4& textureRect);
96
97   /**
98    * Resets the destroying observer pointer so that we know not to call methods of this object any more.
99    */
100   void ObserverDestroyed(AtlasUploadObserver* observer);
101
102 protected:
103   /**
104    * Destructor
105    */
106   ~ImageAtlas();
107
108 private:
109   /**
110    * @copydoc PixelDataRequester::ProcessPixels
111    */
112   void UploadToAtlas(uint32_t id, PixelData pixelData);
113
114   /**
115    * Upload broken image
116    *
117    * @param[in] area The pixel area for uploading.
118    */
119   void UploadBrokenImage(const Rect<unsigned int>& area);
120
121   // Undefined
122   ImageAtlas(const ImageAtlas& imageAtlas);
123
124   // Undefined
125   ImageAtlas& operator=(const ImageAtlas& imageAtlas);
126
127 private:
128   /**
129    * Each loading task( identified with an ID ) is associated with a rect region for packing the loaded pixel data into the atlas,
130    * and an AtlasUploadObserver whose UploadCompleted method should get executed once the sub texture is ready.
131    */
132   struct LoadingTaskInfo
133   {
134     LoadingTaskInfo(unsigned short       loadTaskId,
135                     unsigned int         packPositionX,
136                     unsigned int         packPositionY,
137                     unsigned int         width,
138                     unsigned int         height,
139                     AtlasUploadObserver* observer)
140     : loadTaskId(loadTaskId),
141       packRect(packPositionX, packPositionY, width, height),
142       observer(observer)
143     {
144     }
145
146     unsigned short       loadTaskId;
147     Rect<unsigned int>   packRect;
148     AtlasUploadObserver* observer;
149   };
150
151   OwnerContainer<LoadingTaskInfo*> mLoadingTaskInfoContainer;
152
153   Texture                   mAtlas;
154   AtlasPacker               mPacker;
155   Toolkit::AsyncImageLoader mAsyncLoader;
156   std::string               mBrokenImageUrl;
157   ImageDimensions           mBrokenImageSize;
158   float                     mWidth;
159   float                     mHeight;
160   Pixel::Format             mPixelFormat;
161 };
162
163 } // namespace Internal
164
165 inline const Internal::ImageAtlas& GetImplementation(const Toolkit::ImageAtlas& imageAtlas)
166 {
167   DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
168
169   const BaseObject& handle = imageAtlas.GetBaseObject();
170
171   return static_cast<const Internal::ImageAtlas&>(handle);
172 }
173
174 inline Internal::ImageAtlas& GetImplementation(Toolkit::ImageAtlas& imageAtlas)
175 {
176   DALI_ASSERT_ALWAYS(imageAtlas && "ImageAtlas handle is empty");
177
178   BaseObject& handle = imageAtlas.GetBaseObject();
179
180   return static_cast<Internal::ImageAtlas&>(handle);
181 }
182
183 } // namespace Toolkit
184
185 } // namespace Dali
186
187 #endif // DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H