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>
35 typedef Dali::ResourceImage::LoadPolicy LoadPolicy;
37 const LoadPolicy IMAGE_LOAD_POLICY_DEFAULT = Dali::ResourceImage::IMMEDIATE;
40 typedef IntrusivePtr<ResourceImage> ResourceImagePtr;
43 * ResourceImage is an image loaded using a URL, it is an image resource that can be added to actors etc.
45 class ResourceImage : public Image
50 * Creates a pointer to an uninitialized Image object.
51 * @return a pointer to a newly created object.
53 static ResourceImagePtr New();
56 * Creates object and loads image from filesystem
57 * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
58 * @param [in] url The URL of the image file.
59 * @param [in] attributes requested parameters for loading (size, scaling etc.)
60 * if width or height is specified as 0, the natural size will be used.
61 * @param [in] loadPol controls time of loading a resource from the filesystem (default: load when Image is created).
62 * @param [in] releasePol optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
63 * @return a pointer to a newly created object.
65 static ResourceImagePtr New( const std::string& url,
66 const Dali::ImageAttributes& attributes,
67 LoadPolicy loadPol = IMAGE_LOAD_POLICY_DEFAULT,
68 ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
71 * @copydoc Dali::ResourceImage::GetLoadingState()
73 Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
76 * @copydoc Dali::ResourceImage::GetLoadPolicy()
78 LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
81 * @copydoc Dali::ResourceImage::LoadingFinishedSignal()
83 Dali::ResourceImage::ResourceImageSignal& LoadingFinishedSignal() { return mLoadingFinished; }
86 * Connects a callback function with the object's signals.
87 * @param[in] object The object providing the signal.
88 * @param[in] tracker Used to disconnect the signal.
89 * @param[in] signalName The signal to connect to.
90 * @param[in] functor A newly allocated FunctorDelegate.
91 * @return True if the signal was connected.
92 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
94 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
97 * Get the attributes of the image.
98 * Only to be used after the image has finished loading.
99 * (Ticket's LoadingSucceeded callback was called)
100 * Reflects the last cached values after a LoadComplete.
101 * If requested width or height was 0, they are replaced by concrete dimensions.
102 * @return a copy of the attributes
104 const Dali::ImageAttributes& GetAttributes() const;
107 * @copydoc Dali::ResourceImage::GetUrl()
109 const std::string& GetUrl() const;
112 * @copydoc Dali::ResourceImage::Reload()
117 * @copydoc Dali::Image::GetWidth()
119 virtual unsigned int GetWidth() const;
122 * @copydoc Dali::Image::GetWidth()
124 virtual unsigned int GetHeight() const;
127 * @copydoc Dali::Internal::Image::GetNaturalSize()
129 virtual Vector2 GetNaturalSize() const;
132 * Indicates that the image is used.
134 virtual void Connect();
137 * Indicates that the image is not used anymore.
139 virtual void Disconnect();
141 public: // From ResourceTicketObserver
144 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
146 virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
149 * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
151 virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
156 * A reference counted object may only be deleted by calling Unreference()
158 virtual ~ResourceImage();
161 * Constructor, with default parameters
163 ResourceImage( LoadPolicy loadPol = IMAGE_LOAD_POLICY_DEFAULT, ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
168 * Helper method to determine if the filename indicates that the image has a 9 patch border.
169 * @param [in] url The URL of the image file.
170 * @return true if it is a 9 patch image
172 static bool IsNinePatch( const std::string& url );
175 * Helper method to set new resource ticket. Stops observing current ticket if any, and starts observing
176 * the new one or just resets the intrusive pointer.
177 * @param[in] ticket pointer to new resource Ticket or NULL.
179 void SetTicket( ResourceTicket* ticket );
183 ImageFactory& mImageFactory;
185 ImageFactoryCache::RequestPtr mRequest; ///< contains the initially requested attributes for image. Request is reissued when memory was released.
187 Dali::ResourceImage::ResourceImageSignal mLoadingFinished;
189 LoadPolicy mLoadPolicy : 2; ///< 2 bits is enough space
191 // Changes scope, should be at end of class
192 DALI_LOG_OBJECT_STRING_DECLARATION;
195 } // namespace Internal
198 * Helper methods for public API.
200 inline Internal::ResourceImage& GetImplementation(Dali::ResourceImage& image)
202 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
204 BaseObject& handle = image.GetBaseObject();
206 return static_cast<Internal::ResourceImage&>(handle);
209 inline const Internal::ResourceImage& GetImplementation(const Dali::ResourceImage& image)
211 DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
213 const BaseObject& handle = image.GetBaseObject();
215 return static_cast<const Internal::ResourceImage&>(handle);
219 #endif // __DALI_INTERNAL_RESOURCE_IMAGE_H__