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 #ifndef CAMERA_COMMON_1_0_VENDORTAGDESCRIPTOR_H
18 #define CAMERA_COMMON_1_0_VENDORTAGDESCRIPTOR_H
19 
20 #include <system/camera_vendor_tags.h>
21 #include <utils/KeyedVector.h>
22 #include <utils/RefBase.h>
23 #include <utils/String8.h>
24 #include <utils/Vector.h>
25 
26 #include <stdint.h>
27 #include <unordered_map>
28 
29 namespace android {
30 namespace hardware {
31 namespace camera2 {
32 namespace params {
33 
34 /**
35  * VendorTagDescriptor objects are containers for the vendor tag
36  * definitions provided, and are typically used to pass the vendor tag
37  * information enumerated by the HAL to clients of the camera service.
38  */
39 class VendorTagDescriptor {
40   public:
41     virtual ~VendorTagDescriptor();
42 
43     VendorTagDescriptor();
44     VendorTagDescriptor(const VendorTagDescriptor& src);
45     VendorTagDescriptor& operator=(const VendorTagDescriptor& rhs);
46 
47     void copyFrom(const VendorTagDescriptor& src);
48 
49     /**
50      * The following 'get*' methods implement the corresponding
51      * functions defined in
52      * system/media/camera/include/system/camera_vendor_tags.h
53      */
54 
55     // Returns the number of vendor tags defined.
56     int getTagCount() const;
57 
58     // Returns an array containing the id's of vendor tags defined.
59     void getTagArray(uint32_t* tagArray) const;
60 
61     // Returns the section name string for a given vendor tag id.
62     const char* getSectionName(uint32_t tag) const;
63 
64     // Returns the index in section vectors returned in getAllSectionNames()
65     // for a given vendor tag id. -1 if input tag does not exist.
66     ssize_t getSectionIndex(uint32_t tag) const;
67 
68     // Returns the tag name string for a given vendor tag id.
69     const char* getTagName(uint32_t tag) const;
70 
71     // Returns the tag type for a given vendor tag id.
72     int getTagType(uint32_t tag) const;
73 
74     /**
75      * Convenience method to get a vector containing all vendor tag
76      * sections, or an empty vector if none are defined.
77      * The pointer is valid for the lifetime of the VendorTagDescriptor,
78      * or until copyFrom is invoked.
79      */
80     const SortedVector<String8>* getAllSectionNames() const;
81 
82     /**
83      * Lookup the tag id for a given tag name and section.
84      *
85      * Returns OK on success, or a negative error code.
86      */
87     status_t lookupTag(const String8& name, const String8& section, /*out*/ uint32_t* tag) const;
88 
89     /**
90      * Dump the currently configured vendor tags to a file descriptor.
91      */
92     void dump(int fd, int verbosity, int indentation) const;
93 
94   protected:
95     KeyedVector<String8, KeyedVector<String8, uint32_t>*> mReverseMapping;
96     KeyedVector<uint32_t, String8> mTagToNameMap;
97     KeyedVector<uint32_t, uint32_t> mTagToSectionMap;  // Value is offset in mSections
98 
99     std::unordered_map<uint32_t, int32_t> mTagToTypeMap;
100     SortedVector<String8> mSections;
101     // must be int32_t to be compatible with Parcel::writeInt32
102     int32_t mTagCount;
103 
104     vendor_tag_ops mVendorOps;
105 };
106 } /* namespace params */
107 } /* namespace camera2 */
108 
109 namespace camera {
110 namespace common {
111 namespace helper {
112 
113 /**
114  * This version of VendorTagDescriptor must be stored in Android sp<>, and adds support for using it
115  * as a global tag descriptor.
116  *
117  * It's a child class of the basic hardware::camera2::params::VendorTagDescriptor since basic
118  * Parcelable objects cannot require being kept in an sp<> and still work with auto-generated AIDL
119  * interface implementations.
120  */
121 class VendorTagDescriptor : public ::android::hardware::camera2::params::VendorTagDescriptor,
122                             public LightRefBase<VendorTagDescriptor> {
123   public:
124     /**
125      * Create a VendorTagDescriptor object from the given vendor_tag_ops_t
126      * struct.
127      *
128      * Returns OK on success, or a negative error code.
129      */
130     static status_t createDescriptorFromOps(const vendor_tag_ops_t* vOps,
131                                             /*out*/
132                                             sp<VendorTagDescriptor>& descriptor);
133 
134     /**
135      * Sets the global vendor tag descriptor to use for this process.
136      * Camera metadata operations that access vendor tags will use the
137      * vendor tag definitions set this way.
138      *
139      * Returns OK on success, or a negative error code.
140      */
141     static status_t setAsGlobalVendorTagDescriptor(const sp<VendorTagDescriptor>& desc);
142 
143     /**
144      * Returns the global vendor tag descriptor used by this process.
145      * This will contain NULL if no vendor tags are defined.
146      */
147     static sp<VendorTagDescriptor> getGlobalVendorTagDescriptor();
148 
149     /**
150      * Clears the global vendor tag descriptor used by this process.
151      */
152     static void clearGlobalVendorTagDescriptor();
153 };
154 
155 } /* namespace helper */
156 } /* namespace common */
157 } /* namespace camera */
158 
159 namespace camera2 {
160 namespace params {
161 
162 class VendorTagDescriptorCache {
163   public:
164     typedef android::hardware::camera::common::helper::VendorTagDescriptor VendorTagDescriptor;
VendorTagDescriptorCache()165     VendorTagDescriptorCache(){};
166     int32_t addVendorDescriptor(metadata_vendor_id_t id, sp<VendorTagDescriptor> desc);
167 
168     int32_t getVendorTagDescriptor(metadata_vendor_id_t id, sp<VendorTagDescriptor>* desc /*out*/);
169 
170     // Returns the number of vendor tags defined.
171     int getTagCount(metadata_vendor_id_t id) const;
172 
173     // Returns an array containing the id's of vendor tags defined.
174     void getTagArray(uint32_t* tagArray, metadata_vendor_id_t id) const;
175 
176     // Returns the section name string for a given vendor tag id.
177     const char* getSectionName(uint32_t tag, metadata_vendor_id_t id) const;
178 
179     // Returns the tag name string for a given vendor tag id.
180     const char* getTagName(uint32_t tag, metadata_vendor_id_t id) const;
181 
182     // Returns the tag type for a given vendor tag id.
183     int getTagType(uint32_t tag, metadata_vendor_id_t id) const;
184 
185     /**
186      * Dump the currently configured vendor tags to a file descriptor.
187      */
188     void dump(int fd, int verbosity, int indentation) const;
189 
190   protected:
191     std::unordered_map<metadata_vendor_id_t, sp<VendorTagDescriptor>> mVendorMap;
192     struct vendor_tag_cache_ops mVendorCacheOps;
193 };
194 
195 } /* namespace params */
196 } /* namespace camera2 */
197 
198 namespace camera {
199 namespace common {
200 namespace helper {
201 
202 class VendorTagDescriptorCache
203     : public ::android::hardware::camera2::params::VendorTagDescriptorCache,
204       public LightRefBase<VendorTagDescriptorCache> {
205   public:
206     /**
207      * Sets the global vendor tag descriptor cache to use for this process.
208      * Camera metadata operations that access vendor tags will use the
209      * vendor tag definitions set this way.
210      *
211      * Returns OK on success, or a negative error code.
212      */
213     static status_t setAsGlobalVendorTagCache(const sp<VendorTagDescriptorCache>& cache);
214 
215     /**
216      * Returns the global vendor tag cache used by this process.
217      * This will contain NULL if no vendor tags are defined.
218      */
219     static sp<VendorTagDescriptorCache> getGlobalVendorTagCache();
220 
221     /**
222      * Clears the global vendor tag cache used by this process.
223      */
224     static void clearGlobalVendorTagCache();
225 };
226 
227 }  // namespace helper
228 
229 // NOTE: Deprecated namespace. This namespace should no longer be used for the following symbols
230 namespace V1_0::helper {
231 // Export symbols to the old namespace to preserve compatibility
232 typedef android::hardware::camera::common::helper::VendorTagDescriptor VendorTagDescriptor;
233 typedef android::hardware::camera::common::helper::VendorTagDescriptorCache
234         VendorTagDescriptorCache;
235 }  // namespace V1_0::helper
236 
237 }  // namespace common
238 }  // namespace camera
239 }  // namespace hardware
240 }  // namespace android
241 
242 #endif /* CAMERA_COMMON_1_0_VENDORTAGDESCRIPTOR_H */
243