1 #ifndef __DALI_BUFFER_IMAGE_H__
2 #define __DALI_BUFFER_IMAGE_H__
5 * Copyright (c) 2014 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/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>
30 namespace Internal DALI_INTERNAL
35 typedef unsigned char PixelBuffer; ///< pixel data buffer
36 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
40 * @brief BufferImage represents an image resource that can be added to ImageActors.
41 * Its pixel buffer data is provided by the application developer.
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.
49 * The application can free the pixel data after receiving a
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.
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.
63 * If the image is opaque and blending is not required, then the user
64 * should call ImageActor::SetUseImageAlpha(false) on the containing actor.
66 class DALI_IMPORT_API BufferImage : public Image
70 * @brief Constructor which creates an uninitialized BufferImage object.
72 * Use BufferImage::New(...) to create an initialised object.
77 * @brief Create a new BufferImage.
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
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
91 static BufferImage New(unsigned int width,
93 Pixel::Format pixelformat=Pixel::RGBA8888);
96 * @brief Create a new BufferImage.
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.
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
110 static BufferImage New(unsigned int width,
112 Pixel::Format pixelFormat,
113 ReleasePolicy releasePolicy);
116 * @brief Create a new BufferImage, which uses an external data source.
118 * The PixelBuffer has to be allocated by application.
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.
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.
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
136 static BufferImage New(PixelBuffer* pixelBuffer,
139 Pixel::Format pixelFormat=Pixel::RGBA8888,
140 unsigned int stride=0);
143 * @brief Create a new BufferImage, which uses an external data source.
145 * The PixelBuffer has to be allocated by application.
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.
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.
155 * @note in case releasePolicy is "Unused", application has to call
156 * BufferImage::Update() whenever image is re-added to the stage
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
167 static BufferImage New(PixelBuffer* pixelBuffer,
170 Pixel::Format pixelFormat,
172 ReleasePolicy releasePolicy);
175 * @brief Downcast an Object handle to BufferImage.
177 * If handle points to a BufferImage the downcast produces valid
178 * handle. If not the returned handle is left uninitialized.
180 * @param[in] handle to An object
181 * @return handle to a BufferImage or an uninitialized handle
183 static BufferImage DownCast( BaseHandle handle );
188 * This is non-virtual since derived Handle types must not contain data or virtual methods.
193 * @brief This copy constructor is required for (smart) pointer semantics.
195 * @param [in] handle A reference to the copied handle
197 BufferImage(const BufferImage& handle);
200 * @brief This assignment operator is required for (smart) pointer semantics.
202 * @param [in] rhs A reference to the copied handle
203 * @return A reference to this
205 BufferImage& operator=(const BufferImage& rhs);
208 * @brief White pixel as image data.
210 * Can be used to create solid color actors.
211 * @return 1 white pixel with 32 bit colordepth
213 static const BufferImage WHITE();
217 * @brief Returns the pixel buffer of the Image.
219 * The application can write to the buffer to modify its contents.
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.
225 * @return the pixel buffer
227 PixelBuffer* GetBuffer();
230 * @brief Returns buffer size in bytes.
232 * @return the buffer size in bytes
234 unsigned int GetBufferSize() const;
237 * @brief Returns buffer stride (in bytes).
239 * @return the buffer stride
241 unsigned int GetBufferStride() const;
244 * @brief Returns the pixel format of the contained buffer
246 * @return the pixel format
248 Pixel::Format GetPixelFormat() const;
251 * @brief Inform Dali that the contents of the buffer have changed.
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.
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.
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)
270 * @param [in] updateArea area that has changed in buffer
272 void Update( RectArea updateArea );
275 * @brief Returns whether BufferImage uses external data source or not.
277 * If not, dali holds ownership of the PixelBuffer, otherwise the application
278 * is responsible for freeing it.
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.
283 * @return true if application owns data, false otherwise
285 bool IsDataExternal() const;
287 public: // Not intended for application developers
289 explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
294 #endif // __DALI_BUFFER_IMAGE_H__