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"
20 #include <log/log.h>
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 {
30 namespace helper {
31
32 #define ALIGN_TO(val, alignment) (((uintptr_t)(val) + ((alignment)-1)) & ~((alignment)-1))
33
CameraMetadata()34 CameraMetadata::CameraMetadata() : mBuffer(NULL), mLocked(false) {}
35
CameraMetadata(size_t entryCapacity,size_t dataCapacity)36 CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) : mLocked(false) {
37 mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity);
38 }
39
CameraMetadata(const CameraMetadata & other)40 CameraMetadata::CameraMetadata(const CameraMetadata& other) : mLocked(false) {
41 mBuffer = clone_camera_metadata(other.mBuffer);
42 }
43
CameraMetadata(camera_metadata_t * buffer)44 CameraMetadata::CameraMetadata(camera_metadata_t* buffer) : mBuffer(NULL), mLocked(false) {
45 acquire(buffer);
46 }
47
operator =(const CameraMetadata & other)48 CameraMetadata& CameraMetadata::operator=(const CameraMetadata& other) {
49 return operator=(other.mBuffer);
50 }
51
operator =(const camera_metadata_t * buffer)52 CameraMetadata& CameraMetadata::operator=(const camera_metadata_t* buffer) {
53 if (mLocked) {
54 ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__);
55 return *this;
56 }
57
58 if (CC_LIKELY(buffer != mBuffer)) {
59 camera_metadata_t* newBuffer = clone_camera_metadata(buffer);
60 clear();
61 mBuffer = newBuffer;
62 }
63 return *this;
64 }
65
~CameraMetadata()66 CameraMetadata::~CameraMetadata() {
67 mLocked = false;
68 clear();
69 }
70
getAndLock() const71 const camera_metadata_t* CameraMetadata::getAndLock() const {
72 mLocked = true;
73 return mBuffer;
74 }
75
unlock(const camera_metadata_t * buffer) const76 status_t CameraMetadata::unlock(const camera_metadata_t* buffer) const {
77 if (!mLocked) {
78 ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__);
79 return INVALID_OPERATION;
80 }
81 if (buffer != mBuffer) {
82 ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!", __FUNCTION__);
83 return BAD_VALUE;
84 }
85 mLocked = false;
86 return OK;
87 }
88
release()89 camera_metadata_t* CameraMetadata::release() {
90 if (mLocked) {
91 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
92 return NULL;
93 }
94 camera_metadata_t* released = mBuffer;
95 mBuffer = NULL;
96 return released;
97 }
98
clear()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
acquire(camera_metadata_t * buffer)110 void CameraMetadata::acquire(camera_metadata_t* buffer) {
111 if (mLocked) {
112 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
113 return;
114 }
115 clear();
116 mBuffer = buffer;
117
118 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK,
119 "%s: Failed to validate metadata structure %p", __FUNCTION__, buffer);
120 }
121
acquire(CameraMetadata & other)122 void CameraMetadata::acquire(CameraMetadata& other) {
123 if (mLocked) {
124 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
125 return;
126 }
127 acquire(other.release());
128 }
129
append(const CameraMetadata & other)130 status_t CameraMetadata::append(const CameraMetadata& other) {
131 return append(other.mBuffer);
132 }
133
append(const camera_metadata_t * other)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
entryCount() const146 size_t CameraMetadata::entryCount() const {
147 return (mBuffer == NULL) ? 0 : get_camera_metadata_entry_count(mBuffer);
148 }
149
isEmpty() const150 bool CameraMetadata::isEmpty() const {
151 return entryCount() == 0;
152 }
153
sort()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
checkType(uint32_t tag,uint8_t expectedType)162 status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) {
163 int tagType = get_local_camera_metadata_tag_type(tag, mBuffer);
164 if (CC_UNLIKELY(tagType == -1)) {
165 ALOGE("Update metadata entry: Unknown tag %d", tag);
166 return INVALID_OPERATION;
167 }
168 if (CC_UNLIKELY(tagType != expectedType)) {
169 ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; "
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]);
173 return INVALID_OPERATION;
174 }
175 return OK;
176 }
177
update(uint32_t tag,const int32_t * data,size_t data_count)178 status_t CameraMetadata::update(uint32_t tag, const int32_t* data, size_t data_count) {
179 status_t res;
180 if (mLocked) {
181 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
182 return INVALID_OPERATION;
183 }
184 if ((res = checkType(tag, TYPE_INT32)) != OK) {
185 return res;
186 }
187 return updateImpl(tag, (const void*)data, data_count);
188 }
189
update(uint32_t tag,const uint8_t * data,size_t data_count)190 status_t CameraMetadata::update(uint32_t tag, const uint8_t* data, size_t data_count) {
191 status_t res;
192 if (mLocked) {
193 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
194 return INVALID_OPERATION;
195 }
196 if ((res = checkType(tag, TYPE_BYTE)) != OK) {
197 return res;
198 }
199 return updateImpl(tag, (const void*)data, data_count);
200 }
201
update(uint32_t tag,const float * data,size_t data_count)202 status_t CameraMetadata::update(uint32_t tag, const float* data, size_t data_count) {
203 status_t res;
204 if (mLocked) {
205 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
206 return INVALID_OPERATION;
207 }
208 if ((res = checkType(tag, TYPE_FLOAT)) != OK) {
209 return res;
210 }
211 return updateImpl(tag, (const void*)data, data_count);
212 }
213
update(uint32_t tag,const int64_t * data,size_t data_count)214 status_t CameraMetadata::update(uint32_t tag, const int64_t* data, size_t data_count) {
215 status_t res;
216 if (mLocked) {
217 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
218 return INVALID_OPERATION;
219 }
220 if ((res = checkType(tag, TYPE_INT64)) != OK) {
221 return res;
222 }
223 return updateImpl(tag, (const void*)data, data_count);
224 }
225
update(uint32_t tag,const double * data,size_t data_count)226 status_t CameraMetadata::update(uint32_t tag, const double* data, size_t data_count) {
227 status_t res;
228 if (mLocked) {
229 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
230 return INVALID_OPERATION;
231 }
232 if ((res = checkType(tag, TYPE_DOUBLE)) != OK) {
233 return res;
234 }
235 return updateImpl(tag, (const void*)data, data_count);
236 }
237
update(uint32_t tag,const camera_metadata_rational_t * data,size_t data_count)238 status_t CameraMetadata::update(uint32_t tag, const camera_metadata_rational_t* data,
239 size_t data_count) {
240 status_t res;
241 if (mLocked) {
242 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
243 return INVALID_OPERATION;
244 }
245 if ((res = checkType(tag, TYPE_RATIONAL)) != OK) {
246 return res;
247 }
248 return updateImpl(tag, (const void*)data, data_count);
249 }
250
update(uint32_t tag,const String8 & string)251 status_t CameraMetadata::update(uint32_t tag, const String8& string) {
252 status_t res;
253 if (mLocked) {
254 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
255 return INVALID_OPERATION;
256 }
257 if ((res = checkType(tag, TYPE_BYTE)) != OK) {
258 return res;
259 }
260 // string.size() doesn't count the null termination character.
261 return updateImpl(tag, (const void*)string.c_str(), string.size() + 1);
262 }
263
update(const camera_metadata_ro_entry & entry)264 status_t CameraMetadata::update(const camera_metadata_ro_entry& entry) {
265 status_t res;
266 if (mLocked) {
267 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
268 return INVALID_OPERATION;
269 }
270 if ((res = checkType(entry.tag, entry.type)) != OK) {
271 return res;
272 }
273 return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count);
274 }
275
updateImpl(uint32_t tag,const void * data,size_t data_count)276 status_t CameraMetadata::updateImpl(uint32_t tag, const void* data, size_t data_count) {
277 status_t res;
278 if (mLocked) {
279 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
280 return INVALID_OPERATION;
281 }
282 int type = get_local_camera_metadata_tag_type(tag, mBuffer);
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)) {
293 ALOGE("%s: Update attempted with data from the same metadata buffer!", __FUNCTION__);
294 return INVALID_OPERATION;
295 }
296
297 size_t data_size = calculate_camera_metadata_entry_data_size(type, data_count);
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) {
305 res = add_camera_metadata_entry(mBuffer, tag, data, data_count);
306 } else if (res == OK) {
307 res = update_camera_metadata_entry(mBuffer, entry.index, data, data_count, NULL);
308 }
309 }
310
311 if (res != OK) {
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);
315 }
316
317 IF_ALOGV() {
318 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/ NULL) != OK,
319
320 "%s: Failed to validate metadata structure after update %p", __FUNCTION__,
321 mBuffer);
322 }
323
324 return res;
325 }
326
exists(uint32_t tag) const327 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
find(uint32_t tag)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);
341 if (CC_UNLIKELY(res != OK)) {
342 entry.count = 0;
343 entry.data.u8 = NULL;
344 }
345 return entry;
346 }
347
find(uint32_t tag) const348 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);
352 if (CC_UNLIKELY(res != OK)) {
353 entry.count = 0;
354 entry.data.u8 = NULL;
355 }
356 return entry;
357 }
358
erase(uint32_t tag)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) {
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);
373 return res;
374 }
375 res = delete_camera_metadata_entry(mBuffer, entry.index);
376 if (res != OK) {
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);
380 }
381 return res;
382 }
383
dump(int fd,int verbosity,int indentation) const384 void CameraMetadata::dump(int fd, int verbosity, int indentation) const {
385 dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation);
386 }
387
resizeIfNeeded(size_t extraEntries,size_t extraData)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);
398 size_t newEntryCount = currentEntryCount + extraEntries;
399 newEntryCount = (newEntryCount > currentEntryCap) ? newEntryCount * 2 : currentEntryCap;
400
401 size_t currentDataCount = get_camera_metadata_data_count(mBuffer);
402 size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer);
403 size_t newDataCount = currentDataCount + extraData;
404 newDataCount = (newDataCount > currentDataCap) ? newDataCount * 2 : currentDataCap;
405
406 if (newEntryCount > currentEntryCap || newDataCount > currentDataCap) {
407 camera_metadata_t* oldBuffer = mBuffer;
408 mBuffer = allocate_camera_metadata(newEntryCount, newDataCount);
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
swap(CameraMetadata & other)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
getTagFromName(const char * name,const VendorTagDescriptor * vTags,uint32_t * tag)436 status_t CameraMetadata::getTagFromName(const char* name, const VendorTagDescriptor* vTags,
437 uint32_t* tag) {
438 if (name == nullptr || tag == nullptr) return BAD_VALUE;
439
440 size_t nameLength = strlen(name);
441
442 const SortedVector<String8>* vendorSections;
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
451 const char* section = NULL;
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) {
456 const char* str = (i < ANDROID_SECTION_COUNT)
457 ? camera_metadata_section_names[i]
458 : (*vendorSections)[i - ANDROID_SECTION_COUNT].c_str();
459
460 ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
461
462 if (strstr(name, str) == name) { // name begins with the section name
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
478 if (section == NULL) {
479 return NAME_NOT_FOUND;
480 } else {
481 ALOGV("%s: Found matched section '%s' (%zu)", __FUNCTION__, section, sectionIndex);
482 }
483
484 // Get the tag name component of the name
485 const char* nameTagName = name + sectionLength + 1; // x.y.z -> z
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.*)
494 uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
495 tagBegin = camera_metadata_section_bounds[sectionIndex][0];
496 tagEnd = camera_metadata_section_bounds[sectionIndex][1];
497
498 for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) {
499 const char* tagName = get_camera_metadata_tag_name(candidateTag);
500
501 if (strcmp(nameTagName, tagName) == 0) {
502 ALOGV("%s: Found matched tag '%s' (%d)", __FUNCTION__, tagName, candidateTag);
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
525 } // namespace helper
526 } // namespace common
527 } // namespace camera
528 } // namespace hardware
529 } // namespace android
530