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