Merge "Doxygen grouping" into 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
39 typedef Rect<unsigned int>    RectArea;     ///< rectangular area (x,y,w,h)
40
41
42 /**
43  * @brief BufferImage represents an image resource that can be added to ImageActors.
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  * SignalUploaded 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  * SignalUploaded.
54  *
55  * Similarly, once the image is on stage (i.e. it's being used by an
56  * ImageActor that is on stage), the application should only write to
57  * the buffer after receiving a SignalUploaded, 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  *
66  * If the image is opaque and blending is not required, then the user
67  * should call ImageActor::SetUseImageAlpha(false) on the containing actor.
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    */
77   BufferImage();
78
79   /**
80    * @brief Create a new BufferImage.
81    *
82    * Also a pixel buffer for image data is allocated.
83    * Dali has ownership of the buffer.
84    * For better performance and portability use power of two dimensions.
85    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
86    * @note: default resource management policies are Immediate and Never
87    *
88    * @pre width & height are greater than zero
89    * @param [in] width       image width in pixels
90    * @param [in] height      image height in pixels
91    * @param [in] pixelformat the pixel format (rgba 32 bit by default)
92    * @return a handle to a new instance of BufferImage
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    * @pre width & height are greater than zero
107    * @param [in] width          Image width in pixels
108    * @param [in] height         Image height in pixels
109    * @param [in] pixelFormat    The pixel format
110    * @param [in] releasePolicy  Optionally release memory when image is not visible on screen.
111    * @return a handle to a new instance of BufferImage
112    */
113   static BufferImage New(unsigned int  width,
114                          unsigned int  height,
115                          Pixel::Format pixelFormat,
116                          ReleasePolicy releasePolicy);
117
118   /**
119    * @brief Create a new BufferImage, which uses an external data source.
120    *
121    * The PixelBuffer has to be allocated by application.
122    *
123    * The application holds ownership of the buffer. It must not
124    * destroy the PixelBuffer on a staged image if it has called
125    * Update() and hasn't received a SignalUploaded, or if it has just
126    * added it to the stage and has not received a SignalUploaded.
127    *
128    * For better performance and portability use power of two dimensions.
129    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
130    *
131    * @pre width & height are greater than zero
132    * @param [in] pixelBuffer  pixel buffer. has to be allocated by application.
133    * @param [in] width        image width in pixels
134    * @param [in] height       image height in pixels
135    * @param [in] pixelFormat  the pixel format (rgba 32 bit by default)
136    * @param [in] stride       the internal stride of the pixelbuffer in pixels
137    * @return a handle to a new instance of BufferImage
138    */
139   static BufferImage New(PixelBuffer*  pixelBuffer,
140                          unsigned int  width,
141                          unsigned int  height,
142                          Pixel::Format pixelFormat=Pixel::RGBA8888,
143                          unsigned int  stride=0);
144
145   /**
146    * @brief Create a new BufferImage, which uses an external data source.
147    *
148    * The PixelBuffer has to be allocated by application.
149    *
150    * The application holds ownership of the buffer. It must not
151    * destroy the PixelBuffer on a staged image if it has called
152    * Update() and hasn't received a SignalUploaded, or if it has just
153    * added it to the stage and has not received a SignalUploaded.
154    *
155    * For better performance and portability use power of two dimensions.
156    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
157    *
158    * @note in case releasePolicy is "Unused", application has to call
159    * BufferImage::Update() whenever image is re-added to the stage
160    *
161    * @pre width & height are greater than zero
162    * @param [in] pixelBuffer   pixel buffer. has to be allocated by application.
163    * @param [in] width         image width in pixels
164    * @param [in] height        image height in pixels
165    * @param [in] pixelFormat   the pixel format
166    * @param [in] stride        the internal stride of the pixelbuffer in pixels
167    * @param [in] releasePolicy optionally relase memory when image is not visible on screen.
168    * @return a handle to a new instance of BufferImage
169    */
170   static BufferImage New(PixelBuffer*  pixelBuffer,
171                          unsigned int  width,
172                          unsigned int  height,
173                          Pixel::Format pixelFormat,
174                          unsigned int  stride,
175                          ReleasePolicy releasePolicy);
176
177   /**
178    * @brief Downcast an Object handle to BufferImage.
179    *
180    * If handle points to a BufferImage the downcast produces valid
181    * handle. If not the returned handle is left uninitialized.
182    *
183    * @param[in] 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    */
193   ~BufferImage();
194
195   /**
196    * @brief This copy constructor is required for (smart) pointer semantics.
197    *
198    * @param [in] handle A reference to the copied handle
199    */
200   BufferImage(const BufferImage& handle);
201
202   /**
203    * @brief This assignment operator is required for (smart) pointer semantics.
204    *
205    * @param [in] rhs  A reference to the copied handle
206    * @return A reference to this
207    */
208   BufferImage& operator=(const BufferImage& rhs);
209
210   /**
211    * @brief White pixel as image data.
212    *
213    * Can be used to create solid color actors.
214    * @return 1 white pixel with 32 bit colordepth
215    */
216   static const BufferImage WHITE();
217
218 public:
219   /**
220    * @brief Returns the pixel buffer of the Image.
221    *
222    * The application can write to the buffer to modify its contents.
223    *
224    * Whilst the image is on stage, after writing to the buffer the
225    * application should call Update() and wait for the
226    * SignalUploaded() method before writing again.
227    *
228    * @return the pixel buffer
229    */
230   PixelBuffer* GetBuffer();
231
232   /**
233    * @brief Returns buffer size in bytes.
234    *
235    * @return the buffer size in bytes
236    */
237   unsigned int GetBufferSize() const;
238
239   /**
240    * @brief Returns buffer stride (in bytes).
241    *
242    * @return the buffer stride
243    */
244   unsigned int GetBufferStride() const;
245
246   /**
247    * @brief Returns the pixel format of the contained buffer
248    *
249    * @return the pixel format
250    */
251   Pixel::Format GetPixelFormat() const;
252
253   /**
254    * @brief Inform Dali that the contents of the buffer have changed.
255    *
256    * SignalUploaded will be sent in response if the image is on stage
257    * and the image data has been successfully copied to graphics
258    * memory. To avoid visual tearing, the application should wait for
259    * the SignalUploaded before modifying the data.
260    *
261    * The application must not destroy an external PixelBuffer on a staged
262    * image after calling this method until the SignalUploaded has been
263    * successfully received.
264    *
265    * @note: BufferImage::Update might not work with BGR/BGRA formats!
266    * @note: Some GPUs may not support Non power of two buffer updates (for
267    * example C110/SGX540)
268    */
269   void Update();
270
271   /**
272    * @copydoc Update()
273    * @param [in] updateArea area that has changed in buffer
274    */
275   void Update( RectArea updateArea );
276
277   /**
278    * @brief Returns whether BufferImage uses external data source or not.
279    *
280    * If not, dali holds ownership of the PixelBuffer, otherwise the application
281    * is responsible for freeing it.
282    *
283    * The application must not destroy an external PixelBuffer on a staged image
284    * if it has called Update() and hasn't received a SignalUploaded.
285    *
286    * @return true if application owns data, false otherwise
287    */
288   bool IsDataExternal() const;
289
290 public: // Not intended for application developers
291
292   explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
293 };
294
295 /**
296  * @}
297  */
298 } // namespace Dali
299
300 #endif // __DALI_BUFFER_IMAGE_H__