Revert "License conversion from Flora to Apache 2.0"
[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    * Return the natural size of the image.
184    * This is the size that the loaded image will take
185    */
186   Vector2 GetNaturalSize() const;
187
188   /**
189    * @copydoc Dali::Image::GetFilename()
190    */
191   const std::string& GetFilename() const;
192
193   /**
194    * @copydoc Dali::Image::Reload()
195    */
196   void Reload();
197
198 public: // From ResourceTicketObserver
199
200   /**
201    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
202    */
203   virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
204
205   /**
206    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
207    */
208   virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
209
210   /**
211    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
212    */
213   virtual void ResourceUploaded(const ResourceTicket& ticket);
214
215   /**
216    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
217    */
218   virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
219
220   /**
221    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
222    */
223   virtual void ResourceSavingFailed( const ResourceTicket& ticket );
224
225 public:
226
227   /**
228    * Indicates that the image is used.
229    */
230   virtual void Connect();
231
232   /**
233    * Indicates that the image is not used anymore.
234    */
235   virtual void Disconnect();
236
237 private:
238
239   /**
240    * Helper method to set new resource ticket. Stops observing current ticket if any, and starts observing
241    * the new one or just resets the intrusive pointer.
242    * @param[in] ticket pointer to new resource Ticket or NULL.
243    */
244   void SetTicket( ResourceTicket* ticket );
245
246   /**
247    * Helper method to determine if the filename indicates that the image has a 9 patch border.
248    * @param[in] filename The filename to check
249    * @return true if it is a 9 patch image
250    */
251   static bool IsNinePatchFileName( std::string filename );
252
253
254 protected:
255   unsigned int mWidth;
256   unsigned int mHeight;
257
258   ResourceTicketPtr mTicket;
259   ImageFactoryCache::RequestPtr mRequest;         ///< contains the initially requested attributes for image. Request is reissued when memory was released.
260   LoadPolicy     mLoadPolicy;
261   ReleasePolicy  mReleasePolicy;
262
263   unsigned int   mConnectionCount; ///< number of on-stage objects using this image
264   ImageFactory&  mImageFactory;
265
266 private:
267   Dali::Image::ImageSignalV2 mLoadingFinishedV2;
268   Dali::Image::ImageSignalV2 mUploadedV2;
269
270   // Changes scope, should be at end of class
271   DALI_LOG_OBJECT_STRING_DECLARATION;
272 };
273
274 } // namespace Internal
275
276 /**
277  * Helper methods for public API.
278  */
279 inline Internal::Image& GetImplementation(Dali::Image& image)
280 {
281   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
282
283   BaseObject& handle = image.GetBaseObject();
284
285   return static_cast<Internal::Image&>(handle);
286 }
287
288 inline const Internal::Image& GetImplementation(const Dali::Image& image)
289 {
290   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
291
292   const BaseObject& handle = image.GetBaseObject();
293
294   return static_cast<const Internal::Image&>(handle);
295 }
296
297 } // namespace Dali
298 #endif // __DALI_INTERNAL_IMAGE_H__