ImageVisual postpones adding renderer to actor until image is loaded
[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/devel-api/image-loader/async-image-loader.h>
31 #include <dali-toolkit/internal/image-loader/atlas-packer.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    * 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   /**
105    * Destructor
106    */
107   ~ImageAtlas();
108
109 private:
110
111   /**
112    * @copydoc PixelDataRequester::ProcessPixels
113    */
114   void UploadToAtlas( uint32_t id, PixelData pixelData );
115
116   /**
117    * Upload broken image
118    *
119    * @param[in] area The pixel area for uploading.
120    */
121   void UploadBrokenImage( const Rect<unsigned int>& area );
122
123   // Undefined
124   ImageAtlas( const ImageAtlas& imageAtlas);
125
126   // Undefined
127   ImageAtlas& operator=( const ImageAtlas& imageAtlas );
128
129 private:
130
131   /**
132    * Each loading task( identified with an ID ) is associated with a rect region for packing the loaded pixel data into the atlas,
133    * and an AtlasUploadObserver whose UploadCompleted method should get executed once the sub texture is ready.
134    */
135   struct LoadingTaskInfo
136   {
137     LoadingTaskInfo( unsigned short loadTaskId,
138                      unsigned int packPositionX,
139                      unsigned int packPositionY,
140                      unsigned int width,
141                      unsigned int height,
142                      AtlasUploadObserver* observer )
143     : loadTaskId( loadTaskId ),
144       packRect( packPositionX, packPositionY, width, height ),
145       observer( observer )
146     {}
147
148     unsigned short loadTaskId;
149     Rect<unsigned int> packRect;
150     AtlasUploadObserver* observer;
151   };
152
153   OwnerContainer<LoadingTaskInfo*> mLoadingTaskInfoContainer;
154
155   Texture                   mAtlas;
156   AtlasPacker               mPacker;
157   Toolkit::AsyncImageLoader mAsyncLoader;
158   std::string               mBrokenImageUrl;
159   ImageDimensions           mBrokenImageSize;
160   float                     mWidth;
161   float                     mHeight;
162   Pixel::Format             mPixelFormat;
163
164
165 };
166
167 } // namespace Internal
168
169 inline const Internal::ImageAtlas& GetImplementation( const Toolkit::ImageAtlas& imageAtlas )
170 {
171   DALI_ASSERT_ALWAYS( imageAtlas && "ImageAtlas handle is empty" );
172
173   const BaseObject& handle = imageAtlas.GetBaseObject();
174
175   return static_cast<const Internal::ImageAtlas&>(handle);
176 }
177
178 inline Internal::ImageAtlas& GetImplementation( Toolkit::ImageAtlas& imageAtlas )
179 {
180   DALI_ASSERT_ALWAYS( imageAtlas && "ImageAtlas handle is empty" );
181
182   BaseObject& handle = imageAtlas.GetBaseObject();
183
184   return static_cast<Internal::ImageAtlas&>(handle);
185 }
186
187 } // namespace Toolkit
188
189 } // namespace Dali
190
191 #endif /* __DALI_TOOLKIT_IMAGE_ATLAS_IMPL_H__ */