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 | |
| 17 | #define LOG_TAG "CameraMetadata" |
| 18 | #include <utils/Log.h> |
| 19 | #include <utils/Errors.h> |
| 20 | |
| 21 | #include "CameraMetadata.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 25 | namespace camera2 { |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 26 | CameraMetadata::CameraMetadata() : |
| 27 | mBuffer(NULL) { |
| 28 | } |
| 29 | |
| 30 | CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) |
| 31 | { |
| 32 | mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity); |
| 33 | } |
| 34 | |
| 35 | CameraMetadata::CameraMetadata(const CameraMetadata &other) { |
| 36 | mBuffer = clone_camera_metadata(other.mBuffer); |
| 37 | } |
| 38 | |
| 39 | CameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) { |
| 40 | return operator=(other.mBuffer); |
| 41 | } |
| 42 | |
| 43 | CameraMetadata &CameraMetadata::operator=(const camera_metadata_t *buffer) { |
| 44 | if (CC_LIKELY(buffer != mBuffer)) { |
| 45 | camera_metadata_t *newBuffer = clone_camera_metadata(buffer); |
| 46 | clear(); |
| 47 | mBuffer = newBuffer; |
| 48 | } |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | CameraMetadata::~CameraMetadata() { |
| 53 | clear(); |
| 54 | } |
| 55 | |
| 56 | camera_metadata_t* CameraMetadata::release() { |
| 57 | camera_metadata_t *released = mBuffer; |
| 58 | mBuffer = NULL; |
| 59 | return released; |
| 60 | } |
| 61 | |
| 62 | void CameraMetadata::clear() { |
| 63 | if (mBuffer) { |
| 64 | free_camera_metadata(mBuffer); |
| 65 | mBuffer = NULL; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void CameraMetadata::acquire(camera_metadata_t *buffer) { |
| 70 | clear(); |
| 71 | mBuffer = buffer; |
| 72 | } |
| 73 | |
| 74 | void CameraMetadata::acquire(CameraMetadata &other) { |
| 75 | acquire(other.release()); |
| 76 | } |
| 77 | |
| 78 | status_t CameraMetadata::append(const CameraMetadata &other) { |
| 79 | return append_camera_metadata(mBuffer, other.mBuffer); |
| 80 | } |
| 81 | |
| 82 | size_t CameraMetadata::entryCount() const { |
| 83 | return (mBuffer == NULL) ? 0 : |
| 84 | get_camera_metadata_entry_count(mBuffer); |
| 85 | } |
| 86 | |
| 87 | status_t CameraMetadata::sort() { |
| 88 | return sort_camera_metadata(mBuffer); |
| 89 | } |
| 90 | |
| 91 | status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) { |
| 92 | int tagType = get_camera_metadata_tag_type(tag); |
| 93 | if ( CC_UNLIKELY(tagType == -1)) { |
| 94 | ALOGE("Update metadata entry: Unknown tag %d", tag); |
| 95 | return INVALID_OPERATION; |
| 96 | } |
| 97 | if ( CC_UNLIKELY(tagType != expectedType) ) { |
| 98 | ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; " |
| 99 | "got type %s data instead ", |
| 100 | get_camera_metadata_tag_name(tag), tag, |
| 101 | camera_metadata_type_names[tagType], |
| 102 | camera_metadata_type_names[expectedType]); |
| 103 | return INVALID_OPERATION; |
| 104 | } |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | status_t CameraMetadata::update(uint32_t tag, |
| 109 | const int32_t *data, size_t data_count) { |
| 110 | status_t res; |
| 111 | if ( (res = checkType(tag, TYPE_INT32)) != OK) { |
| 112 | return res; |
| 113 | } |
| 114 | return update(tag, (const void*)data, data_count); |
| 115 | } |
| 116 | |
| 117 | status_t CameraMetadata::update(uint32_t tag, |
| 118 | const uint8_t *data, size_t data_count) { |
| 119 | status_t res; |
| 120 | if ( (res = checkType(tag, TYPE_BYTE)) != OK) { |
| 121 | return res; |
| 122 | } |
| 123 | return update(tag, (const void*)data, data_count); |
| 124 | } |
| 125 | |
| 126 | status_t CameraMetadata::update(uint32_t tag, |
| 127 | const float *data, size_t data_count) { |
| 128 | status_t res; |
| 129 | if ( (res = checkType(tag, TYPE_FLOAT)) != OK) { |
| 130 | return res; |
| 131 | } |
| 132 | return update(tag, (const void*)data, data_count); |
| 133 | } |
| 134 | |
| 135 | status_t CameraMetadata::update(uint32_t tag, |
| 136 | const int64_t *data, size_t data_count) { |
| 137 | status_t res; |
| 138 | if ( (res = checkType(tag, TYPE_INT64)) != OK) { |
| 139 | return res; |
| 140 | } |
| 141 | return update(tag, (const void*)data, data_count); |
| 142 | } |
| 143 | |
| 144 | status_t CameraMetadata::update(uint32_t tag, |
| 145 | const double *data, size_t data_count) { |
| 146 | status_t res; |
| 147 | if ( (res = checkType(tag, TYPE_DOUBLE)) != OK) { |
| 148 | return res; |
| 149 | } |
| 150 | return update(tag, (const void*)data, data_count); |
| 151 | } |
| 152 | |
| 153 | status_t CameraMetadata::update(uint32_t tag, |
| 154 | const camera_metadata_rational_t *data, size_t data_count) { |
| 155 | status_t res; |
| 156 | if ( (res = checkType(tag, TYPE_RATIONAL)) != OK) { |
| 157 | return res; |
| 158 | } |
| 159 | return update(tag, (const void*)data, data_count); |
| 160 | } |
| 161 | |
| 162 | status_t CameraMetadata::update(uint32_t tag, |
| 163 | const String8 &string) { |
| 164 | status_t res; |
| 165 | if ( (res = checkType(tag, TYPE_BYTE)) != OK) { |
| 166 | return res; |
| 167 | } |
| 168 | return update(tag, (const void*)string.string(), string.size()); |
| 169 | } |
| 170 | |
| 171 | status_t CameraMetadata::update(uint32_t tag, const void *data, |
| 172 | size_t data_count) { |
| 173 | status_t res; |
| 174 | int type = get_camera_metadata_tag_type(tag); |
| 175 | if (type == -1) { |
| 176 | ALOGE("%s: Tag %d not found", __FUNCTION__, tag); |
| 177 | return BAD_VALUE; |
| 178 | } |
| 179 | size_t data_size = calculate_camera_metadata_entry_data_size(type, |
| 180 | data_count); |
| 181 | |
| 182 | res = resizeIfNeeded(1, data_size); |
| 183 | |
| 184 | if (res == OK) { |
| 185 | camera_metadata_entry_t entry; |
| 186 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 187 | if (res == NAME_NOT_FOUND) { |
| 188 | res = add_camera_metadata_entry(mBuffer, |
| 189 | tag, data, data_count); |
| 190 | } else if (res == OK) { |
| 191 | res = update_camera_metadata_entry(mBuffer, |
| 192 | entry.index, data, data_count, NULL); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (res != OK) { |
| 197 | ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)", |
| 198 | __FUNCTION__, get_camera_metadata_section_name(tag), |
| 199 | get_camera_metadata_tag_name(tag), tag, strerror(-res), res); |
| 200 | } |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | camera_metadata_entry_t CameraMetadata::find(uint32_t tag) { |
| 205 | status_t res; |
| 206 | camera_metadata_entry entry; |
| 207 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 208 | if (CC_UNLIKELY( res != OK )) { |
| 209 | entry.count = 0; |
| 210 | entry.data.u8 = NULL; |
| 211 | } |
| 212 | return entry; |
| 213 | } |
| 214 | |
| 215 | camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const { |
| 216 | status_t res; |
| 217 | camera_metadata_ro_entry entry; |
| 218 | res = find_camera_metadata_ro_entry(mBuffer, tag, &entry); |
| 219 | if (CC_UNLIKELY( res != OK )) { |
| 220 | entry.count = 0; |
| 221 | entry.data.u8 = NULL; |
| 222 | } |
| 223 | return entry; |
| 224 | } |
| 225 | |
| 226 | status_t CameraMetadata::erase(uint32_t tag) { |
| 227 | camera_metadata_entry_t entry; |
| 228 | status_t res; |
| 229 | res = find_camera_metadata_entry(mBuffer, tag, &entry); |
| 230 | if (res == NAME_NOT_FOUND) { |
| 231 | return OK; |
| 232 | } else if (res != OK) { |
| 233 | ALOGE("%s: Error looking for entry %s.%s (%x): %s %d", |
| 234 | __FUNCTION__, |
| 235 | get_camera_metadata_section_name(tag), |
| 236 | get_camera_metadata_tag_name(tag), tag, strerror(-res), res); |
| 237 | return res; |
| 238 | } |
| 239 | res = delete_camera_metadata_entry(mBuffer, entry.index); |
| 240 | if (res != OK) { |
| 241 | ALOGE("%s: Error deleting entry %s.%s (%x): %s %d", |
| 242 | __FUNCTION__, |
| 243 | get_camera_metadata_section_name(tag), |
| 244 | get_camera_metadata_tag_name(tag), tag, strerror(-res), res); |
| 245 | } |
| 246 | return res; |
| 247 | } |
| 248 | |
| 249 | void CameraMetadata::dump(int fd, int verbosity, int indentation) const { |
| 250 | dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation); |
| 251 | } |
| 252 | |
| 253 | status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) { |
| 254 | if (mBuffer == NULL) { |
| 255 | mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2); |
| 256 | if (mBuffer == NULL) { |
| 257 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 258 | return NO_MEMORY; |
| 259 | } |
| 260 | } else { |
| 261 | size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer); |
| 262 | size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer); |
| 263 | size_t newEntryCount = currentEntryCount + |
| 264 | extraEntries; |
| 265 | newEntryCount = (newEntryCount > currentEntryCap) ? |
| 266 | newEntryCount * 2 : currentEntryCap; |
| 267 | |
| 268 | size_t currentDataCount = get_camera_metadata_data_count(mBuffer); |
| 269 | size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer); |
| 270 | size_t newDataCount = currentDataCount + |
| 271 | extraData; |
| 272 | newDataCount = (newDataCount > currentDataCap) ? |
| 273 | newDataCount * 2 : currentDataCap; |
| 274 | |
| 275 | if (newEntryCount > currentEntryCap || |
| 276 | newDataCount > currentDataCap) { |
| 277 | camera_metadata_t *oldBuffer = mBuffer; |
| 278 | mBuffer = allocate_camera_metadata(newEntryCount, |
| 279 | newDataCount); |
| 280 | if (mBuffer == NULL) { |
| 281 | ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__); |
| 282 | return NO_MEMORY; |
| 283 | } |
| 284 | append_camera_metadata(mBuffer, oldBuffer); |
| 285 | free_camera_metadata(oldBuffer); |
| 286 | } |
| 287 | } |
| 288 | return OK; |
| 289 | } |
| 290 | |
Eino-Ville Talvala | 2e19c3c | 2012-08-26 09:29:28 -0700 | [diff] [blame] | 291 | }; // namespace camera2 |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 292 | }; // namespace android |