blob: e18a4540d8c1aee69d4be8aa7ff80b59f95c138e [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-Cohen5d753232016-08-10 14:27:36 -070026Metadata::Metadata() {
27 HAL_LOG_ENTER();
28}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070029
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070030Metadata::~Metadata() {
31 HAL_LOG_ENTER();
32}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070033
34void Metadata::AddComponent(
35 std::unique_ptr<PartialMetadataInterface> component) {
36 HAL_LOG_ENTER();
37
38 components_.push_back(std::move(component));
39}
40
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070041int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070042 HAL_LOG_ENTER();
43
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-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-Cohenb1af4ff2016-08-04 13:01:05 -0700127int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700128 HAL_LOG_ENTER();
129
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700130 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700131 if (metadata.isEmpty())
132 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700133
134 for (auto& component : components_) {
135 int res = component->SetRequestValues(metadata);
136 if (res) {
137 // Exit early if possible.
138 HAL_LOGE("Failed to set all requested settings.");
139 return res;
140 }
141 }
142
143 return 0;
144}
145
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700146int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700147 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700148 // Prevent components from potentially overriding others.
149 android::CameraMetadata additional_metadata;
150 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700151 if (res) {
152 // Exit early if possible.
153 HAL_LOGE("Failed to get all dynamic result fields.");
154 return res;
155 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700156 // Add it to the overall result.
157 if (!additional_metadata.isEmpty()) {
158 res = metadata->append(additional_metadata);
159 if (res != android::OK) {
160 HAL_LOGE("Failed to append all dynamic result fields.");
161 return res;
162 }
163 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700164 }
165
166 return 0;
167}
168
169} // namespace v4l2_camera_hal