(Images) Reducing thread tennis
[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] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
82    * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
83    * @return a pointer to a newly created object.
84    */
85   static Image* New(const std::string& filename,
86                     LoadPolicy loadPol=ImageLoadPolicyDefault,
87                     ReleasePolicy releasePol=ImageReleasePolicyDefault);
88
89   /**
90    * Creates object and loads image from filesystem
91    * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
92    * @param [in] filename   the path of the image on the filesystem
93    * @param [in] attributes requested parameters for loading (size, cropping etc.)
94    *                        if width or height is specified as 0, the natural size will be used.
95    * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
96    * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
97    * @return a pointer to a newly created object.
98    */
99   static Image* New(const std::string& filename,
100                     const Dali::ImageAttributes& attributes,
101                     LoadPolicy loadPol=ImageLoadPolicyDefault,
102                     ReleasePolicy releasePol=ImageReleasePolicyDefault);
103
104   /**
105    * Creates object with already loaded NativeImage
106    * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
107    * @pre nativeImg should be initialised
108    * @param [in] nativeImg already initialised NativeImage
109    * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
110    * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
111    * @return a pointer to a newly created object.
112    */
113   static Image* New(NativeImage& nativeImg,
114                     LoadPolicy loadPol=ImageLoadPolicyDefault,
115                     ReleasePolicy releasePol=ImageReleasePolicyDefault);
116
117   /**
118    * @copydoc Dali::Image::GetLoadingState()
119    */
120   Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
121
122   /**
123    * @copydoc Dali::Image::GetLoadPolicy()
124    */
125   LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
126
127   /**
128    * @copydoc Dali::Image::GetReleasePolicy()
129    */
130   ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
131
132   /**
133    * @copydoc Dali::Image::LoadingFinishedSignal()
134    */
135   Dali::Image::ImageSignalV2& LoadingFinishedSignal() { return mLoadingFinishedV2; }
136
137   /**
138    * @copydoc Dali::Image::UploadedSignal()
139    */
140   Dali::Image::ImageSignalV2& UploadedSignal() { return mUploadedV2; }
141
142   /**
143    * Connects a callback function with the object's signals.
144    * @param[in] object The object providing the signal.
145    * @param[in] tracker Used to disconnect the signal.
146    * @param[in] signalName The signal to connect to.
147    * @param[in] functor A newly allocated FunctorDelegate.
148    * @return True if the signal was connected.
149    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
150    */
151   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
152
153   /**
154    * returns the Id used for lookups
155    * @note if LoadPolicy::OnDemand is used and Image is off Stage, this will return 0.
156    * @return the unique ID of the image data resource. This is actually also the same as Dali Texture id.
157    */
158   ResourceId GetResourceId() const;
159
160   /**
161    * Get the attributes of the image.
162    * Only to be used after the image has finished loading.
163    * (Ticket's LoadingSucceeded callback was called)
164    * Reflects the last cached values after a LoadComplete.
165    * If requested width or height was 0, they are replaced by concrete dimensions.
166    * @return a copy of the attributes
167    */
168   const Dali::ImageAttributes& GetAttributes() const;
169
170   /**
171    * Get the width of the image.
172    * Only to be used after the image has finished loading.
173    * (Ticket's LoadingSucceeded callback was called)
174    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
175    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
176    * @pre image should be loaded
177    */
178   unsigned int GetWidth() const;
179
180   /**
181    * Get the height of the image.
182    * Only to be used after the image has finished loading.
183    * (Ticket's LoadingSucceeded callback was called)
184    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
185    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
186    * @pre image should be loaded
187    */
188   unsigned int GetHeight() const;
189
190   /**
191    * @copydoc Dali::Image::GetFilename()
192    */
193   const std::string& GetFilename() const;
194
195   /**
196    * @copydoc Dali::Image::Reload()
197    */
198   void Reload();
199
200 public: // From ResourceTicketObserver
201
202   /**
203    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
204    */
205   virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
206
207   /**
208    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
209    */
210   virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
211
212   /**
213    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
214    */
215   virtual void ResourceUploaded(const ResourceTicket& ticket);
216
217   /**
218    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
219    */
220   virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
221
222   /**
223    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
224    */
225   virtual void ResourceSavingFailed( const ResourceTicket& ticket );
226
227 public:
228
229   /**
230    * Indicates that the image is used.
231    */
232   virtual void Connect();
233
234   /**
235    * Indicates that the image is not used anymore.
236    */
237   virtual void Disconnect();
238
239 private:
240
241   /**
242    * Helper method to set new resource ticket. Stops observing current ticket if any, and starts observing
243    * the new one or just resets the intrusive pointer.
244    * @param[in] ticket pointer to new resource Ticket or NULL.
245    */
246   void SetTicket( ResourceTicket* ticket );
247
248 protected:
249   unsigned int mWidth;
250   unsigned int mHeight;
251
252   ResourceTicketPtr mTicket;
253   ImageFactoryCache::RequestPtr mRequest;         ///< contains the initially requested attributes for image. Request is reissued when memory was released.
254   LoadPolicy     mLoadPolicy;
255   ReleasePolicy  mReleasePolicy;
256
257   unsigned int   mConnectionCount; ///< number of on-stage objects using this image
258   ImageFactory&  mImageFactory;
259
260 private:
261   Dali::Image::ImageSignalV2 mLoadingFinishedV2;
262   Dali::Image::ImageSignalV2 mUploadedV2;
263
264   // Changes scope, should be at end of class
265   DALI_LOG_OBJECT_STRING_DECLARATION;
266 };
267
268 } // namespace Internal
269
270 /**
271  * Helper methods for public API.
272  */
273 inline Internal::Image& GetImplementation(Dali::Image& image)
274 {
275   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
276
277   BaseObject& handle = image.GetBaseObject();
278
279   return static_cast<Internal::Image&>(handle);
280 }
281
282 inline const Internal::Image& GetImplementation(const Dali::Image& image)
283 {
284   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
285
286   const BaseObject& handle = image.GetBaseObject();
287
288   return static_cast<const Internal::Image&>(handle);
289 }
290
291 } // namespace Dali
292 #endif // __DALI_INTERNAL_IMAGE_H__