Merge "DALi Version 1.0.30" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / images / image-impl.h
1 #ifndef __DALI_INTERNAL_IMAGE_H__
2 #define __DALI_INTERNAL_IMAGE_H__
3
4 /*
5  * Copyright (c) 2014 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/image.h>
26 #include <dali/public-api/images/native-image.h>
27 #include <dali/public-api/object/base-object.h>
28 #include <dali/internal/event/resources/resource-client.h>
29 #include <dali/internal/event/resources/resource-ticket-observer.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 typedef Dali::Image::ReleasePolicy ReleasePolicy;
38
39 class Image;
40 class ImageFactory;
41 typedef IntrusivePtr<Image> ImagePtr;
42
43 const ReleasePolicy IMAGE_RELEASE_POLICY_DEFAULT = Dali::Image::NEVER;
44
45 /**
46  * Image represents an image resource that can be added to actors etc.
47  * When the Image object is created, resource loading will be attempted.
48  * Provided this is successful, the resource will exist until the Image is destroyed.
49  */
50 class Image : public BaseObject, public ResourceTicketObserver
51 {
52 public:
53
54   /**
55    * Creates object with already loaded NativeImage
56    * the maximum size of the image is limited by GL_MAX_TEXTURE_SIZE
57    * @pre nativeImg should be initialised
58    * @param [in] nativeImg already initialised NativeImage
59    * @return a pointer to a newly created object.
60    */
61   static ImagePtr New( NativeImage& nativeImg );
62
63   /**
64    * @copydoc Dali::Image::GetReleasePolicy()
65    */
66   ReleasePolicy GetReleasePolicy () const { return mReleasePolicy; }
67
68   /**
69    * @copydoc Dali::Image::UploadedSignal()
70    */
71   Dali::Image::ImageSignalType& UploadedSignal() { return mUploaded; }
72
73   /**
74    * Connects a callback function with the object's signals.
75    * @param[in] object The object providing the signal.
76    * @param[in] tracker Used to disconnect the signal.
77    * @param[in] signalName The signal to connect to.
78    * @param[in] functor A newly allocated FunctorDelegate.
79    * @return True if the signal was connected.
80    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
81    */
82   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
83
84   /**
85    * returns the Id used for lookups
86    * @note if LoadPolicy::OnDemand is used and Image is off Stage, this will return 0.
87    * @return the unique ID of the image data resource. This is actually also the same as Dali Texture id.
88    */
89   ResourceId GetResourceId() const;
90
91   /**
92    * Get the width of the image.
93    * Only to be used after the image has finished loading.
94    * (Ticket's LoadingSucceeded callback was called)
95    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
96    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
97    * @pre image should be loaded
98    */
99   virtual unsigned int GetWidth() const;
100
101   /**
102    * Get the height of the image.
103    * Only to be used after the image has finished loading.
104    * (Ticket's LoadingSucceeded callback was called)
105    * The returned value will reflect the true image dimensions once the asynchronous loading has finished.
106    * Connect to SignalLoadingFinished or use GetLoadingState to make sure this value is actual.
107    * @pre image should be loaded
108    */
109   virtual unsigned int GetHeight() const;
110
111   /**
112    * Return the natural size of the image.
113    * This is the size that the loaded image will take
114    */
115   virtual Vector2 GetNaturalSize() const;
116
117 public: // From ResourceTicketObserver
118
119   /**
120    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingFailed()
121    */
122   virtual void ResourceLoadingFailed(const ResourceTicket& ticket);
123
124   /**
125    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceLoadingSucceeded()
126    */
127   virtual void ResourceLoadingSucceeded(const ResourceTicket& ticket);
128
129   /**
130    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceUploaded()
131    */
132   virtual void ResourceUploaded(const ResourceTicket& ticket);
133
134   /**
135    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingSucceeded()
136    */
137   virtual void ResourceSavingSucceeded( const ResourceTicket& ticket );
138
139   /**
140    * @copydoc Dali::Internal::ResourceTicketObserver::ResourceSavingFailed()
141    */
142   virtual void ResourceSavingFailed( const ResourceTicket& ticket );
143
144 public:
145
146   /**
147    * Indicates that the image is used.
148    */
149   virtual void Connect() {}
150
151   /**
152    * Indicates that the image is not used anymore.
153    */
154   virtual void Disconnect() {}
155
156 protected:
157
158   /**
159    * A reference counted object may only be deleted by calling Unreference()
160    */
161   virtual ~Image();
162
163   /**
164    * Constructor, with default parameters
165    */
166   Image( ReleasePolicy releasePol = IMAGE_RELEASE_POLICY_DEFAULT );
167
168   /**
169    * Second stage initialization
170    */
171   void Initialize();
172
173 protected:
174
175   ResourceTicketPtr mTicket;              ///< smart pointer to the ticket object that gets completed when load finishes
176
177   mutable unsigned int mWidth;     ///< natural width of the image, needs to be mutable for lazy resolving and as the API for GetWidth is const
178   mutable unsigned int mHeight;    ///< natural height of the image, needs to be mutable for lazy resolving and as the API for GetHeight is const
179
180   unsigned int mConnectionCount; ///< number of on-stage objects using this image
181
182   ReleasePolicy  mReleasePolicy : 2; ///< 2 bits is enough space
183
184 private:
185
186   Dali::Image::ImageSignalType mUploaded;
187 };
188
189 } // namespace Internal
190
191 /**
192  * Helper methods for public API.
193  */
194 inline Internal::Image& GetImplementation(Dali::Image& image)
195 {
196   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
197
198   BaseObject& handle = image.GetBaseObject();
199
200   return static_cast<Internal::Image&>(handle);
201 }
202
203 inline const Internal::Image& GetImplementation(const Dali::Image& image)
204 {
205   DALI_ASSERT_ALWAYS( image && "Image handle is empty" );
206
207   const BaseObject& handle = image.GetBaseObject();
208
209   return static_cast<const Internal::Image&>(handle);
210 }
211
212 } // namespace Dali
213 #endif // __DALI_INTERNAL_IMAGE_H__