Merge "Updates to handle context loss and regain" into tizen
[platform/core/uifw/dali-core.git] / dali / integration-api / bitmap.h
1 #ifndef __DALI_INTEGRATION_BITMAP_H__
2 #define __DALI_INTEGRATION_BITMAP_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22
23 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 namespace Integration
34 {
35
36 /**
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)
41  */
42 DALI_IMPORT_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
43
44 class Bitmap;
45 typedef IntrusivePtr<Bitmap>    BitmapPtr;
46 typedef unsigned char                 PixelBuffer;  ///< Pixel data buffers are composed of these
47
48 /**
49  * Bitmap class.
50  * An abstract container for image data.
51  * \sa{BitmapPackedPixel BitmapCompressed BitmapExternal} for concrete
52  * subclasses.
53  */
54 class DALI_IMPORT_API Bitmap : public Dali::RefObject
55 {
56 protected:
57
58   /**
59    * Constructor
60    * Use the static function Bitmap::New() to create instances.
61    * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
62    * @param[in] pixBuf External buffer of pixel data or null.
63    */
64   Bitmap( ResourcePolicy::Discardable discardable = ResourcePolicy::RETAIN, Dali::Integration::PixelBuffer* pixBuf = 0 );
65
66   /**
67    * Initializes internal class members
68    * @param[in] pixelFormat   pixel format
69    * @param[in] width         Image width in pixels
70    * @param[in] height        Image height in pixels
71    */
72   void Initialize(Pixel::Format pixelFormat,
73                            unsigned int width,
74                            unsigned int height);
75
76
77 public:
78   /** Defines the characteristics of the Bitmap returned from the factory
79    *  function. */
80   enum Profile
81   {
82     /** A 2D array of pixels where each pixel is a whole number of bytes
83      * and each scanline of the backing memory buffer may have additional
84      * bytes off the right edge if requested, and there may be additional
85      * scanlines past the bottom of the image in the buffer if requested.*/
86     BITMAP_2D_PACKED_PIXELS,
87     /** The data for the bitmap is buffered in an opaque form.*/
88     BITMAP_COMPRESSED
89   };
90
91   /**
92    * Create a new instance of a Bitmap with the required profile.
93    * @return Pointer to created Bitmap subclass. Clients should immediately
94    * wrap this in a reference-counting smart pointer or store it in a similarly
95    * automatic owning collection.
96    * @param[in] profile Defines required features of the bitmap (\sa Profile).
97    * @param[in] discardable If this is set to DISCARD, the bitmap
98    * object owns it's own buffer of pixel data and can delete it. If
99    * it's set to RETAIN, then the lifetime of the pixel buffer is
100    * managed by an external component and is guaranteed to remain
101    * dereferenceable at least as long as the Bitmap remains alive.
102    **/
103   static Bitmap* New( Profile profile, ResourcePolicy::Discardable discardable );
104
105   /** \name GeneralFeatures
106    * Features that are generic between profiles. */
107   /**@{*/
108
109   /**
110    * Get the width of the image
111    * @return The width of the image
112    */
113   unsigned int GetImageWidth() const
114   {
115     return mImageWidth;
116   }
117
118   /**
119    * Get the height of the image
120    * @return The height of the image
121    */
122   unsigned int GetImageHeight() const
123   {
124     return mImageHeight;
125   }
126
127   /**
128    * Get the pixel format
129    * @return The pixel format
130    */
131   Pixel::Format GetPixelFormat() const
132   {
133     return mPixelFormat;
134   }
135
136   /**
137    * Get the pixel buffer if it's present.
138    * @return The buffer if present, or NULL if there is no pixel buffer.
139    * You can modify its contents.
140    * @sa ReserveBuffer GetBufferSize
141    */
142   virtual PixelBuffer* GetBuffer()
143   {
144     return mData;
145   }
146
147   /**
148    * Get the pixel buffer size in bytes
149    * @return The buffer size in bytes.
150    * @sa ReserveBuffer GetBuffer
151    */
152   virtual size_t GetBufferSize() const = 0;
153
154   /**
155    * Queries if the bitmap has an alpha channel
156    * @return true if there is an alpha channel
157    */
158   bool HasAlphaChannel() const
159   {
160     return mHasAlphaChannel;
161   }
162
163   /**
164    * Queries if the bitmap has any transparent data
165    * @return true if the bitmap has alpha data
166    */
167   bool IsFullyOpaque() const
168   {
169     // check pixel format for alpha channel
170     return !(HasAlphaChannel() && mAlphaChannelUsed);
171   }
172
173   /**@}*/ ///< End of generic features
174
175
176   /** \name PackedPixelsProfile
177    * Features that are active only if the Bitmap was created with a
178    * BITMAP_2D_PACKED_PIXELS profile. */
179   /**@{*/
180
181   class PackedPixelsProfile
182   {
183   public:
184
185     /**
186      * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
187      * Dali has ownership of the buffer, but its contents can be modified.
188      * Bitmap stores given size information about the image.
189      * @pre bufferWidth, bufferHeight have to be power of two
190      * @param[in] pixelFormat   pixel format
191      * @param[in] width         Image width in pixels
192      * @param[in] height        Image height in pixels
193      * @param[in] bufferWidth   Buffer width (stride) in pixels
194      * @param[in] bufferHeight  Buffer height in pixels
195      * @return pixel buffer pointer
196      */
197     virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
198                                        unsigned int width,
199                                        unsigned int height,
200                                        unsigned int bufferWidth = 0,
201                                        unsigned int bufferHeight = 0) = 0;
202
203     /**
204      * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
205      * Dali has ownership of the buffer, but it iss allowable to modify its
206      * contents after it is assigned, but before it is used.
207      * Bitmap stores the provided size information about the image.
208      *
209      * The buffer must have been allocated with the C++ array new operator, not
210      * with malloc or as a local or static object. The precise form is as follows:
211      *
212      *    PixelBuffer * buffer = new PixelBuffer[bufSize];
213      *
214      * @pre bufferWidth, bufferHeight have to be power of two
215      * @param[in] pixelFormat   pixel format
216      * @param[in] buffer        the pixel buffer
217      * @param[in] bufferSize    size of the pixel buffer
218      * @param[in] width         Image width in pixels
219      * @param[in] height        Image height in pixels
220      * @param[in] bufferWidth   Buffer width (stride) in pixels
221      * @param[in] bufferHeight  Buffer height in pixels
222      */
223     virtual void AssignBuffer(Pixel::Format pixelFormat,
224                               PixelBuffer* buffer,
225                               std::size_t bufferSize,
226                               unsigned int width,
227                               unsigned int height,
228                               unsigned int bufferWidth = 0,
229                               unsigned int bufferHeight = 0) = 0;
230     /**
231      * Get the width of the buffer (stride)
232      * @return The width of the buffer in pixels
233      */
234     virtual unsigned int GetBufferWidth() const = 0;
235
236     /**
237      * Get the height of the buffer
238      * @return The height of the buffer in pixels
239      */
240     virtual unsigned int GetBufferHeight() const = 0;
241
242     /**
243      * Get the pixel buffer stride.
244      * @return The buffer stride (in bytes) if this is bitmap of non-compressed
245      * packed pixels for which a stride is meaningful or 0 otherwise.
246      */
247     virtual unsigned int GetBufferStride() const = 0;
248
249     /**
250      * Check the bitmap data and test whether it has any transparent pixels.
251      * This property can then be tested for with IsFullyOpaque().
252      */
253     virtual void TestForTransparency() = 0;
254   };
255
256   /**
257    * Get interface to features that are active only if the Bitmap was created
258    * with a BITMAP_2D_PACKED_PIXELS profile. */
259   virtual const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
260   /**
261    * Get interface to features that are active only if the Bitmap was created
262    * with a BITMAP_2D_PACKED_PIXELS profile. */
263   virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
264
265   /**@}*/ ///< End of packed pixel features.
266
267
268   /** \name CompressedProfile
269    * Features that only apply to opaque/compressed formats. */
270   /**@{*/
271
272   class CompressedProfile
273   {
274   public:
275     /**
276      * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer
277      * is deleted.
278      * Dali has ownership of the buffer, and contents are opaque and immutable.
279      * Bitmap stores given size information about the image which the client is assumed
280      * to have retrieved from out-of-band image metadata.
281      * @param[in] pixelFormat   pixel format
282      * @param[in] width         Image width in pixels
283      * @param[in] height        Image height in pixels
284      * @param[in] bufferSize    Buffer size in bytes
285      * @return pixel buffer pointer
286      */
287     virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
288                                        const unsigned width,
289                                        const unsigned height,
290                                        const size_t numBytes ) = 0;
291   };
292
293   virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
294   virtual CompressedProfile* GetCompressedProfile() { return 0; }
295   /**@}*/
296
297
298   /**
299    * Inform the bitmap that its pixel buffer is no longer required and can be
300    * deleted to free up memory if the bitmap owns the buffer.
301    */
302   void DiscardBuffer();
303
304   /**
305    * Check if the pixel buffer can be discarded
306    * @return true if the pixel buffer can be discarded
307    */
308   bool IsDiscardable() const
309   {
310     return mDiscardable == ResourcePolicy::DISCARD;
311   }
312
313  /**
314    * Transfer ownership of the pixel buffer to the calling function.
315    * @post bitmaps pixel data is set to NULL
316    * @return the bitmaps pixel buffer
317    */
318   PixelBuffer* ReleaseBuffer();
319   /**
320    * Delete the pixel buffer data
321    */
322   void DeletePixelBuffer();
323
324   /**
325    * A reference counted object may only be deleted by calling Unreference()
326    */
327   virtual ~Bitmap();
328
329 protected:
330
331   unsigned int  mImageWidth;          ///< Image width in pixels
332   unsigned int  mImageHeight;         ///< Image height in pixels
333   Pixel::Format mPixelFormat;         ///< Pixel format
334   bool          mHasAlphaChannel;   ///< Whether the image has an alpha channel
335   bool          mAlphaChannelUsed;  ///< Whether the alpha channel is used in case the image owns one.
336   PixelBuffer*  mData;            ///< Raw pixel data
337
338 private:
339
340   ResourcePolicy::Discardable mDiscardable; ///< Should delete the buffer when discard buffer is called.
341
342   Bitmap(const Bitmap& other);  ///< defined private to prevent use
343   Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
344
345   // Changes scope, should be at end of class
346   DALI_LOG_OBJECT_STRING_DECLARATION;
347 };
348
349 } // namespace Integration
350
351 } // namespace Dali
352
353 #endif // __DALI_INTEGRATION_BITMAP_H__