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