blob: 976a77d16032a1eb02062bcb97a32567b97aa96f [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>
20
21#include "../common.h"
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070022#include "metadata_common.h"
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070023
24namespace v4l2_camera_hal {
25
Ari Hausman-Cohenffdc6282016-08-19 12:54:22 -070026Metadata::Metadata(PartialMetadataSet components)
27 : components_(std::move(components)) {
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070028 HAL_LOG_ENTER();
29}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070030
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070031Metadata::~Metadata() {
32 HAL_LOG_ENTER();
33}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070034
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070035int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070036 HAL_LOG_ENTER();
37
38 std::vector<int32_t> static_tags;
39 std::vector<int32_t> control_tags;
40 std::vector<int32_t> dynamic_tags;
41 int res = 0;
42
43 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070044 // Prevent components from potentially overriding others.
45 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070046 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070047 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070048 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070049 HAL_LOGE("Failed to get all static properties.");
50 return res;
51 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070052 // Add it to the overall result.
53 if (!additional_metadata.isEmpty()) {
54 res = metadata->append(additional_metadata);
55 if (res != android::OK) {
56 HAL_LOGE("Failed to append all static properties.");
57 return res;
58 }
59 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070060
61 // Note what tags the component adds.
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070062 std::vector<int32_t> tags = component->StaticTags();
63 std::move(tags.begin(),
64 tags.end(),
65 std::inserter(static_tags, static_tags.end()));
66 tags = component->ControlTags();
67 std::move(tags.begin(),
68 tags.end(),
69 std::inserter(control_tags, control_tags.end()));
70 tags = component->DynamicTags();
71 std::move(tags.begin(),
72 tags.end(),
73 std::inserter(dynamic_tags, dynamic_tags.end()));
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070074 }
75
76 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070077 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070078 res = UpdateMetadata(
79 metadata, ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, control_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070080 if (res != android::OK) {
81 HAL_LOGE("Failed to add request keys meta key.");
82 return -ENODEV;
83 }
84 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070085 res = UpdateMetadata(
86 metadata, ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, dynamic_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070087 if (res != android::OK) {
88 HAL_LOGE("Failed to add result keys meta key.");
89 return -ENODEV;
90 }
91 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070092 res = UpdateMetadata(
93 metadata, ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, static_tags);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070094 if (res != android::OK) {
95 HAL_LOGE("Failed to add characteristics keys meta key.");
96 return -ENODEV;
97 }
98
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070099 return 0;
100}
101
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700102bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700103 HAL_LOG_ENTER();
104
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700105 // Empty means "use previous settings", which are inherently valid.
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700106 if (metadata.isEmpty())
107 return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700108
109 for (auto& component : components_) {
110 // Check that all components support the values requested of them.
111 bool valid_request = component->SupportsRequestValues(metadata);
112 if (!valid_request) {
113 // Exit early if possible.
114 return false;
115 }
116 }
117
118 return true;
119}
120
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700121int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700122 HAL_LOG_ENTER();
123
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700124 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700125 if (metadata.isEmpty())
126 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700127
128 for (auto& component : components_) {
129 int res = component->SetRequestValues(metadata);
130 if (res) {
131 // Exit early if possible.
132 HAL_LOGE("Failed to set all requested settings.");
133 return res;
134 }
135 }
136
137 return 0;
138}
139
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700140int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700141 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700142 // Prevent components from potentially overriding others.
143 android::CameraMetadata additional_metadata;
144 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700145 if (res) {
146 // Exit early if possible.
147 HAL_LOGE("Failed to get all dynamic result fields.");
148 return res;
149 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700150 // Add it to the overall result.
151 if (!additional_metadata.isEmpty()) {
152 res = metadata->append(additional_metadata);
153 if (res != android::OK) {
154 HAL_LOGE("Failed to append all dynamic result fields.");
155 return res;
156 }
157 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700158 }
159
160 return 0;
161}
162
163} // namespace v4l2_camera_hal