Memory Pool Logging
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / pixel-buffer-impl.h
index b0fe5ce..45ff5dd 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
 
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
@@ -20,6 +20,7 @@
 
 // INTERNAL INCLUDES
 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
+#include <dali/integration-api/debug.h>
 #include <dali/public-api/images/image-operations.h> // For ImageDimensions
 #include <dali/public-api/images/pixel-data.h>
 #include <dali/public-api/object/base-object.h>
 
 namespace Dali
 {
-
 namespace Internal
 {
-
 namespace Adaptor
 {
-
 class PixelBuffer;
 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
 
 class PixelBuffer : public BaseObject
 {
 public:
-
   /**
    * @brief Create a PixelBuffer object with a pre-allocated buffer.
    * The PixelBuffer object owns this buffer, which may be retrieved
@@ -53,9 +50,9 @@ public:
    * @param [in] height           Buffer height in pixels
    * @param [in] pixelFormat      The pixel format
    */
-  static PixelBufferPtr New( unsigned int width,
-                             unsigned int height,
-                             Pixel::Format pixelFormat );
+  static PixelBufferPtr New(uint32_t      width,
+                            uint32_t      height,
+                            Pixel::Format pixelFormat);
 
   /**
    * @brief Create a PixelBuffer object. For internal use only.
@@ -64,14 +61,15 @@ public:
    * @param [in] bufferSize       The size of the buffer in bytes
    * @param [in] width            Buffer width in pixels
    * @param [in] height           Buffer height in pixels
+   * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
    * @param [in] pixelFormat      The pixel format
-   * @param [in] releaseFunction  The function used to release the memory.
    */
-  static PixelBufferPtr New( unsigned char* buffer,
-                             unsigned int bufferSize,
-                             unsigned int width,
-                             unsigned int height,
-                             Pixel::Format pixelFormat );
+  static PixelBufferPtr New(uint8_t*      buffer,
+                            uint32_t      bufferSize,
+                            uint32_t      width,
+                            uint32_t      height,
+                            uint32_t      stride,
+                            Pixel::Format pixelFormat);
 
   /**
    * Convert a pixelBuffer object into a PixelData object.
@@ -80,7 +78,7 @@ public:
    * @param[in] pixelBuffer The buffer to convert
    * @return the pixelData
    */
-  static Dali::PixelData Convert( PixelBuffer& pixelBuffer );
+  static Dali::PixelData Convert(PixelBuffer& pixelBuffer);
 
   /**
    * @brief Constructor.
@@ -89,36 +87,54 @@ public:
    * @param [in] bufferSize       The size of the buffer in bytes
    * @param [in] width            Buffer width in pixels
    * @param [in] height           Buffer height in pixels
+   * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
    * @param [in] pixelFormat      The pixel format
    */
-  PixelBuffer( unsigned char* buffer,
-               unsigned int bufferSize,
-               unsigned int width,
-               unsigned int height,
-               Pixel::Format pixelFormat );
+  PixelBuffer(uint8_t*      buffer,
+              uint32_t      bufferSize,
+              uint32_t      width,
+              uint32_t      height,
+              uint32_t      stride,
+              Pixel::Format pixelFormat);
 
 protected:
-
   /**
    * @brief Destructor.
    *
    * Release the pixel buffer if exists.
    */
-  ~PixelBuffer();
+  ~PixelBuffer() override;
 
 public:
+  /**
+   * Get the total allocated size of current pixel buffers
+   */
+  static uint32_t GetTotalAllocatedSize()
+  {
+#if defined(DEBUG_ENABLED)
+    return gPixelBufferAllocationTotal;
+#else
+    return 0;
+#endif
+  }
 
   /**
    * Get the width of the buffer in pixels.
    * @return The width of the buffer in pixels
    */
-  unsigned int GetWidth() const;
+  uint32_t GetWidth() const;
 
   /**
    * Get the height of the buffer in pixels
    * @return The height of the buffer in pixels
    */
-  unsigned int GetHeight() const;
+  uint32_t GetHeight() const;
+
+  /**
+   * @brief Gets the stride of the buffer in pixels.
+   * @return The stride of the buffer in pixels. 0 means the buffer is tightly packed.
+   */
+  uint32_t GetStride() const;
 
   /**
    * Get the pixel format
@@ -130,13 +146,18 @@ public:
    * Get the pixel buffer if it's present.
    * @return The buffer if exists, or NULL if there is no pixel buffer.
    */
-  unsigned char* GetBuffer() const;
+  uint8_t* GetBuffer() const;
+
+  /**
+   * @copydoc Devel::PixelBuffer::GetBuffer()
+   */
+  const uint8_t* GetConstBuffer() const;
 
   /**
    * Get the size of the buffer in bytes
    * @return The size of the buffer
    */
-  unsigned int GetBufferSize() const;
+  uint32_t GetBufferSize() const;
 
   /**
    * Copy the buffer into a new PixelData
@@ -154,14 +175,14 @@ public:
    * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
    * mask to the content size (false)
    */
-  void ApplyMask( const PixelBuffer& mask, float contentScale, bool cropToMask );
+  void ApplyMask(const PixelBuffer& mask, float contentScale, bool cropToMask);
 
   /**
    * @brief Apply a Gaussian blur to the current buffer with the given radius.
    *
    * @param[in] blurRadius The radius for Gaussian blur
    */
-  void ApplyGaussianBlur( const float blurRadius );
+  void ApplyGaussianBlur(const float blurRadius);
 
   /**
    * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
@@ -170,14 +191,14 @@ public:
    * @param[in] y The top left corner's y
    * @param[in] cropDimensions The dimensions of the crop
    */
-  void Crop( uint16_t x, uint16_t y, ImageDimensions cropDimensions );
+  void Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions);
 
   /**
    * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
    * or Mitchell for upscaling
    * @param[in] outDimensions The new dimensions
    */
-  void Resize( ImageDimensions outDimensions );
+  void Resize(ImageDimensions outDimensions);
 
   /**
    * Multiplies the image's color values by the alpha value. This provides better
@@ -190,7 +211,7 @@ public:
    *
    * @param map Property map containing Exif fields
    */
-  void SetMetadata( const Property::Map& map );
+  void SetMetadata(const Property::Map& map);
 
   /**
    * @brief Returns image metadata as a property map
@@ -210,12 +231,22 @@ public:
    * Allocates fixed amount of memory for the pixel data. Used by compressed formats.
    * @param[in] size Size of memory to be allocated
    */
-  void AllocateFixedSize( uint32_t size );
+  void AllocateFixedSize(uint32_t size);
 
   /**
    * @copydoc Devel::PixelBuffer::Rotate()
    */
-  bool Rotate( Degree angle );
+  bool Rotate(Degree angle);
+
+  /**
+   * @copydoc Devel::PixelBuffer::IsAlphaPreMultiplied()
+   */
+  bool IsAlphaPreMultiplied() const;
+
+  /**
+   * @copydoc Devel::PixelBuffer::GetBrightness()
+   */
+  uint32_t GetBrightness() const;
 
 private:
   /*
@@ -226,17 +257,17 @@ private:
   /*
    * Undefined assignment operator.
    */
-  PixelBuffer& operator= (const PixelBuffer& other);
+  PixelBuffer& operator=(const PixelBuffer& other);
 
   /**
    * Internal method to apply the mask to this buffer. Expects that they are the same size.
    */
-  void ApplyMaskInternal( const PixelBuffer& mask );
+  void ApplyMaskInternal(const PixelBuffer& mask);
 
   /**
    * Takes ownership of the other object's pixel buffer.
    */
-  void TakeOwnershipOfBuffer( PixelBuffer& pixelBuffer );
+  void TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer);
 
   /**
    * Release the buffer
@@ -247,7 +278,7 @@ private:
    * Scales this buffer buffer by the given factor, and crops at the center to the
    * given dimensions.
    */
-  void ScaleAndCrop( float scaleFactor, ImageDimensions cropDimensions );
+  void ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions);
 
   /**
    * Creates a new buffer which is a crop of the passed in buffer,
@@ -259,7 +290,7 @@ private:
    * @param[in] cropDimensions The dimensions of the crop
    * @return the new pixel buffer
    */
-  static PixelBufferPtr NewCrop( const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions );
+  static PixelBufferPtr NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions);
 
   /**
    * Creates a new buffer which is a resized version of the passed in buffer.
@@ -268,16 +299,21 @@ private:
    * @param[in] outDimensions The new dimensions
    * @return a new buffer of the given size.
    */
-  static PixelBufferPtr NewResize( const PixelBuffer& inBuffer, ImageDimensions outDimensions );
+  static PixelBufferPtr NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions);
 
 private:
-
-  std::unique_ptr<Property::Map>  mMetadata;         ///< Metadata fields
-  unsigned char*                  mBuffer;           ///< The raw pixel data
-  unsigned int                    mBufferSize;       ///< Buffer sized in bytes
-  unsigned int                    mWidth;            ///< Buffer width in pixels
-  unsigned int                    mHeight;           ///< Buffer height in pixels
-  Pixel::Format                   mPixelFormat;      ///< Pixel format
+  std::unique_ptr<Property::Map> mMetadata;      ///< Metadata fields
+  uint8_t*                       mBuffer;        ///< The raw pixel data
+  uint32_t                       mBufferSize;    ///< Buffer sized in bytes
+  uint32_t                       mWidth;         ///< Buffer width in pixels
+  uint32_t                       mHeight;        ///< Buffer height in pixels
+  uint32_t                       mStride;        ///< Buffer stride in bytes, 0 means the buffer is tightly packed
+  Pixel::Format                  mPixelFormat;   ///< Pixel format
+  bool                           mPreMultiplied; ///< PreMultiplied
+
+#if defined(DEBUG_ENABLED)
+  static uint32_t gPixelBufferAllocationTotal;
+#endif
 };
 
 } // namespace Adaptor
@@ -287,24 +323,24 @@ private:
 /**
  * Helper methods for public API
  */
-inline Internal::Adaptor::PixelBuffer& GetImplementation( Devel::PixelBuffer& handle )
+inline Internal::Adaptor::PixelBuffer& GetImplementation(Devel::PixelBuffer& handle)
 {
-  DALI_ASSERT_ALWAYS( handle && "handle is empty" );
+  DALI_ASSERT_ALWAYS(handle && "handle is empty");
 
   BaseObject& object = handle.GetBaseObject();
 
-  return static_cast<Internal::Adaptor::PixelBuffer&>( object );
+  return static_cast<Internal::Adaptor::PixelBuffer&>(object);
 }
 
-inline const Internal::Adaptor::PixelBuffer& GetImplementation( const Devel::PixelBuffer& handle )
+inline const Internal::Adaptor::PixelBuffer& GetImplementation(const Devel::PixelBuffer& handle)
 {
-  DALI_ASSERT_ALWAYS( handle && "handle is empty" );
+  DALI_ASSERT_ALWAYS(handle && "handle is empty");
 
   const BaseObject& object = handle.GetBaseObject();
 
-  return static_cast<const Internal::Adaptor::PixelBuffer&>( object );
+  return static_cast<const Internal::Adaptor::PixelBuffer&>(object);
 }
 
 } // namespace Dali
 
-#endif // __DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H__
+#endif // DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H