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