blob: c5106c710155fc6b53722e07bd0b53fe21be9113 [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
Sergii Piatakov2ad591f2018-08-03 11:41:06 +030017//#define LOG_NDEBUG 0
18#define LOG_TAG "Metadata"
19
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070020#include "metadata.h"
21
22#include <camera/CameraMetadata.h>
Ari Hausman-Cohen10264772016-08-22 13:49:43 -070023#include <hardware/camera3.h>
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070024
25#include "../common.h"
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070026#include "metadata_common.h"
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070027
28namespace v4l2_camera_hal {
29
Ari Hausman-Cohenffdc6282016-08-19 12:54:22 -070030Metadata::Metadata(PartialMetadataSet components)
31 : components_(std::move(components)) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070032 HAL_LOG_ENTER();
33}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070034
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070035Metadata::~Metadata() {
36 HAL_LOG_ENTER();
37}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070038
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070039int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070040 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070041 if (!metadata) {
42 HAL_LOGE("Can't fill null metadata.");
43 return -EINVAL;
44 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070045
46 std::vector<int32_t> static_tags;
47 std::vector<int32_t> control_tags;
48 std::vector<int32_t> dynamic_tags;
49 int res = 0;
50
51 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070052 // Prevent components from potentially overriding others.
53 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070054 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070055 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070056 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070057 HAL_LOGE("Failed to get all static properties.");
58 return res;
59 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070060 // Add it to the overall result.
61 if (!additional_metadata.isEmpty()) {
62 res = metadata->append(additional_metadata);
63 if (res != android::OK) {
64 HAL_LOGE("Failed to append all static properties.");
65 return res;
66 }
67 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070068
69 // Note what tags the component adds.
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070070 std::vector<int32_t> tags = component->StaticTags();
71 std::move(tags.begin(),
72 tags.end(),
73 std::inserter(static_tags, static_tags.end()));
74 tags = component->ControlTags();
75 std::move(tags.begin(),
76 tags.end(),
77 std::inserter(control_tags, control_tags.end()));
78 tags = component->DynamicTags();
79 std::move(tags.begin(),
80 tags.end(),
81 std::inserter(dynamic_tags, dynamic_tags.end()));
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070082 }
83
84 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070085 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070086 res = UpdateMetadata(
87 metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070088 if (res != android::OK) {
89 HAL_LOGE("Failed to add request keys meta key.");
90 return -ENODEV;
91 }
92 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070093 res = UpdateMetadata(
94 metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070095 if (res != android::OK) {
96 HAL_LOGE("Failed to add result keys meta key.");
97 return -ENODEV;
98 }
99 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700100 res = UpdateMetadata(
101 metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700102 if (res != android::OK) {
103 HAL_LOGE("Failed to add characteristics keys meta key.");
104 return -ENODEV;
105 }
106
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700107 // TODO(b/31018853): cache result.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700108 return 0;
109}
110
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700111bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700112 HAL_LOG_ENTER();
113
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700114 // Empty means "use previous settings", which are inherently valid.
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700115 if (metadata.isEmpty())
116 return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700117
118 for (auto& component : components_) {
119 // Check that all components support the values requested of them.
120 bool valid_request = component->SupportsRequestValues(metadata);
121 if (!valid_request) {
122 // Exit early if possible.
123 return false;
124 }
125 }
126
127 return true;
128}
129
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700130int Metadata::GetRequestTemplate(int template_type,
131 android::CameraMetadata* template_metadata) {
132 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700133 if (!template_metadata) {
134 HAL_LOGE("Can't fill null template.");
135 return -EINVAL;
136 }
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700137
138 // Templates are numbered 1 through COUNT-1 for some reason.
139 if (template_type < 1 || template_type >= CAMERA3_TEMPLATE_COUNT) {
140 HAL_LOGE("Unrecognized template type %d.", template_type);
141 return -EINVAL;
142 }
143
144 for (auto& component : components_) {
145 // Prevent components from potentially overriding others.
146 android::CameraMetadata additional_metadata;
147 int res =
148 component->PopulateTemplateRequest(template_type, &additional_metadata);
149 if (res) {
150 HAL_LOGE("Failed to get all default request fields.");
151 return res;
152 }
153 // Add it to the overall result.
154 if (!additional_metadata.isEmpty()) {
155 res = template_metadata->append(additional_metadata);
156 if (res != android::OK) {
157 HAL_LOGE("Failed to append all default request fields.");
158 return res;
159 }
160 }
161 }
162
163 // TODO(b/31018853): cache result.
164 return 0;
165}
166
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700167int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700168 HAL_LOG_ENTER();
169
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700170 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700171 if (metadata.isEmpty())
172 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700173
174 for (auto& component : components_) {
175 int res = component->SetRequestValues(metadata);
176 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700177 HAL_LOGE("Failed to set all requested settings.");
178 return res;
179 }
180 }
181
182 return 0;
183}
184
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700185int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700186 HAL_LOG_ENTER();
187 if (!metadata) {
188 HAL_LOGE("Can't fill null metadata.");
189 return -EINVAL;
190 }
191
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700192 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700193 // Prevent components from potentially overriding others.
194 android::CameraMetadata additional_metadata;
195 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700196 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700197 HAL_LOGE("Failed to get all dynamic result fields.");
198 return res;
199 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700200 // Add it to the overall result.
201 if (!additional_metadata.isEmpty()) {
202 res = metadata->append(additional_metadata);
203 if (res != android::OK) {
204 HAL_LOGE("Failed to append all dynamic result fields.");
205 return res;
206 }
207 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700208 }
209
210 return 0;
211}
212
213} // namespace v4l2_camera_hal