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_tizen 2.4
39 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h) @since_tizen 2.4
42 * @brief BufferImage represents an image resource that can be added to ImageViews.
44 * Its pixel buffer data is provided by the application developer.
46 * Care should be taken with pixel data allocated by the application,
47 * as the data is copied to GL both when the image is added to the
48 * stage and after a call to Update(). In both of these cases, a
49 * Image::UploadedSignal will be sent to the application confirming that the
50 * operation has completed.
52 * The application can free the pixel data after receiving a
53 * Image::UploadedSignal.
55 * Similarly, once the image is on stage (i.e. it's being used by an
56 * ImageView that is on stage), the application should only write to
57 * the buffer after receiving a Image::UploadedSignal, then call Update()
58 * once the write is finished. This avoids the pixel data being changed
59 * whilst it's being copied to GL. Writing to the buffer without waiting
60 * for the signal will likely result in visible tearing.
62 * If the pixel format of the pixel buffer contains an alpha channel,
63 * then the image is considered to be have transparent pixels without
64 * regard for the actual content of the channel, and will be blended.
67 class DALI_IMPORT_API BufferImage : public Image
71 * @brief Constructor which creates an uninitialized BufferImage object.
73 * Use BufferImage::New(...) to create an initialised object.
79 * @brief Create a new BufferImage.
81 * Also a pixel buffer for image data is allocated.
82 * Dali has ownership of the buffer.
83 * For better performance and portability use power of two dimensions.
84 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
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 * @pre width & height are greater than zero
91 * @note default resource management policies are Immediate and Never
94 static BufferImage New(unsigned int width,
96 Pixel::Format pixelformat=Pixel::RGBA8888);
99 * @brief Create a new BufferImage.
101 * Also a pixel buffer for image data is allocated.
102 * Dali has ownership of the buffer.
103 * For better performance and portability use power of two dimensions.
104 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
107 * @remarks ReleasePolicy is an experimental feature and might not be supported in the next release.
108 * We do recommend not to use this method.
109 * Please refer the Remarks of ReleasePolicy for more information.
110 * @param [in] width Image width in pixels
111 * @param [in] height Image height in pixels
112 * @param [in] pixelFormat The pixel format
113 * @param [in] releasePolicy Optionally release memory when image is not visible on screen.
114 * @return A handle to a new instance of BufferImage
115 * @pre width & height are greater than zero
117 static BufferImage New(unsigned int width,
119 Pixel::Format pixelFormat,
120 ReleasePolicy releasePolicy);
123 * @brief Create a new BufferImage, which uses an external data source.
125 * The PixelBuffer has to be allocated by application.
127 * The application holds ownership of the buffer. It must not
128 * destroy the PixelBuffer on a staged image if it has called
129 * Update() and hasn't received a Image::UploadedSignal, or if it has just
130 * added it to the stage and has not received a Image::UploadedSignal.
132 * For better performance and portability use power of two dimensions.
133 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
136 * @param [in] pixelBuffer Pixel buffer. has to be allocated by application.
137 * @param [in] width Image width in pixels
138 * @param [in] height Image height in pixels
139 * @param [in] pixelFormat The pixel format (rgba 32 bit by default)
140 * @param [in] stride The internal stride of the pixelbuffer in pixels
141 * @return A handle to a new instance of BufferImage
142 * @pre width & height are greater than zero
144 static BufferImage New(PixelBuffer* pixelBuffer,
147 Pixel::Format pixelFormat=Pixel::RGBA8888,
148 unsigned int stride=0);
151 * @brief Create a new BufferImage, which uses an external data source.
153 * The PixelBuffer has to be allocated by application.
155 * The application holds ownership of the buffer. It must not
156 * destroy the PixelBuffer on a staged image if it has called
157 * Update() and hasn't received a Image::UploadedSignal, or if it has just
158 * added it to the stage and has not received a Image::UploadedSignal.
160 * For better performance and portability use power of two dimensions.
161 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
164 * @remarks ReleasePolicy is an experimental feature and might not be supported in the next release.
165 * We do recommend not to use this method.
166 * Please refer the Remarks of ReleasePolicy for more information.
167 * @param [in] pixelBuffer Pixel buffer. has to be allocated by application.
168 * @param [in] width Image width in pixels
169 * @param [in] height Image height in pixels
170 * @param [in] pixelFormat The pixel format
171 * @param [in] stride The internal stride of the pixelbuffer in pixels
172 * @param [in] releasePolicy Optionally relase memory when image is not visible on screen.
173 * @return A handle to a new instance of BufferImage
174 * @pre width & height are greater than zero
175 * @note in case releasePolicy is Image::UNUSED, application has to call
176 * BufferImage::Update() whenever image is re-added to the stage
179 static BufferImage New(PixelBuffer* pixelBuffer,
182 Pixel::Format pixelFormat,
184 ReleasePolicy releasePolicy);
187 * @brief Downcast a handle to BufferImage handle.
189 * If handle points to a BufferImage the downcast produces valid
190 * handle. If not the returned handle is left uninitialized.
193 * @param[in] handle Handle to an object
194 * @return Handle to a BufferImage or an uninitialized handle
196 static BufferImage DownCast( BaseHandle handle );
201 * This is non-virtual since derived Handle types must not contain data or virtual methods.
207 * @brief This copy constructor is required for (smart) pointer semantics.
210 * @param [in] handle A reference to the copied handle
212 BufferImage(const BufferImage& handle);
215 * @brief This assignment operator is required for (smart) pointer semantics.
218 * @param [in] rhs A reference to the copied handle
219 * @return A reference to this
221 BufferImage& operator=(const BufferImage& rhs);
224 * @brief White pixel as image data.
226 * Can be used to create solid color actors.
228 * @return 1 white pixel with 32 bit colordepth
230 static const BufferImage WHITE();
234 * @brief Returns the pixel buffer of the Image.
236 * The application can write to the buffer to modify its contents.
238 * Whilst the image is on stage, after writing to the buffer the
239 * application should call Update() and wait for the
240 * Image::UploadedSignal() method before writing again.
243 * @return The pixel buffer
245 PixelBuffer* GetBuffer();
248 * @brief Returns buffer size in bytes.
251 * @return The buffer size in bytes
253 unsigned int GetBufferSize() const;
256 * @brief Returns buffer stride (in bytes).
259 * @return The buffer stride
261 unsigned int GetBufferStride() const;
264 * @brief Returns the pixel format of the contained buffer
267 * @return The pixel format
269 Pixel::Format GetPixelFormat() const;
272 * @brief Inform Dali that the contents of the buffer have changed.
274 * Image::UploadedSignal will be sent in response if the image is on stage
275 * and the image data has been successfully copied to graphics
276 * memory. To avoid visual tearing, the application should wait for
277 * the Image::UploadedSignal before modifying the data.
279 * The application must not destroy an external PixelBuffer on a staged
280 * image after calling this method until the Image::UploadedSignal has been
281 * successfully received.
284 * @note BufferImage::Update might not work with BGR/BGRA formats!
285 * @note Some GPUs may not support Non power of two buffer updates (for
286 * example C110/SGX540)
292 * @param [in] updateArea Area that has changed in buffer
294 void Update( RectArea updateArea );
297 * @brief Returns whether BufferImage uses external data source or not.
299 * If not, dali holds ownership of the PixelBuffer, otherwise the application
300 * is responsible for freeing it.
302 * The application must not destroy an external PixelBuffer on a staged image
303 * if it has called Update() and hasn't received a Image::UploadedSignal.
306 * @return True if application owns data, false otherwise
308 bool IsDataExternal() const;
310 public: // Not intended for application developers
312 explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
320 #endif // __DALI_BUFFER_IMAGE_H__