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