1 #ifndef __DALI_INTERNAL_RESOURCE_IMAGE_H__
2 #define __DALI_INTERNAL_RESOURCE_IMAGE_H__
5 * Copyright (c) 2015 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/resource-image.h>
26 #include <dali/internal/event/images/image-impl.h>
27 #include <dali/internal/event/images/image-factory-cache.h>
28 #include <dali/integration-api/debug.h> // For DALI_LOG_OBJECT_STRING_DECLARATION
36 typedef Dali::ResourceImage::LoadPolicy LoadPolicy;
38 const LoadPolicy IMAGE_LOAD_POLICY_DEFAULT = Dali::ResourceImage::IMMEDIATE;
41 typedef IntrusivePtr<ResourceImage> ResourceImagePtr;
44 * ResourceImage is an image loaded using a URL, it is an image resource that can be added to actors etc.
46 class ResourceImage : public Image
51 * Creates a pointer to an uninitialized Image object.
52 * @return a pointer to a newly created object.
54 static ResourceImagePtr New();
57 * Creates object and loads image from filesystem
58 * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
59 * @param [in] url The URL of the image file.
60 * @param [in] attributes requested parameters for loading (size, scaling etc.)
61 * if width or height is specified as 0, the natural size will be used.
62 * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
63 * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
64 * @return a pointer to a newly created object.
66 static ResourceImagePtr New( const std::string& url,
67 const ImageAttributes& attributes,
68 LoadPolicy loadPol = IMAGE_LOAD_POLICY_DEFAULT,
69 ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
72 * @copydoc Dali::ResourceImage::GetLoadingState()
74 Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
77 * @copydoc Dali::ResourceImage::GetLoadPolicy()
79 LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
82 * @copydoc Dali::ResourceImage::LoadingFinishedSignal()
84 Dali::ResourceImage::ResourceImageSignal& LoadingFinishedSignal() { return mLoadingFinished; }
87 * Connects a callback function with the object's signals.
88 * @param[in] object The object providing the signal.
89 * @param[in] tracker Used to disconnect the signal.
90 * @param[in] signalName The signal to connect to.
91 * @param[in] functor A newly allocated FunctorDelegate.
92 * @return True if the signal was connected.
93 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
95 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
98 * Get the attributes of the image.
99 * Only to be used after the image has finished loading.
100 * (Ticket's LoadingSucceeded callback was called)
101 * Reflects the last cached values after a LoadComplete.
102 * If requested width or height was 0, they are replaced by concrete dimensions.
103 * @return a copy of the attributes
105 const ImageAttributes& GetAttributes() const;
108 * @copydoc Dali::ResourceImage::GetUrl()
110 const std::string& GetUrl() const;
113 * @copydoc Dali::ResourceImage::Reload()
118 * @copydoc Dali::Image::GetWidth()
120 virtual unsigned int GetWidth() const;
123 * @copydoc Dali::Image::GetWidth()
125 virtual unsigned int GetHeight() const;
128 * @copydoc Dali::Internal::Image::GetNaturalSize()
130 virtual Vector2 GetNaturalSize() const;
133 * Indicates that the image is used.
135 virtual void Connect();
138 * Indicates that the image is not used anymore.
140 virtual void Disconnect();
142 public: // From ResourceTicketObserver
145 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
147 virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
150 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
152 virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
157 * A reference counted object may only be deleted by calling Unreference()
159 virtual ~ResourceImage();
162 * Constructor, with default parameters
164 ResourceImage( LoadPolicy loadPol = IMAGE_LOAD_POLICY_DEFAULT, ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
169 * Helper method to determine if the filename indicates that the image has a 9 patch border.
170 * @param [in] url The URL of the image file.
171 * @return true if it is a 9 patch image
173 static bool IsNinePatch( const std::string& url );
176 * Helper method to set new resource ticket. Stops observing current ticket if any, and starts observing
177 * the new one or just resets the intrusive pointer.
178 * @param[in] ticket pointer to new resource Ticket or NULL.
180 void SetTicket( ResourceTicket* ticket );
184 ImageFactory& mImageFactory;
186 ImageFactoryCache::RequestPtr mRequest; ///< contains the initially requested attributes for image. Request is reissued when memory was released.
188 Dali::ResourceImage::ResourceImageSignal mLoadingFinished;
190 LoadPolicy mLoadPolicy : 2; ///< 2 bits is enough space
192 // Changes scope, should be at end of class
193 DALI_LOG_OBJECT_STRING_DECLARATION;
196 } // namespace Internal
199 * Helper methods for public API.
201 inline Internal::ResourceImage& GetImplementation(Dali::ResourceImage& image)
203 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
205 BaseObject& handle = image.GetBaseObject();
207 return static_cast<Internal::ResourceImage&>(handle);
210 inline const Internal::ResourceImage& GetImplementation(const Dali::ResourceImage& image)
212 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
214 const BaseObject& handle = image.GetBaseObject();
216 return static_cast<const Internal::ResourceImage&>(handle);
220 #endif // __DALI_INTERNAL_RESOURCE_IMAGE_H__