Merge "Updated all code to new format" into devel/master
[platform/core/uifw/dali-core.git] / dali / integration-api / bitmap.h
index d196f2d..1500f12 100644 (file)
@@ -1,64 +1,62 @@
-#ifndef __DALI_INTEGRATION_BITMAP_H__
-#define __DALI_INTEGRATION_BITMAP_H__
-
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+#ifndef DALI_INTEGRATION_BITMAP_H
+#define DALI_INTEGRATION_BITMAP_H
+
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // EXTERNAL INCLUDES
 
 // INTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/resource-policies.h>
 #include <dali/public-api/common/intrusive-ptr.h>
 #include <dali/public-api/images/pixel.h>
 #include <dali/public-api/object/ref-object.h>
 
 namespace Dali
 {
-
 namespace Integration
 {
-
 /**
  * Returns GL data type and internal format for specified pixel format
  * @param[in]  pixelformat    pixel format (eg. Pixel::RGBA32)
  * @param[out] pixelDataType  pixel data type (eg. GL_UNSIGNED_BYTE)
  * @param[out] internalFormat pixel internal format (eg. GL_RGBA)
  */
-DALI_IMPORT_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
+DALI_CORE_API void ConvertToGlFormat(Pixel::Format pixelformat, unsigned& pixelDataType, unsigned& internalFormat);
 
 class Bitmap;
-typedef IntrusivePtr<Bitmap>    BitmapPtr;
-typedef unsigned char                 PixelBuffer;  ///< Pixel data buffers are composed of these
+using BitmapPtr   = IntrusivePtr<Bitmap>;
+using PixelBuffer = uint8_t; ///< Pixel data buffers are composed of these
 
 /**
  * Bitmap class.
  * An abstract container for image data.
- * \sa{BitmapPackedPixel BitmapCompressed BitmapExternal} for concrete
- * subclasses.
  */
-class DALI_IMPORT_API Bitmap : public Dali::RefObject
+class DALI_CORE_API Bitmap : public Dali::RefObject
 {
 protected:
   /**
    * Constructor
-   * Use the static function Bitmap::Create() to create instances.
+   * Use the static function Bitmap::New() to create instances.
    * @param[in] discardable Flag to tell the bitmap if it can delete the buffer with the pixel data.
    * @param[in] pixBuf External buffer of pixel data or null.
    */
-  Bitmap( bool discardable = false, Dali::Integration::PixelBuffer* pixBuf = 0 );
+  Bitmap(ResourcePolicy::Discardable discardable = ResourcePolicy::OWNED_RETAIN, Dali::Integration::PixelBuffer* pixBuf = nullptr);
 
   /**
    * Initializes internal class members
@@ -67,9 +65,8 @@ protected:
    * @param[in] height        Image height in pixels
    */
   void Initialize(Pixel::Format pixelFormat,
-                           unsigned int width,
-                           unsigned int height);
-
+                  uint32_t      width,
+                  uint32_t      height);
 
 public:
   /** Defines the characteristics of the Bitmap returned from the factory
@@ -85,18 +82,24 @@ public:
     BITMAP_COMPRESSED
   };
 
+  enum ReleaseFunction
+  {
+    FREE,         ///< Use free function to release the buffer
+    DELETE_ARRAY, ///< Use delete[] operator to release the buffer
+  };
+
   /**
-   * Create a new instance of a Bitmap with the profile asked for.
+   * Create a new instance of a Bitmap with the required profile.
    * @return Pointer to created Bitmap subclass. Clients should immediately
    * wrap this in a reference-counting smart pointer or store it in a similarly
    * automatic owning collection.
    * @param[in] profile Defines required features of the bitmap (\sa Profile).
-   * @param[in] managePixelBuffer If true, the bitmap object owns it's own
-   * buffer of pixel data and can delete it, else the lifetime of the pixel
-   * buffer is managed by an external component and is guaranteed to remain
-   * dereferenceable at least as long as the Bitmap remains alive.
-   **/
-  static Bitmap* New(Profile profile, bool managePixelBuffer);
+   * @param[in] discardable OWNED_DISCARD means that the data is owned by bitmap,
+   * and may released away after uploading to GPU.
+   * OWNED_RETAIN means that the data is owned and must be kept in CPU memory
+   * e.g. for an image that cannot be reloaded from disk.
+   */
+  static Bitmap* New(Profile profile, ResourcePolicy::Discardable discardable);
 
   /** \name GeneralFeatures
    * Features that are generic between profiles. */
@@ -106,7 +109,7 @@ public:
    * Get the width of the image
    * @return The width of the image
    */
-  unsigned int GetImageWidth() const
+  uint32_t GetImageWidth() const
   {
     return mImageWidth;
   }
@@ -115,7 +118,7 @@ public:
    * Get the height of the image
    * @return The height of the image
    */
-  unsigned int GetImageHeight() const
+  uint32_t GetImageHeight() const
   {
     return mImageHeight;
   }
@@ -130,22 +133,29 @@ public:
   }
 
   /**
-   * Get the pixel buffer
-   * @return The buffer. You can modify its contents.
+   * Get the pixel buffer if it's present.
+   * @return The buffer if present, or NULL if there is no pixel buffer.
+   * You can modify its contents.
    * @sa ReserveBuffer GetBufferSize
    */
   virtual PixelBuffer* GetBuffer()
   {
-    DALI_ASSERT_DEBUG(mData != NULL);
     return mData;
   }
 
   /**
+   * Get the pixel buffer if it's present and take over the ownership.
+   * @note With this function called, the bitmap loses the ownership and is no longer responsible for the release of pixel buffer.
+   * @return The raw pointer pointing to the pixel buffer
+   */
+  PixelBuffer* GetBufferOwnership();
+
+  /**
    * Get the pixel buffer size in bytes
    * @return The buffer size in bytes.
    * @sa ReserveBuffer GetBuffer
    */
-  virtual size_t GetBufferSize() const = 0;
+  virtual uint32_t GetBufferSize() const = 0;
 
   /**
    * Queries if the bitmap has an alpha channel
@@ -166,8 +176,13 @@ public:
     return !(HasAlphaChannel() && mAlphaChannelUsed);
   }
 
-  /**@}*/ ///< End of generic features
+  /**
+   * Returns which release function has to be called to release the data in the bitmap
+   * @return FREE if memory has been allocated with malloc DELETE_ARRAY if memory has been allocated with new
+   */
+  virtual ReleaseFunction GetReleaseFunction() = 0;
 
+  /**@}*/ ///< End of generic features
 
   /** \name PackedPixelsProfile
    * Features that are active only if the Bitmap was created with a
@@ -177,7 +192,6 @@ public:
   class PackedPixelsProfile
   {
   public:
-
     /**
      * (Re-)Allocate pixel buffer for the Bitmap. Any previously allocated pixel buffer is deleted.
      * Dali has ownership of the buffer, but its contents can be modified.
@@ -191,10 +205,10 @@ public:
      * @return pixel buffer pointer
      */
     virtual PixelBuffer* ReserveBuffer(Pixel::Format pixelFormat,
-                                       unsigned int width,
-                                       unsigned int height,
-                                       unsigned int bufferWidth = 0,
-                                       unsigned int bufferHeight = 0) = 0;
+                                       uint32_t      width,
+                                       uint32_t      height,
+                                       uint32_t      bufferWidth  = 0,
+                                       uint32_t      bufferHeight = 0) = 0;
 
     /**
      * Assign a pixel buffer. Any previously allocated pixel buffer is deleted.
@@ -217,50 +231,61 @@ public:
      * @param[in] bufferHeight  Buffer height in pixels
      */
     virtual void AssignBuffer(Pixel::Format pixelFormat,
-                              PixelBuffer* buffer,
-                              std::size_t bufferSize,
-                              unsigned int width,
-                              unsigned int height,
-                              unsigned int bufferWidth = 0,
-                              unsigned int bufferHeight = 0) = 0;
+                              PixelBuffer*  buffer,
+                              uint32_t      bufferSize,
+                              uint32_t      width,
+                              uint32_t      height,
+                              uint32_t      bufferWidth  = 0,
+                              uint32_t      bufferHeight = 0) = 0;
     /**
      * Get the width of the buffer (stride)
      * @return The width of the buffer in pixels
      */
-    virtual unsigned int GetBufferWidth() const = 0;
+    virtual uint32_t GetBufferWidth() const = 0;
 
     /**
      * Get the height of the buffer
      * @return The height of the buffer in pixels
      */
-    virtual unsigned int GetBufferHeight() const = 0;
+    virtual uint32_t GetBufferHeight() const = 0;
 
     /**
      * Get the pixel buffer stride.
      * @return The buffer stride (in bytes) if this is bitmap of non-compressed
      * packed pixels for which a stride is meaningful or 0 otherwise.
      */
-    virtual unsigned int GetBufferStride() const = 0;
+    virtual uint32_t GetBufferStride() const = 0;
 
     /**
      * Check the bitmap data and test whether it has any transparent pixels.
      * This property can then be tested for with IsFullyOpaque().
      */
     virtual void TestForTransparency() = 0;
+
+  protected:
+    /**
+     * Virtual destructor, no deletion through this interface
+     */
+    virtual ~PackedPixelsProfile() = default;
   };
 
   /**
    * Get interface to features that are active only if the Bitmap was created
    * with a BITMAP_2D_PACKED_PIXELS profile. */
-  virtual const PackedPixelsProfile* GetPackedPixelsProfile() const { return 0; }
+  virtual const PackedPixelsProfile* GetPackedPixelsProfile() const
+  {
+    return nullptr;
+  }
   /**
    * Get interface to features that are active only if the Bitmap was created
    * with a BITMAP_2D_PACKED_PIXELS profile. */
-  virtual PackedPixelsProfile* GetPackedPixelsProfile() { return 0; }
+  virtual PackedPixelsProfile* GetPackedPixelsProfile()
+  {
+    return nullptr;
+  }
 
   /**@}*/ ///< End of packed pixel features.
 
-
   /** \name CompressedProfile
    * Features that only apply to opaque/compressed formats. */
   /**@{*/
@@ -280,17 +305,28 @@ public:
      * @param[in] bufferSize    Buffer size in bytes
      * @return pixel buffer pointer
      */
-    virtual PixelBuffer* ReserveBufferOfSize( Pixel::Format pixelFormat,
-                                       const unsigned width,
-                                       const unsigned height,
-                                       const size_t numBytes ) = 0;
+    virtual PixelBuffer* ReserveBufferOfSize(Pixel::Format  pixelFormat,
+                                             const unsigned width,
+                                             const unsigned height,
+                                             const uint32_t numBytes) = 0;
+
+  protected:
+    /**
+     * Virtual destructor, no deletion through this interface
+     */
+    virtual ~CompressedProfile() = default;
   };
 
-  virtual const CompressedProfile* GetCompressedProfile() const { return 0; }
-  virtual CompressedProfile* GetCompressedProfile() { return 0; }
+  virtual const CompressedProfile* GetCompressedProfile() const
+  {
+    return nullptr;
+  }
+  virtual CompressedProfile* GetCompressedProfile()
+  {
+    return nullptr;
+  }
   /**@}*/
 
-
   /**
    * Inform the bitmap that its pixel buffer is no longer required and can be
    * deleted to free up memory if the bitmap owns the buffer.
@@ -298,45 +334,38 @@ public:
   void DiscardBuffer();
 
   /**
-   * @return True if the buffer of pixel data is owned by this Bitmap itself,
-   * or false if the buffer is owned by an external component.
-   **/
-  bool BufferIsOwned() const
+   * Check if the pixel buffer can be discarded
+   * @return true if the pixel buffer can be discarded
+   */
+  bool IsDiscardable() const
   {
-    return mDataIsOwned;
+    return mDiscardable == ResourcePolicy::OWNED_DISCARD;
   }
 
- /**
-   * Transfer ownership of the pixel buffer to the calling function.
-   * @post bitmaps pixel data is set to NULL
-   * @return the bitmaps pixel buffer
-   */
-  PixelBuffer* ReleaseBuffer();
   /**
    * Delete the pixel buffer data
    */
   void DeletePixelBuffer();
 
+protected:
   /**
    * A reference counted object may only be deleted by calling Unreference()
    */
-  virtual ~Bitmap();
+  ~Bitmap() override;
 
 protected:
-
-  unsigned int  mImageWidth;          ///< Image width in pixels
-  unsigned int  mImageHeight;         ///< Image height in pixels
-  Pixel::Format mPixelFormat;         ///< Pixel format
-  bool          mHasAlphaChannel;   ///< Whether the image has an alpha channel
-  bool          mAlphaChannelUsed;  ///< Whether the alpha channel is used in case the image owns one.
-  PixelBuffer*  mData;            ///< Raw pixel data
+  uint32_t      mImageWidth;       ///< Image width in pixels
+  uint32_t      mImageHeight;      ///< Image height in pixels
+  Pixel::Format mPixelFormat;      ///< Pixel format
+  bool          mHasAlphaChannel;  ///< Whether the image has an alpha channel
+  bool          mAlphaChannelUsed; ///< Whether the alpha channel is used in case the image owns one.
+  PixelBuffer*  mData;             ///< Raw pixel data
 
 private:
+  ResourcePolicy::Discardable mDiscardable; ///< Should delete the buffer when discard buffer is called.
 
-  bool          mDataIsOwned;     ///< Should delete the buffer when discard buffer is called.
-
-  Bitmap(const Bitmap& other);  ///< defined private to prevent use
-  Bitmap& operator = (const Bitmap& other); ///< defined private to prevent use
+  Bitmap(const Bitmap& other);            ///< defined private to prevent use
+  Bitmap& operator=(const Bitmap& other); ///< defined private to prevent use
 
   // Changes scope, should be at end of class
   DALI_LOG_OBJECT_STRING_DECLARATION;
@@ -346,4 +375,4 @@ private:
 
 } // namespace Dali
 
-#endif // __DALI_INTEGRATION_BITMAP_H__
+#endif // DALI_INTEGRATION_BITMAP_H