blob: efc9959275f8662b0e7cb8c53c70d22238c40527 [file] [log] [blame]
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -07001/*
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#include "metadata.h"
18
19#include <camera/CameraMetadata.h>
Ari Hausman-Cohen10264772016-08-22 13:49:43 -070020#include <hardware/camera3.h>
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070021
22#include "../common.h"
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070023#include "metadata_common.h"
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070024
25namespace v4l2_camera_hal {
26
Ari Hausman-Cohenffdc6282016-08-19 12:54:22 -070027Metadata::Metadata(PartialMetadataSet components)
28 : components_(std::move(components)) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070029 HAL_LOG_ENTER();
30}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070031
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070032Metadata::~Metadata() {
33 HAL_LOG_ENTER();
34}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070035
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070036int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070037 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070038 if (!metadata) {
39 HAL_LOGE("Can't fill null metadata.");
40 return -EINVAL;
41 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070042
43 std::vector<int32_t> static_tags;
44 std::vector<int32_t> control_tags;
45 std::vector<int32_t> dynamic_tags;
46 int res = 0;
47
48 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070049 // Prevent components from potentially overriding others.
50 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070051 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070052 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070053 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070054 HAL_LOGE("Failed to get all static properties.");
55 return res;
56 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070057 // Add it to the overall result.
58 if (!additional_metadata.isEmpty()) {
59 res = metadata->append(additional_metadata);
60 if (res != android::OK) {
61 HAL_LOGE("Failed to append all static properties.");
62 return res;
63 }
64 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070065
66 // Note what tags the component adds.
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070067 std::vector<int32_t> tags = component->StaticTags();
68 std::move(tags.begin(),
69 tags.end(),
70 std::inserter(static_tags, static_tags.end()));
71 tags = component->ControlTags();
72 std::move(tags.begin(),
73 tags.end(),
74 std::inserter(control_tags, control_tags.end()));
75 tags = component->DynamicTags();
76 std::move(tags.begin(),
77 tags.end(),
78 std::inserter(dynamic_tags, dynamic_tags.end()));
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070079 }
80
81 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070082 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070083 res = UpdateMetadata(
84 metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070085 if (res != android::OK) {
86 HAL_LOGE("Failed to add request keys meta key.");
87 return -ENODEV;
88 }
89 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070090 res = UpdateMetadata(
91 metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070092 if (res != android::OK) {
93 HAL_LOGE("Failed to add result keys meta key.");
94 return -ENODEV;
95 }
96 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070097 res = UpdateMetadata(
98 metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070099 if (res != android::OK) {
100 HAL_LOGE("Failed to add characteristics keys meta key.");
101 return -ENODEV;
102 }
103
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700104 // TODO(b/31018853): cache result.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700105 return 0;
106}
107
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700108bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700109 HAL_LOG_ENTER();
110
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700111 // Empty means "use previous settings", which are inherently valid.
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700112 if (metadata.isEmpty())
113 return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700114
115 for (auto& component : components_) {
116 // Check that all components support the values requested of them.
117 bool valid_request = component->SupportsRequestValues(metadata);
118 if (!valid_request) {
119 // Exit early if possible.
120 return false;
121 }
122 }
123
124 return true;
125}
126
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700127int Metadata::GetRequestTemplate(int template_type,
128 android::CameraMetadata* template_metadata) {
129 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700130 if (!template_metadata) {
131 HAL_LOGE("Can't fill null template.");
132 return -EINVAL;
133 }
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700134
135 // Templates are numbered 1 through COUNT-1 for some reason.
136 if (template_type < 1 || template_type >= CAMERA3_TEMPLATE_COUNT) {
137 HAL_LOGE("Unrecognized template type %d.", template_type);
138 return -EINVAL;
139 }
140
141 for (auto& component : components_) {
142 // Prevent components from potentially overriding others.
143 android::CameraMetadata additional_metadata;
144 int res =
145 component->PopulateTemplateRequest(template_type, &additional_metadata);
146 if (res) {
147 HAL_LOGE("Failed to get all default request fields.");
148 return res;
149 }
150 // Add it to the overall result.
151 if (!additional_metadata.isEmpty()) {
152 res = template_metadata->append(additional_metadata);
153 if (res != android::OK) {
154 HAL_LOGE("Failed to append all default request fields.");
155 return res;
156 }
157 }
158 }
159
160 // TODO(b/31018853): cache result.
161 return 0;
162}
163
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700164int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700165 HAL_LOG_ENTER();
166
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700167 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700168 if (metadata.isEmpty())
169 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700170
171 for (auto& component : components_) {
172 int res = component->SetRequestValues(metadata);
173 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700174 HAL_LOGE("Failed to set all requested settings.");
175 return res;
176 }
177 }
178
179 return 0;
180}
181
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700182int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700183 HAL_LOG_ENTER();
184 if (!metadata) {
185 HAL_LOGE("Can't fill null metadata.");
186 return -EINVAL;
187 }
188
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700189 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700190 // Prevent components from potentially overriding others.
191 android::CameraMetadata additional_metadata;
192 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700193 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700194 HAL_LOGE("Failed to get all dynamic result fields.");
195 return res;
196 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700197 // Add it to the overall result.
198 if (!additional_metadata.isEmpty()) {
199 res = metadata->append(additional_metadata);
200 if (res != android::OK) {
201 HAL_LOGE("Failed to append all dynamic result fields.");
202 return res;
203 }
204 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700205 }
206
207 return 0;
208}
209
210} // namespace v4l2_camera_hal