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 namespace Internal DALI_INTERNAL
34 typedef unsigned char PixelBuffer; ///< pixel data buffer
35 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
39 * @brief BufferImage represents an image resource that can be added to ImageActors.
40 * Its pixel buffer data is provided by the application developer.
42 * Care should be taken with pixel data allocated by the application,
43 * as the data is copied to GL both when the image is added to the
44 * stage and after a call to Update(). In both of these cases, a
45 * SignalUploaded will be sent to the application confirming that the
46 * operation has completed.
48 * The application can free the pixel data after receiving a
51 * Similarly, once the image is on stage (i.e. it's being used by an
52 * ImageActor that is on stage), the application should only write to
53 * the buffer after receiving a SignalUploaded, then call Update()
54 * once the write is finished. This avoids the pixel data being changed
55 * whilst it's being copied to GL. Writing to the buffer without waiting
56 * for the signal will likely result in visible tearing.
58 * If the pixel format of the pixel buffer contains an alpha channel,
59 * then the image is considered to be have transparent pixels without
60 * regard for the actual content of the channel, and will be blended.
62 * If the image is opaque and blending is not required, then the user
63 * should call ImageActor::SetUseImageAlpha(false) on the containing actor.
65 class DALI_IMPORT_API BufferImage : public Image
69 * @brief Constructor which creates an uninitialized BufferImage object.
71 * Use BufferImage::New(...) to create an initialised object.
76 * @brief Create a new BufferImage.
78 * Also a pixel buffer for image data is allocated.
79 * Dali has ownership of the buffer.
80 * For better performance and portability use power of two dimensions.
81 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
82 * @note: default resource management policies are Immediate and Never
84 * @pre width & height are greater than zero
85 * @param [in] width image width in pixels
86 * @param [in] height image height in pixels
87 * @param [in] pixelformat the pixel format (rgba 32 bit by default)
88 * @return a handle to a new instance of BufferImage
90 static BufferImage New(unsigned int width,
92 Pixel::Format pixelformat=Pixel::RGBA8888);
95 * @brief Create a new BufferImage.
97 * Also a pixel buffer for image data is allocated.
98 * Dali has ownership of the buffer.
99 * For better performance and portability use power of two dimensions.
100 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
102 * @pre width & height are greater than zero
103 * @param [in] width Image width in pixels
104 * @param [in] height Image height in pixels
105 * @param [in] pixelFormat The pixel format
106 * @param [in] releasePolicy Optionally release memory when image is not visible on screen.
107 * @return a handle to a new instance of BufferImage
109 static BufferImage New(unsigned int width,
111 Pixel::Format pixelFormat,
112 ReleasePolicy releasePolicy);
115 * @brief Create a new BufferImage, which uses an external data source.
117 * The PixelBuffer has to be allocated by application.
119 * The application holds ownership of the buffer. It must not
120 * destroy the PixelBuffer on a staged image if it has called
121 * Update() and hasn't received a SignalUploaded, or if it has just
122 * added it to the stage and has not received a SignalUploaded.
124 * For better performance and portability use power of two dimensions.
125 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
127 * @pre width & height are greater than zero
128 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
129 * @param [in] width image width in pixels
130 * @param [in] height image height in pixels
131 * @param [in] pixelFormat the pixel format (rgba 32 bit by default)
132 * @param [in] stride the internal stride of the pixelbuffer in pixels
133 * @return a handle to a new instance of BufferImage
135 static BufferImage New(PixelBuffer* pixelBuffer,
138 Pixel::Format pixelFormat=Pixel::RGBA8888,
139 unsigned int stride=0);
142 * @brief Create a new BufferImage, which uses an external data source.
144 * The PixelBuffer has to be allocated by application.
146 * The application holds ownership of the buffer. It must not
147 * destroy the PixelBuffer on a staged image if it has called
148 * Update() and hasn't received a SignalUploaded, or if it has just
149 * added it to the stage and has not received a SignalUploaded.
151 * For better performance and portability use power of two dimensions.
152 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
154 * @note in case releasePolicy is "Unused", application has to call
155 * BufferImage::Update() whenever image is re-added to the stage
157 * @pre width & height are greater than zero
158 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
159 * @param [in] width image width in pixels
160 * @param [in] height image height in pixels
161 * @param [in] pixelFormat the pixel format
162 * @param [in] stride the internal stride of the pixelbuffer in pixels
163 * @param [in] releasePolicy optionally relase memory when image is not visible on screen.
164 * @return a handle to a new instance of BufferImage
166 static BufferImage New(PixelBuffer* pixelBuffer,
169 Pixel::Format pixelFormat,
171 ReleasePolicy releasePolicy);
174 * @brief Downcast an Object handle to BufferImage.
176 * If handle points to a BufferImage the downcast produces valid
177 * handle. If not the returned handle is left uninitialized.
179 * @param[in] handle to An object
180 * @return handle to a BufferImage or an uninitialized handle
182 static BufferImage DownCast( BaseHandle handle );
187 * This is non-virtual since derived Handle types must not contain data or virtual methods.
192 * @brief This copy constructor is required for (smart) pointer semantics.
194 * @param [in] handle A reference to the copied handle
196 BufferImage(const BufferImage& handle);
199 * @brief This assignment operator is required for (smart) pointer semantics.
201 * @param [in] rhs A reference to the copied handle
202 * @return A reference to this
204 BufferImage& operator=(const BufferImage& rhs);
207 * @brief White pixel as image data.
209 * Can be used to create solid color actors.
210 * @return 1 white pixel with 32 bit colordepth
212 static const BufferImage WHITE();
216 * @brief Returns the pixel buffer of the Image.
218 * The application can write to the buffer to modify its contents.
220 * Whilst the image is on stage, after writing to the buffer the
221 * application should call Update() and wait for the
222 * SignalUploaded() method before writing again.
224 * @return the pixel buffer
226 PixelBuffer* GetBuffer();
229 * @brief Returns buffer size in bytes.
231 * @return the buffer size in bytes
233 unsigned int GetBufferSize() const;
236 * @brief Returns buffer stride (in bytes).
238 * @return the buffer stride
240 unsigned int GetBufferStride() const;
243 * @brief Returns the pixel format of the contained buffer
245 * @return the pixel format
247 Pixel::Format GetPixelFormat() const;
250 * @brief Inform Dali that the contents of the buffer have changed.
252 * SignalUploaded will be sent in response if the image is on stage
253 * and the image data has been successfully copied to graphics
254 * memory. To avoid visual tearing, the application should wait for
255 * the SignalUploaded before modifying the data.
257 * The application must not destroy an external PixelBuffer on a staged
258 * image after calling this method until the SignalUploaded has been
259 * successfully received.
261 * @note: BufferImage::Update might not work with BGR/BGRA formats!
262 * @note: Some GPUs may not support Non power of two buffer updates (for
263 * example C110/SGX540)
269 * @param [in] updateArea area that has changed in buffer
271 void Update( RectArea updateArea );
274 * @brief Returns whether BufferImage uses external data source or not.
276 * If not, dali holds ownership of the PixelBuffer, otherwise the application
277 * is responsible for freeing it.
279 * The application must not destroy an external PixelBuffer on a staged image
280 * if it has called Update() and hasn't received a SignalUploaded.
282 * @return true if application owns data, false otherwise
284 bool IsDataExternal() const;
286 public: // Not intended for application developers
288 explicit DALI_INTERNAL BufferImage(Internal::BufferImage*);
293 #endif // __DALI_BUFFER_IMAGE_H__