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