Merge "Memory Pool Logging" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / pixel-buffer-impl.h
1 #ifndef DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
2 #define DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H
3
4 /*
5  * Copyright (c) 2022 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/devel-api/adaptor-framework/pixel-buffer.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/images/image-operations.h> // For ImageDimensions
25 #include <dali/public-api/images/pixel-data.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/public-api/object/property-map.h>
28
29 // EXTERNAL INCLUDES
30 #include <memory>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38 class PixelBuffer;
39 typedef IntrusivePtr<PixelBuffer> PixelBufferPtr;
40
41 class PixelBuffer : public BaseObject
42 {
43 public:
44   /**
45    * @brief Create a PixelBuffer object with a pre-allocated buffer.
46    * The PixelBuffer object owns this buffer, which may be retrieved
47    * and modified using GetBuffer().
48    *
49    * @param [in] width            Buffer width in pixels
50    * @param [in] height           Buffer height in pixels
51    * @param [in] pixelFormat      The pixel format
52    */
53   static PixelBufferPtr New(uint32_t      width,
54                             uint32_t      height,
55                             Pixel::Format pixelFormat);
56
57   /**
58    * @brief Create a PixelBuffer object. For internal use only.
59    *
60    * @param [in] buffer           The raw pixel data.
61    * @param [in] bufferSize       The size of the buffer in bytes
62    * @param [in] width            Buffer width in pixels
63    * @param [in] height           Buffer height in pixels
64    * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
65    * @param [in] pixelFormat      The pixel format
66    */
67   static PixelBufferPtr New(uint8_t*      buffer,
68                             uint32_t      bufferSize,
69                             uint32_t      width,
70                             uint32_t      height,
71                             uint32_t      stride,
72                             Pixel::Format pixelFormat);
73
74   /**
75    * Convert a pixelBuffer object into a PixelData object.
76    * The new object takes ownership of the buffer data, and the
77    * mBuffer pointer is reset to NULL.
78    * @param[in] pixelBuffer The buffer to convert
79    * @return the pixelData
80    */
81   static Dali::PixelData Convert(PixelBuffer& pixelBuffer);
82
83   /**
84    * @brief Constructor.
85    *
86    * @param [in] buffer           The raw pixel data.
87    * @param [in] bufferSize       The size of the buffer in bytes
88    * @param [in] width            Buffer width in pixels
89    * @param [in] height           Buffer height in pixels
90    * @param [in] stride           Buffer stride in pixels, 0 means the buffer is tightly packed
91    * @param [in] pixelFormat      The pixel format
92    */
93   PixelBuffer(uint8_t*      buffer,
94               uint32_t      bufferSize,
95               uint32_t      width,
96               uint32_t      height,
97               uint32_t      stride,
98               Pixel::Format pixelFormat);
99
100 protected:
101   /**
102    * @brief Destructor.
103    *
104    * Release the pixel buffer if exists.
105    */
106   ~PixelBuffer() override;
107
108 public:
109   /**
110    * Get the total allocated size of current pixel buffers
111    */
112   static uint32_t GetTotalAllocatedSize()
113   {
114 #if defined(DEBUG_ENABLED)
115     return gPixelBufferAllocationTotal;
116 #else
117     return 0;
118 #endif
119   }
120
121   /**
122    * Get the width of the buffer in pixels.
123    * @return The width of the buffer in pixels
124    */
125   uint32_t GetWidth() const;
126
127   /**
128    * Get the height of the buffer in pixels
129    * @return The height of the buffer in pixels
130    */
131   uint32_t GetHeight() const;
132
133   /**
134    * @brief Gets the stride of the buffer in pixels.
135    * @return The stride of the buffer in pixels. 0 means the buffer is tightly packed.
136    */
137   uint32_t GetStride() const;
138
139   /**
140    * Get the pixel format
141    * @return The pixel format
142    */
143   Pixel::Format GetPixelFormat() const;
144
145   /**
146    * Get the pixel buffer if it's present.
147    * @return The buffer if exists, or NULL if there is no pixel buffer.
148    */
149   uint8_t* GetBuffer() const;
150
151   /**
152    * @copydoc Devel::PixelBuffer::GetBuffer()
153    */
154   const uint8_t* GetConstBuffer() const;
155
156   /**
157    * Get the size of the buffer in bytes
158    * @return The size of the buffer
159    */
160   uint32_t GetBufferSize() const;
161
162   /**
163    * Copy the buffer into a new PixelData
164    */
165   Dali::PixelData CreatePixelData() const;
166
167   /**
168    * @brief Apply the mask to the current buffer.
169    *
170    * This method may update the internal object - e.g. the new buffer
171    * may have a different pixel format - as an alpha channel may be
172    * added.
173    * @param[in] mask The mask to apply to this pixel buffer
174    * @param[in] contentScale The scaling factor to apply to the content
175    * @param[in] cropToMask Whether to crop the output to the mask size (true) or scale the
176    * mask to the content size (false)
177    */
178   void ApplyMask(const PixelBuffer& mask, float contentScale, bool cropToMask);
179
180   /**
181    * @brief Apply a Gaussian blur to the current buffer with the given radius.
182    *
183    * @param[in] blurRadius The radius for Gaussian blur
184    */
185   void ApplyGaussianBlur(const float blurRadius);
186
187   /**
188    * Crops this buffer to the given crop rectangle. Assumes the crop rectangle
189    * is within the bounds of this size.
190    * @param[in] x The top left corner's X
191    * @param[in] y The top left corner's y
192    * @param[in] cropDimensions The dimensions of the crop
193    */
194   void Crop(uint16_t x, uint16_t y, ImageDimensions cropDimensions);
195
196   /**
197    * Resizes the buffer to the given dimensions. Uses either Lanczos4 for downscaling
198    * or Mitchell for upscaling
199    * @param[in] outDimensions The new dimensions
200    */
201   void Resize(ImageDimensions outDimensions);
202
203   /**
204    * Multiplies the image's color values by the alpha value. This provides better
205    * blending capability.
206    */
207   void MultiplyColorByAlpha();
208
209   /**
210    * @brief Sets image metadata
211    *
212    * @param map Property map containing Exif fields
213    */
214   void SetMetadata(const Property::Map& map);
215
216   /**
217    * @brief Returns image metadata as a property map
218    * @param[out] outMetadata Property map to copy the data into
219    * @return True on success
220    */
221   bool GetMetadata(Property::Map& outMetadata) const;
222
223   /**
224    * @brief Sets metadata property map for the pixel buffer
225    * @note The function takes over the ownership of the property map
226    * @param[in] metadata Property map to copy the data into
227    */
228   void SetMetadata(std::unique_ptr<Property::Map> metadata);
229
230   /**
231    * Allocates fixed amount of memory for the pixel data. Used by compressed formats.
232    * @param[in] size Size of memory to be allocated
233    */
234   void AllocateFixedSize(uint32_t size);
235
236   /**
237    * @copydoc Devel::PixelBuffer::Rotate()
238    */
239   bool Rotate(Degree angle);
240
241   /**
242    * @copydoc Devel::PixelBuffer::IsAlphaPreMultiplied()
243    */
244   bool IsAlphaPreMultiplied() const;
245
246   /**
247    * @copydoc Devel::PixelBuffer::GetBrightness()
248    */
249   uint32_t GetBrightness() const;
250
251 private:
252   /*
253    * Undefined copy constructor.
254    */
255   PixelBuffer(const PixelBuffer& other);
256
257   /*
258    * Undefined assignment operator.
259    */
260   PixelBuffer& operator=(const PixelBuffer& other);
261
262   /**
263    * Internal method to apply the mask to this buffer. Expects that they are the same size.
264    */
265   void ApplyMaskInternal(const PixelBuffer& mask);
266
267   /**
268    * Takes ownership of the other object's pixel buffer.
269    */
270   void TakeOwnershipOfBuffer(PixelBuffer& pixelBuffer);
271
272   /**
273    * Release the buffer
274    */
275   void ReleaseBuffer();
276
277   /**
278    * Scales this buffer buffer by the given factor, and crops at the center to the
279    * given dimensions.
280    */
281   void ScaleAndCrop(float scaleFactor, ImageDimensions cropDimensions);
282
283   /**
284    * Creates a new buffer which is a crop of the passed in buffer,
285    * using the given crop rectangle. Assumes the crop rectangle is
286    * within the bounds of this size.
287    * @param[in] inBuffer The source buffer
288    * @param[in] x The top left corner's X
289    * @param[in] y The top left corner's y
290    * @param[in] cropDimensions The dimensions of the crop
291    * @return the new pixel buffer
292    */
293   static PixelBufferPtr NewCrop(const PixelBuffer& inBuffer, uint16_t x, uint16_t y, ImageDimensions cropDimensions);
294
295   /**
296    * Creates a new buffer which is a resized version of the passed in buffer.
297    * Uses either Lanczos4 for downscaling, or Mitchell for upscaling.
298    * @param[in] inBuffer The source buffer
299    * @param[in] outDimensions The new dimensions
300    * @return a new buffer of the given size.
301    */
302   static PixelBufferPtr NewResize(const PixelBuffer& inBuffer, ImageDimensions outDimensions);
303
304 private:
305   std::unique_ptr<Property::Map> mMetadata;      ///< Metadata fields
306   uint8_t*                       mBuffer;        ///< The raw pixel data
307   uint32_t                       mBufferSize;    ///< Buffer sized in bytes
308   uint32_t                       mWidth;         ///< Buffer width in pixels
309   uint32_t                       mHeight;        ///< Buffer height in pixels
310   uint32_t                       mStride;        ///< Buffer stride in bytes, 0 means the buffer is tightly packed
311   Pixel::Format                  mPixelFormat;   ///< Pixel format
312   bool                           mPreMultiplied; ///< PreMultiplied
313
314 #if defined(DEBUG_ENABLED)
315   static uint32_t gPixelBufferAllocationTotal;
316 #endif
317 };
318
319 } // namespace Adaptor
320
321 } // namespace Internal
322
323 /**
324  * Helper methods for public API
325  */
326 inline Internal::Adaptor::PixelBuffer& GetImplementation(Devel::PixelBuffer& handle)
327 {
328   DALI_ASSERT_ALWAYS(handle && "handle is empty");
329
330   BaseObject& object = handle.GetBaseObject();
331
332   return static_cast<Internal::Adaptor::PixelBuffer&>(object);
333 }
334
335 inline const Internal::Adaptor::PixelBuffer& GetImplementation(const Devel::PixelBuffer& handle)
336 {
337   DALI_ASSERT_ALWAYS(handle && "handle is empty");
338
339   const BaseObject& object = handle.GetBaseObject();
340
341   return static_cast<const Internal::Adaptor::PixelBuffer&>(object);
342 }
343
344 } // namespace Dali
345
346 #endif // DALI_INTERNAL_ADAPTOR_PIXEL_BUFFER_H