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