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