(ImageLoader) Added synchronous method to get closest image size
[platform/core/uifw/dali-core.git] / dali / internal / event / images / image-impl.h
1 #ifndef __DALI_INTERNAL_IMAGE_H__
2 #define __DALI_INTERNAL_IMAGE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 <string>
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/images/image.h>
25 #include <dali/public-api/images/native-image.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/internal/render/gl-resources/texture.h>
28 #include <dali/internal/event/images/image-factory-cache.h>
29 #include <dali/internal/event/resources/image-ticket.h>
30 #include <dali/internal/event/resources/resource-client.h>
31 #include <dali/internal/event/resources/resource-ticket-observer.h>
32
33 namespace Dali
34 {
35
36 class NativeImage;
37
38 namespace Internal
39 {
40
41 typedef Dali::Image::LoadPolicy    LoadPolicy;
42 typedef Dali::Image::ReleasePolicy ReleasePolicy;
43
44 class Image;
45 class ImageFactory;
46 typedef IntrusivePtr<Image> ImagePtr;
47
48 const LoadPolicy    ImageLoadPolicyDefault    = Dali::Image::Immediate;
49 const ReleasePolicy ImageReleasePolicyDefault = Dali::Image::Never;
50
51 /**
52  * Image represents an image resource that can be added to actors etc.
53  * When the Image object is created, resource loading will be attempted.
54  * Provided this is successful, the resource will exist until the Image is destroyed.
55  */
56 class Image : public BaseObject, public ResourceTicketObserver
57 {
58 protected:
59
60   /**
61    * A reference counted object may only be deleted by calling Unreference()
62    */
63   virtual ~Image();
64
65   /**
66    * Constructor
67    */
68   Image(LoadPolicy loadPol=ImageLoadPolicyDefault, ReleasePolicy releasePol=ImageReleasePolicyDefault);
69
70 public:
71   /**
72    * Creates a pointer to an uninitialized Image object.
73    * @return a pointer to a newly created object.
74    */
75   static Image* New();
76
77   /**
78    * Creates object and loads image from filesystem
79    * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
80    * @param [in] filename   the path of the image on the filesystem
81    * @param [in] attributes requested parameters for loading (size, cropping etc.)
82    *                        if width or height is specified as 0, the natural size will be used.
83    * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
84    * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
85    * @return a pointer to a newly created object.
86    */
87   static Image* New(const std::string& filename,
88                     const Dali::ImageAttributes& attributes=Dali::ImageAttributes::DEFAULT_ATTRIBUTES,
89                     LoadPolicy loadPol=ImageLoadPolicyDefault,
90                     ReleasePolicy releasePol=ImageReleasePolicyDefault);
91
92
93
94 public:
95
96   /**
97    * Creates object with already loaded NativeImage
98    * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
99    * @pre nativeImg should be initialised
100    * @param [in] nativeImg already initialised NativeImage
101    * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
102    * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
103    * @return a pointer to a newly created object.
104    */
105   static Image* New(NativeImage& nativeImg,
106                     LoadPolicy loadPol=ImageLoadPolicyDefault,
107                     ReleasePolicy releasePol=ImageReleasePolicyDefault);
108
109   /**
110    * @copydoc Dali::Image::GetLoadingState()
111    */
112   Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
113
114   /**
115    * @copydoc Dali::Image::GetLoadPolicy()
116    */
117   LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
118
119   /**
120    * @copydoc Dali::Image::GetReleasePolicy()
121    */
122   ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
123
124   /**
125    * @copydoc Dali::Image::LoadingFinishedSignal()
126    */
127   Dali::Image::ImageSignalV2& LoadingFinishedSignal() { return mLoadingFinishedV2; }
128
129   /**
130    * @copydoc Dali::Image::UploadedSignal()
131    */
132   Dali::Image::ImageSignalV2& UploadedSignal() { return mUploadedV2; }
133
134   /**
135    * Connects a callback function with the object's signals.
136    * @param[in] object The object providing the signal.
137    * @param[in] tracker Used to disconnect the signal.
138    * @param[in] signalName The signal to connect to.
139    * @param[in] functor A newly allocated FunctorDelegate.
140    * @return True if the signal was connected.
141    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
142    */
143   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
144
145   /**
146    * returns the Id used for lookups
147    * @note if LoadPolicy::OnDemand is used and Image is off Stage, this will return 0.
148    * @return the unique ID of the image data resource. This is actually also the same as Dali Texture id.
149    */
150   ResourceId GetResourceId() const;
151
152   /**
153    * Get the attributes of the image.
154    * Only to be used after the image has finished loading.
155    * (Ticket's LoadingSucceeded callback was called)
156    * Reflects the last cached values after a LoadComplete.
157    * If requested width or height was 0, they are replaced by concrete dimensions.
158    * @return a copy of the attributes
159    */
160   const Dali::ImageAttributes& GetAttributes() const;
161
162   /**
163    * Get the width of the image.
164    * Only to be used after the image has finished loading.
165    * (Ticket's LoadingSucceeded callback was called)
166    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
167    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
168    * @pre image should be loaded
169    */
170   unsigned int GetWidth() const;
171
172   /**
173    * Get the height of the image.
174    * Only to be used after the image has finished loading.
175    * (Ticket's LoadingSucceeded callback was called)
176    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
177    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
178    * @pre image should be loaded
179    */
180   unsigned int GetHeight() const;
181
182   /**
183    * @copydoc Dali::Image::GetFilename()
184    */
185   const std::string& GetFilename() const;
186
187   /**
188    * @copydoc Dali::Image::Reload()
189    */
190   void Reload();
191
192 public: // From ResourceTicketObserver
193
194   /**
195    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
196    */
197   virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
198
199   /**
200    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
201    */
202   virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
203
204   /**
205    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
206    */
207   virtual void ResourceUploaded(const ResourceTicket& ticket);
208
209   /**
210    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
211    */
212   virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
213
214   /**
215    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
216    */
217   virtual void ResourceSavingFailed( const ResourceTicket& ticket );
218
219 public:
220
221   /**
222    * Indicates that the image is used.
223    */
224   virtual void Connect();
225
226   /**
227    * Indicates that the image is not used anymore.
228    */
229   virtual void Disconnect();
230
231 private:
232
233   /**
234    * Helper method to set new resource ticket. Stops observing current ticket if any, and starts observing
235    * the new one or just resets the intrusive pointer.
236    * @param[in] ticket pointer to new resource Ticket or NULL.
237    */
238   void SetTicket( ResourceTicket* ticket );
239
240 protected:
241   unsigned int mWidth;
242   unsigned int mHeight;
243
244   ResourceTicketPtr mTicket;
245   ImageFactoryCache::RequestPtr mRequest;         ///< contains the initially requested attributes for image. Request is reissued when memory was released.
246   LoadPolicy     mLoadPolicy;
247   ReleasePolicy  mReleasePolicy;
248
249   unsigned int   mConnectionCount; ///< number of on-stage objects using this image
250   ImageFactory&  mImageFactory;
251
252 private:
253   Dali::Image::ImageSignalV2 mLoadingFinishedV2;
254   Dali::Image::ImageSignalV2 mUploadedV2;
255
256   // Changes scope, should be at end of class
257   DALI_LOG_OBJECT_STRING_DECLARATION;
258 };
259
260 } // namespace Internal
261
262 /**
263  * Helper methods for public API.
264  */
265 inline Internal::Image& GetImplementation(Dali::Image& image)
266 {
267   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
268
269   BaseObject& handle = image.GetBaseObject();
270
271   return static_cast<Internal::Image&>(handle);
272 }
273
274 inline const Internal::Image& GetImplementation(const Dali::Image& image)
275 {
276   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
277
278   const BaseObject& handle = image.GetBaseObject();
279
280   return static_cast<const Internal::Image&>(handle);
281 }
282
283 } // namespace Dali
284 #endif // __DALI_INTERNAL_IMAGE_H__