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