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