blob: 61336fe2b665728b7b7fe7b07c26bdeeb88b116b [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"
22
23namespace v4l2_camera_hal {
24
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070025Metadata::Metadata() {
26 HAL_LOG_ENTER();
27}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070028
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070029Metadata::~Metadata() {
30 HAL_LOG_ENTER();
31}
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070032
33void Metadata::AddComponent(
34 std::unique_ptr<PartialMetadataInterface> component) {
35 HAL_LOG_ENTER();
36
37 components_.push_back(std::move(component));
38}
39
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070040int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070041 HAL_LOG_ENTER();
42
43 std::vector<int32_t> static_tags;
44 std::vector<int32_t> control_tags;
45 std::vector<int32_t> dynamic_tags;
46 int res = 0;
47
48 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070049 // Prevent components from potentially overriding others.
50 android::CameraMetadata additional_metadata;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070051 // Populate the fields.
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070052 res = component->PopulateStaticFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070053 if (res) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070054 HAL_LOGE("Failed to get all static properties.");
55 return res;
56 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070057 // Add it to the overall result.
58 if (!additional_metadata.isEmpty()) {
59 res = metadata->append(additional_metadata);
60 if (res != android::OK) {
61 HAL_LOGE("Failed to append all static properties.");
62 return res;
63 }
64 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070065
66 // Note what tags the component adds.
67 const std::vector<int32_t>* tags = &component->StaticTags();
68 static_tags.insert(static_tags.end(), tags->begin(), tags->end());
69 tags = &component->ControlTags();
70 control_tags.insert(control_tags.end(), tags->begin(), tags->end());
71 tags = &component->DynamicTags();
72 dynamic_tags.insert(dynamic_tags.end(), tags->begin(), tags->end());
73 }
74
75 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070076 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070077 res = metadata->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070078 control_tags.data(),
79 control_tags.size());
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-Cohenb1af4ff2016-08-04 13:01:05 -070085 res = metadata->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070086 dynamic_tags.data(),
87 dynamic_tags.size());
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-Cohenb1af4ff2016-08-04 13:01:05 -070093 res = metadata->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -070094 static_tags.data(),
95 static_tags.size());
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070096 if (res != android::OK) {
97 HAL_LOGE("Failed to add characteristics keys meta key.");
98 return -ENODEV;
99 }
100
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-Cohenb1af4ff2016-08-04 13:01:05 -0700123int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700124 HAL_LOG_ENTER();
125
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700126 // Empty means "use previous settings".
Ari Hausman-Cohen5d753232016-08-10 14:27:36 -0700127 if (metadata.isEmpty())
128 return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700129
130 for (auto& component : components_) {
131 int res = component->SetRequestValues(metadata);
132 if (res) {
133 // Exit early if possible.
134 HAL_LOGE("Failed to set all requested settings.");
135 return res;
136 }
137 }
138
139 return 0;
140}
141
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700142int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700143 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700144 // Prevent components from potentially overriding others.
145 android::CameraMetadata additional_metadata;
146 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700147 if (res) {
148 // Exit early if possible.
149 HAL_LOGE("Failed to get all dynamic result fields.");
150 return res;
151 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700152 // Add it to the overall result.
153 if (!additional_metadata.isEmpty()) {
154 res = metadata->append(additional_metadata);
155 if (res != android::OK) {
156 HAL_LOGE("Failed to append all dynamic result fields.");
157 return res;
158 }
159 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700160 }
161
162 return 0;
163}
164
165} // namespace v4l2_camera_hal