Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Igor Murashkin | e2d1e3d | 2013-04-30 18:18:06 -0700 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | |
Eino-Ville Talvala | 4bb8118 | 2012-09-24 09:46:53 -0700 | [diff] [blame] | 19 | #define LOG_TAG "Camera2-Metadata" |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | #include <utils/Errors.h> |
| 22 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 23 | #include <binder/Parcel.h> |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 24 | #include <camera/CameraMetadata.h> |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 28 | #define ALIGN_TO(val, alignment) \ |
| 29 | (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1)) |
| 30 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 31 | typedef Parcel::WritableBlob WritableBlob; |
| 32 | typedef Parcel::ReadableBlob ReadableBlob; |
| 33 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 34 | CameraMetadata::CameraMetadata() : |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 35 | mBuffer(NULL), mLocked(false) { |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 38 | CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) : |
| 39 | mLocked(false) |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 40 | { |
| 41 | mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity); |
| 42 | } |
| 43 | |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 44 | CameraMetadata::CameraMetadata(const CameraMetadata &other) : |
| 45 | mLocked(false) { |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 46 | mBuffer = clone_camera_metadata(other.mBuffer); |
| 47 | } |
| 48 | |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 49 | CameraMetadata::CameraMetadata(CameraMetadata &&other) :mBuffer(NULL), mLocked(false) { |
| 50 | acquire(other); |
| 51 | } |
| 52 | |
| 53 | CameraMetadata &CameraMetadata::operator=(CameraMetadata &&other) { |
| 54 | acquire(other); |
| 55 | return *this; |
| 56 | } |
| 57 | |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 58 | CameraMetadata::CameraMetadata(camera_metadata_t *buffer) : |
| 59 | mBuffer(NULL), mLocked(false) { |
Igor Murashkin | 7efa520 | 2013-02-13 15:53:56 -0800 | [diff] [blame] | 60 | acquire(buffer); |
| 61 | } |
| 62 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 63 | CameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) { |
| 64 | return operator=(other.mBuffer); |
| 65 | } |
| 66 | |
| 67 | CameraMetadata &CameraMetadata::operator=(const camera_metadata_t *buffer) { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 68 | if (mLocked) { |
| 69 | ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__); |
| 70 | return *this; |
| 71 | } |
| 72 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 73 | if (CC_LIKELY(buffer != mBuffer)) { |
| 74 | camera_metadata_t *newBuffer = clone_camera_metadata(buffer); |
| 75 | clear(); |
| 76 | mBuffer = newBuffer; |
| 77 | } |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | CameraMetadata::~CameraMetadata() { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 82 | mLocked = false; |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 83 | clear(); |
| 84 | } |
| 85 | |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 86 | const camera_metadata_t* CameraMetadata::getAndLock() const { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 87 | mLocked = true; |
| 88 | return mBuffer; |
| 89 | } |
| 90 | |
Yin-Chia Yeh | 8aac03f | 2016-03-03 15:45:23 -0800 | [diff] [blame] | 91 | status_t CameraMetadata::unlock(const camera_metadata_t *buffer) const { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 92 | if (!mLocked) { |
| 93 | ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__); |
| 94 | return INVALID_OPERATION; |
| 95 | } |
| 96 | if (buffer != mBuffer) { |
| 97 | ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!", |
| 98 | __FUNCTION__); |
| 99 | return BAD_VALUE; |
| 100 | } |
| 101 | mLocked = false; |
| 102 | return OK; |
| 103 | } |
| 104 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 105 | camera_metadata_t* CameraMetadata::release() { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 106 | if (mLocked) { |
| 107 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 108 | return NULL; |
| 109 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 110 | camera_metadata_t *released = mBuffer; |
| 111 | mBuffer = NULL; |
| 112 | return released; |
| 113 | } |
| 114 | |
| 115 | void CameraMetadata::clear() { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 116 | if (mLocked) { |
| 117 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 118 | return; |
| 119 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 120 | if (mBuffer) { |
| 121 | free_camera_metadata(mBuffer); |
| 122 | mBuffer = NULL; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void CameraMetadata::acquire(camera_metadata_t *buffer) { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 127 | if (mLocked) { |
| 128 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 129 | return; |
| 130 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 131 | clear(); |
| 132 | mBuffer = buffer; |
Igor Murashkin | e2d1e3d | 2013-04-30 18:18:06 -0700 | [diff] [blame] | 133 | |
| 134 | ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) != OK, |
| 135 | "%s: Failed to validate metadata structure %p", |
| 136 | __FUNCTION__, buffer); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void CameraMetadata::acquire(CameraMetadata &other) { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 140 | if (mLocked) { |
| 141 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 142 | return; |
| 143 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 144 | acquire(other.release()); |
| 145 | } |
| 146 | |
| 147 | status_t CameraMetadata::append(const CameraMetadata &other) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 148 | return append(other.mBuffer); |
| 149 | } |
| 150 | |
| 151 | status_t CameraMetadata::append(const camera_metadata_t* other) { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 152 | if (mLocked) { |
| 153 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 154 | return INVALID_OPERATION; |
| 155 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 156 | size_t extraEntries = get_camera_metadata_entry_count(other); |
| 157 | size_t extraData = get_camera_metadata_data_count(other); |
| 158 | resizeIfNeeded(extraEntries, extraData); |
| 159 | |
| 160 | return append_camera_metadata(mBuffer, other); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | size_t CameraMetadata::entryCount() const { |
| 164 | return (mBuffer == NULL) ? 0 : |
| 165 | get_camera_metadata_entry_count(mBuffer); |
| 166 | } |
| 167 | |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 168 | bool CameraMetadata::isEmpty() const { |
| 169 | return entryCount() == 0; |
| 170 | } |
| 171 | |
Emilian Peev | 6de3239 | 2020-09-09 17:29:48 -0700 | [diff] [blame] | 172 | size_t CameraMetadata::bufferSize() const { |
| 173 | return (mBuffer == NULL) ? 0 : |
| 174 | get_camera_metadata_size(mBuffer); |
| 175 | } |
| 176 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 177 | status_t CameraMetadata::sort() { |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 178 | if (mLocked) { |
| 179 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 180 | return INVALID_OPERATION; |
| 181 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 182 | return sort_camera_metadata(mBuffer); |
| 183 | } |
| 184 | |
| 185 | status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) { |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 186 | int tagType = get_local_camera_metadata_tag_type(tag, mBuffer); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 187 | if ( CC_UNLIKELY(tagType == -1)) { |
| 188 | ALOGE("Update metadata entry: Unknown tag %d", tag); |
| 189 | return INVALID_OPERATION; |
| 190 | } |
| 191 | if ( CC_UNLIKELY(tagType != expectedType) ) { |
| 192 | ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; " |
| 193 | "got type %s data instead ", |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 194 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 195 | camera_metadata_type_names[tagType], |
| 196 | camera_metadata_type_names[expectedType]); |
| 197 | return INVALID_OPERATION; |
| 198 | } |
| 199 | return OK; |
| 200 | } |
| 201 | |
| 202 | status_t CameraMetadata::update(uint32_t tag, |
| 203 | const int32_t *data, size_t data_count) { |
| 204 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 205 | if (mLocked) { |
| 206 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 207 | return INVALID_OPERATION; |
| 208 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 209 | if ( (res = checkType(tag, TYPE_INT32)) != OK) { |
| 210 | return res; |
| 211 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 212 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | status_t CameraMetadata::update(uint32_t tag, |
| 216 | const uint8_t *data, size_t data_count) { |
| 217 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 218 | if (mLocked) { |
| 219 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 220 | return INVALID_OPERATION; |
| 221 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 222 | if ( (res = checkType(tag, TYPE_BYTE)) != OK) { |
| 223 | return res; |
| 224 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 225 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | status_t CameraMetadata::update(uint32_t tag, |
| 229 | const float *data, size_t data_count) { |
| 230 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 231 | if (mLocked) { |
| 232 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 233 | return INVALID_OPERATION; |
| 234 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 235 | if ( (res = checkType(tag, TYPE_FLOAT)) != OK) { |
| 236 | return res; |
| 237 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 238 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | status_t CameraMetadata::update(uint32_t tag, |
| 242 | const int64_t *data, size_t data_count) { |
| 243 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 244 | if (mLocked) { |
| 245 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 246 | return INVALID_OPERATION; |
| 247 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 248 | if ( (res = checkType(tag, TYPE_INT64)) != OK) { |
| 249 | return res; |
| 250 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 251 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | status_t CameraMetadata::update(uint32_t tag, |
| 255 | const double *data, size_t data_count) { |
| 256 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 257 | if (mLocked) { |
| 258 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 259 | return INVALID_OPERATION; |
| 260 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 261 | if ( (res = checkType(tag, TYPE_DOUBLE)) != OK) { |
| 262 | return res; |
| 263 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 264 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | status_t CameraMetadata::update(uint32_t tag, |
| 268 | const camera_metadata_rational_t *data, size_t data_count) { |
| 269 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 270 | if (mLocked) { |
| 271 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 272 | return INVALID_OPERATION; |
| 273 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 274 | if ( (res = checkType(tag, TYPE_RATIONAL)) != OK) { |
| 275 | return res; |
| 276 | } |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 277 | return updateImpl(tag, (const void*)data, data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | status_t CameraMetadata::update(uint32_t tag, |
| 281 | const String8 &string) { |
| 282 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 283 | if (mLocked) { |
| 284 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 285 | return INVALID_OPERATION; |
| 286 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 287 | if ( (res = checkType(tag, TYPE_BYTE)) != OK) { |
| 288 | return res; |
| 289 | } |
Zhijun He | 7595c47 | 2014-03-27 16:46:15 -0700 | [diff] [blame] | 290 | // string.size() doesn't count the null termination character. |
| 291 | return updateImpl(tag, (const void*)string.string(), string.size() + 1); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 294 | status_t CameraMetadata::update(const camera_metadata_ro_entry &entry) { |
| 295 | status_t res; |
| 296 | if (mLocked) { |
| 297 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 298 | return INVALID_OPERATION; |
| 299 | } |
| 300 | if ( (res = checkType(entry.tag, entry.type)) != OK) { |
| 301 | return res; |
| 302 | } |
| 303 | return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count); |
| 304 | } |
| 305 | |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 306 | status_t CameraMetadata::updateImpl(uint32_t tag, const void *data, |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 307 | size_t data_count) { |
| 308 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 309 | if (mLocked) { |
| 310 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 311 | return INVALID_OPERATION; |
| 312 | } |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 313 | int type = get_local_camera_metadata_tag_type(tag, mBuffer); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 314 | if (type == -1) { |
| 315 | ALOGE("%s: Tag %d not found", __FUNCTION__, tag); |
| 316 | return BAD_VALUE; |
| 317 | } |
Eino-Ville Talvala | e2b60c8 | 2015-07-17 16:21:44 -0700 | [diff] [blame] | 318 | // Safety check - ensure that data isn't pointing to this metadata, since |
| 319 | // that would get invalidated if a resize is needed |
| 320 | size_t bufferSize = get_camera_metadata_size(mBuffer); |
| 321 | uintptr_t bufAddr = reinterpret_cast<uintptr_t>(mBuffer); |
| 322 | uintptr_t dataAddr = reinterpret_cast<uintptr_t>(data); |
| 323 | if (dataAddr > bufAddr && dataAddr < (bufAddr + bufferSize)) { |
| 324 | ALOGE("%s: Update attempted with data from the same metadata buffer!", |
| 325 | __FUNCTION__); |
| 326 | return INVALID_OPERATION; |
| 327 | } |
| 328 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 329 | size_t data_size = calculate_camera_metadata_entry_data_size(type, |
| 330 | data_count); |
| 331 | |
| 332 | res = resizeIfNeeded(1, data_size); |
| 333 | |
| 334 | if (res == OK) { |
| 335 | camera_metadata_entry_t entry; |
| 336 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 337 | if (res == NAME_NOT_FOUND) { |
| 338 | res = add_camera_metadata_entry(mBuffer, |
| 339 | tag, data, data_count); |
| 340 | } else if (res == OK) { |
| 341 | res = update_camera_metadata_entry(mBuffer, |
| 342 | entry.index, data, data_count, NULL); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (res != OK) { |
| 347 | ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)", |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 348 | __FUNCTION__, get_local_camera_metadata_section_name(tag, mBuffer), |
| 349 | get_local_camera_metadata_tag_name(tag, mBuffer), tag, |
| 350 | strerror(-res), res); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 351 | } |
Igor Murashkin | e2d1e3d | 2013-04-30 18:18:06 -0700 | [diff] [blame] | 352 | |
| 353 | IF_ALOGV() { |
| 354 | ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) != |
| 355 | OK, |
| 356 | |
| 357 | "%s: Failed to validate metadata structure after update %p", |
| 358 | __FUNCTION__, mBuffer); |
| 359 | } |
| 360 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 361 | return res; |
| 362 | } |
| 363 | |
Igor Murashkin | fc42642a | 2013-02-13 18:23:39 -0800 | [diff] [blame] | 364 | bool CameraMetadata::exists(uint32_t tag) const { |
| 365 | camera_metadata_ro_entry entry; |
| 366 | return find_camera_metadata_ro_entry(mBuffer, tag, &entry) == 0; |
| 367 | } |
| 368 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 369 | camera_metadata_entry_t CameraMetadata::find(uint32_t tag) { |
| 370 | status_t res; |
| 371 | camera_metadata_entry entry; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 372 | if (mLocked) { |
| 373 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 374 | entry.count = 0; |
| 375 | return entry; |
| 376 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 377 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 378 | if (CC_UNLIKELY( res != OK )) { |
| 379 | entry.count = 0; |
| 380 | entry.data.u8 = NULL; |
| 381 | } |
| 382 | return entry; |
| 383 | } |
| 384 | |
| 385 | camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const { |
| 386 | status_t res; |
| 387 | camera_metadata_ro_entry entry; |
| 388 | res = find_camera_metadata_ro_entry(mBuffer, tag, &entry); |
| 389 | if (CC_UNLIKELY( res != OK )) { |
| 390 | entry.count = 0; |
| 391 | entry.data.u8 = NULL; |
| 392 | } |
| 393 | return entry; |
| 394 | } |
| 395 | |
| 396 | status_t CameraMetadata::erase(uint32_t tag) { |
| 397 | camera_metadata_entry_t entry; |
| 398 | status_t res; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 399 | if (mLocked) { |
| 400 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 401 | return INVALID_OPERATION; |
| 402 | } |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 403 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 404 | if (res == NAME_NOT_FOUND) { |
| 405 | return OK; |
| 406 | } else if (res != OK) { |
| 407 | ALOGE("%s: Error looking for entry %s.%s (%x): %s %d", |
| 408 | __FUNCTION__, |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 409 | get_local_camera_metadata_section_name(tag, mBuffer), |
| 410 | get_local_camera_metadata_tag_name(tag, mBuffer), |
| 411 | tag, strerror(-res), res); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 412 | return res; |
| 413 | } |
| 414 | res = delete_camera_metadata_entry(mBuffer, entry.index); |
| 415 | if (res != OK) { |
| 416 | ALOGE("%s: Error deleting entry %s.%s (%x): %s %d", |
| 417 | __FUNCTION__, |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 418 | get_local_camera_metadata_section_name(tag, mBuffer), |
| 419 | get_local_camera_metadata_tag_name(tag, mBuffer), |
| 420 | tag, strerror(-res), res); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 421 | } |
| 422 | return res; |
| 423 | } |
| 424 | |
Emilian Peev | e20c637 | 2018-08-14 18:45:53 +0100 | [diff] [blame] | 425 | status_t CameraMetadata::removePermissionEntries(metadata_vendor_id_t vendorId, |
| 426 | std::vector<int32_t> *tagsRemoved) { |
| 427 | uint32_t tagCount = 0; |
| 428 | std::vector<uint32_t> tagsToRemove; |
| 429 | |
| 430 | if (tagsRemoved == nullptr) { |
| 431 | return BAD_VALUE; |
| 432 | } |
| 433 | |
| 434 | sp<VendorTagDescriptor> vTags = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 435 | if ((nullptr == vTags.get()) || (0 >= vTags->getTagCount())) { |
| 436 | sp<VendorTagDescriptorCache> cache = |
| 437 | VendorTagDescriptorCache::getGlobalVendorTagCache(); |
| 438 | if (cache.get()) { |
| 439 | cache->getVendorTagDescriptor(vendorId, &vTags); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if ((nullptr != vTags.get()) && (vTags->getTagCount() > 0)) { |
| 444 | tagCount = vTags->getTagCount(); |
| 445 | uint32_t *vendorTags = new uint32_t[tagCount]; |
| 446 | if (nullptr == vendorTags) { |
| 447 | return NO_MEMORY; |
| 448 | } |
| 449 | vTags->getTagArray(vendorTags); |
| 450 | |
| 451 | tagsToRemove.reserve(tagCount); |
| 452 | tagsToRemove.insert(tagsToRemove.begin(), vendorTags, vendorTags + tagCount); |
| 453 | |
| 454 | delete [] vendorTags; |
| 455 | tagCount = 0; |
| 456 | } |
| 457 | |
| 458 | auto tagsNeedingPermission = get_camera_metadata_permission_needed(&tagCount); |
| 459 | if (tagCount > 0) { |
| 460 | tagsToRemove.reserve(tagsToRemove.capacity() + tagCount); |
| 461 | tagsToRemove.insert(tagsToRemove.end(), tagsNeedingPermission, |
| 462 | tagsNeedingPermission + tagCount); |
| 463 | } |
| 464 | |
| 465 | tagsRemoved->reserve(tagsToRemove.size()); |
| 466 | for (const auto &it : tagsToRemove) { |
| 467 | if (exists(it)) { |
| 468 | auto rc = erase(it); |
| 469 | if (NO_ERROR != rc) { |
| 470 | ALOGE("%s: Failed to erase tag: %x", __func__, it); |
| 471 | return rc; |
| 472 | } |
| 473 | tagsRemoved->push_back(it); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Update the available characterstics accordingly |
| 478 | if (exists(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS)) { |
| 479 | std::vector<uint32_t> currentKeys; |
| 480 | |
| 481 | std::sort(tagsRemoved->begin(), tagsRemoved->end()); |
| 482 | auto keys = find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); |
| 483 | currentKeys.reserve(keys.count); |
| 484 | currentKeys.insert(currentKeys.end(), keys.data.i32, keys.data.i32 + keys.count); |
| 485 | std::sort(currentKeys.begin(), currentKeys.end()); |
| 486 | |
| 487 | std::vector<int32_t> newKeys(keys.count); |
| 488 | auto end = std::set_difference(currentKeys.begin(), currentKeys.end(), tagsRemoved->begin(), |
| 489 | tagsRemoved->end(), newKeys.begin()); |
| 490 | newKeys.resize(end - newKeys.begin()); |
| 491 | |
| 492 | update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, newKeys.data(), newKeys.size()); |
| 493 | } |
| 494 | |
| 495 | return NO_ERROR; |
| 496 | } |
| 497 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 498 | void CameraMetadata::dump(int fd, int verbosity, int indentation) const { |
| 499 | dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation); |
| 500 | } |
| 501 | |
| 502 | status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) { |
| 503 | if (mBuffer == NULL) { |
| 504 | mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2); |
| 505 | if (mBuffer == NULL) { |
| 506 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 507 | return NO_MEMORY; |
| 508 | } |
| 509 | } else { |
| 510 | size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer); |
| 511 | size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer); |
| 512 | size_t newEntryCount = currentEntryCount + |
| 513 | extraEntries; |
| 514 | newEntryCount = (newEntryCount > currentEntryCap) ? |
| 515 | newEntryCount * 2 : currentEntryCap; |
| 516 | |
| 517 | size_t currentDataCount = get_camera_metadata_data_count(mBuffer); |
| 518 | size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer); |
| 519 | size_t newDataCount = currentDataCount + |
| 520 | extraData; |
| 521 | newDataCount = (newDataCount > currentDataCap) ? |
| 522 | newDataCount * 2 : currentDataCap; |
| 523 | |
| 524 | if (newEntryCount > currentEntryCap || |
| 525 | newDataCount > currentDataCap) { |
| 526 | camera_metadata_t *oldBuffer = mBuffer; |
| 527 | mBuffer = allocate_camera_metadata(newEntryCount, |
| 528 | newDataCount); |
| 529 | if (mBuffer == NULL) { |
| 530 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 531 | return NO_MEMORY; |
| 532 | } |
| 533 | append_camera_metadata(mBuffer, oldBuffer); |
| 534 | free_camera_metadata(oldBuffer); |
| 535 | } |
| 536 | } |
| 537 | return OK; |
| 538 | } |
| 539 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 540 | status_t CameraMetadata::readFromParcel(const Parcel& data, |
| 541 | camera_metadata_t** out) { |
Jayant Chowdhary | 2e310da | 2020-05-12 12:40:36 -0700 | [diff] [blame] | 542 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 543 | status_t err = OK; |
| 544 | |
| 545 | camera_metadata_t* metadata = NULL; |
| 546 | |
| 547 | if (out) { |
| 548 | *out = NULL; |
| 549 | } |
| 550 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 551 | // See CameraMetadata::writeToParcel for parcel data layout diagram and explanation. |
| 552 | // arg0 = blobSize (int32) |
| 553 | int32_t blobSizeTmp = -1; |
| 554 | if ((err = data.readInt32(&blobSizeTmp)) != OK) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 555 | ALOGE("%s: Failed to read metadata size (error %d %s)", |
| 556 | __FUNCTION__, err, strerror(-err)); |
| 557 | return err; |
| 558 | } |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 559 | const size_t blobSize = static_cast<size_t>(blobSizeTmp); |
| 560 | const size_t alignment = get_camera_metadata_alignment(); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 561 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 562 | // Special case: zero blob size means zero sized (NULL) metadata. |
| 563 | if (blobSize == 0) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 564 | ALOGV("%s: Read 0-sized metadata", __FUNCTION__); |
| 565 | return OK; |
| 566 | } |
| 567 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 568 | if (blobSize <= alignment) { |
| 569 | ALOGE("%s: metadata blob is malformed, blobSize(%zu) should be larger than alignment(%zu)", |
| 570 | __FUNCTION__, blobSize, alignment); |
| 571 | return BAD_VALUE; |
| 572 | } |
| 573 | |
| 574 | const size_t metadataSize = blobSize - alignment; |
| 575 | |
| 576 | // NOTE: this doesn't make sense to me. shouldn't the blob |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 577 | // know how big it is? why do we have to specify the size |
| 578 | // to Parcel::readBlob ? |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 579 | ReadableBlob blob; |
| 580 | // arg1 = metadata (blob) |
| 581 | do { |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 582 | if ((err = data.readBlob(blobSize, &blob)) != OK) { |
| 583 | ALOGE("%s: Failed to read metadata blob (sized %zu). Possible " |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 584 | " serialization bug. Error %d %s", |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 585 | __FUNCTION__, blobSize, err, strerror(-err)); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 586 | break; |
| 587 | } |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 588 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 589 | // arg2 = offset (blob) |
| 590 | // Must be after blob since we don't know offset until after writeBlob. |
| 591 | int32_t offsetTmp; |
| 592 | if ((err = data.readInt32(&offsetTmp)) != OK) { |
| 593 | ALOGE("%s: Failed to read metadata offsetTmp (error %d %s)", |
| 594 | __FUNCTION__, err, strerror(-err)); |
| 595 | break; |
| 596 | } |
| 597 | const size_t offset = static_cast<size_t>(offsetTmp); |
| 598 | if (offset >= alignment) { |
| 599 | ALOGE("%s: metadata offset(%zu) should be less than alignment(%zu)", |
| 600 | __FUNCTION__, blobSize, alignment); |
| 601 | err = BAD_VALUE; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | const uintptr_t metadataStart = reinterpret_cast<uintptr_t>(blob.data()) + offset; |
| 606 | const camera_metadata_t* tmp = |
| 607 | reinterpret_cast<const camera_metadata_t*>(metadataStart); |
| 608 | ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu", |
| 609 | __FUNCTION__, alignment, tmp, offset); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 610 | metadata = allocate_copy_camera_metadata_checked(tmp, metadataSize); |
| 611 | if (metadata == NULL) { |
| 612 | // We consider that allocation only fails if the validation |
| 613 | // also failed, therefore the readFromParcel was a failure. |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 614 | ALOGE("%s: metadata allocation and copy failed", __FUNCTION__); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 615 | err = BAD_VALUE; |
| 616 | } |
| 617 | } while(0); |
| 618 | blob.release(); |
| 619 | |
| 620 | if (out) { |
| 621 | ALOGV("%s: Set out metadata to %p", __FUNCTION__, metadata); |
| 622 | *out = metadata; |
| 623 | } else if (metadata != NULL) { |
| 624 | ALOGV("%s: Freed camera metadata at %p", __FUNCTION__, metadata); |
| 625 | free_camera_metadata(metadata); |
| 626 | } |
| 627 | |
| 628 | return err; |
| 629 | } |
| 630 | |
| 631 | status_t CameraMetadata::writeToParcel(Parcel& data, |
| 632 | const camera_metadata_t* metadata) { |
| 633 | status_t res = OK; |
| 634 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 635 | /** |
| 636 | * Below is the camera metadata parcel layout: |
| 637 | * |
| 638 | * |--------------------------------------------| |
| 639 | * | arg0: blobSize | |
| 640 | * | (length = 4) | |
| 641 | * |--------------------------------------------|<--Skip the rest if blobSize == 0. |
| 642 | * | | |
| 643 | * | | |
| 644 | * | arg1: blob | |
| 645 | * | (length = variable, see arg1 layout below) | |
| 646 | * | | |
| 647 | * | | |
| 648 | * |--------------------------------------------| |
| 649 | * | arg2: offset | |
| 650 | * | (length = 4) | |
| 651 | * |--------------------------------------------| |
| 652 | */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 653 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 654 | // arg0 = blobSize (int32) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 655 | if (metadata == NULL) { |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 656 | // Write zero blobSize for null metadata. |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 657 | return data.writeInt32(0); |
| 658 | } |
| 659 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 660 | /** |
| 661 | * Always make the blob size sufficiently larger, as we need put alignment |
| 662 | * padding and metadata into the blob. Since we don't know the alignment |
| 663 | * offset before writeBlob. Then write the metadata to aligned offset. |
| 664 | */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 665 | const size_t metadataSize = get_camera_metadata_compact_size(metadata); |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 666 | const size_t alignment = get_camera_metadata_alignment(); |
| 667 | const size_t blobSize = metadataSize + alignment; |
| 668 | res = data.writeInt32(static_cast<int32_t>(blobSize)); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 669 | if (res != OK) { |
| 670 | return res; |
| 671 | } |
| 672 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 673 | size_t offset = 0; |
| 674 | /** |
| 675 | * arg1 = metadata (blob). |
| 676 | * |
| 677 | * The blob size is the sum of front padding size, metadata size and back padding |
| 678 | * size, which is equal to metadataSize + alignment. |
| 679 | * |
| 680 | * The blob layout is: |
| 681 | * |------------------------------------|<----Start address of the blob (unaligned). |
| 682 | * | front padding | |
| 683 | * | (size = offset) | |
| 684 | * |------------------------------------|<----Aligned start address of metadata. |
| 685 | * | | |
| 686 | * | | |
| 687 | * | metadata | |
| 688 | * | (size = metadataSize) | |
| 689 | * | | |
| 690 | * | | |
| 691 | * |------------------------------------| |
| 692 | * | back padding | |
| 693 | * | (size = alignment - offset) | |
| 694 | * |------------------------------------|<----End address of blob. |
| 695 | * (Blob start address + blob size). |
| 696 | */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 697 | WritableBlob blob; |
| 698 | do { |
Jeff Brown | e8df539 | 2015-06-05 15:10:44 -0700 | [diff] [blame] | 699 | res = data.writeBlob(blobSize, false, &blob); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 700 | if (res != OK) { |
| 701 | break; |
| 702 | } |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 703 | const uintptr_t metadataStart = ALIGN_TO(blob.data(), alignment); |
| 704 | offset = metadataStart - reinterpret_cast<uintptr_t>(blob.data()); |
| 705 | ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu", |
Mark Salyzyn | 1a93f0c | 2014-06-09 16:34:58 -0700 | [diff] [blame] | 706 | __FUNCTION__, alignment, |
| 707 | reinterpret_cast<const void *>(metadataStart), offset); |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 708 | copy_camera_metadata(reinterpret_cast<void*>(metadataStart), metadataSize, metadata); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 709 | |
| 710 | // Not too big of a problem since receiving side does hard validation |
| 711 | // Don't check the size since the compact size could be larger |
| 712 | if (validate_camera_metadata_structure(metadata, /*size*/NULL) != OK) { |
| 713 | ALOGW("%s: Failed to validate metadata %p before writing blob", |
| 714 | __FUNCTION__, metadata); |
| 715 | } |
| 716 | |
| 717 | } while(false); |
| 718 | blob.release(); |
| 719 | |
Zhijun He | 146aed1 | 2013-12-05 07:46:51 -0800 | [diff] [blame] | 720 | // arg2 = offset (int32) |
| 721 | res = data.writeInt32(static_cast<int32_t>(offset)); |
| 722 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 723 | return res; |
| 724 | } |
| 725 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 726 | status_t CameraMetadata::readFromParcel(const Parcel *parcel) { |
Jayant Chowdhary | 2e310da | 2020-05-12 12:40:36 -0700 | [diff] [blame] | 727 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 728 | ALOGV("%s: parcel = %p", __FUNCTION__, parcel); |
| 729 | |
| 730 | status_t res = OK; |
| 731 | |
| 732 | if (parcel == NULL) { |
| 733 | ALOGE("%s: parcel is null", __FUNCTION__); |
| 734 | return BAD_VALUE; |
| 735 | } |
| 736 | |
| 737 | if (mLocked) { |
| 738 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 739 | return INVALID_OPERATION; |
| 740 | } |
| 741 | |
| 742 | camera_metadata *buffer = NULL; |
| 743 | // TODO: reading should return a status code, in case validation fails |
| 744 | res = CameraMetadata::readFromParcel(*parcel, &buffer); |
| 745 | |
| 746 | if (res != NO_ERROR) { |
| 747 | ALOGE("%s: Failed to read from parcel. Metadata is unchanged.", |
| 748 | __FUNCTION__); |
| 749 | return res; |
| 750 | } |
| 751 | |
| 752 | clear(); |
| 753 | mBuffer = buffer; |
| 754 | |
| 755 | return OK; |
| 756 | } |
| 757 | |
| 758 | status_t CameraMetadata::writeToParcel(Parcel *parcel) const { |
Jayant Chowdhary | 2e310da | 2020-05-12 12:40:36 -0700 | [diff] [blame] | 759 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 760 | ALOGV("%s: parcel = %p", __FUNCTION__, parcel); |
| 761 | |
| 762 | if (parcel == NULL) { |
| 763 | ALOGE("%s: parcel is null", __FUNCTION__); |
| 764 | return BAD_VALUE; |
| 765 | } |
| 766 | |
| 767 | return CameraMetadata::writeToParcel(*parcel, mBuffer); |
| 768 | } |
| 769 | |
| 770 | void CameraMetadata::swap(CameraMetadata& other) { |
| 771 | if (mLocked) { |
| 772 | ALOGE("%s: CameraMetadata is locked", __FUNCTION__); |
| 773 | return; |
| 774 | } else if (other.mLocked) { |
| 775 | ALOGE("%s: Other CameraMetadata is locked", __FUNCTION__); |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | camera_metadata* thisBuf = mBuffer; |
| 780 | camera_metadata* otherBuf = other.mBuffer; |
| 781 | |
| 782 | other.mBuffer = thisBuf; |
| 783 | mBuffer = otherBuf; |
| 784 | } |
| 785 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 786 | status_t CameraMetadata::getTagFromName(const char *name, |
| 787 | const VendorTagDescriptor* vTags, uint32_t *tag) { |
Jayant Chowdhary | 2e310da | 2020-05-12 12:40:36 -0700 | [diff] [blame] | 788 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 789 | if (name == nullptr || tag == nullptr) return BAD_VALUE; |
| 790 | |
| 791 | size_t nameLength = strlen(name); |
| 792 | |
| 793 | const SortedVector<String8> *vendorSections; |
| 794 | size_t vendorSectionCount = 0; |
| 795 | |
| 796 | if (vTags != NULL) { |
| 797 | vendorSections = vTags->getAllSectionNames(); |
| 798 | vendorSectionCount = vendorSections->size(); |
| 799 | } |
| 800 | |
| 801 | // First, find the section by the longest string match |
| 802 | const char *section = NULL; |
| 803 | size_t sectionIndex = 0; |
| 804 | size_t sectionLength = 0; |
| 805 | size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount; |
| 806 | for (size_t i = 0; i < totalSectionCount; ++i) { |
| 807 | |
| 808 | const char *str = (i < ANDROID_SECTION_COUNT) ? camera_metadata_section_names[i] : |
| 809 | (*vendorSections)[i - ANDROID_SECTION_COUNT].string(); |
| 810 | |
| 811 | ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str); |
| 812 | |
| 813 | if (strstr(name, str) == name) { // name begins with the section name |
| 814 | size_t strLength = strlen(str); |
| 815 | |
| 816 | ALOGV("%s: Name begins with section name", __FUNCTION__); |
| 817 | |
| 818 | // section name is the longest we've found so far |
| 819 | if (section == NULL || sectionLength < strLength) { |
| 820 | section = str; |
| 821 | sectionIndex = i; |
| 822 | sectionLength = strLength; |
| 823 | |
| 824 | ALOGV("%s: Found new best section (%s)", __FUNCTION__, section); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // TODO: Make above get_camera_metadata_section_from_name ? |
| 830 | |
| 831 | if (section == NULL) { |
| 832 | return NAME_NOT_FOUND; |
| 833 | } else { |
| 834 | ALOGV("%s: Found matched section '%s' (%zu)", |
| 835 | __FUNCTION__, section, sectionIndex); |
| 836 | } |
| 837 | |
| 838 | // Get the tag name component of the name |
| 839 | const char *nameTagName = name + sectionLength + 1; // x.y.z -> z |
| 840 | if (sectionLength + 1 >= nameLength) { |
| 841 | return BAD_VALUE; |
| 842 | } |
| 843 | |
| 844 | // Match rest of name against the tag names in that section only |
| 845 | uint32_t candidateTag = 0; |
| 846 | if (sectionIndex < ANDROID_SECTION_COUNT) { |
| 847 | // Match built-in tags (typically android.*) |
| 848 | uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd) |
| 849 | tagBegin = camera_metadata_section_bounds[sectionIndex][0]; |
| 850 | tagEnd = camera_metadata_section_bounds[sectionIndex][1]; |
| 851 | |
| 852 | for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) { |
| 853 | const char *tagName = get_camera_metadata_tag_name(candidateTag); |
| 854 | |
| 855 | if (strcmp(nameTagName, tagName) == 0) { |
| 856 | ALOGV("%s: Found matched tag '%s' (%d)", |
| 857 | __FUNCTION__, tagName, candidateTag); |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | if (candidateTag == tagEnd) { |
| 863 | return NAME_NOT_FOUND; |
| 864 | } |
| 865 | } else if (vTags != NULL) { |
| 866 | // Match vendor tags (typically com.*) |
| 867 | const String8 sectionName(section); |
| 868 | const String8 tagName(nameTagName); |
| 869 | |
| 870 | status_t res = OK; |
| 871 | if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) { |
| 872 | return NAME_NOT_FOUND; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | *tag = candidateTag; |
| 877 | return OK; |
| 878 | } |
| 879 | |
| 880 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 881 | }; // namespace android |