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