blob: 0d03d5f5e8e92f21142c726e8488477fb73f5699 [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
25Metadata::Metadata() { HAL_LOG_ENTER(); }
26
27Metadata::~Metadata() { HAL_LOG_ENTER(); }
28
29void Metadata::AddComponent(
30 std::unique_ptr<PartialMetadataInterface> component) {
31 HAL_LOG_ENTER();
32
33 components_.push_back(std::move(component));
34}
35
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.
63 const std::vector<int32_t>* tags = &component->StaticTags();
64 static_tags.insert(static_tags.end(), tags->begin(), tags->end());
65 tags = &component->ControlTags();
66 control_tags.insert(control_tags.end(), tags->begin(), tags->end());
67 tags = &component->DynamicTags();
68 dynamic_tags.insert(dynamic_tags.end(), tags->begin(), tags->end());
69 }
70
71 // Populate the meta fields.
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070072 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070073 res = metadata->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
74 control_tags.data(), control_tags.size());
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070075 if (res != android::OK) {
76 HAL_LOGE("Failed to add request keys meta key.");
77 return -ENODEV;
78 }
79 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070080 res = metadata->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
81 dynamic_tags.data(), dynamic_tags.size());
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070082 if (res != android::OK) {
83 HAL_LOGE("Failed to add result keys meta key.");
84 return -ENODEV;
85 }
86 static_tags.push_back(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070087 res = metadata->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
88 static_tags.data(), static_tags.size());
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070089 if (res != android::OK) {
90 HAL_LOGE("Failed to add characteristics keys meta key.");
91 return -ENODEV;
92 }
93
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070094 return 0;
95}
96
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -070097bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -070098 HAL_LOG_ENTER();
99
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700100 // Empty means "use previous settings", which are inherently valid.
101 if (metadata.isEmpty()) return true;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700102
103 for (auto& component : components_) {
104 // Check that all components support the values requested of them.
105 bool valid_request = component->SupportsRequestValues(metadata);
106 if (!valid_request) {
107 // Exit early if possible.
108 return false;
109 }
110 }
111
112 return true;
113}
114
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700115int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700116 HAL_LOG_ENTER();
117
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700118 // Empty means "use previous settings".
119 if (metadata.isEmpty()) return 0;
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700120
121 for (auto& component : components_) {
122 int res = component->SetRequestValues(metadata);
123 if (res) {
124 // Exit early if possible.
125 HAL_LOGE("Failed to set all requested settings.");
126 return res;
127 }
128 }
129
130 return 0;
131}
132
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700133int Metadata::FillResultMetadata(android::CameraMetadata* metadata) {
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700134 for (auto& component : components_) {
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700135 // Prevent components from potentially overriding others.
136 android::CameraMetadata additional_metadata;
137 int res = component->PopulateDynamicFields(&additional_metadata);
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700138 if (res) {
139 // Exit early if possible.
140 HAL_LOGE("Failed to get all dynamic result fields.");
141 return res;
142 }
Ari Hausman-Cohenb1af4ff2016-08-04 13:01:05 -0700143 // Add it to the overall result.
144 if (!additional_metadata.isEmpty()) {
145 res = metadata->append(additional_metadata);
146 if (res != android::OK) {
147 HAL_LOGE("Failed to append all dynamic result fields.");
148 return res;
149 }
150 }
Ari Hausman-Cohen10481a32016-08-02 10:41:27 -0700151 }
152
153 return 0;
154}
155
156} // namespace v4l2_camera_hal