blob: 682d78d939c115048a15e10b73804c7640c261dc [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
Ari Hausman-Cohen10264772016-08-22 13:49:43 -070022#include <hardware/camera3.h>
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070023
Sergii Piatakovb8f073f2018-10-10 17:22:10 +030024#include "common.h"
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070025
26namespace v4l2_camera_hal {
27
Ari Hausman-Cohenffdc6282016-08-19 12:54:22 -070028Metadata::Metadata(PartialMetadataSet components)
29 : components_(std::move(components)) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070030 HAL_LOG_ENTER();
31}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070032
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070033Metadata::~Metadata() {
34 HAL_LOG_ENTER();
35}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070036
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070037int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070038 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -070039 if (!metadata) {
40 HAL_LOGE("Can't fill null metadata.");
41 return -EINVAL;
42 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070043
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_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070050 // Prevent components from potentially overriding others.
51 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070052 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070053 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070054 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070055 HAL_LOGE("Failed to get all static properties.");
56 return res;
57 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070058 // 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 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070066
67 // Note what tags the component adds.
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070068 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()));
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070080 }
81
82 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070083 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070084 res = UpdateMetadata(
85 metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070086 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);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070091 res = UpdateMetadata(
92 metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070093 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);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070098 res = UpdateMetadata(
99 metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700100 if (res != android::OK) {
101 HAL_LOGE("Failed to add characteristics keys meta key.");
102 return -ENODEV;
103 }
104
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700105 // TODO(b/31018853): cache result.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700106 return 0;
107}
108
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700109bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700110 HAL_LOG_ENTER();
111
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700112 // Empty means "use previous settings", which are inherently valid.
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700113 if (metadata.isEmpty())
114 return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700115
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
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700128int Metadata::GetRequestTemplate(int template_type,
129 android::CameraMetadata* template_metadata) {
130 HAL_LOG_ENTER();
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700131 if (!template_metadata) {
132 HAL_LOGE("Can't fill null template.");
133 return -EINVAL;
134 }
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700135
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
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700165int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700166 HAL_LOG_ENTER();
167
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700168 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700169 if (metadata.isEmpty())
170 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700171
172 for (auto& component : components_) {
173 int res = component->SetRequestValues(metadata);
174 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700175 HAL_LOGE("Failed to set all requested settings.");
176 return res;
177 }
178 }
179
180 return 0;
181}
182
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700183int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohenabbf9cc2016-08-23 11:59:59 -0700184 HAL_LOG_ENTER();
185 if (!metadata) {
186 HAL_LOGE("Can't fill null metadata.");
187 return -EINVAL;
188 }
189
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700190 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700191 // Prevent components from potentially overriding others.
192 android::CameraMetadata additional_metadata;
193 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700194 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700195 HAL_LOGE("Failed to get all dynamic result fields.");
196 return res;
197 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700198 // 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 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700206 }
207
208 return 0;
209}
210
211} // namespace v4l2_camera_hal