xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/common/CameraProviderInfoTemplated.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2022 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 #ifndef ANDROID_SERVERS_CAMERA_CAMERAPROVIDERINFO_TEMPLATEDH
17 #define ANDROID_SERVERS_CAMERA_CAMERAPROVIDERINFO_TEMPLATEDH
18 
19 #include "common/CameraProviderManager.h"
20 
21 namespace android {
22 
23 template <class VendorTagSectionVectorType, class VendorTagSectionType>
createDescriptorFromIdl(const VendorTagSectionVectorType & vts,sp<VendorTagDescriptor> & descriptor)24 status_t IdlVendorTagDescriptor::createDescriptorFromIdl(
25         const VendorTagSectionVectorType& vts,
26         sp<VendorTagDescriptor>& descriptor) {
27 
28     int tagCount = 0;
29 
30     for (size_t s = 0; s < vts.size(); s++) {
31         tagCount += vts[s].tags.size();
32     }
33 
34     if (tagCount < 0 || tagCount > INT32_MAX) {
35         ALOGE("%s: tag count %d from vendor tag sections is invalid.", __FUNCTION__, tagCount);
36         return BAD_VALUE;
37     }
38 
39     Vector<uint32_t> tagArray;
40     LOG_ALWAYS_FATAL_IF(tagArray.resize(tagCount) != tagCount,
41             "%s: too many (%u) vendor tags defined.", __FUNCTION__, tagCount);
42     sp<IdlVendorTagDescriptor> desc = new IdlVendorTagDescriptor();
43     desc->mTagCount = tagCount;
44 
45     SortedVector<String8> sections;
46     KeyedVector<uint32_t, String8> tagToSectionMap;
47 
48     int idx = 0;
49     for (size_t s = 0; s < vts.size(); s++) {
50         const VendorTagSectionType& section = vts[s];
51         const char *sectionName = section.sectionName.c_str();
52         if (sectionName == NULL) {
53             ALOGE("%s: no section name defined for vendor tag section %zu.", __FUNCTION__, s);
54             return BAD_VALUE;
55         }
56         String8 sectionString(sectionName);
57         sections.add(sectionString);
58 
59         for (size_t j = 0; j < section.tags.size(); j++) {
60             uint32_t tag = section.tags[j].tagId;
61             if (tag < CAMERA_METADATA_VENDOR_TAG_BOUNDARY) {
62                 ALOGE("%s: vendor tag %d not in vendor tag section.", __FUNCTION__, tag);
63                 return BAD_VALUE;
64             }
65 
66             tagArray.editItemAt(idx++) = section.tags[j].tagId;
67 
68             const char *tagName = section.tags[j].tagName.c_str();
69             if (tagName == NULL) {
70                 ALOGE("%s: no tag name defined for vendor tag %d.", __FUNCTION__, tag);
71                 return BAD_VALUE;
72             }
73             desc->mTagToNameMap.add(tag, String8(tagName));
74             tagToSectionMap.add(tag, sectionString);
75 
76             int tagType = (int) section.tags[j].tagType;
77             if (tagType < 0 || tagType >= NUM_TYPES) {
78                 ALOGE("%s: tag type %d from vendor ops does not exist.", __FUNCTION__, tagType);
79                 return BAD_VALUE;
80             }
81             desc->mTagToTypeMap.add(tag, tagType);
82         }
83     }
84 
85     desc->mSections = sections;
86 
87     for (size_t i = 0; i < tagArray.size(); ++i) {
88         uint32_t tag = tagArray[i];
89         String8 sectionString = tagToSectionMap.valueFor(tag);
90 
91         // Set up tag to section index map
92         ssize_t index = sections.indexOf(sectionString);
93         LOG_ALWAYS_FATAL_IF(index < 0, "index %zd must be non-negative", index);
94         desc->mTagToSectionMap.add(tag, static_cast<uint32_t>(index));
95 
96         // Set up reverse mapping
97         ssize_t reverseIndex = -1;
98         if ((reverseIndex = desc->mReverseMapping.indexOfKey(sectionString)) < 0) {
99             KeyedVector<String8, uint32_t>* nameMapper = new KeyedVector<String8, uint32_t>();
100             reverseIndex = desc->mReverseMapping.add(sectionString, nameMapper);
101         }
102         desc->mReverseMapping[reverseIndex]->add(desc->mTagToNameMap.valueFor(tag), tag);
103     }
104 
105     descriptor = std::move(desc);
106     return OK;
107 }
108 
109 
110 } // namespace android
111 
112 #endif
113