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
39 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
43 * @brief BufferImage represents an image resource that can be added to ImageActors.
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 * SignalUploaded will be sent to the application confirming that the
50 * operation has completed.
52 * The application can free the pixel data after receiving a
55 * Similarly, once the image is on stage (i.e. it's being used by an
56 * ImageActor that is on stage), the application should only write to
57 * the buffer after receiving a SignalUploaded, 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.
66 * If the image is opaque and blending is not required, then the user
67 * should call ImageActor::SetUseImageAlpha(false) on the containing actor.
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.
80 * @brief Create a new BufferImage.
82 * Also a pixel buffer for image data is allocated.
83 * Dali has ownership of the buffer.
84 * For better performance and portability use power of two dimensions.
85 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
86 * @note: default resource management policies are Immediate and Never
88 * @pre width & height are greater than zero
89 * @param [in] width image width in pixels
90 * @param [in] height image height in pixels
91 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
92 * @return a handle to a new instance of BufferImage
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.
106 * @pre width & height are greater than zero
107 * @param [in] width Image width in pixels
108 * @param [in] height Image height in pixels
109 * @param [in] pixelFormat The pixel format
110 * @param [in] releasePolicy Optionally release memory when image is not visible on screen.
111 * @return a handle to a new instance of BufferImage
113 static BufferImage New(unsigned int width,
115 Pixel::Format pixelFormat,
116 ReleasePolicy releasePolicy);
119 * @brief Create a new BufferImage, which uses an external data source.
121 * The PixelBuffer has to be allocated by application.
123 * The application holds ownership of the buffer. It must not
124 * destroy the PixelBuffer on a staged image if it has called
125 * Update() and hasn't received a SignalUploaded, or if it has just
126 * added it to the stage and has not received a SignalUploaded.
128 * For better performance and portability use power of two dimensions.
129 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
131 * @pre width & height are greater than zero
132 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
133 * @param [in] width image width in pixels
134 * @param [in] height image height in pixels
135 * @param [in] pixelFormat the pixel format (rgba 32 bit by default)
136 * @param [in] stride the internal stride of the pixelbuffer in pixels
137 * @return a handle to a new instance of BufferImage
139 static BufferImage New(PixelBuffer* pixelBuffer,
142 Pixel::Format pixelFormat=Pixel::RGBA8888,
143 unsigned int stride=0);
146 * @brief Create a new BufferImage, which uses an external data source.
148 * The PixelBuffer has to be allocated by application.
150 * The application holds ownership of the buffer. It must not
151 * destroy the PixelBuffer on a staged image if it has called
152 * Update() and hasn't received a SignalUploaded, or if it has just
153 * added it to the stage and has not received a SignalUploaded.
155 * For better performance and portability use power of two dimensions.
156 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
158 * @note in case releasePolicy is "Unused", application has to call
159 * BufferImage::Update() whenever image is re-added to the stage
161 * @pre width & height are greater than zero
162 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
163 * @param [in] width image width in pixels
164 * @param [in] height image height in pixels
165 * @param [in] pixelFormat the pixel format
166 * @param [in] stride the internal stride of the pixelbuffer in pixels
167 * @param [in] releasePolicy optionally relase memory when image is not visible on screen.
168 * @return a handle to a new instance of BufferImage
170 static BufferImage New(PixelBuffer* pixelBuffer,
173 Pixel::Format pixelFormat,
175 ReleasePolicy releasePolicy);
178 * @brief Downcast an Object handle to BufferImage.
180 * If handle points to a BufferImage the downcast produces valid
181 * handle. If not the returned handle is left uninitialized.
183 * @param[in] handle to An object
184 * @return handle to a BufferImage or an uninitialized handle
186 static BufferImage DownCast( BaseHandle handle );
191 * This is non-virtual since derived Handle types must not contain data or virtual methods.
196 * @brief This copy constructor is required for (smart) pointer semantics.
198 * @param [in] handle A reference to the copied handle
200 BufferImage(const BufferImage& handle);
203 * @brief This assignment operator is required for (smart) pointer semantics.
205 * @param [in] rhs A reference to the copied handle
206 * @return A reference to this
208 BufferImage& operator=(const BufferImage& rhs);
211 * @brief White pixel as image data.
213 * Can be used to create solid color actors.
214 * @return 1 white pixel with 32 bit colordepth
216 static const BufferImage WHITE();
220 * @brief Returns the pixel buffer of the Image.
222 * The application can write to the buffer to modify its contents.
224 * Whilst the image is on stage, after writing to the buffer the
225 * application should call Update() and wait for the
226 * SignalUploaded() method before writing again.
228 * @return the pixel buffer
230 PixelBuffer* GetBuffer();
233 * @brief Returns buffer size in bytes.
235 * @return the buffer size in bytes
237 unsigned int GetBufferSize() const;
240 * @brief Returns buffer stride (in bytes).
242 * @return the buffer stride
244 unsigned int GetBufferStride() const;
247 * @brief Returns the pixel format of the contained buffer
249 * @return the pixel format
251 Pixel::Format GetPixelFormat() const;
254 * @brief Inform Dali that the contents of the buffer have changed.
256 * SignalUploaded will be sent in response if the image is on stage
257 * and the image data has been successfully copied to graphics
258 * memory. To avoid visual tearing, the application should wait for
259 * the SignalUploaded before modifying the data.
261 * The application must not destroy an external PixelBuffer on a staged
262 * image after calling this method until the SignalUploaded has been
263 * successfully received.
265 * @note: BufferImage::Update might not work with BGR/BGRA formats!
266 * @note: Some GPUs may not support Non power of two buffer updates (for
267 * example C110/SGX540)
273 * @param [in] updateArea area that has changed in buffer
275 void Update( RectArea updateArea );
278 * @brief Returns whether BufferImage uses external data source or not.
280 * If not, dali holds ownership of the PixelBuffer, otherwise the application
281 * is responsible for freeing it.
283 * The application must not destroy an external PixelBuffer on a staged image
284 * if it has called Update() and hasn't received a SignalUploaded.
286 * @return true if application owns data, false otherwise
288 bool IsDataExternal() const;
290 public: // Not intended for application developers
292 explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
300 #endif // __DALI_BUFFER_IMAGE_H__