1 #ifndef __DALI_BUFFER_IMAGE_H__
2 #define __DALI_BUFFER_IMAGE_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/public-api/images/image.h>
23 #include <dali/public-api/images/pixel.h>
24 #include <dali/public-api/math/rect.h>
29 * @addtogroup dali_core_images
33 namespace Internal DALI_INTERNAL
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
43 * @brief BufferImage represents an image resource that can be added to ImageViews.
45 * Its pixel buffer data is provided by the application developer.
47 * Care should be taken with pixel data allocated by the application,
48 * as the data is copied to GL both when the image is added to the
49 * stage and after a call to Update(). In both of these cases, a
50 * Image::UploadedSignal will be sent to the application confirming that the
51 * operation has completed.
53 * The application can free the pixel data after receiving a
54 * Image::UploadedSignal.
56 * Similarly, once the image is on stage (i.e. it's being used by an
57 * ImageView that is on stage), the application should only write to
58 * the buffer after receiving a Image::UploadedSignal, then call Update()
59 * once the write is finished. This avoids the pixel data being changed
60 * whilst it's being copied to GL. Writing to the buffer without waiting
61 * for the signal will likely result in visible tearing.
63 * If the pixel format of the pixel buffer contains an alpha channel,
64 * then the image is considered to be have transparent pixels without
65 * regard for the actual content of the channel, and will be blended.
69 class DALI_IMPORT_API BufferImage : public Image
73 * @brief Constructor which creates an uninitialized BufferImage object.
75 * Use BufferImage::New(...) to create an initialised object.
81 * @brief Create a new BufferImage.
83 * Also a pixel buffer for image data is allocated.
84 * Dali has ownership of the buffer.
85 * For better performance and portability use power of two dimensions.
86 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
88 * @param [in] width Image width in pixels
89 * @param [in] height Image height in pixels
90 * @param [in] pixelformat The pixel format (rgba 32 bit by default)
91 * @return A handle to a new instance of BufferImage
92 * @pre width & height are greater than zero
93 * @note default resource management policies are Immediate and Never
96 static BufferImage New(unsigned int width,
98 Pixel::Format pixelformat=Pixel::RGBA8888);
101 * @DEPRECATED_1_1.3. Use New( unsigned int width, unsigned int height ) instead.
103 * @brief Create a new BufferImage.
105 * Also a pixel buffer for image data is allocated.
106 * Dali has ownership of the buffer.
107 * For better performance and portability use power of two dimensions.
108 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
111 * @remarks ReleasePolicy is an experimental feature and might not be supported in the next release.
112 * We do recommend not to use this method.
113 * Please refer the Remarks of ReleasePolicy for more information.
114 * @param [in] width Image width in pixels
115 * @param [in] height Image height in pixels
116 * @param [in] pixelFormat The pixel format
117 * @param [in] releasePolicy Optionally release memory when image is not visible on screen.
118 * @return A handle to a new instance of BufferImage
119 * @pre width & height are greater than zero
121 static BufferImage New(unsigned int width,
123 Pixel::Format pixelFormat,
124 ReleasePolicy releasePolicy);
127 * @brief Create a new BufferImage, which uses an external data source.
129 * The PixelBuffer has to be allocated by application.
131 * The application holds ownership of the buffer. It must not
132 * destroy the PixelBuffer on a staged image if it has called
133 * Update() and hasn't received a Image::UploadedSignal, or if it has just
134 * added it to the stage and has not received a Image::UploadedSignal.
136 * For better performance and portability use power of two dimensions.
137 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
140 * @param [in] pixelBuffer Pixel buffer. has to be allocated by application.
141 * @param [in] width Image width in pixels
142 * @param [in] height Image height in pixels
143 * @param [in] pixelFormat The pixel format (rgba 32 bit by default)
144 * @param [in] stride The internal stride of the pixelbuffer in pixels
145 * @return A handle to a new instance of BufferImage
146 * @pre width & height are greater than zero
148 static BufferImage New(PixelBuffer* pixelBuffer,
151 Pixel::Format pixelFormat=Pixel::RGBA8888,
152 unsigned int stride=0);
155 * @DEPRECATED_1_1.3. Use New( PixelBuffer* pixelBuffer, unsigned int width, unsigned int height ) instead.
157 * @brief Create a new BufferImage, which uses an external data source.
159 * The PixelBuffer has to be allocated by application.
161 * The application holds ownership of the buffer. It must not
162 * destroy the PixelBuffer on a staged image if it has called
163 * Update() and hasn't received a Image::UploadedSignal, or if it has just
164 * added it to the stage and has not received a Image::UploadedSignal.
166 * For better performance and portability use power of two dimensions.
167 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
170 * @remarks ReleasePolicy is an experimental feature and might not be supported in the next release.
171 * We do recommend not to use this method.
172 * Please refer the Remarks of ReleasePolicy for more information.
173 * @param [in] pixelBuffer Pixel buffer. has to be allocated by application.
174 * @param [in] width Image width in pixels
175 * @param [in] height Image height in pixels
176 * @param [in] pixelFormat The pixel format
177 * @param [in] stride The internal stride of the pixelbuffer in pixels
178 * @param [in] releasePolicy Optionally relase memory when image is not visible on screen.
179 * @return A handle to a new instance of BufferImage
180 * @pre width & height are greater than zero
181 * @note in case releasePolicy is Image::UNUSED, application has to call
182 * BufferImage::Update() whenever image is re-added to the stage
185 static BufferImage New(PixelBuffer* pixelBuffer,
188 Pixel::Format pixelFormat,
190 ReleasePolicy releasePolicy);
193 * @brief Downcast a handle to BufferImage handle.
195 * If handle points to a BufferImage the downcast produces valid
196 * handle. If not the returned handle is left uninitialized.
199 * @param[in] handle Handle to an object
200 * @return Handle to a BufferImage or an uninitialized handle
202 static BufferImage DownCast( BaseHandle handle );
207 * This is non-virtual since derived Handle types must not contain data or virtual methods.
213 * @brief This copy constructor is required for (smart) pointer semantics.
216 * @param [in] handle A reference to the copied handle
218 BufferImage(const BufferImage& handle);
221 * @brief This assignment operator is required for (smart) pointer semantics.
224 * @param [in] rhs A reference to the copied handle
225 * @return A reference to this
227 BufferImage& operator=(const BufferImage& rhs);
230 * @brief White pixel as image data.
232 * Can be used to create solid color actors.
234 * @return 1 white pixel with 32 bit colordepth
236 static const BufferImage WHITE();
240 * @brief Returns the pixel buffer of the Image.
242 * The application can write to the buffer to modify its contents.
244 * Whilst the image is on stage, after writing to the buffer the
245 * application should call Update() and wait for the
246 * Image::UploadedSignal() method before writing again.
249 * @return The pixel buffer
251 PixelBuffer* GetBuffer();
254 * @brief Returns buffer size in bytes.
257 * @return The buffer size in bytes
259 unsigned int GetBufferSize() const;
262 * @brief Returns buffer stride (in bytes).
265 * @return The buffer stride
267 unsigned int GetBufferStride() const;
270 * @brief Returns the pixel format of the contained buffer
273 * @return The pixel format
275 Pixel::Format GetPixelFormat() const;
278 * @brief Inform Dali that the contents of the buffer have changed.
280 * Image::UploadedSignal will be sent in response if the image is on stage
281 * and the image data has been successfully copied to graphics
282 * memory. To avoid visual tearing, the application should wait for
283 * the Image::UploadedSignal before modifying the data.
285 * The application must not destroy an external PixelBuffer on a staged
286 * image after calling this method until the Image::UploadedSignal has been
287 * successfully received.
290 * @note BufferImage::Update might not work with BGR/BGRA formats!
291 * @note Some GPUs may not support Non power of two buffer updates (for
292 * example C110/SGX540)
298 * @param [in] updateArea Area that has changed in buffer
300 void Update( RectArea updateArea );
303 * @brief Returns whether BufferImage uses external data source or not.
305 * If not, dali holds ownership of the PixelBuffer, otherwise the application
306 * is responsible for freeing it.
308 * The application must not destroy an external PixelBuffer on a staged image
309 * if it has called Update() and hasn't received a Image::UploadedSignal.
312 * @return true if application owns data, false otherwise
314 bool IsDataExternal() const;
316 public: // Not intended for application developers
318 explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
326 #endif // __DALI_BUFFER_IMAGE_H__