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