1 #ifndef __DALI_INTERNAL_IMAGE_H__
2 #define __DALI_INTERNAL_IMAGE_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
41 typedef Dali::Image::LoadPolicy LoadPolicy;
42 typedef Dali::Image::ReleasePolicy ReleasePolicy;
46 typedef IntrusivePtr<Image> ImagePtr;
48 const LoadPolicy ImageLoadPolicyDefault = Dali::Image::Immediate;
49 const ReleasePolicy ImageReleasePolicyDefault = Dali::Image::Never;
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.
56 class Image : public BaseObject, public ResourceTicketObserver
61 * A reference counted object may only be deleted by calling Unreference()
66 * Constructor, with default parameters
68 Image(LoadPolicy loadPol=ImageLoadPolicyDefault, ReleasePolicy releasePol=ImageReleasePolicyDefault);
72 * Creates a pointer to an uninitialized Image object.
73 * @return a pointer to a newly created object.
75 static ImagePtr New();
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.
87 static ImagePtr New( const std::string& filename,
88 const Dali::ImageAttributes& attributes=Dali::ImageAttributes::DEFAULT_ATTRIBUTES,
89 LoadPolicy loadPol=ImageLoadPolicyDefault,
90 ReleasePolicy releasePol=ImageReleasePolicyDefault );
93 * Creates object with already loaded NativeImage
94 * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
95 * @pre nativeImg should be initialised
96 * @param [in] nativeImg already initialised NativeImage
97 * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
98 * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
99 * @return a pointer to a newly created object.
101 static ImagePtr New( NativeImage& nativeImg,
102 LoadPolicy loadPol=ImageLoadPolicyDefault,
103 ReleasePolicy releasePol=ImageReleasePolicyDefault );
106 * @copydoc Dali::Image::GetLoadingState()
108 Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
111 * @copydoc Dali::Image::GetLoadPolicy()
113 LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
116 * @copydoc Dali::Image::GetReleasePolicy()
118 ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
121 * @copydoc Dali::Image::LoadingFinishedSignal()
123 Dali::Image::ImageSignalV2& LoadingFinishedSignal() { return mLoadingFinishedV2; }
126 * @copydoc Dali::Image::UploadedSignal()
128 Dali::Image::ImageSignalV2& UploadedSignal() { return mUploadedV2; }
131 * Connects a callback function with the object's signals.
132 * @param[in] object The object providing the signal.
133 * @param[in] tracker Used to disconnect the signal.
134 * @param[in] signalName The signal to connect to.
135 * @param[in] functor A newly allocated FunctorDelegate.
136 * @return True if the signal was connected.
137 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
139 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
142 * returns the Id used for lookups
143 * @note if LoadPolicy::OnDemand is used and Image is off Stage, this will return 0.
144 * @return the unique ID of the image data resource. This is actually also the same as Dali Texture id.
146 ResourceId GetResourceId() const;
149 * Get the attributes of the image.
150 * Only to be used after the image has finished loading.
151 * (Ticket's LoadingSucceeded callback was called)
152 * Reflects the last cached values after a LoadComplete.
153 * If requested width or height was 0, they are replaced by concrete dimensions.
154 * @return a copy of the attributes
156 const Dali::ImageAttributes& GetAttributes() const;
159 * Get the width of the image.
160 * Only to be used after the image has finished loading.
161 * (Ticket's LoadingSucceeded callback was called)
162 * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
163 * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
164 * @pre image should be loaded
166 unsigned int GetWidth() const;
169 * Get the height of the image.
170 * Only to be used after the image has finished loading.
171 * (Ticket's LoadingSucceeded callback was called)
172 * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
173 * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
174 * @pre image should be loaded
176 unsigned int GetHeight() const;
179 * Return the natural size of the image.
180 * This is the size that the loaded image will take
182 Vector2 GetNaturalSize() const;
185 * @copydoc Dali::Image::GetFilename()
187 const std::string& GetFilename() const;
190 * @copydoc Dali::Image::Reload()
194 public: // From ResourceTicketObserver
197 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
199 virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
202 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
204 virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
207 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
209 virtual void ResourceUploaded(const ResourceTicket& ticket);
212 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
214 virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
217 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
219 virtual void ResourceSavingFailed( const ResourceTicket& ticket );
224 * Indicates that the image is used.
226 virtual void Connect();
229 * Indicates that the image is not used anymore.
231 virtual void Disconnect();
235 * Second stage initialization
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.
246 void SetTicket( ResourceTicket* ticket );
249 * Helper method to determine if the filename indicates that the image has a 9 patch border.
250 * @param[in] filename The filename to check
251 * @return true if it is a 9 patch image
253 static bool IsNinePatchFileName( std::string filename );
258 unsigned int mHeight;
260 ResourceTicketPtr mTicket;
261 ImageFactoryCache::RequestPtr mRequest; ///< contains the initially requested attributes for image. Request is reissued when memory was released.
262 LoadPolicy mLoadPolicy;
263 ReleasePolicy mReleasePolicy;
265 unsigned int mConnectionCount; ///< number of on-stage objects using this image
266 ImageFactory& mImageFactory;
269 Dali::Image::ImageSignalV2 mLoadingFinishedV2;
270 Dali::Image::ImageSignalV2 mUploadedV2;
272 // Changes scope, should be at end of class
273 DALI_LOG_OBJECT_STRING_DECLARATION;
276 } // namespace Internal
279 * Helper methods for public API.
281 inline Internal::Image& GetImplementation(Dali::Image& image)
283 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
285 BaseObject& handle = image.GetBaseObject();
287 return static_cast<Internal::Image&>(handle);
290 inline const Internal::Image& GetImplementation(const Dali::Image& image)
292 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
294 const BaseObject& handle = image.GetBaseObject();
296 return static_cast<const Internal::Image&>(handle);
300 #endif // __DALI_INTERNAL_IMAGE_H__