blob: ddd73c15781106ac96e96691c0e81dc23b12ef92 [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();
38
39 std::vector<int32_t> static_tags;
40 std::vector<int32_t> control_tags;
41 std::vector<int32_t> dynamic_tags;
42 int res = 0;
43
44 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070045 // Prevent components from potentially overriding others.
46 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070047 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070048 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070049 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070050 HAL_LOGE("Failed to get all static properties.");
51 return res;
52 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070053 // Add it to the overall result.
54 if (!additional_metadata.isEmpty()) {
55 res = metadata->append(additional_metadata);
56 if (res != android::OK) {
57 HAL_LOGE("Failed to append all static properties.");
58 return res;
59 }
60 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070061
62 // Note what tags the component adds.
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070063 std::vector<int32_t> tags = component->StaticTags();
64 std::move(tags.begin(),
65 tags.end(),
66 std::inserter(static_tags, static_tags.end()));
67 tags = component->ControlTags();
68 std::move(tags.begin(),
69 tags.end(),
70 std::inserter(control_tags, control_tags.end()));
71 tags = component->DynamicTags();
72 std::move(tags.begin(),
73 tags.end(),
74 std::inserter(dynamic_tags, dynamic_tags.end()));
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070075 }
76
77 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070078 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070079 res = UpdateMetadata(
80 metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070081 if (res != android::OK) {
82 HAL_LOGE("Failed to add request keys meta key.");
83 return -ENODEV;
84 }
85 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070086 res = UpdateMetadata(
87 metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070088 if (res != android::OK) {
89 HAL_LOGE("Failed to add result keys meta key.");
90 return -ENODEV;
91 }
92 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070093 res = UpdateMetadata(
94 metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070095 if (res != android::OK) {
96 HAL_LOGE("Failed to add characteristics keys meta key.");
97 return -ENODEV;
98 }
99
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700100 // TODO(b/31018853): cache result.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700101 return 0;
102}
103
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700104bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700105 HAL_LOG_ENTER();
106
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700107 // Empty means "use previous settings", which are inherently valid.
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700108 if (metadata.isEmpty())
109 return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700110
111 for (auto& component : components_) {
112 // Check that all components support the values requested of them.
113 bool valid_request = component->SupportsRequestValues(metadata);
114 if (!valid_request) {
115 // Exit early if possible.
116 return false;
117 }
118 }
119
120 return true;
121}
122
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700123int Metadata::GetRequestTemplate(int template_type,
124 android::CameraMetadata* template_metadata) {
125 HAL_LOG_ENTER();
126
127 // Templates are numbered 1 through COUNT-1 for some reason.
128 if (template_type < 1 || template_type >= CAMERA3_TEMPLATE_COUNT) {
129 HAL_LOGE("Unrecognized template type %d.", template_type);
130 return -EINVAL;
131 }
132
133 for (auto& component : components_) {
134 // Prevent components from potentially overriding others.
135 android::CameraMetadata additional_metadata;
136 int res =
137 component->PopulateTemplateRequest(template_type, &additional_metadata);
138 if (res) {
139 HAL_LOGE("Failed to get all default request fields.");
140 return res;
141 }
142 // Add it to the overall result.
143 if (!additional_metadata.isEmpty()) {
144 res = template_metadata->append(additional_metadata);
145 if (res != android::OK) {
146 HAL_LOGE("Failed to append all default request fields.");
147 return res;
148 }
149 }
150 }
151
152 // TODO(b/31018853): cache result.
153 return 0;
154}
155
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700156int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700157 HAL_LOG_ENTER();
158
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700159 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700160 if (metadata.isEmpty())
161 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700162
163 for (auto& component : components_) {
164 int res = component->SetRequestValues(metadata);
165 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700166 HAL_LOGE("Failed to set all requested settings.");
167 return res;
168 }
169 }
170
171 return 0;
172}
173
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700174int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700175 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700176 // Prevent components from potentially overriding others.
177 android::CameraMetadata additional_metadata;
178 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700179 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700180 HAL_LOGE("Failed to get all dynamic result fields.");
181 return res;
182 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700183 // Add it to the overall result.
184 if (!additional_metadata.isEmpty()) {
185 res = metadata->append(additional_metadata);
186 if (res != android::OK) {
187 HAL_LOGE("Failed to append all dynamic result fields.");
188 return res;
189 }
190 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700191 }
192
193 return 0;
194}
195
196} // namespace v4l2_camera_hal