1 #ifndef __DALI_INTERNAL_IMAGE_H__
2 #define __DALI_INTERNAL_IMAGE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
42 typedef Dali::Image::LoadPolicy LoadPolicy;
43 typedef Dali::Image::ReleasePolicy ReleasePolicy;
47 typedef IntrusivePtr<Image> ImagePtr;
49 const LoadPolicy ImageLoadPolicyDefault = Dali::Image::Immediate;
50 const ReleasePolicy ImageReleasePolicyDefault = Dali::Image::Never;
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.
57 class Image : public BaseObject, public ResourceTicketObserver
62 * A reference counted object may only be deleted by calling Unreference()
67 * Constructor, with default parameters
69 Image(LoadPolicy loadPol=ImageLoadPolicyDefault, ReleasePolicy releasePol=ImageReleasePolicyDefault);
73 * Creates a pointer to an uninitialized Image object.
74 * @return a pointer to a newly created object.
76 static ImagePtr New();
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, scaling 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.
88 static ImagePtr New( const std::string& filename,
89 const Dali::ImageAttributes& attributes,
90 LoadPolicy loadPol=ImageLoadPolicyDefault,
91 ReleasePolicy releasePol=ImageReleasePolicyDefault );
94 * Creates object with already loaded NativeImage
95 * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
96 * @pre nativeImg should be initialised
97 * @param [in] nativeImg already initialised NativeImage
98 * @return a pointer to a newly created object.
100 static ImagePtr New( NativeImage& nativeImg );
103 * @copydoc Dali::Image::GetLoadingState()
105 Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
108 * @copydoc Dali::Image::GetLoadPolicy()
110 LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
113 * @copydoc Dali::Image::GetReleasePolicy()
115 ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
118 * @copydoc Dali::Image::LoadingFinishedSignal()
120 Dali::Image::ImageSignalV2& LoadingFinishedSignal() { return mLoadingFinishedV2; }
123 * @copydoc Dali::Image::UploadedSignal()
125 Dali::Image::ImageSignalV2& UploadedSignal() { return mUploadedV2; }
128 * Connects a callback function with the object's signals.
129 * @param[in] object The object providing the signal.
130 * @param[in] tracker Used to disconnect the signal.
131 * @param[in] signalName The signal to connect to.
132 * @param[in] functor A newly allocated FunctorDelegate.
133 * @return True if the signal was connected.
134 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
136 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
139 * returns the Id used for lookups
140 * @note if LoadPolicy::OnDemand is used and Image is off Stage, this will return 0.
141 * @return the unique ID of the image data resource. This is actually also the same as Dali Texture id.
143 ResourceId GetResourceId() const;
146 * Get the attributes of the image.
147 * Only to be used after the image has finished loading.
148 * (Ticket's LoadingSucceeded callback was called)
149 * Reflects the last cached values after a LoadComplete.
150 * If requested width or height was 0, they are replaced by concrete dimensions.
151 * @return a copy of the attributes
153 const Dali::ImageAttributes& GetAttributes() const;
156 * Get the width of the image.
157 * Only to be used after the image has finished loading.
158 * (Ticket's LoadingSucceeded callback was called)
159 * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
160 * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
161 * @pre image should be loaded
163 unsigned int GetWidth() const;
166 * Get the height of the image.
167 * Only to be used after the image has finished loading.
168 * (Ticket's LoadingSucceeded callback was called)
169 * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
170 * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
171 * @pre image should be loaded
173 unsigned int GetHeight() const;
176 * Return the natural size of the image.
177 * This is the size that the loaded image will take
179 Vector2 GetNaturalSize() const;
182 * @copydoc Dali::Image::GetFilename()
184 const std::string& GetFilename() const;
187 * @copydoc Dali::Image::Reload()
191 public: // From ResourceTicketObserver
194 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
196 virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
199 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
201 virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
204 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
206 virtual void ResourceUploaded(const ResourceTicket& ticket);
209 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
211 virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
214 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
216 virtual void ResourceSavingFailed( const ResourceTicket& ticket );
221 * Indicates that the image is used.
223 virtual void Connect();
226 * Indicates that the image is not used anymore.
228 virtual void Disconnect();
233 * Second stage initialization
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( const std::string& filename );
253 protected: //@TODO these should not be protected
255 ImageFactory& mImageFactory;
257 ImageFactoryCache::RequestPtr mRequest; ///< contains the initially requested attributes for image. Request is reissued when memory was released.
258 ResourceTicketPtr mTicket; ///< smart pointer to the ticket object that gets completed when load finishes
260 mutable unsigned int mWidth; ///< natural width of the image, needs to be mutable for lazy resolving and as the API for GetWidth is const
261 mutable unsigned int mHeight; ///< natural height of the image, needs to be mutable for lazy resolving and as the API for GetHeight is const
263 unsigned int mConnectionCount; ///< number of on-stage objects using this image
265 LoadPolicy mLoadPolicy:2; ///< 2 bits is enough space
266 ReleasePolicy mReleasePolicy:2; ///< 2 bits is enough space
270 Dali::Image::ImageSignalV2 mLoadingFinishedV2;
271 Dali::Image::ImageSignalV2 mUploadedV2;
273 // Changes scope, should be at end of class
274 DALI_LOG_OBJECT_STRING_DECLARATION;
277 } // namespace Internal
280 * Helper methods for public API.
282 inline Internal::Image& GetImplementation(Dali::Image& image)
284 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
286 BaseObject& handle = image.GetBaseObject();
288 return static_cast<Internal::Image&>(handle);
291 inline const Internal::Image& GetImplementation(const Dali::Image& image)
293 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
295 const BaseObject& handle = image.GetBaseObject();
297 return static_cast<const Internal::Image&>(handle);
301 #endif // __DALI_INTERNAL_IMAGE_H__