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