Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace v4l2_camera_hal { |
| 24 | |
| 25 | Metadata::Metadata() { HAL_LOG_ENTER(); } |
| 26 | |
| 27 | Metadata::~Metadata() { HAL_LOG_ENTER(); } |
| 28 | |
| 29 | void Metadata::AddComponent( |
| 30 | std::unique_ptr<PartialMetadataInterface> component) { |
| 31 | HAL_LOG_ENTER(); |
| 32 | |
| 33 | components_.push_back(std::move(component)); |
| 34 | } |
| 35 | |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 36 | int Metadata::FillStaticMetadata(android::CameraMetadata* metadata) { |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 37 | 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-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 45 | // Prevent components from potentially overriding others. |
| 46 | android::CameraMetadata additional_metadata; |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 47 | // Populate the fields. |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 48 | res = component->PopulateStaticFields(&additional_metadata); |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 49 | if (res) { |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 50 | HAL_LOGE("Failed to get all static properties."); |
| 51 | return res; |
| 52 | } |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 53 | // 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-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 61 | |
| 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-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 72 | static_tags.push_back(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS); |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 73 | res = metadata->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, |
| 74 | control_tags.data(), control_tags.size()); |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 75 | 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-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 80 | res = metadata->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, |
| 81 | dynamic_tags.data(), dynamic_tags.size()); |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 82 | 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-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 87 | res = metadata->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, |
| 88 | static_tags.data(), static_tags.size()); |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 89 | if (res != android::OK) { |
| 90 | HAL_LOGE("Failed to add characteristics keys meta key."); |
| 91 | return -ENODEV; |
| 92 | } |
| 93 | |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 97 | bool Metadata::IsValidRequest(const android::CameraMetadata& metadata) { |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 98 | HAL_LOG_ENTER(); |
| 99 | |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 100 | // Empty means "use previous settings", which are inherently valid. |
| 101 | if (metadata.isEmpty()) return true; |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 102 | |
| 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-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 115 | int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) { |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 116 | HAL_LOG_ENTER(); |
| 117 | |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 118 | // Empty means "use previous settings". |
| 119 | if (metadata.isEmpty()) return 0; |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 120 | |
| 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-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 133 | int Metadata::FillResultMetadata(android::CameraMetadata* metadata) { |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 134 | for (auto& component : components_) { |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 135 | // Prevent components from potentially overriding others. |
| 136 | android::CameraMetadata additional_metadata; |
| 137 | int res = component->PopulateDynamicFields(&additional_metadata); |
Ari Hausman-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 138 | if (res) { |
| 139 | // Exit early if possible. |
| 140 | HAL_LOGE("Failed to get all dynamic result fields."); |
| 141 | return res; |
| 142 | } |
Ari Hausman-Cohen | b1af4ff | 2016-08-04 13:01:05 -0700 | [diff] [blame^] | 143 | // 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-Cohen | 10481a3 | 2016-08-02 10:41:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | } // namespace v4l2_camera_hal |