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