b9186355b023b88b92320b910199c7748140ab22
[platform/core/uifw/dali-core.git] / dali / internal / event / images / bitmap-image-impl.h
1 #ifndef __DALI_INTERNAL_BITMAP_IMAGE_H__
2 #define __DALI_INTERNAL_BITMAP_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 // INTERNAL INCLUDES
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/internal/event/images/image-impl.h>
24 #include <dali/public-api/images/image.h>
25 #include <dali/public-api/images/bitmap-image.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 class BitmapImage;
34 typedef IntrusivePtr<BitmapImage> BitmapImagePtr;
35
36 class ResourceClient;
37 class ResourceManager;
38
39 /**
40  * BitmapImage represents an image resource that can be added to actors etc.
41  * Its pixel buffer data is provided by the application developer.
42  * Pixel buffer memory allocation can be handled by dali or application.
43  */
44 class BitmapImage : public Image
45 {
46 public:
47   /**
48    * Create a new BitmapImage.
49    * Also a pixel buffer for image data is allocated.
50    * Dali has ownership of the buffer.
51    * For better performance and portability use power of two dimensions.
52    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
53    * @param [in] width       image width in pixels
54    * @param [in] height      image height in pixels
55    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
56    * @param [in] loadPol     controls time of loading a resource from the filesystem (default: load when Image is created).
57    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
58    */
59   static BitmapImagePtr New( unsigned int width,
60                              unsigned int height,
61                              Pixel::Format pixelformat,
62                              LoadPolicy loadPol=ImageLoadPolicyDefault,
63                              ReleasePolicy releasePol=ImageReleasePolicyDefault);
64
65   /**
66    * Create a new BitmapImage, which uses external data source.
67    * Pixel buffer has to be allocated by application.
68    * Application holds ownership of the buffer.
69    * For better performance and portability use power of two dimensions.
70    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
71    * @note  in case releasePol is "OffStage", application has to call Image::BufferUpdated() whenever image is re-added to the stage
72    * @param [in] pixBuf      pixel buffer. has to be allocated by application.
73    * @param [in] width       image width in pixels
74    * @param [in] height      image height in pixels
75    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
76    * @param [in] stride      the internal stride of the pixelbuffer in pixels
77    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
78    */
79   static BitmapImagePtr New( PixelBuffer* pixBuf,
80                              unsigned int width,
81                              unsigned int height,
82                              Pixel::Format pixelformat,
83                              unsigned int stride,
84                              ReleasePolicy releasePol=ImageReleasePolicyDefault );
85
86   /**
87    * Create a new BitmapImage.
88    * Also a pixel buffer for image data is allocated.
89    * Dali has ownership of the buffer.
90    * For better performance use power of two dimensions.
91    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
92    * @param [in] width image width in pixels
93    * @param [in] height image height in pixels
94    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
95    * @param [in] loadPol optionally delay loading until it used (default: immediate loading)
96    * @param [in] releasePol optionally release memory when image is not visible on screen (default: keep image data until Image object is alive).
97    */
98   BitmapImage(unsigned int width,
99               unsigned int height,
100               Pixel::Format pixelformat,
101               LoadPolicy loadPol=ImageLoadPolicyDefault,
102               ReleasePolicy releasePol=ImageReleasePolicyDefault);
103
104   /**
105    * Create a new BitmapImage, which uses external data source.
106    * Pixel buffer has to be allocated by application.
107    * Application holds ownership of the buffer.
108    * For better performance and portability use power of two dimensions.
109    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
110    * @note  in case releasePol is "OffStage", application has to call Image::BufferUpdated() whenever image is re-added to the stage
111    * @param [in] pixBuf      pixel buffer. has to be allocated by application.
112    * @param [in] width       image width in pixels
113    * @param [in] height      image height in pixels
114    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
115    * @param [in] stride      the internal stride of the pixelbuffer in pixels
116    * @param [in] releasePol  optionally relase memory when image is not visible on screen (default: keep image data until Image object is alive).
117    */
118   BitmapImage(PixelBuffer* pixBuf,
119               unsigned int width,
120               unsigned int height,
121               Pixel::Format pixelformat,
122               unsigned int stride,
123               ReleasePolicy releasePol=ImageReleasePolicyDefault);
124
125 protected:
126   /**
127    * A reference counted object may only be deleted by calling Unreference()
128    */
129   virtual ~BitmapImage();
130
131 public:
132   /**
133    * Notify Dali that the contents of the buffer have changed.
134    * @param [in] updateArea area that has changed in buffer. An empty rect means the whole buffer has changed.
135    */
136   void Update (RectArea& updateArea);
137
138   /**
139    * @copydoc Dali::BitmapImage::IsDataExternal
140    */
141   bool IsDataExternal() const;
142
143   /**
144    * Returns the pixel buffer of the Image.
145    * The application developer can write to the buffer.
146    * Upload the modified contents with BufferUpdated.
147    * @return the pixel buffer
148    */
149   PixelBuffer* GetBuffer();
150
151   /**
152    * Returns buffer size in bytes.
153    * @return the buffer size in bytes
154    */
155   unsigned int GetBufferSize() const;
156
157   /**
158    * Returns buffer stride (in bytes).
159    * @return the buffer stride
160    */
161   unsigned int GetBufferStride() const;
162
163   /**
164    * Get the pixel format
165    * @return The pixel format
166    */
167   Pixel::Format GetPixelFormat() const;
168
169 protected: // From Resource
170   /**
171    * @copydoc Dali::Internal::Image::Connect
172    */
173   virtual void Connect();
174
175   /**
176    * @copydoc Dali::Internal::Image::Disconnect
177    */
178   virtual void Disconnect();
179
180   /**
181    * Get the bitmap from local cache or ticket.
182    **/
183   Integration::Bitmap * GetBitmap() const;
184
185 private:
186   bool mIsDataExternal; ///< whether application holds ownership of pixel buffer or not
187
188   ResourceClient*            mResourceClient;
189
190 protected:
191   Integration::BitmapPtr     mBitmapCached;
192 };
193
194 } // namespace Internal
195
196 /**
197  * Helper methods for public API.
198  */
199 inline Internal::BitmapImage& GetImplementation(Dali::BitmapImage& image)
200 {
201   DALI_ASSERT_ALWAYS( image && "BitmapImage handle is empty" );
202
203   BaseObject& handle = image.GetBaseObject();
204
205   return static_cast<Internal::BitmapImage&>(handle);
206 }
207
208 inline const Internal::BitmapImage& GetImplementation(const Dali::BitmapImage& image)
209 {
210   DALI_ASSERT_ALWAYS( image && "BitmapImage handle is empty" );
211
212   const BaseObject& handle = image.GetBaseObject();
213
214   return static_cast<const Internal::BitmapImage&>(handle);
215 }
216
217 } // namespace Dali
218
219 #endif // __DALI_INTERNAL_BITMAP_IMAGE_H__