1 /*
2  * Copyright (C) 2019 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 HARDWARE_GOOGLE_CAMERA_HAL_CAMERA_VENDOR_TAG_UTILS_H
18 #define HARDWARE_GOOGLE_CAMERA_HAL_CAMERA_VENDOR_TAG_UTILS_H
19 
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "hal_types.h"
26 #include "vendor_tag_interface.h"
27 
28 namespace android {
29 namespace google_camera_hal {
30 namespace vendor_tag_utils {
31 
32 // Returns the list of combined vendor tags. Returns an error if any tag IDs
33 // overlap, or if any vendor tag (section name + tag name) overlaps
34 status_t CombineVendorTags(const std::vector<VendorTagSection>& source1,
35                            const std::vector<VendorTagSection>& source2,
36                            std::vector<VendorTagSection>* destination);
37 
38 }  // namespace vendor_tag_utils
39 
40 // Utility class to create vendor tag descriptors from a list of vendor tag
41 // sections, in order to provide to camera metadata framework. This class helps
42 // build a wrapper around vendor tag operations expected by the camera
43 // framework. These are eseentially the 'vendor_tag_ops_t' to be set by calling
44 // set_camera_metadata_vendor_ops(). This wrapper is a singleton because there
45 // could be only one set of callbacks set per camera provider. The HWL or HAL
46 // layers should use this wrapper instead of directly invoking
47 // set_camera_metadata_vendor_ops()
48 class VendorTagManager : public VendorTagInterface {
49  public:
50   static VendorTagManager& GetInstance();
51 
52   virtual ~VendorTagManager() = default;
53 
54   // Add a set of vendor tags, combine them with any tags added earlier, and set
55   // callbacks for the camera metadata framework if they haven't been set
56   // already.
57   status_t AddTags(const std::vector<VendorTagSection>& tag_sections);
58 
59   // Get the combined list of all tags that have been added so far.
60   const std::vector<VendorTagSection>& GetTags() const;
61 
62   // Clears all the vendor tag data that was set via AddTags(), and resets
63   // the vendor tag operations previously set to the camera metadata framework
64   void Reset();
65 
66   // Vendor tag operations needed by camera metadata framework, as defined in
67   // vendor_tag_ops_t struct.
68   int GetCount() const;
69   void GetAllTags(uint32_t* tag_array) const;
70   const char* GetSectionName(uint32_t tag_id) const;
71   const char* GetTagName(uint32_t tag_id) const;
72   int GetTagType(uint32_t tag_id) const;
73 
74   // Disallow copy and assignment operators
75   VendorTagManager(VendorTagManager const&) = delete;
76   VendorTagManager& operator=(VendorTagManager const&) = delete;
77 
78   // Get Tag info for the give tag id
79   status_t GetTagInfo(uint32_t tag_id, VendorTagInfo* tag_info) override;
80 
81   // Get the tag id for the given section/name
82   status_t GetTag(const std::string section_name, const std::string tag_name,
83                   uint32_t* tag_id) override;
84 
85  private:
86   VendorTagManager() = default;
87 
88   // Map from vendor tag ID to VendorTagInfo. Used for camera framework
89   // vendor tag callbacks, protected by api_mutex_.
90   std::unordered_map<uint32_t, VendorTagInfo> vendor_tag_map_;
91 
92   using TagString = std::pair<std::string, std::string>;
93 
94   struct TagStringHash {
operatorTagStringHash95     size_t operator()(const TagString& pair) const {
96       std::hash<TagString::first_type> h1;
97       std::hash<TagString::second_type> h2;
98       return h1(pair.first) ^ h2(pair.second);
99     }
100   };
101 
102   std::unordered_map<const TagString, uint32_t, TagStringHash>
103       vendor_tag_inverse_map_;
104 
105   // Protects the public entry points into this class.
106   mutable std::mutex api_mutex_;
107 
108   // Combined list of all tags added with AddTags().
109   std::vector<VendorTagSection> tag_sections_;
110 };
111 }  // namespace google_camera_hal
112 }  // namespace android
113 
114 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_CAMERA_VENDOR_TAG_UTILS_H
115