1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "image_buffer.h"
17
18 namespace OHOS::Camera {
ImageBuffer()19 ImageBuffer::ImageBuffer() {}
20
ImageBuffer(const int32_t source)21 ImageBuffer::ImageBuffer(const int32_t source)
22 {
23 sourceType_ = source;
24 }
25
ImageBuffer(const int32_t source,const uint32_t width,const uint32_t height,const uint64_t usage,const uint32_t format)26 ImageBuffer::ImageBuffer(const int32_t source,
27 const uint32_t width,
28 const uint32_t height,
29 const uint64_t usage,
30 const uint32_t format)
31 {
32 sourceType_ = source;
33 width_ = width;
34 height_ = height;
35 usage_ = usage;
36 format_ = format;
37 }
38
~ImageBuffer()39 ImageBuffer::~ImageBuffer()
40 {
41 Free();
42 }
43
GetIndex() const44 int32_t ImageBuffer::GetIndex() const
45 {
46 return index_;
47 }
48
GetWidth() const49 uint32_t ImageBuffer::GetWidth() const
50 {
51 return width_;
52 }
53
GetHeight() const54 uint32_t ImageBuffer::GetHeight() const
55 {
56 return height_;
57 }
58
GetStride() const59 uint32_t ImageBuffer::GetStride() const
60 {
61 return stride_;
62 }
63
GetFormat() const64 int32_t ImageBuffer::GetFormat() const
65 {
66 return format_;
67 }
68
GetSize() const69 uint32_t ImageBuffer::GetSize() const
70 {
71 return size_;
72 }
73
GetUsage() const74 uint64_t ImageBuffer::GetUsage() const
75 {
76 return usage_;
77 }
78
GetVirAddress() const79 void* ImageBuffer::GetVirAddress() const
80 {
81 return virAddr_;
82 }
83
GetPhyAddress() const84 uint64_t ImageBuffer::GetPhyAddress() const
85 {
86 return phyAddr_;
87 }
88
GetFileDescriptor() const89 int32_t ImageBuffer::GetFileDescriptor() const
90 {
91 return fd_;
92 }
93
GetSourceType() const94 int32_t ImageBuffer::GetSourceType() const
95 {
96 return sourceType_;
97 }
98
GetTimestamp() const99 uint64_t ImageBuffer::GetTimestamp() const
100 {
101 return timeStamp_;
102 }
103
GetFrameNumber() const104 uint64_t ImageBuffer::GetFrameNumber() const
105 {
106 return frameNumber_;
107 }
108
GetPoolId() const109 int64_t ImageBuffer::GetPoolId() const
110 {
111 return poolId_;
112 }
113
GetCaptureId() const114 int32_t ImageBuffer::GetCaptureId() const
115 {
116 return captureId_;
117 }
118
GetBufferStatus() const119 CameraBufferStatus ImageBuffer::GetBufferStatus() const
120 {
121 return status_;
122 }
123
GetSequenceId() const124 int32_t ImageBuffer::GetSequenceId() const
125 {
126 return sequenceId_;
127 }
128
GetFenceId() const129 int32_t ImageBuffer::GetFenceId() const
130 {
131 return fenceId_;
132 }
133
GetEsFrameInfo() const134 EsFrameInfo ImageBuffer::GetEsFrameInfo() const
135 {
136 return esInfo_;
137 }
138
GetEncodeType() const139 int32_t ImageBuffer::GetEncodeType() const
140 {
141 return encodeType_;
142 }
143
GetStreamId() const144 int32_t ImageBuffer::GetStreamId() const
145 {
146 return streamId_;
147 }
148
SetIndex(const int32_t index)149 void ImageBuffer::SetIndex(const int32_t index)
150 {
151 std::lock_guard<std::mutex> l(l_);
152 index_ = index;
153 return;
154 }
155
SetWidth(const uint32_t width)156 void ImageBuffer::SetWidth(const uint32_t width)
157 {
158 std::lock_guard<std::mutex> l(l_);
159 width_ = width;
160 return;
161 }
162
SetHeight(const uint32_t height)163 void ImageBuffer::SetHeight(const uint32_t height)
164 {
165 std::lock_guard<std::mutex> l(l_);
166 height_ = height;
167 return;
168 }
169
SetStride(const uint32_t stride)170 void ImageBuffer::SetStride(const uint32_t stride)
171 {
172 std::lock_guard<std::mutex> l(l_);
173 stride_ = stride;
174 return;
175 }
176
SetFormat(const int32_t format)177 void ImageBuffer::SetFormat(const int32_t format)
178 {
179 std::lock_guard<std::mutex> l(l_);
180 format_ = format;
181 return;
182 }
183
SetSize(const uint32_t size)184 void ImageBuffer::SetSize(const uint32_t size)
185 {
186 std::lock_guard<std::mutex> l(l_);
187 size_ = size;
188 return;
189 }
190
SetUsage(const uint64_t usage)191 void ImageBuffer::SetUsage(const uint64_t usage)
192 {
193 std::lock_guard<std::mutex> l(l_);
194 usage_ = usage;
195 return;
196 }
197
SetVirAddress(const void * addr)198 void ImageBuffer::SetVirAddress(const void* addr)
199 {
200 std::lock_guard<std::mutex> l(l_);
201 virAddr_ = const_cast<void*>(addr);
202 return;
203 }
204
SetPhyAddress(const uint64_t addr)205 void ImageBuffer::SetPhyAddress(const uint64_t addr)
206 {
207 std::lock_guard<std::mutex> l(l_);
208 phyAddr_ = addr;
209 return;
210 }
211
SetFileDescriptor(const int32_t fd)212 void ImageBuffer::SetFileDescriptor(const int32_t fd)
213 {
214 std::lock_guard<std::mutex> l(l_);
215 fd_ = fd;
216 return;
217 }
218
SetTimestamp(const uint64_t timeStamp)219 void ImageBuffer::SetTimestamp(const uint64_t timeStamp)
220 {
221 std::lock_guard<std::mutex> l(l_);
222 timeStamp_ = timeStamp;
223 return;
224 }
225
SetFrameNumber(const uint64_t frameNumber)226 void ImageBuffer::SetFrameNumber(const uint64_t frameNumber)
227 {
228 std::lock_guard<std::mutex> l(l_);
229 frameNumber_ = frameNumber;
230 return;
231 }
232
SetPoolId(const int64_t id)233 void ImageBuffer::SetPoolId(const int64_t id)
234 {
235 std::lock_guard<std::mutex> l(l_);
236 poolId_ = id;
237 return;
238 }
239
SetCaptureId(const int32_t id)240 void ImageBuffer::SetCaptureId(const int32_t id)
241 {
242 std::lock_guard<std::mutex> l(l_);
243 captureId_ = id;
244 return;
245 }
246
SetBufferStatus(const CameraBufferStatus flag)247 void ImageBuffer::SetBufferStatus(const CameraBufferStatus flag)
248 {
249 std::lock_guard<std::mutex> l(l_);
250 status_ = flag;
251 return;
252 }
253
SetSequenceId(const int32_t sequence)254 void ImageBuffer::SetSequenceId(const int32_t sequence)
255 {
256 std::lock_guard<std::mutex> l(l_);
257 sequenceId_ = sequence;
258 return;
259 }
260
SetFenceId(const int32_t fence)261 void ImageBuffer::SetFenceId(const int32_t fence)
262 {
263 std::lock_guard<std::mutex> l(l_);
264 fenceId_ = fence;
265 return;
266 }
267
SetEsFrameSize(const int32_t frameSize)268 void ImageBuffer::SetEsFrameSize(const int32_t frameSize)
269 {
270 std::lock_guard<std::mutex> l(l_);
271 esInfo_.size = frameSize;
272 return;
273 }
274
SetEsTimestamp(const int64_t timeStamp)275 void ImageBuffer::SetEsTimestamp(const int64_t timeStamp)
276 {
277 std::lock_guard<std::mutex> l(l_);
278 esInfo_.timestamp = timeStamp;
279 return;
280 }
281
SetEsKeyFrame(const int32_t isKey)282 void ImageBuffer::SetEsKeyFrame(const int32_t isKey)
283 {
284 std::lock_guard<std::mutex> l(l_);
285 esInfo_.isKey = isKey;
286 return;
287 }
288
SetEsFrameNum(const int32_t frameNum)289 void ImageBuffer::SetEsFrameNum(const int32_t frameNum)
290 {
291 std::lock_guard<std::mutex> l(l_);
292 esInfo_.frameNum = frameNum;
293 return;
294 }
295
SetEncodeType(const int32_t type)296 void ImageBuffer::SetEncodeType(const int32_t type)
297 {
298 std::lock_guard<std::mutex> l(l_);
299 encodeType_ = type;
300 return;
301 }
302
SetStreamId(const int32_t streamId)303 void ImageBuffer::SetStreamId(const int32_t streamId)
304 {
305 std::lock_guard<std::mutex> l(l_);
306 streamId_ = streamId;
307 return;
308 }
309
Free()310 void ImageBuffer::Free()
311 {
312 index_ = -1;
313 width_ = 0;
314 height_ = 0;
315 stride_ = 0;
316 format_ = CAMERA_FORMAT_INVALID;
317 size_ = 0;
318 usage_ = 0;
319 virAddr_ = nullptr;
320 phyAddr_ = 0;
321 fd_ = -1;
322
323 return;
324 }
325
operator ==(const IBuffer & u)326 bool ImageBuffer::operator==(const IBuffer& u)
327 {
328 if (u.GetSourceType() != sourceType_) {
329 return false;
330 }
331
332 if (u.GetPhyAddress() == 0 || phyAddr_ == 0) {
333 return u.GetVirAddress() == virAddr_;
334 }
335
336 return u.GetPhyAddress() == phyAddr_;
337 }
338 } // namespace OHOS::Camera
339