Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | // #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #define LOG_TAG "CamComm1.0-MD" |
Steven Moreland | 4e7a307 | 2017-04-06 12:15:23 -0700 | [diff] [blame] | 20 | #include <log/log.h> |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | #include "CameraMetadata.h" |
| 24 | #include "VendorTagDescriptor.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace camera { |
| 29 | namespace common { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 30 | namespace helper { |
| 31 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 32 | #define ALIGN_TO(val, alignment) (((uintptr_t)(val) + ((alignment)-1)) & ~((alignment)-1)) |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 33 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 34 | CameraMetadata::CameraMetadata() : mBuffer(NULL), mLocked(false) {} |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 35 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 36 | CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) : mLocked(false) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 37 | mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity); |
| 38 | } |
| 39 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 40 | CameraMetadata::CameraMetadata(const CameraMetadata& other) : mLocked(false) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 41 | mBuffer = clone_camera_metadata(other.mBuffer); |
| 42 | } |
| 43 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 44 | CameraMetadata::CameraMetadata(camera_metadata_t* buffer) : mBuffer(NULL), mLocked(false) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 45 | acquire(buffer); |
| 46 | } |
| 47 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 48 | CameraMetadata& CameraMetadata::operator=(const CameraMetadata& other) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 49 | return operator=(other.mBuffer); |
| 50 | } |
| 51 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 52 | CameraMetadata& CameraMetadata::operator=(const camera_metadata_t* buffer) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 53 | if (mLocked) { |
| 54 | ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__); |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | if (CC_LIKELY(buffer != mBuffer)) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 59 | camera_metadata_t* newBuffer = clone_camera_metadata(buffer); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 60 | clear(); |
| 61 | mBuffer = newBuffer; |
| 62 | } |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | CameraMetadata::~CameraMetadata() { |
| 67 | mLocked = false; |
| 68 | clear(); |
| 69 | } |
| 70 | |
| 71 | const camera_metadata_t* CameraMetadata::getAndLock() const { |
| 72 | mLocked = true; |
| 73 | return mBuffer; |
| 74 | } |
| 75 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 76 | status_t CameraMetadata::unlock(const camera_metadata_t* buffer) const { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 77 | if (!mLocked) { |
| 78 | ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__); |
| 79 | return INVALID_OPERATION; |
| 80 | } |
| 81 | if (buffer != mBuffer) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 82 | ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!", __FUNCTION__); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 83 | return BAD_VALUE; |
| 84 | } |
| 85 | mLocked = false; |
| 86 | return OK; |
| 87 | } |
| 88 | |
| 89 | camera_metadata_t* CameraMetadata::release() { |
| 90 | if (mLocked) { |
| 91 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 92 | return NULL; |
| 93 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 94 | camera_metadata_t* released = mBuffer; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 95 | mBuffer = NULL; |
| 96 | return released; |
| 97 | } |
| 98 | |
| 99 | void CameraMetadata::clear() { |
| 100 | if (mLocked) { |
| 101 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 102 | return; |
| 103 | } |
| 104 | if (mBuffer) { |
| 105 | free_camera_metadata(mBuffer); |
| 106 | mBuffer = NULL; |
| 107 | } |
| 108 | } |
| 109 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 110 | void CameraMetadata::acquire(camera_metadata_t* buffer) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 111 | if (mLocked) { |
| 112 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 113 | return; |
| 114 | } |
| 115 | clear(); |
| 116 | mBuffer = buffer; |
| 117 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 118 | ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK, |
| 119 | "%s: Failed to validate metadata structure %p", __FUNCTION__, buffer); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 122 | void CameraMetadata::acquire(CameraMetadata& other) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 123 | if (mLocked) { |
| 124 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 125 | return; |
| 126 | } |
| 127 | acquire(other.release()); |
| 128 | } |
| 129 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 130 | status_t CameraMetadata::append(const CameraMetadata& other) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 131 | return append(other.mBuffer); |
| 132 | } |
| 133 | |
| 134 | status_t CameraMetadata::append(const camera_metadata_t* other) { |
| 135 | if (mLocked) { |
| 136 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 137 | return INVALID_OPERATION; |
| 138 | } |
| 139 | size_t extraEntries = get_camera_metadata_entry_count(other); |
| 140 | size_t extraData = get_camera_metadata_data_count(other); |
| 141 | resizeIfNeeded(extraEntries, extraData); |
| 142 | |
| 143 | return append_camera_metadata(mBuffer, other); |
| 144 | } |
| 145 | |
| 146 | size_t CameraMetadata::entryCount() const { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 147 | return (mBuffer == NULL) ? 0 : get_camera_metadata_entry_count(mBuffer); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool CameraMetadata::isEmpty() const { |
| 151 | return entryCount() == 0; |
| 152 | } |
| 153 | |
| 154 | status_t CameraMetadata::sort() { |
| 155 | if (mLocked) { |
| 156 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 157 | return INVALID_OPERATION; |
| 158 | } |
| 159 | return sort_camera_metadata(mBuffer); |
| 160 | } |
| 161 | |
| 162 | status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) { |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 163 | int tagType = get_local_camera_metadata_tag_type(tag, mBuffer); |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 164 | if (CC_UNLIKELY(tagType == -1)) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 165 | ALOGE("Update metadata entry: Unknown tag %d", tag); |
| 166 | return INVALID_OPERATION; |
| 167 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 168 | if (CC_UNLIKELY(tagType != expectedType)) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 169 | ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; " |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 170 | "got type %s data instead ", |
| 171 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, |
| 172 | camera_metadata_type_names[tagType], camera_metadata_type_names[expectedType]); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 173 | return INVALID_OPERATION; |
| 174 | } |
| 175 | return OK; |
| 176 | } |
| 177 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 178 | status_t CameraMetadata::update(uint32_t tag, const int32_t* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 179 | status_t res; |
| 180 | if (mLocked) { |
| 181 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 182 | return INVALID_OPERATION; |
| 183 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 184 | if ((res = checkType(tag, TYPE_INT32)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 185 | return res; |
| 186 | } |
| 187 | return updateImpl(tag, (const void*)data, data_count); |
| 188 | } |
| 189 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 190 | status_t CameraMetadata::update(uint32_t tag, const uint8_t* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 191 | status_t res; |
| 192 | if (mLocked) { |
| 193 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 194 | return INVALID_OPERATION; |
| 195 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 196 | if ((res = checkType(tag, TYPE_BYTE)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 197 | return res; |
| 198 | } |
| 199 | return updateImpl(tag, (const void*)data, data_count); |
| 200 | } |
| 201 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 202 | status_t CameraMetadata::update(uint32_t tag, const float* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 203 | status_t res; |
| 204 | if (mLocked) { |
| 205 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 206 | return INVALID_OPERATION; |
| 207 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 208 | if ((res = checkType(tag, TYPE_FLOAT)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 209 | return res; |
| 210 | } |
| 211 | return updateImpl(tag, (const void*)data, data_count); |
| 212 | } |
| 213 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 214 | status_t CameraMetadata::update(uint32_t tag, const int64_t* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 215 | status_t res; |
| 216 | if (mLocked) { |
| 217 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 218 | return INVALID_OPERATION; |
| 219 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 220 | if ((res = checkType(tag, TYPE_INT64)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 221 | return res; |
| 222 | } |
| 223 | return updateImpl(tag, (const void*)data, data_count); |
| 224 | } |
| 225 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 226 | status_t CameraMetadata::update(uint32_t tag, const double* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 227 | status_t res; |
| 228 | if (mLocked) { |
| 229 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 230 | return INVALID_OPERATION; |
| 231 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 232 | if ((res = checkType(tag, TYPE_DOUBLE)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 233 | return res; |
| 234 | } |
| 235 | return updateImpl(tag, (const void*)data, data_count); |
| 236 | } |
| 237 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 238 | status_t CameraMetadata::update(uint32_t tag, const camera_metadata_rational_t* data, |
| 239 | size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 240 | status_t res; |
| 241 | if (mLocked) { |
| 242 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 243 | return INVALID_OPERATION; |
| 244 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 245 | if ((res = checkType(tag, TYPE_RATIONAL)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 246 | return res; |
| 247 | } |
| 248 | return updateImpl(tag, (const void*)data, data_count); |
| 249 | } |
| 250 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 251 | status_t CameraMetadata::update(uint32_t tag, const String8& string) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 252 | status_t res; |
| 253 | if (mLocked) { |
| 254 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 255 | return INVALID_OPERATION; |
| 256 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 257 | if ((res = checkType(tag, TYPE_BYTE)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 258 | return res; |
| 259 | } |
| 260 | // string.size() doesn't count the null termination character. |
Tomasz Wasilczyk | dbec48b | 2023-08-23 18:46:52 +0000 | [diff] [blame] | 261 | return updateImpl(tag, (const void*)string.c_str(), string.size() + 1); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 264 | status_t CameraMetadata::update(const camera_metadata_ro_entry& entry) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 265 | status_t res; |
| 266 | if (mLocked) { |
| 267 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 268 | return INVALID_OPERATION; |
| 269 | } |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 270 | if ((res = checkType(entry.tag, entry.type)) != OK) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 271 | return res; |
| 272 | } |
| 273 | return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count); |
| 274 | } |
| 275 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 276 | status_t CameraMetadata::updateImpl(uint32_t tag, const void* data, size_t data_count) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 277 | status_t res; |
| 278 | if (mLocked) { |
| 279 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 280 | return INVALID_OPERATION; |
| 281 | } |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 282 | int type = get_local_camera_metadata_tag_type(tag, mBuffer); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 283 | if (type == -1) { |
| 284 | ALOGE("%s: Tag %d not found", __FUNCTION__, tag); |
| 285 | return BAD_VALUE; |
| 286 | } |
| 287 | // Safety check - ensure that data isn't pointing to this metadata, since |
| 288 | // that would get invalidated if a resize is needed |
| 289 | size_t bufferSize = get_camera_metadata_size(mBuffer); |
| 290 | uintptr_t bufAddr = reinterpret_cast<uintptr_t>(mBuffer); |
| 291 | uintptr_t dataAddr = reinterpret_cast<uintptr_t>(data); |
| 292 | if (dataAddr > bufAddr && dataAddr < (bufAddr + bufferSize)) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 293 | ALOGE("%s: Update attempted with data from the same metadata buffer!", __FUNCTION__); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 294 | return INVALID_OPERATION; |
| 295 | } |
| 296 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 297 | size_t data_size = calculate_camera_metadata_entry_data_size(type, data_count); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 298 | |
| 299 | res = resizeIfNeeded(1, data_size); |
| 300 | |
| 301 | if (res == OK) { |
| 302 | camera_metadata_entry_t entry; |
| 303 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 304 | if (res == NAME_NOT_FOUND) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 305 | res = add_camera_metadata_entry(mBuffer, tag, data, data_count); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 306 | } else if (res == OK) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 307 | res = update_camera_metadata_entry(mBuffer, entry.index, data, data_count, NULL); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | if (res != OK) { |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 312 | ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)", __FUNCTION__, |
| 313 | get_local_camera_metadata_section_name(tag, mBuffer), |
| 314 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | IF_ALOGV() { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 318 | ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK, |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 319 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 320 | "%s: Failed to validate metadata structure after update %p", __FUNCTION__, |
| 321 | mBuffer); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return res; |
| 325 | } |
| 326 | |
| 327 | bool CameraMetadata::exists(uint32_t tag) const { |
| 328 | camera_metadata_ro_entry entry; |
| 329 | return find_camera_metadata_ro_entry(mBuffer, tag, &entry) == 0; |
| 330 | } |
| 331 | |
| 332 | camera_metadata_entry_t CameraMetadata::find(uint32_t tag) { |
| 333 | status_t res; |
| 334 | camera_metadata_entry entry; |
| 335 | if (mLocked) { |
| 336 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 337 | entry.count = 0; |
| 338 | return entry; |
| 339 | } |
| 340 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 341 | if (CC_UNLIKELY(res != OK)) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 342 | entry.count = 0; |
| 343 | entry.data.u8 = NULL; |
| 344 | } |
| 345 | return entry; |
| 346 | } |
| 347 | |
| 348 | camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const { |
| 349 | status_t res; |
| 350 | camera_metadata_ro_entry entry; |
| 351 | res = find_camera_metadata_ro_entry(mBuffer, tag, &entry); |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 352 | if (CC_UNLIKELY(res != OK)) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 353 | entry.count = 0; |
| 354 | entry.data.u8 = NULL; |
| 355 | } |
| 356 | return entry; |
| 357 | } |
| 358 | |
| 359 | status_t CameraMetadata::erase(uint32_t tag) { |
| 360 | camera_metadata_entry_t entry; |
| 361 | status_t res; |
| 362 | if (mLocked) { |
| 363 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 364 | return INVALID_OPERATION; |
| 365 | } |
| 366 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 367 | if (res == NAME_NOT_FOUND) { |
| 368 | return OK; |
| 369 | } else if (res != OK) { |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 370 | ALOGE("%s: Error looking for entry %s.%s (%x): %s %d", __FUNCTION__, |
| 371 | get_local_camera_metadata_section_name(tag, mBuffer), |
| 372 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 373 | return res; |
| 374 | } |
| 375 | res = delete_camera_metadata_entry(mBuffer, entry.index); |
| 376 | if (res != OK) { |
Jayant Chowdhary | 661f441 | 2019-04-01 10:29:53 -0700 | [diff] [blame] | 377 | ALOGE("%s: Error deleting entry %s.%s (%x): %s %d", __FUNCTION__, |
| 378 | get_local_camera_metadata_section_name(tag, mBuffer), |
| 379 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, strerror(-res), res); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 380 | } |
| 381 | return res; |
| 382 | } |
| 383 | |
| 384 | void CameraMetadata::dump(int fd, int verbosity, int indentation) const { |
| 385 | dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation); |
| 386 | } |
| 387 | |
| 388 | status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) { |
| 389 | if (mBuffer == NULL) { |
| 390 | mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2); |
| 391 | if (mBuffer == NULL) { |
| 392 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 393 | return NO_MEMORY; |
| 394 | } |
| 395 | } else { |
| 396 | size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer); |
| 397 | size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer); |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 398 | size_t newEntryCount = currentEntryCount + extraEntries; |
| 399 | newEntryCount = (newEntryCount > currentEntryCap) ? newEntryCount * 2 : currentEntryCap; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 400 | |
| 401 | size_t currentDataCount = get_camera_metadata_data_count(mBuffer); |
| 402 | size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer); |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 403 | size_t newDataCount = currentDataCount + extraData; |
| 404 | newDataCount = (newDataCount > currentDataCap) ? newDataCount * 2 : currentDataCap; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 405 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 406 | if (newEntryCount > currentEntryCap || newDataCount > currentDataCap) { |
| 407 | camera_metadata_t* oldBuffer = mBuffer; |
| 408 | mBuffer = allocate_camera_metadata(newEntryCount, newDataCount); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 409 | if (mBuffer == NULL) { |
| 410 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 411 | return NO_MEMORY; |
| 412 | } |
| 413 | append_camera_metadata(mBuffer, oldBuffer); |
| 414 | free_camera_metadata(oldBuffer); |
| 415 | } |
| 416 | } |
| 417 | return OK; |
| 418 | } |
| 419 | |
| 420 | void CameraMetadata::swap(CameraMetadata& other) { |
| 421 | if (mLocked) { |
| 422 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 423 | return; |
| 424 | } else if (other.mLocked) { |
| 425 | ALOGE("%s: Other CameraMetadata is locked", __FUNCTION__); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | camera_metadata* thisBuf = mBuffer; |
| 430 | camera_metadata* otherBuf = other.mBuffer; |
| 431 | |
| 432 | other.mBuffer = thisBuf; |
| 433 | mBuffer = otherBuf; |
| 434 | } |
| 435 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 436 | status_t CameraMetadata::getTagFromName(const char* name, const VendorTagDescriptor* vTags, |
| 437 | uint32_t* tag) { |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 438 | if (name == nullptr || tag == nullptr) return BAD_VALUE; |
| 439 | |
| 440 | size_t nameLength = strlen(name); |
| 441 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 442 | const SortedVector<String8>* vendorSections; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 443 | size_t vendorSectionCount = 0; |
| 444 | |
| 445 | if (vTags != NULL) { |
| 446 | vendorSections = vTags->getAllSectionNames(); |
| 447 | vendorSectionCount = vendorSections->size(); |
| 448 | } |
| 449 | |
| 450 | // First, find the section by the longest string match |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 451 | const char* section = NULL; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 452 | size_t sectionIndex = 0; |
| 453 | size_t sectionLength = 0; |
| 454 | size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount; |
| 455 | for (size_t i = 0; i < totalSectionCount; ++i) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 456 | const char* str = (i < ANDROID_SECTION_COUNT) |
| 457 | ? camera_metadata_section_names[i] |
Tomasz Wasilczyk | dbec48b | 2023-08-23 18:46:52 +0000 | [diff] [blame] | 458 | : (*vendorSections)[i - ANDROID_SECTION_COUNT].c_str(); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 459 | |
| 460 | ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str); |
| 461 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 462 | if (strstr(name, str) == name) { // name begins with the section name |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 463 | size_t strLength = strlen(str); |
| 464 | |
| 465 | ALOGV("%s: Name begins with section name", __FUNCTION__); |
| 466 | |
| 467 | // section name is the longest we've found so far |
| 468 | if (section == NULL || sectionLength < strLength) { |
| 469 | section = str; |
| 470 | sectionIndex = i; |
| 471 | sectionLength = strLength; |
| 472 | |
| 473 | ALOGV("%s: Found new best section (%s)", __FUNCTION__, section); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 478 | if (section == NULL) { |
| 479 | return NAME_NOT_FOUND; |
| 480 | } else { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 481 | ALOGV("%s: Found matched section '%s' (%zu)", __FUNCTION__, section, sectionIndex); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // Get the tag name component of the name |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 485 | const char* nameTagName = name + sectionLength + 1; // x.y.z -> z |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 486 | if (sectionLength + 1 >= nameLength) { |
| 487 | return BAD_VALUE; |
| 488 | } |
| 489 | |
| 490 | // Match rest of name against the tag names in that section only |
| 491 | uint32_t candidateTag = 0; |
| 492 | if (sectionIndex < ANDROID_SECTION_COUNT) { |
| 493 | // Match built-in tags (typically android.*) |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 494 | uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd) |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 495 | tagBegin = camera_metadata_section_bounds[sectionIndex][0]; |
| 496 | tagEnd = camera_metadata_section_bounds[sectionIndex][1]; |
| 497 | |
| 498 | for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 499 | const char* tagName = get_camera_metadata_tag_name(candidateTag); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 500 | |
| 501 | if (strcmp(nameTagName, tagName) == 0) { |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 502 | ALOGV("%s: Found matched tag '%s' (%d)", __FUNCTION__, tagName, candidateTag); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (candidateTag == tagEnd) { |
| 508 | return NAME_NOT_FOUND; |
| 509 | } |
| 510 | } else if (vTags != NULL) { |
| 511 | // Match vendor tags (typically com.*) |
| 512 | const String8 sectionName(section); |
| 513 | const String8 tagName(nameTagName); |
| 514 | |
| 515 | status_t res = OK; |
| 516 | if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) { |
| 517 | return NAME_NOT_FOUND; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | *tag = candidateTag; |
| 522 | return OK; |
| 523 | } |
| 524 | |
Avichal Rakesh | 0d2d8a4 | 2022-06-14 17:23:40 -0700 | [diff] [blame] | 525 | } // namespace helper |
| 526 | } // namespace common |
| 527 | } // namespace camera |
| 528 | } // namespace hardware |
| 529 | } // namespace android |