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