Make PixelData flag that we release buffer after texture upload
[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) 2024 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    * @param [in] releaseAfterUpload Whether we release buffer after texture upload or not.
48    */
49   static PixelDataPtr New(uint8_t*                         buffer,
50                           uint32_t                         bufferSize,
51                           uint32_t                         width,
52                           uint32_t                         height,
53                           uint32_t                         stride,
54                           Pixel::Format                    pixelFormat,
55                           Dali::PixelData::ReleaseFunction releaseFunction,
56                           bool                             releaseAfterUpload);
57
58   /**
59    * @brief Constructor.
60    *
61    * @param [in] buffer             The raw pixel data.
62    * @param [in] bufferSize         The size of the buffer in bytes
63    * @param [in] width              Buffer width in pixels
64    * @param [in] height             Buffer height in pixels
65    * @param [in] stride             Buffer stride in pixels, 0 means the buffer is tightly packed
66    * @param [in] pixelFormat        The pixel format
67    * @param [in] releaseFunction    The function used to release the memory.
68    * @param [in] releaseAfterUpload Whether we release buffer after texture upload or not.
69    */
70   PixelData(uint8_t*                         buffer,
71             uint32_t                         bufferSize,
72             uint32_t                         width,
73             uint32_t                         height,
74             uint32_t                         stride,
75             Pixel::Format                    pixelFormat,
76             Dali::PixelData::ReleaseFunction releaseFunction,
77             bool                             releaseAfterUpload);
78
79 protected:
80   /**
81    * @brief Destructor.
82    *
83    * Release the pixel buffer if exists.
84    */
85   ~PixelData() override;
86
87 public:
88   /**
89    * Get the width of the buffer in pixels.
90    * @return The width of the buffer in pixels
91    */
92   uint32_t GetWidth() const;
93
94   /**
95    * Get the height of the buffer in pixels
96    * @return The height of the buffer in pixels
97    */
98   uint32_t GetHeight() const;
99
100   /**
101    * Get the pixel format
102    * @return The pixel format
103    */
104   Pixel::Format GetPixelFormat() const;
105
106   /**
107    * Get the pixel buffer if it's present.
108    * @return The buffer if exists, or NULL if there is no pixel buffer.
109    */
110   uint8_t* GetBuffer() const;
111
112   /**
113    * Get the size of the buffer in bytes
114    * @return The size of the buffer
115    */
116   uint32_t GetBufferSize() const;
117
118   /**
119    * Release the buffer data and reset the internal buffer to zero.
120    */
121   void ReleasePixelDataBuffer();
122
123   /**
124    * Return the buffer pointer.
125    * @return The buffer pointer.
126    */
127   Dali::Integration::PixelDataBuffer GetPixelDataBuffer() const;
128
129   /**
130    * Get whether we need to release pixel data after texture upload or not.
131    * @note This function can be called from another thread. Be careful.
132    * @return True if we need to release pixel data after texture upload. False otherwise.
133    */
134   bool IsPixelDataReleaseAfterUpload() const
135   {
136     return mReleaseAfterUpload;
137   }
138
139   /**
140    * @copydoc PixelData::GetStride()
141    */
142   uint32_t GetStride() const;
143
144   /**
145    * Class method to get the total currently allocated size of pixel buffers
146    */
147   static uint32_t GetTotalAllocatedSize()
148   {
149 #if defined(DEBUG_ENABLED)
150     return gPixelDataAllocationTotal;
151 #else
152     return 0;
153 #endif
154   }
155
156 private:
157   /*
158    * Undefined copy constructor.
159    */
160   PixelData(const PixelData& other);
161
162   /*
163    * Undefined assignment operator.
164    */
165   PixelData& operator=(const PixelData& other);
166
167 private:
168   uint8_t*                         mBuffer;          ///< The raw pixel data
169   uint32_t                         mBufferSize;      ///< Buffer size in bytes
170   uint32_t                         mWidth;           ///< Buffer width in pixels
171   uint32_t                         mHeight;          ///< Buffer height in pixels
172   uint32_t                         mStride;          ///< Buffer stride in pixels, 0 means the buffer is tightly packed
173   Pixel::Format                    mPixelFormat;     ///< Pixel format
174   Dali::PixelData::ReleaseFunction mReleaseFunction; ///< Function for releasing memory
175
176   const bool mReleaseAfterUpload;
177
178 #if defined(DEBUG_ENABLED)
179   static uint32_t gPixelDataAllocationTotal;
180 #endif
181 };
182
183 } // namespace Internal
184
185 /**
186  * Helper methods for public API
187  */
188 inline Internal::PixelData& GetImplementation(Dali::PixelData& handle)
189 {
190   DALI_ASSERT_ALWAYS(handle && "handle is empty");
191
192   BaseObject& object = handle.GetBaseObject();
193
194   return static_cast<Internal::PixelData&>(object);
195 }
196
197 inline const Internal::PixelData& GetImplementation(const Dali::PixelData& handle)
198 {
199   DALI_ASSERT_ALWAYS(handle && "handle is empty");
200
201   const BaseObject& object = handle.GetBaseObject();
202
203   return static_cast<const Internal::PixelData&>(object);
204 }
205
206 } // namespace Dali
207
208 #endif // __DALI_INTERNAL_PIXEL_DATA_H__