[dali_1.2.6] 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 @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    * @brief Create a new BufferImage, which uses an external data source.
86    *
87    * The PixelBuffer has to be allocated by application.
88    *
89    * The application holds ownership of the buffer. It must not
90    * destroy the PixelBuffer on a staged image if it has called
91    * Update() and hasn't received a Image::UploadedSignal, or if it has just
92    * added it to the stage and has not received a Image::UploadedSignal.
93    *
94    * For better performance and portability use power of two dimensions.
95    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
96    *
97    * @SINCE_1_0.0
98    * @param [in] pixelBuffer  Pixel buffer. has to be allocated by application.
99    * @param [in] width        Image width in pixels
100    * @param [in] height       Image height in pixels
101    * @param [in] pixelFormat  The pixel format (rgba 32 bit by default)
102    * @param [in] stride       The internal stride of the pixelbuffer in pixels
103    * @return A handle to a new instance of BufferImage
104    * @pre width & height are greater than zero
105    */
106   static BufferImage New(PixelBuffer*  pixelBuffer,
107                          unsigned int  width,
108                          unsigned int  height,
109                          Pixel::Format pixelFormat=Pixel::RGBA8888,
110                          unsigned int  stride=0);
111
112   /**
113    * @brief Downcast a handle to BufferImage handle.
114    *
115    * If handle points to a BufferImage the downcast produces valid
116    * handle. If not the returned handle is left uninitialized.
117    *
118    * @SINCE_1_0.0
119    * @param[in] handle Handle to an object
120    * @return Handle to a BufferImage or an uninitialized handle
121    */
122   static BufferImage DownCast( BaseHandle handle );
123
124   /**
125    * @brief Destructor
126    *
127    * This is non-virtual since derived Handle types must not contain data or virtual methods.
128    * @SINCE_1_0.0
129    */
130   ~BufferImage();
131
132   /**
133    * @brief This copy constructor is required for (smart) pointer semantics.
134    *
135    * @SINCE_1_0.0
136    * @param [in] handle A reference to the copied handle
137    */
138   BufferImage(const BufferImage& handle);
139
140   /**
141    * @brief This assignment operator is required for (smart) pointer semantics.
142    *
143    * @SINCE_1_0.0
144    * @param [in] rhs A reference to the copied handle
145    * @return A reference to this
146    */
147   BufferImage& operator=(const BufferImage& rhs);
148
149   /**
150    * @brief White pixel as image data.
151    *
152    * Can be used to create solid color actors.
153    * @SINCE_1_0.0
154    * @return 1 white pixel with 32 bit colordepth
155    */
156   static const BufferImage WHITE();
157
158 public:
159   /**
160    * @brief Returns the pixel buffer of the Image.
161    *
162    * The application can write to the buffer to modify its contents.
163    *
164    * Whilst the image is on stage, after writing to the buffer the
165    * application should call Update() and wait for the
166    * Image::UploadedSignal() method before writing again.
167    *
168    * @SINCE_1_0.0
169    * @return The pixel buffer
170    */
171   PixelBuffer* GetBuffer();
172
173   /**
174    * @brief Returns buffer size in bytes.
175    *
176    * @SINCE_1_0.0
177    * @return The buffer size in bytes
178    */
179   unsigned int GetBufferSize() const;
180
181   /**
182    * @brief Returns buffer stride (in bytes).
183    *
184    * @SINCE_1_0.0
185    * @return The buffer stride
186    */
187   unsigned int GetBufferStride() const;
188
189   /**
190    * @brief Returns the pixel format of the contained buffer
191    *
192    * @SINCE_1_0.0
193    * @return The pixel format
194    */
195   Pixel::Format GetPixelFormat() const;
196
197   /**
198    * @brief Inform Dali that the contents of the buffer have changed.
199    *
200    * Image::UploadedSignal will be sent in response if the image is on stage
201    * and the image data has been successfully copied to graphics
202    * memory. To avoid visual tearing, the application should wait for
203    * the Image::UploadedSignal before modifying the data.
204    *
205    * The application must not destroy an external PixelBuffer on a staged
206    * image after calling this method until the Image::UploadedSignal has been
207    * successfully received.
208    *
209    * @SINCE_1_0.0
210    * @note BufferImage::Update might not work with BGR/BGRA formats!
211    * @note Some GPUs may not support Non power of two buffer updates (for
212    * example C110/SGX540)
213    */
214   void Update();
215
216   /**
217    * @copydoc Update()
218    * @param [in] updateArea Area that has changed in buffer
219    */
220   void Update( RectArea updateArea );
221
222   /**
223    * @brief Returns whether BufferImage uses external data source or not.
224    *
225    * If not, dali holds ownership of the PixelBuffer, otherwise the application
226    * is responsible for freeing it.
227    *
228    * The application must not destroy an external PixelBuffer on a staged image
229    * if it has called Update() and hasn't received a Image::UploadedSignal.
230    *
231    * @SINCE_1_0.0
232    * @return true if application owns data, false otherwise
233    */
234   bool IsDataExternal() const;
235
236 public: // Not intended for application developers
237
238   explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
239 };
240
241 /**
242  * @}
243  */
244 } // namespace Dali
245
246 #endif // __DALI_BUFFER_IMAGE_H__