[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / images / pixel-data-impl.cpp
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/images/pixel-data-impl.h>
20
21 #include <dali/integration-api/debug.h>
22
23 #if defined(DEBUG_ENABLED)
24 Debug::Filter* gPixelDataLogFilter = Debug::Filter::New(Debug::NoLogging, false, "DALI_LOG_PIXEL_DATA_SIZE");
25 #endif
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 #if defined(DEBUG_ENABLED)
32 // Only track data allocation if debug is enabled
33 uint32_t PixelData::gPixelDataAllocationTotal{0};
34 #endif
35
36 PixelData::PixelData(uint8_t*                         buffer,
37                      uint32_t                         bufferSize,
38                      uint32_t                         width,
39                      uint32_t                         height,
40                      uint32_t                         stride,
41                      Pixel::Format                    pixelFormat,
42                      Dali::PixelData::ReleaseFunction releaseFunction,
43                      bool                             releaseAfterUpload)
44 : mBuffer(buffer),
45   mBufferSize(bufferSize),
46   mWidth(width),
47   mHeight(height),
48   mStride(stride),
49   mPixelFormat(pixelFormat),
50   mReleaseFunction(releaseFunction),
51   mReleaseAfterUpload(releaseAfterUpload)
52 {
53   DALI_LOG_INFO(gPixelDataLogFilter, Debug::Concise, "Allocated PixelData of size %u\n", bufferSize);
54 #if defined(DEBUG_ENABLED)
55   gPixelDataAllocationTotal += mBufferSize;
56 #endif
57 }
58
59 PixelData::~PixelData()
60 {
61   ReleasePixelDataBuffer();
62 }
63
64 PixelDataPtr PixelData::New(uint8_t*                         buffer,
65                             uint32_t                         bufferSize,
66                             uint32_t                         width,
67                             uint32_t                         height,
68                             uint32_t                         stride,
69                             Pixel::Format                    pixelFormat,
70                             Dali::PixelData::ReleaseFunction releaseFunction,
71                             bool                             releaseAfterUpload)
72 {
73   return new PixelData(buffer, bufferSize, width, height, stride, pixelFormat, releaseFunction, releaseAfterUpload);
74 }
75
76 uint32_t PixelData::GetWidth() const
77 {
78   return mWidth;
79 }
80
81 uint32_t PixelData::GetHeight() const
82 {
83   return mHeight;
84 }
85
86 Pixel::Format PixelData::GetPixelFormat() const
87 {
88   return mPixelFormat;
89 }
90
91 uint8_t* PixelData::GetBuffer() const
92 {
93   return mBuffer;
94 }
95
96 uint32_t PixelData::GetBufferSize() const
97 {
98   return mBufferSize;
99 }
100
101 void PixelData::ReleasePixelDataBuffer()
102 {
103   if(mBuffer)
104   {
105     if(mReleaseFunction == Dali::PixelData::FREE)
106     {
107       free(mBuffer);
108     }
109     else
110     {
111       delete[] mBuffer;
112     }
113     mBuffer = nullptr;
114 #if defined(DEBUG_ENABLED)
115     gPixelDataAllocationTotal -= mBufferSize;
116 #endif
117   }
118 }
119
120 Dali::Integration::PixelDataBuffer PixelData::GetPixelDataBuffer() const
121 {
122   Dali::Integration::PixelDataBuffer pixelDataBuffer(mBuffer, mBufferSize, mWidth, mHeight, mStride);
123   return pixelDataBuffer;
124 }
125
126 uint32_t PixelData::GetStride() const
127 {
128   return mStride;
129 }
130
131 } // namespace Internal
132
133 } // namespace Dali