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