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_UTILS_HAL_CAMERA_METADATA_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_HAL_CAMERA_METADATA_H_
19 
20 #include <system/camera_metadata.h>
21 #include <utils/Errors.h>
22 #include <memory>
23 #include <mutex>
24 #include <unordered_set>
25 #include <vector>
26 
27 namespace android {
28 namespace google_camera_hal {
29 
30 using ::android::status_t;
31 
32 // kOnlyTagEntry: Only tag entry information
33 // kTagEntryWith16Data: Tag entry information plus at most 16 data values
34 // kAllInformation: All information
35 enum class MetadataDumpVerbosity : uint32_t {
36   kOnlyTagEntry = 0,
37   kTagEntryWith16Data,
38   kAllInformation,
39 };
40 
41 class HalCameraMetadata {
42  public:
43   // Create a HalCameraMetadata and allocate camera_metadata.
44   // entry_capacity is the number of entries in the camera_metadata.
45   // data_capacity is the number of bytes of the data in the camera_metadata.
46   static std::unique_ptr<HalCameraMetadata> Create(size_t entry_capacity,
47                                                    size_t data_capacity);
48 
49   // Create a HalCameraMetadata to own camera_metadata.
50   // metadata will be owned by this HalCameraMetadata.
51   // This will return nullptr if metadata is nullptr.
52   // If this returns nullptr, the caller still has the ownership for metadata.
53   static std::unique_ptr<HalCameraMetadata> Create(camera_metadata_t* metadata);
54 
55   // Create a HalCameraMetadata and clone the metadata.
56   // metadata will be cloned and still owned by the caller.
57   // This will return nullptr if metadata is nullptr.
58   static std::unique_ptr<HalCameraMetadata> Clone(
59       const camera_metadata_t* metadata);
60 
61   // Create a HalCameraMetadata and clone the metadata.
62   // hal_metadata will be cloned and still owned by the caller.
63   // This will return nullptr if metadata is nullptr.
64   static std::unique_ptr<HalCameraMetadata> Clone(
65       const HalCameraMetadata* hal_metadata);
66 
67   virtual ~HalCameraMetadata();
68 
69   // Return the camera_metadata owned by this HalCameraMetadata and transfer
70   // the ownership to the caller. Caller is responsible for freeing the
71   // camera metadata.
72   camera_metadata_t* ReleaseCameraMetadata();
73 
74   // Returns the raw camera metadata pointer bound to this class. Note that it
75   // would be an error to free this metadata or to change it in any way outside
76   // this class while it's still owned by this class.
77   const camera_metadata_t* GetRawCameraMetadata() const;
78 
79   // Get the size of the metadata in the metadata in bytes.
80   size_t GetCameraMetadataSize() const;
81 
82   // Set metadata entry for setting a key's value
83   status_t Set(uint32_t tag, const uint8_t* data, uint32_t data_count);
84   status_t Set(uint32_t tag, const int32_t* data, uint32_t data_count);
85   status_t Set(uint32_t tag, const float* data, uint32_t data_count);
86   status_t Set(uint32_t tag, const int64_t* data, uint32_t data_count);
87   status_t Set(uint32_t tag, const double* data, uint32_t data_count);
88   status_t Set(uint32_t tag, const camera_metadata_rational_t* data,
89                uint32_t data_count);
90   status_t Set(uint32_t tag, const std::string& string);
91   // This is a helper function, it will call related Set(...).
92   // You don't need to set type in the entry, it gets type from tag id.
93   status_t Set(const camera_metadata_ro_entry& entry);
94 
95   // Get a key's value by tag. Returns NAME_NOT_FOUND if the tag does not exist
96   status_t Get(uint32_t tag, camera_metadata_ro_entry* entry) const;
97 
98   // Get a key's value by entry index. Returns NAME_NOT_FOUND if the tag does not exist
99   status_t GetByIndex(camera_metadata_ro_entry* entry, size_t entry_index) const;
100 
101   // Erase a key. This is an expensive operation resulting in revalidation of
102   // the entire metadata structure
103   status_t Erase(uint32_t tag);
104 
105   // Erase all the given keys. This is an expensive operation and will result in
106   // de-allocating memory for the original metadata container and allocating a
107   // new smaller container to shrink the size. This will be considerably more
108   // efficient than calling the 'Erase(uint32_t tag)' counterpart multiple
109   // times, but less efficient if the number of tags being erased is very small.
110   status_t Erase(const std::unordered_set<uint32_t>& tags);
111 
112   // Dump metadata
113   // fd >= 0, dump metadata to file
114   // fd < 0, dump metadata to logcat
115   void Dump(int32_t fd,
116             MetadataDumpVerbosity verbosity =
117                 MetadataDumpVerbosity::kTagEntryWith16Data,
118             uint32_t indentation = 0) const;
119 
120   // Append metadata from HalCameraMetadata object
121   status_t Append(std::unique_ptr<HalCameraMetadata> hal_metadata);
122 
123   // Append metadata from a raw camera_metadata buffer
124   status_t Append(camera_metadata_t* metadata);
125 
126   // Get metadata entry size
127   size_t GetEntryCount() const;
128 
129  protected:
130   HalCameraMetadata(camera_metadata_t* metadata);
131 
132  private:
133   // For dump metadata
134   static void PrintData(const uint8_t* data, int32_t type, int32_t count,
135                         uint32_t indentation);
136 
137   bool IsTypeValid(uint32_t tag, int32_t expected_type);
138 
139   // Base Set entry method.
140   status_t SetMetadataRaw(uint32_t tag, const void* data, size_t data_count);
141 
142   status_t ResizeIfNeeded(size_t extra_entries, size_t extra_data);
143 
144   // Copy entry at the given index from source buffer to destination buffer
145   status_t CopyEntry(const camera_metadata_t* src, camera_metadata_t* dest,
146                      size_t entry_index) const;
147 
148   // Camera metadata owned by this HalCameraMetadata.
149   mutable std::mutex metadata_lock_;
150   camera_metadata_t* metadata_ = nullptr;
151 };
152 
153 }  // namespace google_camera_hal
154 }  // namespace android
155 
156 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_HAL_CAMERA_METADATA_H_
157