1 /*
2  * Copyright 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Metadata"
19 
20 #include "metadata.h"
21 
22 #include <hardware/camera3.h>
23 
24 #include "common.h"
25 
26 namespace v4l2_camera_hal {
27 
Metadata(PartialMetadataSet components)28 Metadata::Metadata(PartialMetadataSet components)
29     : components_(std::move(components)) {
30   HAL_LOG_ENTER();
31 }
32 
~Metadata()33 Metadata::~Metadata() {
34   HAL_LOG_ENTER();
35 }
36 
FillStaticMetadata(android::CameraMetadata * metadata)37 int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
38   HAL_LOG_ENTER();
39   if (!metadata) {
40     HAL_LOGE("Can't fill null metadata.");
41     return -EINVAL;
42   }
43 
44   std::vector<int32_t> static_tags;
45   std::vector<int32_t> control_tags;
46   std::vector<int32_t> dynamic_tags;
47   int res = 0;
48 
49   for (auto& component : components_) {
50     // Prevent components from potentially overriding others.
51     android::CameraMetadata additional_metadata;
52     // Populate the fields.
53     res = component->PopulateStaticFields(&additional_metadata);
54     if (res) {
55       HAL_LOGE("Failed to get all static properties.");
56       return res;
57     }
58     // Add it to the overall result.
59     if (!additional_metadata.isEmpty()) {
60       res = metadata->append(additional_metadata);
61       if (res != android::OK) {
62         HAL_LOGE("Failed to append all static properties.");
63         return res;
64       }
65     }
66 
67     // Note what tags the component adds.
68     std::vector<int32_t> tags = component->StaticTags();
69     std::move(tags.begin(),
70               tags.end(),
71               std::inserter(static_tags, static_tags.end()));
72     tags = component->ControlTags();
73     std::move(tags.begin(),
74               tags.end(),
75               std::inserter(control_tags, control_tags.end()));
76     tags = component->DynamicTags();
77     std::move(tags.begin(),
78               tags.end(),
79               std::inserter(dynamic_tags, dynamic_tags.end()));
80   }
81 
82   // Populate the meta fields.
83   static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
84   res = UpdateMetadata(
85       metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
86   if (res != android::OK) {
87     HAL_LOGE("Failed to add request keys meta key.");
88     return -ENODEV;
89   }
90   static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
91   res = UpdateMetadata(
92       metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
93   if (res != android::OK) {
94     HAL_LOGE("Failed to add result keys meta key.");
95     return -ENODEV;
96   }
97   static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
98   res = UpdateMetadata(
99       metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
100   if (res != android::OK) {
101     HAL_LOGE("Failed to add characteristics keys meta key.");
102     return -ENODEV;
103   }
104 
105   // TODO(b/31018853): cache result.
106   return 0;
107 }
108 
IsValidRequest(const android::CameraMetadata & metadata)109 bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
110   HAL_LOG_ENTER();
111 
112   // Empty means "use previous settings", which are inherently valid.
113   if (metadata.isEmpty())
114     return true;
115 
116   for (auto& component : components_) {
117     // Check that all components support the values requested of them.
118     bool valid_request = component->SupportsRequestValues(metadata);
119     if (!valid_request) {
120       // Exit early if possible.
121       return false;
122     }
123   }
124 
125   return true;
126 }
127 
GetRequestTemplate(int template_type,android::CameraMetadata * template_metadata)128 int Metadata::GetRequestTemplate(int template_type,
129                                  android::CameraMetadata* template_metadata) {
130   HAL_LOG_ENTER();
131   if (!template_metadata) {
132     HAL_LOGE("Can't fill null template.");
133     return -EINVAL;
134   }
135 
136   // Templates are numbered 1 through COUNT-1 for some reason.
137   if (template_type < 1 || template_type >= CAMERA3_TEMPLATE_COUNT) {
138     HAL_LOGE("Unrecognized template type %d.", template_type);
139     return -EINVAL;
140   }
141 
142   for (auto& component : components_) {
143     // Prevent components from potentially overriding others.
144     android::CameraMetadata additional_metadata;
145     int res =
146         component->PopulateTemplateRequest(template_type, &additional_metadata);
147     if (res) {
148       HAL_LOGE("Failed to get all default request fields.");
149       return res;
150     }
151     // Add it to the overall result.
152     if (!additional_metadata.isEmpty()) {
153       res = template_metadata->append(additional_metadata);
154       if (res != android::OK) {
155         HAL_LOGE("Failed to append all default request fields.");
156         return res;
157       }
158     }
159   }
160 
161   // TODO(b/31018853): cache result.
162   return 0;
163 }
164 
SetRequestSettings(const android::CameraMetadata & metadata)165 int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
166   HAL_LOG_ENTER();
167 
168   // Empty means "use previous settings".
169   if (metadata.isEmpty())
170     return 0;
171 
172   for (auto& component : components_) {
173     int res = component->SetRequestValues(metadata);
174     if (res) {
175       HAL_LOGE("Failed to set all requested settings.");
176       return res;
177     }
178   }
179 
180   return 0;
181 }
182 
FillResultMetadata(android::CameraMetadata * metadata)183 int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
184   HAL_LOG_ENTER();
185   if (!metadata) {
186     HAL_LOGE("Can't fill null metadata.");
187     return -EINVAL;
188   }
189 
190   for (auto& component : components_) {
191     // Prevent components from potentially overriding others.
192     android::CameraMetadata additional_metadata;
193     int res = component->PopulateDynamicFields(&additional_metadata);
194     if (res) {
195       HAL_LOGE("Failed to get all dynamic result fields.");
196       return res;
197     }
198     // Add it to the overall result.
199     if (!additional_metadata.isEmpty()) {
200       res = metadata->append(additional_metadata);
201       if (res != android::OK) {
202         HAL_LOGE("Failed to append all dynamic result fields.");
203         return res;
204       }
205     }
206   }
207 
208   return 0;
209 }
210 
211 }  // namespace v4l2_camera_hal
212