1 /*
2 * Copyright (C) 2014 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 "VendorTagDescriptor"
18
19 #include <binder/Parcel.h>
20 #include <utils/Errors.h>
21 #include <utils/Log.h>
22 #include <utils/Mutex.h>
23 #include <utils/Vector.h>
24 #include <utils/SortedVector.h>
25 #include <system/camera_metadata.h>
26 #include <camera_metadata_hidden.h>
27
28 #include "camera/VendorTagDescriptor.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <inttypes.h>
33
34 namespace android {
35
36 extern "C" {
37
38 static int vendor_tag_descriptor_get_tag_count(const vendor_tag_ops_t* v);
39 static void vendor_tag_descriptor_get_all_tags(const vendor_tag_ops_t* v, uint32_t* tagArray);
40 static const char* vendor_tag_descriptor_get_section_name(const vendor_tag_ops_t* v, uint32_t tag);
41 static const char* vendor_tag_descriptor_get_tag_name(const vendor_tag_ops_t* v, uint32_t tag);
42 static int vendor_tag_descriptor_get_tag_type(const vendor_tag_ops_t* v, uint32_t tag);
43
44 static int vendor_tag_descriptor_cache_get_tag_count(metadata_vendor_id_t id);
45 static void vendor_tag_descriptor_cache_get_all_tags(uint32_t* tagArray,
46 metadata_vendor_id_t id);
47 static const char* vendor_tag_descriptor_cache_get_section_name(uint32_t tag,
48 metadata_vendor_id_t id);
49 static const char* vendor_tag_descriptor_cache_get_tag_name(uint32_t tag,
50 metadata_vendor_id_t id);
51 static int vendor_tag_descriptor_cache_get_tag_type(uint32_t tag,
52 metadata_vendor_id_t id);
53
54 } /* extern "C" */
55
56
57 static Mutex sLock;
58 static sp<VendorTagDescriptor> sGlobalVendorTagDescriptor;
59 static sp<VendorTagDescriptorCache> sGlobalVendorTagDescriptorCache;
60
61 namespace hardware {
62 namespace camera2 {
63 namespace params {
64
~VendorTagDescriptor()65 VendorTagDescriptor::~VendorTagDescriptor() {
66 size_t len = mReverseMapping.size();
67 for (size_t i = 0; i < len; ++i) {
68 delete mReverseMapping[i];
69 }
70 }
71
VendorTagDescriptor()72 VendorTagDescriptor::VendorTagDescriptor() :
73 mTagCount(0),
74 mVendorOps() {
75 }
76
VendorTagDescriptor(const VendorTagDescriptor & src)77 VendorTagDescriptor::VendorTagDescriptor(const VendorTagDescriptor& src) {
78 copyFrom(src);
79 }
80
operator =(const VendorTagDescriptor & rhs)81 VendorTagDescriptor& VendorTagDescriptor::operator=(const VendorTagDescriptor& rhs) {
82 copyFrom(rhs);
83 return *this;
84 }
85
copyFrom(const VendorTagDescriptor & src)86 void VendorTagDescriptor::copyFrom(const VendorTagDescriptor& src) {
87 if (this == &src) return;
88
89 size_t len = mReverseMapping.size();
90 for (size_t i = 0; i < len; ++i) {
91 delete mReverseMapping[i];
92 }
93 mReverseMapping.clear();
94
95 len = src.mReverseMapping.size();
96 // Have to copy KeyedVectors inside mReverseMapping
97 for (size_t i = 0; i < len; ++i) {
98 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
99 *nameMapper = *(src.mReverseMapping.valueAt(i));
100 mReverseMapping.add(src.mReverseMapping.keyAt(i), nameMapper);
101 }
102 // Everything else is simple
103 mTagToNameMap = src.mTagToNameMap;
104 mTagToSectionMap = src.mTagToSectionMap;
105 mTagToTypeMap = src.mTagToTypeMap;
106 mSections = src.mSections;
107 mTagCount = src.mTagCount;
108 mVendorOps = src.mVendorOps;
109 }
110
readFromParcel(const android::Parcel * parcel)111 status_t VendorTagDescriptor::readFromParcel(const android::Parcel* parcel) {
112 status_t res = OK;
113 if (parcel == NULL) {
114 ALOGE("%s: parcel argument was NULL.", __FUNCTION__);
115 return BAD_VALUE;
116 }
117
118 int32_t tagCount = 0;
119 if ((res = parcel->readInt32(&tagCount)) != OK) {
120 ALOGE("%s: could not read tag count from parcel", __FUNCTION__);
121 return res;
122 }
123
124 if (tagCount < 0 || tagCount > INT32_MAX) {
125 ALOGE("%s: tag count %d from vendor ops is invalid.", __FUNCTION__, tagCount);
126 return BAD_VALUE;
127 }
128
129 mTagCount = tagCount;
130
131 uint32_t tag, sectionIndex;
132 uint32_t maxSectionIndex = 0;
133 int32_t tagType;
134 Vector<uint32_t> allTags;
135 for (int32_t i = 0; i < tagCount; ++i) {
136 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(&tag))) != OK) {
137 ALOGE("%s: could not read tag id from parcel for index %d", __FUNCTION__, i);
138 break;
139 }
140 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) {
141 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag);
142 res = BAD_VALUE;
143 break;
144 }
145 if ((res = parcel->readInt32(&tagType)) != OK) {
146 ALOGE("%s: could not read tag type from parcel for tag %d", __FUNCTION__, tag);
147 break;
148 }
149 if (tagType < 0 || tagType >= NUM_TYPES) {
150 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType);
151 res = BAD_VALUE;
152 break;
153 }
154 String8 tagName = parcel->readString8();
155 if (tagName.empty()) {
156 ALOGE("%s: parcel tag name was NULL for tag %d.", __FUNCTION__, tag);
157 res = NOT_ENOUGH_DATA;
158 break;
159 }
160
161 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(§ionIndex))) != OK) {
162 ALOGE("%s: could not read section index for tag %d.", __FUNCTION__, tag);
163 break;
164 }
165
166 maxSectionIndex = (maxSectionIndex >= sectionIndex) ? maxSectionIndex : sectionIndex;
167
168 allTags.add(tag);
169 mTagToNameMap.add(tag, tagName);
170 mTagToSectionMap.add(tag, sectionIndex);
171 mTagToTypeMap.add(tag, tagType);
172 }
173
174 if (res != OK) {
175 return res;
176 }
177
178 size_t sectionCount = 0;
179 if (tagCount > 0) {
180 if ((res = parcel->readInt32(reinterpret_cast<int32_t*>(§ionCount))) != OK) {
181 ALOGE("%s: could not read section count for.", __FUNCTION__);
182 return res;
183 }
184 if (sectionCount < (maxSectionIndex + 1)) {
185 ALOGE("%s: Incorrect number of sections defined, received %zu, needs %d.",
186 __FUNCTION__, sectionCount, (maxSectionIndex + 1));
187 return BAD_VALUE;
188 }
189 LOG_ALWAYS_FATAL_IF(mSections.setCapacity(sectionCount) <= 0,
190 "Vector capacity must be positive");
191 for (size_t i = 0; i < sectionCount; ++i) {
192 String8 sectionName = parcel->readString8();
193 if (sectionName.empty()) {
194 ALOGE("%s: parcel section name was NULL for section %zu.",
195 __FUNCTION__, i);
196 return NOT_ENOUGH_DATA;
197 }
198 mSections.add(sectionName);
199 }
200 }
201
202 LOG_ALWAYS_FATAL_IF(static_cast<size_t>(tagCount) != allTags.size(),
203 "tagCount must be the same as allTags size");
204 // Set up reverse mapping
205 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) {
206 uint32_t tag = allTags[i];
207 const String8& sectionString = mSections[mTagToSectionMap.valueFor(tag)];
208
209 ssize_t reverseIndex = -1;
210 if ((reverseIndex = mReverseMapping.indexOfKey(sectionString)) < 0) {
211 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
212 reverseIndex = mReverseMapping.add(sectionString, nameMapper);
213 }
214 mReverseMapping[reverseIndex]->add(mTagToNameMap.valueFor(tag), tag);
215 }
216
217 return res;
218 }
219
getTagCount() const220 int VendorTagDescriptor::getTagCount() const {
221 size_t size = mTagToNameMap.size();
222 if (size == 0) {
223 return VENDOR_TAG_COUNT_ERR;
224 }
225 return size;
226 }
227
getTagArray(uint32_t * tagArray) const228 void VendorTagDescriptor::getTagArray(uint32_t* tagArray) const {
229 size_t size = mTagToNameMap.size();
230 for (size_t i = 0; i < size; ++i) {
231 tagArray[i] = mTagToNameMap.keyAt(i);
232 }
233 }
234
getSectionName(uint32_t tag) const235 const char* VendorTagDescriptor::getSectionName(uint32_t tag) const {
236 ssize_t index = mTagToSectionMap.indexOfKey(tag);
237 if (index < 0) {
238 return VENDOR_SECTION_NAME_ERR;
239 }
240 return mSections[mTagToSectionMap.valueAt(index)].c_str();
241 }
242
getTagName(uint32_t tag) const243 const char* VendorTagDescriptor::getTagName(uint32_t tag) const {
244 ssize_t index = mTagToNameMap.indexOfKey(tag);
245 if (index < 0) {
246 return VENDOR_TAG_NAME_ERR;
247 }
248 return mTagToNameMap.valueAt(index).c_str();
249 }
250
getTagType(uint32_t tag) const251 int VendorTagDescriptor::getTagType(uint32_t tag) const {
252 ssize_t index = mTagToNameMap.indexOfKey(tag);
253 if (index < 0) {
254 return VENDOR_TAG_TYPE_ERR;
255 }
256 return mTagToTypeMap.valueFor(tag);
257 }
258
writeToParcel(android::Parcel * parcel) const259 status_t VendorTagDescriptor::writeToParcel(android::Parcel* parcel) const {
260 status_t res = OK;
261 if (parcel == NULL) {
262 ALOGE("%s: parcel argument was NULL.", __FUNCTION__);
263 return BAD_VALUE;
264 }
265
266 if ((res = parcel->writeInt32(mTagCount)) != OK) {
267 return res;
268 }
269
270 size_t size = mTagToNameMap.size();
271 uint32_t tag, sectionIndex;
272 int32_t tagType;
273 for (size_t i = 0; i < size; ++i) {
274 tag = mTagToNameMap.keyAt(i);
275 String8 tagName = mTagToNameMap[i];
276 sectionIndex = mTagToSectionMap.valueFor(tag);
277 tagType = mTagToTypeMap.valueFor(tag);
278 if ((res = parcel->writeInt32(tag)) != OK) break;
279 if ((res = parcel->writeInt32(tagType)) != OK) break;
280 if ((res = parcel->writeString8(tagName)) != OK) break;
281 if ((res = parcel->writeInt32(sectionIndex)) != OK) break;
282 }
283
284 size_t numSections = mSections.size();
285 if (numSections > 0) {
286 if ((res = parcel->writeInt32(numSections)) != OK) return res;
287 for (size_t i = 0; i < numSections; ++i) {
288 if ((res = parcel->writeString8(mSections[i])) != OK) return res;
289 }
290 }
291
292 return res;
293 }
294
getAllSectionNames() const295 const SortedVector<String8>* VendorTagDescriptor::getAllSectionNames() const {
296 return &mSections;
297 }
298
lookupTag(const String8 & name,const String8 & section,uint32_t * tag) const299 status_t VendorTagDescriptor::lookupTag(const String8& name, const String8& section, /*out*/uint32_t* tag) const {
300 ssize_t index = mReverseMapping.indexOfKey(section);
301 if (index < 0) {
302 ALOGE("%s: Section '%s' does not exist.", __FUNCTION__, section.c_str());
303 return BAD_VALUE;
304 }
305
306 ssize_t nameIndex = mReverseMapping[index]->indexOfKey(name);
307 if (nameIndex < 0) {
308 ALOGE("%s: Tag name '%s' does not exist.", __FUNCTION__, name.c_str());
309 return BAD_VALUE;
310 }
311
312 if (tag != NULL) {
313 *tag = mReverseMapping[index]->valueAt(nameIndex);
314 }
315 return OK;
316 }
317
getSectionIndex(uint32_t tag) const318 ssize_t VendorTagDescriptor::getSectionIndex(uint32_t tag) const {
319 return mTagToSectionMap.valueFor(tag);
320 }
321
dump(int fd,int verbosity,int indentation) const322 void VendorTagDescriptor::dump(int fd, int verbosity, int indentation) const {
323
324 size_t size = mTagToNameMap.size();
325 if (size == 0) {
326 dprintf(fd, "%*sDumping configured vendor tag descriptors: None set\n",
327 indentation, "");
328 return;
329 }
330
331 dprintf(fd, "%*sDumping configured vendor tag descriptors: %zu entries\n",
332 indentation, "", size);
333 for (size_t i = 0; i < size; ++i) {
334 uint32_t tag = mTagToNameMap.keyAt(i);
335
336 if (verbosity < 1) {
337 dprintf(fd, "%*s0x%x\n", indentation + 2, "", tag);
338 continue;
339 }
340 String8 name = mTagToNameMap.valueAt(i);
341 uint32_t sectionId = mTagToSectionMap.valueFor(tag);
342 String8 sectionName = mSections[sectionId];
343 int type = mTagToTypeMap.valueFor(tag);
344 const char* typeName = (type >= 0 && type < NUM_TYPES) ?
345 camera_metadata_type_names[type] : "UNKNOWN";
346 dprintf(fd, "%*s0x%x (%s) with type %d (%s) defined in section %s\n", indentation + 2,
347 "", tag, name.c_str(), type, typeName, sectionName.c_str());
348 }
349
350 }
351
writeToParcel(Parcel * parcel) const352 status_t VendorTagDescriptorCache::writeToParcel(Parcel* parcel) const {
353 status_t res = OK;
354 if (parcel == NULL) {
355 ALOGE("%s: parcel argument was NULL.", __FUNCTION__);
356 return BAD_VALUE;
357 }
358
359 if ((res = parcel->writeInt32(mVendorMap.size())) != OK) {
360 return res;
361 }
362
363 for (const auto &iter : mVendorMap) {
364 if ((res = parcel->writeUint64(iter.first)) != OK) break;
365 if ((res = parcel->writeParcelable(*iter.second)) != OK) break;
366 }
367
368 return res;
369 }
370
371
readFromParcel(const Parcel * parcel)372 status_t VendorTagDescriptorCache::readFromParcel(const Parcel* parcel) {
373 status_t res = OK;
374 if (parcel == NULL) {
375 ALOGE("%s: parcel argument was NULL.", __FUNCTION__);
376 return BAD_VALUE;
377 }
378
379 int32_t vendorCount = 0;
380 if ((res = parcel->readInt32(&vendorCount)) != OK) {
381 ALOGE("%s: could not read vendor count from parcel", __FUNCTION__);
382 return res;
383 }
384
385 if (vendorCount < 0 || vendorCount > INT32_MAX) {
386 ALOGE("%s: vendor count %d from is invalid.", __FUNCTION__, vendorCount);
387 return BAD_VALUE;
388 }
389
390 metadata_vendor_id_t id;
391 for (int32_t i = 0; i < vendorCount; i++) {
392 if ((res = parcel->readUint64(&id)) != OK) {
393 ALOGE("%s: could not read vendor id from parcel for index %d",
394 __FUNCTION__, i);
395 break;
396 }
397 sp<android::VendorTagDescriptor> desc = new android::VendorTagDescriptor();
398 if ((res = parcel->readParcelable(desc.get())) != OK) {
399 ALOGE("%s: could not read vendor tag descriptor from parcel for index %d rc = %d",
400 __FUNCTION__, i, res);
401 break;
402 }
403
404 if ((res = addVendorDescriptor(id, desc)) != OK) {
405 ALOGE("%s: failed to add vendor tag descriptor for index: %d ",
406 __FUNCTION__, i);
407 break;
408 }
409 }
410
411 return res;
412 }
413
414 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>> &
getVendorIdsAndTagDescriptors()415 VendorTagDescriptorCache::getVendorIdsAndTagDescriptors() {
416 return mVendorMap;
417 }
418
getTagCount(metadata_vendor_id_t id) const419 int VendorTagDescriptorCache::getTagCount(metadata_vendor_id_t id) const {
420 int ret = 0;
421 auto desc = mVendorMap.find(id);
422 if (desc != mVendorMap.end()) {
423 ret = desc->second->getTagCount();
424 } else {
425 ALOGE("%s: Vendor descriptor id is missing!", __func__);
426 }
427
428 return ret;
429 }
430
getTagArray(uint32_t * tagArray,metadata_vendor_id_t id) const431 void VendorTagDescriptorCache::getTagArray(uint32_t* tagArray,
432 metadata_vendor_id_t id) const {
433 auto desc = mVendorMap.find(id);
434 if (desc != mVendorMap.end()) {
435 desc->second->getTagArray(tagArray);
436 } else {
437 ALOGE("%s: Vendor descriptor id is missing!", __func__);
438 }
439 }
440
getSectionName(uint32_t tag,metadata_vendor_id_t id) const441 const char* VendorTagDescriptorCache::getSectionName(uint32_t tag,
442 metadata_vendor_id_t id) const {
443 const char *ret = nullptr;
444 auto desc = mVendorMap.find(id);
445 if (desc != mVendorMap.end()) {
446 ret = desc->second->getSectionName(tag);
447 } else {
448 ALOGE("%s: Vendor descriptor id is missing!", __func__);
449 }
450
451 return ret;
452 }
453
getTagName(uint32_t tag,metadata_vendor_id_t id) const454 const char* VendorTagDescriptorCache::getTagName(uint32_t tag,
455 metadata_vendor_id_t id) const {
456 const char *ret = nullptr;
457 auto desc = mVendorMap.find(id);
458 if (desc != mVendorMap.end()) {
459 ret = desc->second->getTagName(tag);
460 } else {
461 ALOGE("%s: Vendor descriptor id is missing!", __func__);
462 }
463
464 return ret;
465 }
466
getTagType(uint32_t tag,metadata_vendor_id_t id) const467 int VendorTagDescriptorCache::getTagType(uint32_t tag,
468 metadata_vendor_id_t id) const {
469 int ret = -1;
470 auto desc = mVendorMap.find(id);
471 if (desc != mVendorMap.end()) {
472 ret = desc->second->getTagType(tag);
473 } else {
474 ALOGE("%s: Vendor descriptor id is missing!", __func__);
475 }
476
477 return ret;
478 }
479
dump(int fd,int verbosity,int indentation) const480 void VendorTagDescriptorCache::dump(int fd, int verbosity,
481 int indentation) const {
482 for (const auto &desc : mVendorMap) {
483 dprintf(fd, "%*sDumping vendor tag descriptors for vendor with"
484 " id %" PRIu64 " \n", indentation, "", desc.first);
485 desc.second->dump(fd, verbosity, indentation);
486 }
487 }
488
addVendorDescriptor(metadata_vendor_id_t id,sp<android::VendorTagDescriptor> desc)489 int32_t VendorTagDescriptorCache::addVendorDescriptor(metadata_vendor_id_t id,
490 sp<android::VendorTagDescriptor> desc) {
491 auto entry = mVendorMap.find(id);
492 if (entry != mVendorMap.end()) {
493 ALOGE("%s: Vendor descriptor with same id already present!", __func__);
494 return BAD_VALUE;
495 }
496
497 mVendorMap.emplace(id, desc);
498 return NO_ERROR;
499 }
500
getVendorTagDescriptor(metadata_vendor_id_t id,sp<android::VendorTagDescriptor> * desc)501 int32_t VendorTagDescriptorCache::getVendorTagDescriptor(
502 metadata_vendor_id_t id, sp<android::VendorTagDescriptor> *desc /*out*/) {
503 auto entry = mVendorMap.find(id);
504 if (entry == mVendorMap.end()) {
505 return NAME_NOT_FOUND;
506 }
507
508 *desc = entry->second;
509
510 return NO_ERROR;
511 }
512
513 } // namespace params
514 } // namespace camera2
515 } // namespace hardware
516
createDescriptorFromOps(const vendor_tag_ops_t * vOps,sp<VendorTagDescriptor> & descriptor)517 status_t VendorTagDescriptor::createDescriptorFromOps(const vendor_tag_ops_t* vOps,
518 /*out*/
519 sp<VendorTagDescriptor>& descriptor) {
520 if (vOps == NULL) {
521 ALOGE("%s: vendor_tag_ops argument was NULL.", __FUNCTION__);
522 return BAD_VALUE;
523 }
524
525 int tagCount = vOps->get_tag_count(vOps);
526 if (tagCount < 0 || tagCount > INT32_MAX) {
527 ALOGE("%s: tag count %d from vendor ops is invalid.", __FUNCTION__, tagCount);
528 return BAD_VALUE;
529 }
530
531 Vector<uint32_t> tagArray;
532 LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount,
533 "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount);
534
535 vOps->get_all_tags(vOps, /*out*/tagArray.editArray());
536
537 sp<VendorTagDescriptor> desc = new VendorTagDescriptor();
538 desc->mTagCount = tagCount;
539
540 SortedVector<String8> sections;
541 KeyedVector<uint32_t, String8> tagToSectionMap;
542
543 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) {
544 uint32_t tag = tagArray[i];
545 if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) {
546 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag);
547 return BAD_VALUE;
548 }
549 const char *tagName = vOps->get_tag_name(vOps, tag);
550 if (tagName == NULL) {
551 ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag);
552 return BAD_VALUE;
553 }
554 desc->mTagToNameMap.add(tag, String8(tagName));
555 const char *sectionName = vOps->get_section_name(vOps, tag);
556 if (sectionName == NULL) {
557 ALOGE("%s: no section name defined for vendor tag %d.", __FUNCTION__, tag);
558 return BAD_VALUE;
559 }
560
561 String8 sectionString(sectionName);
562
563 sections.add(sectionString);
564 tagToSectionMap.add(tag, sectionString);
565
566 int tagType = vOps->get_tag_type(vOps, tag);
567 if (tagType < 0 || tagType >= NUM_TYPES) {
568 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType);
569 return BAD_VALUE;
570 }
571 desc->mTagToTypeMap.add(tag, tagType);
572 }
573
574 desc->mSections = sections;
575
576 for (size_t i = 0; i < static_cast<size_t>(tagCount); ++i) {
577 uint32_t tag = tagArray[i];
578 const String8& sectionString = tagToSectionMap.valueFor(tag);
579
580 // Set up tag to section index map
581 ssize_t index = sections.indexOf(sectionString);
582 LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index);
583 desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index));
584
585 // Set up reverse mapping
586 ssize_t reverseIndex = -1;
587 if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) {
588 KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
589 reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper);
590 }
591 desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag);
592 }
593
594 descriptor = desc;
595 return OK;
596 }
597
setAsGlobalVendorTagDescriptor(const sp<VendorTagDescriptor> & desc)598 status_t VendorTagDescriptor::setAsGlobalVendorTagDescriptor(const sp<VendorTagDescriptor>& desc) {
599 status_t res = OK;
600 Mutex::Autolock al(sLock);
601
602 vendor_tag_ops_t* opsPtr = NULL;
603 if (desc != NULL) {
604 opsPtr = &(desc->mVendorOps);
605 opsPtr->get_tag_count = vendor_tag_descriptor_get_tag_count;
606 opsPtr->get_all_tags = vendor_tag_descriptor_get_all_tags;
607 opsPtr->get_section_name = vendor_tag_descriptor_get_section_name;
608 opsPtr->get_tag_name = vendor_tag_descriptor_get_tag_name;
609 opsPtr->get_tag_type = vendor_tag_descriptor_get_tag_type;
610 }
611 if((res = set_camera_metadata_vendor_ops(opsPtr)) != OK) {
612 ALOGE("%s: Could not set vendor tag descriptor, received error %s (%d)."
613 , __FUNCTION__, strerror(-res), res);
614 }
615
616 sGlobalVendorTagDescriptor = desc;
617
618 return res;
619 }
620
clearGlobalVendorTagDescriptor()621 void VendorTagDescriptor::clearGlobalVendorTagDescriptor() {
622 Mutex::Autolock al(sLock);
623 set_camera_metadata_vendor_ops(NULL);
624 sGlobalVendorTagDescriptor.clear();
625 }
626
getGlobalVendorTagDescriptor()627 sp<VendorTagDescriptor> VendorTagDescriptor::getGlobalVendorTagDescriptor() {
628 Mutex::Autolock al(sLock);
629 return sGlobalVendorTagDescriptor;
630 }
631
setAsGlobalVendorTagCache(const sp<VendorTagDescriptorCache> & cache)632 status_t VendorTagDescriptorCache::setAsGlobalVendorTagCache(
633 const sp<VendorTagDescriptorCache>& cache) {
634 status_t res = OK;
635 Mutex::Autolock al(sLock);
636
637 struct vendor_tag_cache_ops* opsPtr = NULL;
638 if (cache != NULL) {
639 opsPtr = &(cache->mVendorCacheOps);
640 opsPtr->get_tag_count = vendor_tag_descriptor_cache_get_tag_count;
641 opsPtr->get_all_tags = vendor_tag_descriptor_cache_get_all_tags;
642 opsPtr->get_section_name = vendor_tag_descriptor_cache_get_section_name;
643 opsPtr->get_tag_name = vendor_tag_descriptor_cache_get_tag_name;
644 opsPtr->get_tag_type = vendor_tag_descriptor_cache_get_tag_type;
645 }
646 if((res = set_camera_metadata_vendor_cache_ops(opsPtr)) != OK) {
647 ALOGE("%s: Could not set vendor tag cache, received error %s (%d)."
648 , __FUNCTION__, strerror(-res), res);
649 }
650
651 sGlobalVendorTagDescriptorCache = cache;
652
653 return res;
654 }
655
clearGlobalVendorTagCache()656 void VendorTagDescriptorCache::clearGlobalVendorTagCache() {
657 Mutex::Autolock al(sLock);
658 set_camera_metadata_vendor_cache_ops(NULL);
659 sGlobalVendorTagDescriptorCache.clear();
660 }
661
getGlobalVendorTagCache()662 sp<VendorTagDescriptorCache> VendorTagDescriptorCache::getGlobalVendorTagCache() {
663 Mutex::Autolock al(sLock);
664 return sGlobalVendorTagDescriptorCache;
665 }
666
isVendorCachePresent(metadata_vendor_id_t vendorId)667 bool VendorTagDescriptorCache::isVendorCachePresent(metadata_vendor_id_t vendorId) {
668 Mutex::Autolock al(sLock);
669 if ((sGlobalVendorTagDescriptorCache.get() != nullptr) &&
670 (sGlobalVendorTagDescriptorCache->getVendorIdsAndTagDescriptors().find(vendorId) !=
671 sGlobalVendorTagDescriptorCache->getVendorIdsAndTagDescriptors().end())) {
672 return true;
673 }
674 return false;
675 }
676
677 extern "C" {
678
vendor_tag_descriptor_get_tag_count(const vendor_tag_ops_t *)679 int vendor_tag_descriptor_get_tag_count(const vendor_tag_ops_t* /*v*/) {
680 Mutex::Autolock al(sLock);
681 if (sGlobalVendorTagDescriptor == NULL) {
682 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__);
683 return VENDOR_TAG_COUNT_ERR;
684 }
685 return sGlobalVendorTagDescriptor->getTagCount();
686 }
687
vendor_tag_descriptor_get_all_tags(const vendor_tag_ops_t *,uint32_t * tagArray)688 void vendor_tag_descriptor_get_all_tags(const vendor_tag_ops_t* /*v*/, uint32_t* tagArray) {
689 Mutex::Autolock al(sLock);
690 if (sGlobalVendorTagDescriptor == NULL) {
691 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__);
692 return;
693 }
694 sGlobalVendorTagDescriptor->getTagArray(tagArray);
695 }
696
vendor_tag_descriptor_get_section_name(const vendor_tag_ops_t *,uint32_t tag)697 const char* vendor_tag_descriptor_get_section_name(const vendor_tag_ops_t* /*v*/, uint32_t tag) {
698 Mutex::Autolock al(sLock);
699 if (sGlobalVendorTagDescriptor == NULL) {
700 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__);
701 return VENDOR_SECTION_NAME_ERR;
702 }
703 return sGlobalVendorTagDescriptor->getSectionName(tag);
704 }
705
vendor_tag_descriptor_get_tag_name(const vendor_tag_ops_t *,uint32_t tag)706 const char* vendor_tag_descriptor_get_tag_name(const vendor_tag_ops_t* /*v*/, uint32_t tag) {
707 Mutex::Autolock al(sLock);
708 if (sGlobalVendorTagDescriptor == NULL) {
709 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__);
710 return VENDOR_TAG_NAME_ERR;
711 }
712 return sGlobalVendorTagDescriptor->getTagName(tag);
713 }
714
vendor_tag_descriptor_get_tag_type(const vendor_tag_ops_t *,uint32_t tag)715 int vendor_tag_descriptor_get_tag_type(const vendor_tag_ops_t* /*v*/, uint32_t tag) {
716 Mutex::Autolock al(sLock);
717 if (sGlobalVendorTagDescriptor == NULL) {
718 ALOGE("%s: Vendor tag descriptor not initialized.", __FUNCTION__);
719 return VENDOR_TAG_TYPE_ERR;
720 }
721 return sGlobalVendorTagDescriptor->getTagType(tag);
722 }
723
vendor_tag_descriptor_cache_get_tag_count(metadata_vendor_id_t id)724 int vendor_tag_descriptor_cache_get_tag_count(metadata_vendor_id_t id) {
725 Mutex::Autolock al(sLock);
726 if (sGlobalVendorTagDescriptorCache == NULL) {
727 ALOGE("%s: Vendor tag descriptor cache not initialized.", __FUNCTION__);
728 return VENDOR_TAG_COUNT_ERR;
729 }
730 return sGlobalVendorTagDescriptorCache->getTagCount(id);
731 }
732
vendor_tag_descriptor_cache_get_all_tags(uint32_t * tagArray,metadata_vendor_id_t id)733 void vendor_tag_descriptor_cache_get_all_tags(uint32_t* tagArray,
734 metadata_vendor_id_t id) {
735 Mutex::Autolock al(sLock);
736 if (sGlobalVendorTagDescriptorCache == NULL) {
737 ALOGE("%s: Vendor tag descriptor cache not initialized.", __FUNCTION__);
738 }
739 sGlobalVendorTagDescriptorCache->getTagArray(tagArray, id);
740 }
741
vendor_tag_descriptor_cache_get_section_name(uint32_t tag,metadata_vendor_id_t id)742 const char* vendor_tag_descriptor_cache_get_section_name(uint32_t tag,
743 metadata_vendor_id_t id) {
744 Mutex::Autolock al(sLock);
745 if (sGlobalVendorTagDescriptorCache == NULL) {
746 ALOGE("%s: Vendor tag descriptor cache not initialized.", __FUNCTION__);
747 return VENDOR_SECTION_NAME_ERR;
748 }
749 return sGlobalVendorTagDescriptorCache->getSectionName(tag, id);
750 }
751
vendor_tag_descriptor_cache_get_tag_name(uint32_t tag,metadata_vendor_id_t id)752 const char* vendor_tag_descriptor_cache_get_tag_name(uint32_t tag,
753 metadata_vendor_id_t id) {
754 Mutex::Autolock al(sLock);
755 if (sGlobalVendorTagDescriptorCache == NULL) {
756 ALOGE("%s: Vendor tag descriptor cache not initialized.", __FUNCTION__);
757 return VENDOR_TAG_NAME_ERR;
758 }
759 return sGlobalVendorTagDescriptorCache->getTagName(tag, id);
760 }
761
vendor_tag_descriptor_cache_get_tag_type(uint32_t tag,metadata_vendor_id_t id)762 int vendor_tag_descriptor_cache_get_tag_type(uint32_t tag,
763 metadata_vendor_id_t id) {
764 Mutex::Autolock al(sLock);
765 if (sGlobalVendorTagDescriptorCache == NULL) {
766 ALOGE("%s: Vendor tag descriptor cache not initialized.", __FUNCTION__);
767 return VENDOR_TAG_TYPE_ERR;
768 }
769 return sGlobalVendorTagDescriptorCache->getTagType(tag, id);
770 }
771
772 } /* extern "C" */
773 } /* namespace android */
774