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()
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.
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 Image* New(const std::string& filename,
88 const Dali::ImageAttributes& attributes=Dali::ImageAttributes::DEFAULT_ATTRIBUTES,
89 LoadPolicy loadPol=ImageLoadPolicyDefault,
90 ReleasePolicy releasePol=ImageReleasePolicyDefault);
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.
105 static Image* New(NativeImage& nativeImg,
106 LoadPolicy loadPol=ImageLoadPolicyDefault,
107 ReleasePolicy releasePol=ImageReleasePolicyDefault);
110 * @copydoc Dali::Image::GetLoadingState()
112 Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
115 * @copydoc Dali::Image::GetLoadPolicy()
117 LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
120 * @copydoc Dali::Image::GetReleasePolicy()
122 ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
125 * @copydoc Dali::Image::LoadingFinishedSignal()
127 Dali::Image::ImageSignalV2& LoadingFinishedSignal() { return mLoadingFinishedV2; }
130 * @copydoc Dali::Image::UploadedSignal()
132 Dali::Image::ImageSignalV2& UploadedSignal() { return mUploadedV2; }
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.
143 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
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.
150 ResourceId GetResourceId() const;
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
160 const Dali::ImageAttributes& GetAttributes() const;
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
170 unsigned int GetWidth() const;
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
180 unsigned int GetHeight() const;
183 * Return the natural size of the image.
184 * This is the size that the loaded image will take
186 Vector2 GetNaturalSize() const;
189 * @copydoc Dali::Image::GetFilename()
191 const std::string& GetFilename() const;
194 * @copydoc Dali::Image::Reload()
198 public: // From ResourceTicketObserver
201 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
203 virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
206 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
208 virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
211 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
213 virtual void ResourceUploaded(const ResourceTicket& ticket);
216 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
218 virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
221 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
223 virtual void ResourceSavingFailed( const ResourceTicket& ticket );
228 * Indicates that the image is used.
230 virtual void Connect();
233 * Indicates that the image is not used anymore.
235 virtual void Disconnect();
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.
244 void SetTicket( ResourceTicket* ticket );
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
251 static bool IsNinePatchFileName( std::string filename );
256 unsigned int mHeight;
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;
263 unsigned int mConnectionCount; ///< number of on-stage objects using this image
264 ImageFactory& mImageFactory;
267 Dali::Image::ImageSignalV2 mLoadingFinishedV2;
268 Dali::Image::ImageSignalV2 mUploadedV2;
270 // Changes scope, should be at end of class
271 DALI_LOG_OBJECT_STRING_DECLARATION;
274 } // namespace Internal
277 * Helper methods for public API.
279 inline Internal::Image& GetImplementation(Dali::Image& image)
281 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
283 BaseObject& handle = image.GetBaseObject();
285 return static_cast<Internal::Image&>(handle);
288 inline const Internal::Image& GetImplementation(const Dali::Image& image)
290 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
292 const BaseObject& handle = image.GetBaseObject();
294 return static_cast<const Internal::Image&>(handle);
298 #endif // __DALI_INTERNAL_IMAGE_H__