1 #ifndef __DALI_INTEGRATION_BITMAP_H__
2 #define __DALI_INTEGRATION_BITMAP_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.
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/images/pixel.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/integration-api/resource-policies.h>
37 * Returns GL data type and internal format for specified pixel format
38 * @param[in] pixelformat pixel format (eg. Pixel::RGBA32)
39 * @param[out] pixelDataType pixel data type (eg. GL_UNSIGNED_BYTE)
40 * @param[out] internalFormat pixel internal format (eg. GL_RGBA)
42 DALI_IMPORT_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
45 typedef IntrusivePtr<Bitmap> BitmapPtr;
46 typedef unsigned char PixelBuffer; ///< Pixel data buffers are composed of these
50 * An abstract container for image data.
52 class DALI_IMPORT_API Bitmap : public Dali::RefObject
58 * Use the static function Bitmap::New() to create instances.
59 * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
60 * @param[in] pixBuf External buffer of pixel data or null.
62 Bitmap( ResourcePolicy::Discardable discardable = ResourcePolicy::OWNED_RETAIN, Dali::Integration::PixelBuffer* pixBuf = 0 );
65 * Initializes internal class members
66 * @param[in] pixelFormat pixel format
67 * @param[in] width Image width in pixels
68 * @param[in] height Image height in pixels
70 void Initialize(Pixel::Format pixelFormat,
76 /** Defines the characteristics of the Bitmap returned from the factory
80 /** A 2D array of pixels where each pixel is a whole number of bytes
81 * and each scanline of the backing memory buffer may have additional
82 * bytes off the right edge if requested, and there may be additional
83 * scanlines past the bottom of the image in the buffer if requested.*/
84 BITMAP_2D_PACKED_PIXELS,
85 /** The data for the bitmap is buffered in an opaque form.*/
91 FREE, ///< Use free function to release the buffer
92 DELETE_ARRAY, ///< Use delete[] operator to release the buffer
96 * Create a new instance of a Bitmap with the required profile.
97 * @return Pointer to created Bitmap subclass. Clients should immediately
98 * wrap this in a reference-counting smart pointer or store it in a similarly
99 * automatic owning collection.
100 * @param[in] profile Defines required features of the bitmap (\sa Profile).
101 * @param[in] discardable OWNED_DISCARD means that the data is owned by bitmap,
102 * and may released away after uploading to GPU.
103 * OWNED_RETAIN means that the data is owned and must be kept in CPU memory
104 * e.g. for an image that cannot be reloaded from disk.
106 static Bitmap* New( Profile profile, ResourcePolicy::Discardable discardable );
108 /** \name GeneralFeatures
109 * Features that are generic between profiles. */
113 * Get the width of the image
114 * @return The width of the image
116 unsigned int GetImageWidth() const
122 * Get the height of the image
123 * @return The height of the image
125 unsigned int GetImageHeight() const
131 * Get the pixel format
132 * @return The pixel format
134 Pixel::Format GetPixelFormat() const
140 * Get the pixel buffer if it's present.
141 * @return The buffer if present, or NULL if there is no pixel buffer.
142 * You can modify its contents.
143 * @sa ReserveBuffer GetBufferSize
145 virtual PixelBuffer* GetBuffer()
151 * Get the pixel buffer if it's present and take over the ownership.
152 * @note With this function called, the bitmap loses the ownership and is no longer responsible for the release of pixel buffer.
153 * @return The raw pointer pointing to the pixel buffer
155 PixelBuffer* GetBufferOwnership();
158 * Get the pixel buffer size in bytes
159 * @return The buffer size in bytes.
160 * @sa ReserveBuffer GetBuffer
162 virtual size_t GetBufferSize() const = 0;
165 * Queries if the bitmap has an alpha channel
166 * @return true if there is an alpha channel
168 bool HasAlphaChannel() const
170 return mHasAlphaChannel;
174 * Queries if the bitmap has any transparent data
175 * @return true if the bitmap has alpha data
177 bool IsFullyOpaque() const
179 // check pixel format for alpha channel
180 return !(HasAlphaChannel() && mAlphaChannelUsed);
184 * Returns which release function has to be called to release the data in the bitmap
185 * @return FREE if memory has been allocated with malloc DELETE_ARRAY if memory has been allocated with new
187 virtual ReleaseFunction GetReleaseFunction() = 0;
189 /**@}*/ ///< End of generic features
192 /** \name PackedPixelsProfile
193 * Features that are active only if the Bitmap was created with a
194 * BITMAP_2D_PACKED_PIXELS profile. */
197 class PackedPixelsProfile
202 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
203 * Dali has ownership of the buffer, but its contents can be modified.
204 * Bitmap stores given size information about the image.
205 * @pre bufferWidth, bufferHeight have to be power of two
206 * @param[in] pixelFormat pixel format
207 * @param[in] width Image width in pixels
208 * @param[in] height Image height in pixels
209 * @param[in] bufferWidth Buffer width (stride) in pixels
210 * @param[in] bufferHeight Buffer height in pixels
211 * @return pixel buffer pointer
213 virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
216 unsigned int bufferWidth = 0,
217 unsigned int bufferHeight = 0) = 0;
220 * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
221 * Dali has ownership of the buffer, but it iss allowable to modify its
222 * contents after it is assigned, but before it is used.
223 * Bitmap stores the provided size information about the image.
225 * The buffer must have been allocated with the C++ array new operator, not
226 * with malloc or as a local or static object. The precise form is as follows:
228 * PixelBuffer * buffer = new PixelBuffer[bufSize];
230 * @pre bufferWidth, bufferHeight have to be power of two
231 * @param[in] pixelFormat pixel format
232 * @param[in] buffer the pixel buffer
233 * @param[in] bufferSize size of the pixel buffer
234 * @param[in] width Image width in pixels
235 * @param[in] height Image height in pixels
236 * @param[in] bufferWidth Buffer width (stride) in pixels
237 * @param[in] bufferHeight Buffer height in pixels
239 virtual void AssignBuffer(Pixel::Format pixelFormat,
241 std::size_t bufferSize,
244 unsigned int bufferWidth = 0,
245 unsigned int bufferHeight = 0) = 0;
247 * Get the width of the buffer (stride)
248 * @return The width of the buffer in pixels
250 virtual unsigned int GetBufferWidth() const = 0;
253 * Get the height of the buffer
254 * @return The height of the buffer in pixels
256 virtual unsigned int GetBufferHeight() const = 0;
259 * Get the pixel buffer stride.
260 * @return The buffer stride (in bytes) if this is bitmap of non-compressed
261 * packed pixels for which a stride is meaningful or 0 otherwise.
263 virtual unsigned int GetBufferStride() const = 0;
266 * Check the bitmap data and test whether it has any transparent pixels.
267 * This property can then be tested for with IsFullyOpaque().
269 virtual void TestForTransparency() = 0;
273 * Get interface to features that are active only if the Bitmap was created
274 * with a BITMAP_2D_PACKED_PIXELS profile. */
275 virtual const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
277 * Get interface to features that are active only if the Bitmap was created
278 * with a BITMAP_2D_PACKED_PIXELS profile. */
279 virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
281 /**@}*/ ///< End of packed pixel features.
284 /** \name CompressedProfile
285 * Features that only apply to opaque/compressed formats. */
288 class CompressedProfile
292 * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer
294 * Dali has ownership of the buffer, and contents are opaque and immutable.
295 * Bitmap stores given size information about the image which the client is assumed
296 * to have retrieved from out-of-band image metadata.
297 * @param[in] pixelFormat pixel format
298 * @param[in] width Image width in pixels
299 * @param[in] height Image height in pixels
300 * @param[in] bufferSize Buffer size in bytes
301 * @return pixel buffer pointer
303 virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
304 const unsigned width,
305 const unsigned height,
306 const size_t numBytes ) = 0;
309 virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
310 virtual CompressedProfile* GetCompressedProfile() { return 0; }
315 * Inform the bitmap that its pixel buffer is no longer required and can be
316 * deleted to free up memory if the bitmap owns the buffer.
318 void DiscardBuffer();
321 * Check if the pixel buffer can be discarded
322 * @return true if the pixel buffer can be discarded
324 bool IsDiscardable() const
326 return mDiscardable == ResourcePolicy::OWNED_DISCARD;
330 * Delete the pixel buffer data
332 void DeletePixelBuffer();
335 * A reference counted object may only be deleted by calling Unreference()
341 unsigned int mImageWidth; ///< Image width in pixels
342 unsigned int mImageHeight; ///< Image height in pixels
343 Pixel::Format mPixelFormat; ///< Pixel format
344 bool mHasAlphaChannel; ///< Whether the image has an alpha channel
345 bool mAlphaChannelUsed; ///< Whether the alpha channel is used in case the image owns one.
346 PixelBuffer* mData; ///< Raw pixel data
350 ResourcePolicy::Discardable mDiscardable; ///< Should delete the buffer when discard buffer is called.
352 Bitmap(const Bitmap& other); ///< defined private to prevent use
353 Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
355 // Changes scope, should be at end of class
356 DALI_LOG_OBJECT_STRING_DECLARATION;
359 } // namespace Integration
363 #endif // __DALI_INTEGRATION_BITMAP_H__