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