[dali_2.3.5] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / images / pixel-data-impl.h
1 #ifndef DALI_INTERNAL_PIXEL_DATA_H
2 #define DALI_INTERNAL_PIXEL_DATA_H
3
4 /*
5  * Copyright (c) 2023 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 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/pixel-data-integ.h>
24 #include <dali/public-api/images/pixel-data.h>
25 #include <dali/public-api/object/base-object.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 class PixelData;
32 using PixelDataPtr = IntrusivePtr<PixelData>;
33
34 class PixelData : public BaseObject
35 {
36 public:
37   /**
38    * @brief Create a PixelData object.
39    *
40    * @param [in] buffer           The raw pixel data.
41    * @param [in] bufferSize       The size of the buffer in bytes
42    * @param [in] width            Buffer width in pixels
43    * @param [in] height           Buffer height in pixels
44    * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
45    * @param [in] pixelFormat      The pixel format
46    * @param [in] releaseFunction  The function used to release the memory.
47    */
48   static PixelDataPtr New(uint8_t*                         buffer,
49                           uint32_t                         bufferSize,
50                           uint32_t                         width,
51                           uint32_t                         height,
52                           uint32_t                         stride,
53                           Pixel::Format                    pixelFormat,
54                           Dali::PixelData::ReleaseFunction releaseFunction);
55
56   /**
57    * @brief Constructor.
58    *
59    * @param [in] buffer           The raw pixel data.
60    * @param [in] bufferSize       The size of the buffer in bytes
61    * @param [in] width            Buffer width in pixels
62    * @param [in] height           Buffer height in pixels
63    * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
64    * @param [in] pixelFormat      The pixel format
65    * @param [in] releaseFunction  The function used to release the memory.
66    */
67   PixelData(uint8_t*                         buffer,
68             uint32_t                         bufferSize,
69             uint32_t                         width,
70             uint32_t                         height,
71             uint32_t                         stride,
72             Pixel::Format                    pixelFormat,
73             Dali::PixelData::ReleaseFunction releaseFunction);
74
75 protected:
76   /**
77    * @brief Destructor.
78    *
79    * Release the pixel buffer if exists.
80    */
81   ~PixelData() override;
82
83 public:
84   /**
85    * Get the width of the buffer in pixels.
86    * @return The width of the buffer in pixels
87    */
88   uint32_t GetWidth() const;
89
90   /**
91    * Get the height of the buffer in pixels
92    * @return The height of the buffer in pixels
93    */
94   uint32_t GetHeight() const;
95
96   /**
97    * Get the pixel format
98    * @return The pixel format
99    */
100   Pixel::Format GetPixelFormat() const;
101
102   /**
103    * Get the pixel buffer if it's present.
104    * @return The buffer if exists, or NULL if there is no pixel buffer.
105    */
106   uint8_t* GetBuffer() const;
107
108   /**
109    * Get the size of the buffer in bytes
110    * @return The size of the buffer
111    */
112   uint32_t GetBufferSize() const;
113
114   /**
115    * Return the buffer pointer and reset the internal buffer to zero.
116    * @return The buffer pointer and associated data.
117    */
118   Dali::Integration::PixelDataBuffer ReleasePixelDataBuffer();
119
120   /**
121    * Return the buffer pointer.
122    * @return The buffer pointer.
123    */
124   Dali::Integration::PixelDataBuffer GetPixelDataBuffer() const;
125
126   /**
127    * @copydoc PixelData::GetStride()
128    */
129   uint32_t GetStride() const;
130
131   /**
132    * Class method to get the total currently allocated size of pixel buffers
133    */
134   static uint32_t GetTotalAllocatedSize()
135   {
136 #if defined(DEBUG_ENABLED)
137     return gPixelDataAllocationTotal;
138 #else
139     return 0;
140 #endif
141   }
142
143 private:
144   /*
145    * Undefined copy constructor.
146    */
147   PixelData(const PixelData& other);
148
149   /*
150    * Undefined assignment operator.
151    */
152   PixelData& operator=(const PixelData& other);
153
154 private:
155   uint8_t*                         mBuffer;          ///< The raw pixel data
156   uint32_t                         mBufferSize;      ///< Buffer size in bytes
157   uint32_t                         mWidth;           ///< Buffer width in pixels
158   uint32_t                         mHeight;          ///< Buffer height in pixels
159   uint32_t                         mStride;          ///< Buffer stride in pixels, 0 means the buffer is tightly packed
160   Pixel::Format                    mPixelFormat;     ///< Pixel format
161   Dali::PixelData::ReleaseFunction mReleaseFunction; ///< Function for releasing memory
162
163 #if defined(DEBUG_ENABLED)
164   static uint32_t gPixelDataAllocationTotal;
165 #endif
166 };
167
168 } // namespace Internal
169
170 /**
171  * Helper methods for public API
172  */
173 inline Internal::PixelData& GetImplementation(Dali::PixelData& handle)
174 {
175   DALI_ASSERT_ALWAYS(handle && "handle is empty");
176
177   BaseObject& object = handle.GetBaseObject();
178
179   return static_cast<Internal::PixelData&>(object);
180 }
181
182 inline const Internal::PixelData& GetImplementation(const Dali::PixelData& handle)
183 {
184   DALI_ASSERT_ALWAYS(handle && "handle is empty");
185
186   const BaseObject& object = handle.GetBaseObject();
187
188   return static_cast<const Internal::PixelData&>(object);
189 }
190
191 } // namespace Dali
192
193 #endif // __DALI_INTERNAL_PIXEL_DATA_H__