2f32ec7173fd7aef3967094b84ebc2aad7dd6c17
[platform/core/uifw/dali-core.git] / dali / public-api / images / bitmap-image.h
1 #ifndef __DALI_BITMAP_IMAGE_H__
2 #define __DALI_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/common/vector-wrapper.h>
23 #include <dali/public-api/images/image.h>
24 #include <dali/public-api/math/rect.h>
25
26 namespace Dali
27 {
28
29 namespace Internal DALI_INTERNAL
30 {
31 class BitmapImage;
32 }
33
34 typedef unsigned char         PixelBuffer;  ///< pixel data buffer
35 typedef Rect<unsigned int>    RectArea;     ///< rectangular area (x,y,w,h)
36
37
38 /**
39  * @brief BitmapImage represents an image resource that can be added to ImageActors.
40  * Its pixel buffer data is provided by the application developer.
41  *
42  * Care should be taken with pixel data allocated by the application,
43  * as the data is copied to GL both when the image is added to the
44  * stage and after a call to Update().  In both of these cases, a
45  * SignalUploaded will be sent to the application confirming that the
46  * operation has completed.
47  *
48  * The application can free the pixel data after receiving a
49  * SignalUploaded.
50  *
51  * Similarly, once the image is on stage (i.e. it's being used by an
52  * ImageActor that is on stage), the application should only write to
53  * the buffer after receiving a SignalUploaded, then call Update()
54  * once the write is finished. This avoids the pixel data being changed
55  * whilst it's being copied to GL. Writing to the buffer without waiting
56  * for the signal will likely result in visible tearing.
57  *
58  * If the pixel format of the pixel buffer contains an alpha channel,
59  * then the image is considered to be have transparent pixels without
60  * regard for the actual content of the channel, and will be blended.
61  *
62  * If the image is opaque and blending is not required, then the user
63  * should call ImageActor::SetUseImageAlpha(false) on the containing actor.
64  */
65 class DALI_IMPORT_API BitmapImage : public Image
66 {
67 public:
68   /**
69    * @brief Constructor which creates an uninitialized BitmapImage object.
70    *
71    * Use Image::New(...) to create an initialised object.
72    */
73   BitmapImage();
74
75   /**
76    * @brief Create a new BitmapImage.
77    *
78    * Also a pixel buffer for image data is allocated.
79    * Dali has ownership of the buffer.
80    * For better performance and portability use power of two dimensions.
81    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
82    * @note: default resource management policies are Immediate and Never
83    *
84    * @pre width & height are greater than zero
85    * @param [in] width       image width in pixels
86    * @param [in] height      image height in pixels
87    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
88    * @return a handle to a new instance of BitmapImage
89    */
90   static BitmapImage New(unsigned int width,
91                          unsigned int height,
92                          Pixel::Format pixelformat=Pixel::RGBA8888);
93
94   /**
95    * @brief Create a new BitmapImage.
96    *
97    * Also a pixel buffer for image data is allocated.
98    * Dali has ownership of the buffer.
99    * For better performance and portability use power of two dimensions.
100    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
101    *
102    * @pre width & height are greater than zero
103    * @param [in] width          Image width in pixels
104    * @param [in] height         Image height in pixels
105    * @param [in] pixelFormat    The pixel format
106    * @param [in] loadPolicy     Controls time of loading a resource from the filesystem.
107    * @param [in] releasePolicy  Optionally release memory when image is not visible on screen.
108    * @return a handle to a new instance of BitmapImage
109    */
110   static BitmapImage New(unsigned int  width,
111                          unsigned int  height,
112                          Pixel::Format pixelFormat,
113                          LoadPolicy    loadPolicy,
114                          ReleasePolicy releasePolicy);
115
116   /**
117    * @brief Create a new BitmapImage, which uses an external data source.
118    *
119    * The PixelBuffer has to be allocated by application.
120    *
121    * The application holds ownership of the buffer. It must not
122    * destroy the PixelBuffer on a staged image if it has called
123    * Update() and hasn't received a SignalUploaded, or if it has just
124    * added it to the stage and has not received a SignalUploaded.
125    *
126    * For better performance and portability use power of two dimensions.
127    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
128    *
129    * @pre width & height are greater than zero
130    * @param [in] pixelBuffer  pixel buffer. has to be allocated by application.
131    * @param [in] width        image width in pixels
132    * @param [in] height       image height in pixels
133    * @param [in] pixelFormat  the pixel format (rgba 32 bit by default)
134    * @param [in] stride       the internal stride of the pixelbuffer in pixels
135    * @return a handle to a new instance of BitmapImage
136    */
137   static BitmapImage New(PixelBuffer*  pixelBuffer,
138                          unsigned int  width,
139                          unsigned int  height,
140                          Pixel::Format pixelFormat=Pixel::RGBA8888,
141                          unsigned int  stride=0);
142
143   /**
144    * @brief Create a new BitmapImage, which uses an external data source.
145    *
146    * The PixelBuffer has to be allocated by application.
147    *
148    * The application holds ownership of the buffer. It must not
149    * destroy the PixelBuffer on a staged image if it has called
150    * Update() and hasn't received a SignalUploaded, or if it has just
151    * added it to the stage and has not received a SignalUploaded.
152    *
153    * For better performance and portability use power of two dimensions.
154    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
155    *
156    * @note in case releasePolicy is "Unused", application has to call
157    * BitmapImage::Update() whenever image is re-added to the stage
158    *
159    * @pre width & height are greater than zero
160    * @param [in] pixelBuffer   pixel buffer. has to be allocated by application.
161    * @param [in] width         image width in pixels
162    * @param [in] height        image height in pixels
163    * @param [in] pixelFormat   the pixel format
164    * @param [in] stride        the internal stride of the pixelbuffer in pixels
165    * @param [in] releasePolicy optionally relase memory when image is not visible on screen.
166    * @return a handle to a new instance of BitmapImage
167    */
168   static BitmapImage New(PixelBuffer*  pixelBuffer,
169                          unsigned int  width,
170                          unsigned int  height,
171                          Pixel::Format pixelFormat,
172                          unsigned int  stride,
173                          ReleasePolicy releasePolicy);
174
175   /**
176    * @brief Downcast an Object handle to BitmapImage.
177    *
178    * If handle points to a BitmapImage the downcast produces valid
179    * handle. If not the returned handle is left uninitialized.
180    *
181    * @param[in] handle to An object
182    * @return handle to a BitmapImage or an uninitialized handle
183    */
184   static BitmapImage DownCast( BaseHandle handle );
185
186   /**
187    * @brief Destructor
188    *
189    * This is non-virtual since derived Handle types must not contain data or virtual methods.
190    */
191   ~BitmapImage();
192
193   /**
194    * @brief This copy constructor is required for (smart) pointer semantics.
195    *
196    * @param [in] handle A reference to the copied handle
197    */
198   BitmapImage(const BitmapImage& handle);
199
200   /**
201    * @brief This assignment operator is required for (smart) pointer semantics.
202    *
203    * @param [in] rhs  A reference to the copied handle
204    * @return A reference to this
205    */
206   BitmapImage& operator=(const BitmapImage& rhs);
207
208   /**
209    * @brief White pixel as image data.
210    *
211    * Can be used to create solid color actors.
212    * @return 1 white pixel with 32 bit colordepth
213    */
214   static const BitmapImage WHITE();
215
216 public:
217   /**
218    * @brief Returns the pixel buffer of the Image.
219    *
220    * The application can write to the buffer to modify its contents.
221    *
222    * Whilst the image is on stage, after writing to the buffer the
223    * application should call Update() and wait for the
224    * SignalUploaded() method before writing again.
225    *
226    * @return the pixel buffer
227    */
228   PixelBuffer* GetBuffer();
229
230   /**
231    * @brief Returns buffer size in bytes.
232    *
233    * @return the buffer size in bytes
234    */
235   unsigned int GetBufferSize() const;
236
237   /**
238    * @brief Returns buffer stride (in bytes).
239    *
240    * @return the buffer stride
241    */
242   unsigned int GetBufferStride() const;
243
244   /**
245    * @brief Returns the pixel format of the contained bitmap
246    *
247    * @return the pixel format
248    */
249   Pixel::Format GetPixelFormat() const;
250
251   /**
252    * @brief Inform Dali that the contents of the buffer have changed.
253    *
254    * SignalUploaded will be sent in response if the image is on stage
255    * and the image data has been successfully copied to graphics
256    * memory. To avoid visual tearing, the application should wait for
257    * the SignalUploaded before modifying the data.
258    *
259    * The application must not destroy an external PixelBuffer on a staged
260    * image after calling this method until the SignalUploaded has been
261    * successfully received.
262    *
263    * @note: BitmapImage::Update might not work with BGR/BGRA formats!
264    * @note: Some GPUs may not support Non power of two buffer updates (for
265    * example C110/SGX540)
266    */
267   void Update();
268
269   /**
270    * @copydoc Update()
271    * @param [in] updateArea area that has changed in buffer
272    */
273   void Update( RectArea updateArea );
274
275   /**
276    * @brief Returns whether BitmapImage uses external data source or not.
277    *
278    * If not, dali holds ownership of the PixelBuffer, otherwise the application
279    * is responsible for freeing it.
280    *
281    * The application must not destroy an external PixelBuffer on a staged image
282    * if it has called Update() and hasn't received a SignalUploaded.
283    *
284    * @return true if application owns data, false otherwise
285    */
286   bool IsDataExternal() const;
287
288 public: // Not intended for application developers
289
290   explicit DALI_INTERNAL BitmapImage(Internal::BitmapImage*);
291 };
292
293 } // namespace Dali
294
295 #endif // __DALI_BITMAP_IMAGE_H__