ResourceImage/Image split
[platform/core/uifw/dali-core.git] / dali / internal / event / images / resource-image-impl.h
1 #ifndef __DALI_INTERNAL_RESOURCE_IMAGE_H__
2 #define __DALI_INTERNAL_RESOURCE_IMAGE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
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
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 typedef Dali::ResourceImage::LoadPolicy LoadPolicy;
36
37 const LoadPolicy IMAGE_LOAD_POLICY_DEFAULT = Dali::ResourceImage::IMMEDIATE;
38
39 class ResourceImage;
40 typedef IntrusivePtr<ResourceImage> ResourceImagePtr;
41
42 /**
43  * ResourceImage is an image loaded using a URL, it is an image resource that can be added to actors etc.
44  */
45 class ResourceImage : public Image
46 {
47 public:
48
49   /**
50    * Creates a pointer to an uninitialized Image object.
51    * @return a pointer to a newly created object.
52    */
53   static ResourceImagePtr New();
54
55   /**
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.
64    */
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 );
69
70   /**
71    * @copydoc Dali::ResourceImage::GetLoadingState()
72    */
73   Dali::LoadingState GetLoadingState() const { return mTicket ? mTicket->GetLoadingState() : ResourceLoading; }
74
75   /**
76    * @copydoc Dali::ResourceImage::GetLoadPolicy()
77    */
78   LoadPolicy GetLoadPolicy () const { return mLoadPolicy; }
79
80   /**
81    * @copydoc Dali::ResourceImage::LoadingFinishedSignal()
82    */
83   Dali::ResourceImage::ResourceImageSignal& LoadingFinishedSignal() { return mLoadingFinished; }
84
85   /**
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.
93    */
94   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
95
96   /**
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
103    */
104   const Dali::ImageAttributes& GetAttributes() const;
105
106   /**
107    * @copydoc Dali::ResourceImage::GetUrl()
108    */
109   const std::string& GetUrl() const;
110
111   /**
112    * @copydoc Dali::ResourceImage::Reload()
113    */
114   void Reload();
115
116   /**
117    * @copydoc Dali::Image::GetWidth()
118    */
119   virtual unsigned int GetWidth() const;
120
121   /**
122    * @copydoc Dali::Image::GetWidth()
123    */
124   virtual unsigned int GetHeight() const;
125
126   /**
127    * @copydoc Dali::Internal::Image::GetNaturalSize()
128    */
129   virtual Vector2 GetNaturalSize() const;
130
131   /**
132    * Indicates that the image is used.
133    */
134   virtual void Connect();
135
136   /**
137    * Indicates that the image is not used anymore.
138    */
139   virtual void Disconnect();
140
141 public: // From ResourceTicketObserver
142
143   /**
144    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
145    */
146   virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
147
148   /**
149    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
150    */
151   virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
152
153 protected:
154
155   /**
156    * A reference counted object may only be deleted by calling Unreference()
157    */
158   virtual ~ResourceImage();
159
160   /**
161    * Constructor, with default parameters
162    */
163   ResourceImage( LoadPolicy loadPol = IMAGE_LOAD_POLICY_DEFAULT, ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
164
165 private:
166
167   /**
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
171    */
172   static bool IsNinePatch( const std::string& url );
173
174   /**
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.
178    */
179   void SetTicket( ResourceTicket* ticket );
180
181 private:
182
183   ImageFactory& mImageFactory;
184
185   ImageFactoryCache::RequestPtr mRequest; ///< contains the initially requested attributes for image. Request is reissued when memory was released.
186
187   Dali::ResourceImage::ResourceImageSignal mLoadingFinished;
188
189   LoadPolicy mLoadPolicy : 2;    ///< 2 bits is enough space
190
191   // Changes scope, should be at end of class
192   DALI_LOG_OBJECT_STRING_DECLARATION;
193 };
194
195 } // namespace Internal
196
197 /**
198  * Helper methods for public API.
199  */
200 inline Internal::ResourceImage& GetImplementation(Dali::ResourceImage& image)
201 {
202   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
203
204   BaseObject& handle = image.GetBaseObject();
205
206   return static_cast<Internal::ResourceImage&>(handle);
207 }
208
209 inline const Internal::ResourceImage& GetImplementation(const Dali::ResourceImage& image)
210 {
211   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
212
213   const BaseObject& handle = image.GetBaseObject();
214
215   return static_cast<const Internal::ResourceImage&>(handle);
216 }
217
218 } // namespace Dali
219 #endif // __DALI_INTERNAL_RESOURCE_IMAGE_H__