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