1 #ifndef __DALI_BITMAP_IMAGE_H__
2 #define __DALI_BITMAP_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/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 BitmapImage 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 BitmapImage : public Image
69 * @brief Constructor which creates an uninitialized BitmapImage object.
71 * Use Image::New(...) to create an initialised object.
76 * @brief Create a new BitmapImage.
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 BitmapImage
90 static BitmapImage New(unsigned int width,
92 Pixel::Format pixelformat=Pixel::RGBA8888);
95 * @brief Create a new BitmapImage.
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] loadPolicy Controls time of loading a resource from the filesystem.
107 * @param [in] releasePolicy Optionally release memory when image is not visible on screen.
108 * @return a handle to a new instance of BitmapImage
110 static BitmapImage New(unsigned int width,
112 Pixel::Format pixelFormat,
113 LoadPolicy loadPolicy,
114 ReleasePolicy releasePolicy);
117 * @brief Create a new BitmapImage, which uses an external data source.
119 * The PixelBuffer has to be allocated by application.
121 * The application holds ownership of the buffer. It must not
122 * destroy the PixelBuffer on a staged image if it has called
123 * Update() and hasn't received a SignalUploaded, or if it has just
124 * added it to the stage and has not received a SignalUploaded.
126 * For better performance and portability use power of two dimensions.
127 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
129 * @pre width & height are greater than zero
130 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
131 * @param [in] width image width in pixels
132 * @param [in] height image height in pixels
133 * @param [in] pixelFormat the pixel format (rgba 32 bit by default)
134 * @param [in] stride the internal stride of the pixelbuffer in pixels
135 * @return a handle to a new instance of BitmapImage
137 static BitmapImage New(PixelBuffer* pixelBuffer,
140 Pixel::Format pixelFormat=Pixel::RGBA8888,
141 unsigned int stride=0);
144 * @brief Create a new BitmapImage, which uses an external data source.
146 * The PixelBuffer has to be allocated by application.
148 * The application holds ownership of the buffer. It must not
149 * destroy the PixelBuffer on a staged image if it has called
150 * Update() and hasn't received a SignalUploaded, or if it has just
151 * added it to the stage and has not received a SignalUploaded.
153 * For better performance and portability use power of two dimensions.
154 * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
156 * @note in case releasePolicy is "Unused", application has to call
157 * BitmapImage::Update() whenever image is re-added to the stage
159 * @pre width & height are greater than zero
160 * @param [in] pixelBuffer pixel buffer. has to be allocated by application.
161 * @param [in] width image width in pixels
162 * @param [in] height image height in pixels
163 * @param [in] pixelFormat the pixel format
164 * @param [in] stride the internal stride of the pixelbuffer in pixels
165 * @param [in] releasePolicy optionally relase memory when image is not visible on screen.
166 * @return a handle to a new instance of BitmapImage
168 static BitmapImage New(PixelBuffer* pixelBuffer,
171 Pixel::Format pixelFormat,
173 ReleasePolicy releasePolicy);
176 * @brief Downcast an Object handle to BitmapImage.
178 * If handle points to a BitmapImage the downcast produces valid
179 * handle. If not the returned handle is left uninitialized.
181 * @param[in] handle to An object
182 * @return handle to a BitmapImage or an uninitialized handle
184 static BitmapImage DownCast( BaseHandle handle );
189 * This is non-virtual since derived Handle types must not contain data or virtual methods.
194 * @brief This copy constructor is required for (smart) pointer semantics.
196 * @param [in] handle A reference to the copied handle
198 BitmapImage(const BitmapImage& handle);
201 * @brief This assignment operator is required for (smart) pointer semantics.
203 * @param [in] rhs A reference to the copied handle
204 * @return A reference to this
206 BitmapImage& operator=(const BitmapImage& rhs);
209 * @brief White pixel as image data.
211 * Can be used to create solid color actors.
212 * @return 1 white pixel with 32 bit colordepth
214 static const BitmapImage WHITE();
218 * @brief Returns the pixel buffer of the Image.
220 * The application can write to the buffer to modify its contents.
222 * Whilst the image is on stage, after writing to the buffer the
223 * application should call Update() and wait for the
224 * SignalUploaded() method before writing again.
226 * @return the pixel buffer
228 PixelBuffer* GetBuffer();
231 * @brief Returns buffer size in bytes.
233 * @return the buffer size in bytes
235 unsigned int GetBufferSize() const;
238 * @brief Returns buffer stride (in bytes).
240 * @return the buffer stride
242 unsigned int GetBufferStride() const;
245 * @brief Returns the pixel format of the contained bitmap
247 * @return the pixel format
249 Pixel::Format GetPixelFormat() const;
252 * @brief Inform Dali that the contents of the buffer have changed.
254 * SignalUploaded will be sent in response if the image is on stage
255 * and the image data has been successfully copied to graphics
256 * memory. To avoid visual tearing, the application should wait for
257 * the SignalUploaded before modifying the data.
259 * The application must not destroy an external PixelBuffer on a staged
260 * image after calling this method until the SignalUploaded has been
261 * successfully received.
263 * @note: BitmapImage::Update might not work with BGR/BGRA formats!
264 * @note: Some GPUs may not support Non power of two buffer updates (for
265 * example C110/SGX540)
271 * @param [in] updateArea area that has changed in buffer
273 void Update( RectArea updateArea );
276 * @brief Returns whether BitmapImage uses external data source or not.
278 * If not, dali holds ownership of the PixelBuffer, otherwise the application
279 * is responsible for freeing it.
281 * The application must not destroy an external PixelBuffer on a staged image
282 * if it has called Update() and hasn't received a SignalUploaded.
284 * @return true if application owns data, false otherwise
286 bool IsDataExternal() const;
288 public: // Not intended for application developers
290 explicit DALI_INTERNAL BitmapImage(Internal::BitmapImage*);
295 #endif // __DALI_BITMAP_IMAGE_H__