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 "camera_metadata_info.h"
17 #include "metadata_log.h"
18
19 namespace OHOS {
20 namespace Camera {
CameraMetadata(size_t itemCapacity,size_t dataCapacity)21 CameraMetadata::CameraMetadata(size_t itemCapacity, size_t dataCapacity)
22 {
23 metadata_ = AllocateCameraMetadataBuffer(itemCapacity, AlignTo(dataCapacity, DATA_ALIGNMENT));
24 }
25
~CameraMetadata()26 CameraMetadata::~CameraMetadata()
27 {
28 if (metadata_) {
29 FreeCameraMetadataBuffer(metadata_);
30 metadata_ = nullptr;
31 }
32 }
33
addEntry(uint32_t item,const void * data,size_t data_count)34 bool CameraMetadata::addEntry(uint32_t item, const void *data, size_t data_count)
35 {
36 if (metadata_ == nullptr) {
37 METADATA_ERR_LOG("metadata_ is null");
38 return false;
39 }
40
41 auto result = AddCameraMetadataItem(metadata_, item, data, data_count);
42 if (!result) {
43 return true;
44 }
45
46 if (result != CAM_META_ITEM_CAP_EXCEED && result != CAM_META_DATA_CAP_EXCEED) {
47 const char *name = GetCameraMetadataItemName(item);
48 (void)name;
49
50 if (name) {
51 METADATA_ERR_LOG("Failed to add tag. tagname = %{public}s", name);
52 } else {
53 METADATA_ERR_LOG("Failed to add unknown tag");
54 }
55 return false;
56 }
57
58 return resize_add_metadata(item, data, data_count);
59 }
60
resize_add_metadata(uint32_t item,const void * data,size_t data_count)61 bool CameraMetadata::resize_add_metadata(uint32_t item, const void *data, size_t data_count)
62 {
63 uint32_t data_type;
64
65 auto itemCapacity = GetCameraMetadataItemCapacity(metadata_);
66 auto data_capacity = GetCameraMetadataDataSize(metadata_);
67
68 int32_t ret = GetCameraMetadataItemType(item, &data_type);
69 if (ret != CAM_META_SUCCESS) {
70 METADATA_ERR_LOG("GetCameraMetadataItemType invalid item type");
71 return false;
72 }
73 size_t size = CalculateCameraMetadataItemDataSize(data_type, data_count);
74
75 common_metadata_header_t *newMetadata = AllocateCameraMetadataBuffer((itemCapacity + 1) * INDEX_COUNTER,
76 AlignTo((data_capacity + size) * INDEX_COUNTER, DATA_ALIGNMENT));
77
78 if (newMetadata == nullptr) {
79 METADATA_ERR_LOG("Failed to resize the metadata buffer");
80 return false;
81 }
82
83 auto result = CopyCameraMetadataItems(newMetadata, metadata_);
84 if (result != CAM_META_SUCCESS) {
85 METADATA_ERR_LOG("Failed to copy the old metadata to new metadata");
86 FreeCameraMetadataBuffer(newMetadata);
87 return false;
88 }
89
90 result = AddCameraMetadataItem(newMetadata, item, data, data_count);
91 if (result != CAM_META_SUCCESS) {
92 METADATA_ERR_LOG("Failed to add new entry");
93 FreeCameraMetadataBuffer(newMetadata);
94 return false;
95 }
96 replace_metadata(newMetadata);
97
98 return true;
99 }
100
replace_metadata(common_metadata_header_t * newMetadata)101 void CameraMetadata::replace_metadata(common_metadata_header_t *newMetadata)
102 {
103 if (metadata_ == newMetadata) {
104 return;
105 }
106
107 FreeCameraMetadataBuffer(metadata_);
108 metadata_ = newMetadata;
109 }
110
updateEntry(uint32_t tag,const void * data,size_t dataCount)111 bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t dataCount)
112 {
113 if (metadata_ == nullptr) {
114 METADATA_ERR_LOG("metadata_ is null");
115 return false;
116 }
117
118 camera_metadata_item_t item;
119 int ret = FindCameraMetadataItem(metadata_, tag, &item);
120 if (ret) {
121 const char *name = GetCameraMetadataItemName(tag);
122 (void)name;
123 METADATA_ERR_LOG("Failed to update tag tagname = %{public}s : not present", (name ? name : "<unknown>"));
124 return false;
125 }
126
127 ret = UpdateCameraMetadataItemByIndex(metadata_, item.index, data, dataCount, nullptr);
128 if (ret) {
129 const char *name = GetCameraMetadataItemName(tag);
130 (void)name;
131 METADATA_ERR_LOG("Failed to update tag tagname = %{public}s", (name ? name : "<unknown>"));
132 return false;
133 }
134
135 return true;
136 }
137
get()138 common_metadata_header_t *CameraMetadata::get()
139 {
140 return metadata_;
141 }
142
get() const143 const common_metadata_header_t *CameraMetadata::get() const
144 {
145 return metadata_;
146 }
147
isValid() const148 bool CameraMetadata::isValid() const
149 {
150 return metadata_ != nullptr;
151 }
152 }
153 }
154