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
GetCurWidth() const54 uint32_t ImageBuffer::GetCurWidth() const
55 {
56 return curWidth_;
57 }
58
GetHeight() const59 uint32_t ImageBuffer::GetHeight() const
60 {
61 return height_;
62 }
63
GetCurHeight() const64 uint32_t ImageBuffer::GetCurHeight() const
65 {
66 return curHeight_;
67 }
68
GetStride() const69 uint32_t ImageBuffer::GetStride() const
70 {
71 return stride_;
72 }
73
GetFormat() const74 int32_t ImageBuffer::GetFormat() const
75 {
76 return format_;
77 }
78
GetCurFormat() const79 int32_t ImageBuffer::GetCurFormat() const
80 {
81 return curFormat_;
82 }
83
GetSize() const84 uint32_t ImageBuffer::GetSize() const
85 {
86 return size_;
87 }
88
GetSuffaceBufferSize() const89 uint32_t ImageBuffer::GetSuffaceBufferSize() const
90 {
91 return sbSize_;
92 }
93
GetSuffaceBufferAddr() const94 void* ImageBuffer::GetSuffaceBufferAddr() const
95 {
96 return sbAddr_;
97 }
98
GetUsage() const99 uint64_t ImageBuffer::GetUsage() const
100 {
101 return usage_;
102 }
103
GetVirAddress() const104 void* ImageBuffer::GetVirAddress() const
105 {
106 return virAddr_;
107 }
108
GetPhyAddress() const109 uint64_t ImageBuffer::GetPhyAddress() const
110 {
111 return phyAddr_;
112 }
113
GetFileDescriptor() const114 int32_t ImageBuffer::GetFileDescriptor() const
115 {
116 return fd_;
117 }
118
GetSourceType() const119 int32_t ImageBuffer::GetSourceType() const
120 {
121 return sourceType_;
122 }
123
GetTimestamp() const124 uint64_t ImageBuffer::GetTimestamp() const
125 {
126 return timeStamp_;
127 }
128
GetFrameNumber() const129 uint64_t ImageBuffer::GetFrameNumber() const
130 {
131 return frameNumber_;
132 }
133
GetPoolId() const134 int64_t ImageBuffer::GetPoolId() const
135 {
136 return poolId_;
137 }
138
GetCaptureId() const139 int32_t ImageBuffer::GetCaptureId() const
140 {
141 return captureId_;
142 }
143
GetBufferStatus() const144 CameraBufferStatus ImageBuffer::GetBufferStatus() const
145 {
146 return status_;
147 }
148
GetSequenceId() const149 int32_t ImageBuffer::GetSequenceId() const
150 {
151 return sequenceId_;
152 }
153
GetFenceId() const154 int32_t ImageBuffer::GetFenceId() const
155 {
156 return fenceId_;
157 }
158
GetEsFrameInfo() const159 EsFrameInfo ImageBuffer::GetEsFrameInfo() const
160 {
161 return esInfo_;
162 }
163
GetEncodeType() const164 int32_t ImageBuffer::GetEncodeType() const
165 {
166 return encodeType_;
167 }
168
GetStreamId() const169 int32_t ImageBuffer::GetStreamId() const
170 {
171 return streamId_;
172 }
173
GetIsValidDataInSurfaceBuffer() const174 bool ImageBuffer::GetIsValidDataInSurfaceBuffer() const
175 {
176 return isDataValidInSurfaceBuffer_;
177 }
178
SetIndex(const int32_t index)179 void ImageBuffer::SetIndex(const int32_t index)
180 {
181 std::lock_guard<std::mutex> l(l_);
182 index_ = index;
183 return;
184 }
185
SetWidth(const uint32_t width)186 void ImageBuffer::SetWidth(const uint32_t width)
187 {
188 std::lock_guard<std::mutex> l(l_);
189 width_ = width;
190 return;
191 }
192
SetCurWidth(const uint32_t width)193 void ImageBuffer::SetCurWidth(const uint32_t width)
194 {
195 std::lock_guard<std::mutex> l(l_);
196 curWidth_ = width;
197 return;
198 }
199
SetHeight(const uint32_t height)200 void ImageBuffer::SetHeight(const uint32_t height)
201 {
202 std::lock_guard<std::mutex> l(l_);
203 height_ = height;
204 return;
205 }
206
SetCurHeight(const uint32_t height)207 void ImageBuffer::SetCurHeight(const uint32_t height)
208 {
209 std::lock_guard<std::mutex> l(l_);
210 curHeight_ = height;
211 return;
212 }
213
SetStride(const uint32_t stride)214 void ImageBuffer::SetStride(const uint32_t stride)
215 {
216 std::lock_guard<std::mutex> l(l_);
217 stride_ = stride;
218 return;
219 }
220
SetFormat(const int32_t format)221 void ImageBuffer::SetFormat(const int32_t format)
222 {
223 std::lock_guard<std::mutex> l(l_);
224 format_ = format;
225 return;
226 }
SetCurFormat(const int32_t format)227 void ImageBuffer::SetCurFormat(const int32_t format)
228 {
229 std::lock_guard<std::mutex> l(l_);
230 curFormat_ = format;
231 return;
232 }
233
SetSize(const uint32_t size)234 void ImageBuffer::SetSize(const uint32_t size)
235 {
236 std::lock_guard<std::mutex> l(l_);
237 size_ = size;
238 return;
239 }
SetSuffaceBufferAddr(const void * addr)240 void ImageBuffer::SetSuffaceBufferAddr(const void* addr)
241 {
242 std::lock_guard<std::mutex> l(l_);
243 sbAddr_ = const_cast<void*>(addr);
244 }
245
SetSuffaceBufferSize(const uint32_t size)246 void ImageBuffer::SetSuffaceBufferSize(const uint32_t size)
247 {
248 std::lock_guard<std::mutex> l(l_);
249 sbSize_ = size;
250 }
251
SetUsage(const uint64_t usage)252 void ImageBuffer::SetUsage(const uint64_t usage)
253 {
254 std::lock_guard<std::mutex> l(l_);
255 usage_ = usage;
256 return;
257 }
258
SetVirAddress(const void * addr)259 void ImageBuffer::SetVirAddress(const void* addr)
260 {
261 std::lock_guard<std::mutex> l(l_);
262 virAddr_ = const_cast<void*>(addr);
263 return;
264 }
265
SetPhyAddress(const uint64_t addr)266 void ImageBuffer::SetPhyAddress(const uint64_t addr)
267 {
268 std::lock_guard<std::mutex> l(l_);
269 phyAddr_ = addr;
270 return;
271 }
272
SetFileDescriptor(const int32_t fd)273 void ImageBuffer::SetFileDescriptor(const int32_t fd)
274 {
275 std::lock_guard<std::mutex> l(l_);
276 fd_ = fd;
277 return;
278 }
279
SetTimestamp(const uint64_t timeStamp)280 void ImageBuffer::SetTimestamp(const uint64_t timeStamp)
281 {
282 std::lock_guard<std::mutex> l(l_);
283 timeStamp_ = timeStamp;
284 return;
285 }
286
SetFrameNumber(const uint64_t frameNumber)287 void ImageBuffer::SetFrameNumber(const uint64_t frameNumber)
288 {
289 std::lock_guard<std::mutex> l(l_);
290 frameNumber_ = frameNumber;
291 return;
292 }
293
SetPoolId(const int64_t id)294 void ImageBuffer::SetPoolId(const int64_t id)
295 {
296 std::lock_guard<std::mutex> l(l_);
297 poolId_ = id;
298 return;
299 }
300
SetCaptureId(const int32_t id)301 void ImageBuffer::SetCaptureId(const int32_t id)
302 {
303 std::lock_guard<std::mutex> l(l_);
304 captureId_ = id;
305 return;
306 }
307
SetBufferStatus(const CameraBufferStatus flag)308 void ImageBuffer::SetBufferStatus(const CameraBufferStatus flag)
309 {
310 std::lock_guard<std::mutex> l(l_);
311 status_ = flag;
312 return;
313 }
314
SetSequenceId(const int32_t sequence)315 void ImageBuffer::SetSequenceId(const int32_t sequence)
316 {
317 std::lock_guard<std::mutex> l(l_);
318 sequenceId_ = sequence;
319 return;
320 }
321
SetFenceId(const int32_t fence)322 void ImageBuffer::SetFenceId(const int32_t fence)
323 {
324 std::lock_guard<std::mutex> l(l_);
325 fenceId_ = fence;
326 return;
327 }
328
SetEsFrameSize(const int32_t frameSize)329 void ImageBuffer::SetEsFrameSize(const int32_t frameSize)
330 {
331 std::lock_guard<std::mutex> l(l_);
332 esInfo_.size = frameSize;
333 return;
334 }
335
SetEsTimestamp(const int64_t timeStamp)336 void ImageBuffer::SetEsTimestamp(const int64_t timeStamp)
337 {
338 std::lock_guard<std::mutex> l(l_);
339 esInfo_.timestamp = timeStamp;
340 return;
341 }
342
SetEsKeyFrame(const int32_t isKey)343 void ImageBuffer::SetEsKeyFrame(const int32_t isKey)
344 {
345 std::lock_guard<std::mutex> l(l_);
346 esInfo_.isKey = isKey;
347 return;
348 }
349
SetEsFrameNum(const int32_t frameNum)350 void ImageBuffer::SetEsFrameNum(const int32_t frameNum)
351 {
352 std::lock_guard<std::mutex> l(l_);
353 esInfo_.frameNum = frameNum;
354 return;
355 }
356
SetEncodeType(const int32_t type)357 void ImageBuffer::SetEncodeType(const int32_t type)
358 {
359 std::lock_guard<std::mutex> l(l_);
360 encodeType_ = type;
361 return;
362 }
363
SetStreamId(const int32_t streamId)364 void ImageBuffer::SetStreamId(const int32_t streamId)
365 {
366 std::lock_guard<std::mutex> l(l_);
367 streamId_ = streamId;
368 return;
369 }
370
SetIsValidDataInSurfaceBuffer(const bool isValid)371 void ImageBuffer::SetIsValidDataInSurfaceBuffer(const bool isValid)
372 {
373 std::lock_guard<std::mutex> l(l_);
374 isDataValidInSurfaceBuffer_ = isValid;
375 }
376
Free()377 void ImageBuffer::Free()
378 {
379 std::lock_guard<std::mutex> l(l_);
380 if (virAddr_ != sbAddr_ && virAddr_ != nullptr) {
381 CAMERA_LOGE("Free VirtualAddr, streamId = %{public}d, index = %{public}d", streamId_, index_);
382 free(virAddr_);
383 }
384 virAddr_ = nullptr;
385 index_ = -1;
386 width_ = 0;
387 height_ = 0;
388 stride_ = 0;
389 format_ = CAMERA_FORMAT_INVALID;
390 size_ = 0;
391 usage_ = 0;
392 phyAddr_ = 0;
393 fd_ = -1;
394 return;
395 }
396
operator ==(const IBuffer & u)397 bool ImageBuffer::operator==(const IBuffer& u)
398 {
399 if (u.GetSourceType() != sourceType_) {
400 return false;
401 }
402
403 if (u.GetPhyAddress() == 0 || phyAddr_ == 0) {
404 return u.GetVirAddress() == virAddr_;
405 }
406
407 return u.GetPhyAddress() == phyAddr_;
408 }
409 } // namespace OHOS::Camera
410