Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #ifndef V4L2_CAMERA_HAL_METADATA_CONTROL_H_ |
| 18 | #define V4L2_CAMERA_HAL_METADATA_CONTROL_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <system/camera_metadata.h> |
| 23 | |
| 24 | #include "../common.h" |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 25 | #include "menu_control_options.h" |
Ari Hausman-Cohen | e55f0c7 | 2016-08-05 15:49:22 -0700 | [diff] [blame] | 26 | #include "metadata_common.h" |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 27 | #include "no_effect_control_delegate.h" |
| 28 | #include "partial_metadata_interface.h" |
| 29 | #include "tagged_control_delegate.h" |
| 30 | #include "tagged_control_options.h" |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 31 | |
| 32 | namespace v4l2_camera_hal { |
| 33 | |
| 34 | // A Control is a PartialMetadata with values that can be gotten/set. |
| 35 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 36 | class Control : public PartialMetadataInterface { |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 37 | public: |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 38 | // Options are optional (i.e. nullable), delegate is not. |
| 39 | Control(std::unique_ptr<TaggedControlDelegate<T>> delegate, |
| 40 | std::unique_ptr<TaggedControlOptions<T>> options = nullptr); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 41 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 42 | virtual std::vector<int32_t> StaticTags() const override; |
| 43 | virtual std::vector<int32_t> ControlTags() const override; |
| 44 | virtual std::vector<int32_t> DynamicTags() const override; |
| 45 | |
| 46 | virtual int PopulateStaticFields( |
| 47 | android::CameraMetadata* metadata) const override; |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 48 | virtual int PopulateDynamicFields( |
| 49 | android::CameraMetadata* metadata) const override; |
| 50 | virtual bool SupportsRequestValues( |
| 51 | const android::CameraMetadata& metadata) const override; |
| 52 | virtual int SetRequestValues( |
| 53 | const android::CameraMetadata& metadata) override; |
| 54 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 55 | // Factory methods for some common combinations of delegates & options. |
| 56 | // NoEffectMenuControl: Some menu options, but they have no effect. |
| 57 | // The default value will be the first element of |options|. |
| 58 | static std::unique_ptr<Control<T>> NoEffectMenuControl( |
| 59 | int32_t delegate_tag, int32_t options_tag, const std::vector<T>& options); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 60 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 61 | protected: |
| 62 | std::unique_ptr<TaggedControlDelegate<T>> delegate_; |
| 63 | std::unique_ptr<TaggedControlOptions<T>> options_; |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(Control); |
| 66 | }; |
| 67 | |
| 68 | // ----------------------------------------------------------------------------- |
| 69 | |
| 70 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 71 | Control<T>::Control(std::unique_ptr<TaggedControlDelegate<T>> delegate, |
| 72 | std::unique_ptr<TaggedControlOptions<T>> options) |
| 73 | : delegate_(std::move(delegate)), options_(std::move(options)) { |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 74 | HAL_LOG_ENTER(); |
| 75 | } |
| 76 | |
| 77 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 78 | std::vector<int32_t> Control<T>::StaticTags() const { |
| 79 | std::vector<int32_t> result; |
| 80 | if (options_) { |
| 81 | result.push_back(options_->tag()); |
| 82 | } |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | template <typename T> |
| 87 | std::vector<int32_t> Control<T>::ControlTags() const { |
| 88 | return {delegate_->tag()}; |
| 89 | } |
| 90 | |
| 91 | template <typename T> |
| 92 | std::vector<int32_t> Control<T>::DynamicTags() const { |
| 93 | return {delegate_->tag()}; |
| 94 | } |
| 95 | |
| 96 | template <typename T> |
| 97 | int Control<T>::PopulateStaticFields(android::CameraMetadata* metadata) const { |
| 98 | HAL_LOG_ENTER(); |
| 99 | |
| 100 | if (!options_) { |
| 101 | HAL_LOGV("No options for control, nothing to populate."); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | return UpdateMetadata( |
| 106 | metadata, options_->tag(), options_->MetadataRepresentation()); |
| 107 | } |
| 108 | |
| 109 | template <typename T> |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 110 | int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const { |
| 111 | HAL_LOG_ENTER(); |
| 112 | |
| 113 | // Populate the current setting. |
| 114 | T value; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 115 | int res = delegate_->GetValue(&value); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 116 | if (res) { |
| 117 | return res; |
| 118 | } |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 119 | return UpdateMetadata(metadata, delegate_->tag(), value); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | template <typename T> |
| 123 | bool Control<T>::SupportsRequestValues( |
| 124 | const android::CameraMetadata& metadata) const { |
| 125 | HAL_LOG_ENTER(); |
| 126 | if (metadata.isEmpty()) { |
| 127 | // Implicitly supported. |
| 128 | return true; |
| 129 | } |
| 130 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 131 | // Get the requested setting for this control. |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 132 | T requested; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 133 | int res = SingleTagValue(metadata, delegate_->tag(), &requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 134 | if (res == -ENOENT) { |
| 135 | // Nothing requested of this control, that's fine. |
| 136 | return true; |
| 137 | } else if (res) { |
| 138 | HAL_LOGE("Failure while searching for request value for tag %d", |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 139 | delegate_->tag()); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 142 | |
| 143 | // Check that the requested setting is in the supported options. |
| 144 | if (!options_) { |
| 145 | HAL_LOGV("No options for control %d; request implicitly supported.", |
| 146 | delegate_->tag()); |
| 147 | return true; |
| 148 | } |
| 149 | return options_->IsSupported(requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | template <typename T> |
| 153 | int Control<T>::SetRequestValues(const android::CameraMetadata& metadata) { |
| 154 | HAL_LOG_ENTER(); |
| 155 | if (metadata.isEmpty()) { |
| 156 | // No changes necessary. |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | // Get the requested value. |
| 161 | T requested; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 162 | int res = SingleTagValue(metadata, delegate_->tag(), &requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 163 | if (res == -ENOENT) { |
| 164 | // Nothing requested of this control, nothing to do. |
| 165 | return 0; |
| 166 | } else if (res) { |
| 167 | HAL_LOGE("Failure while searching for request value for tag %d", |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 168 | delegate_->tag()); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 169 | return res; |
| 170 | } |
| 171 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame^] | 172 | // Check that the value is supported. |
| 173 | if (options_ && !options_->IsSupported(requested)) { |
| 174 | HAL_LOGE("Unsupported value requested for control %d.", delegate_->tag()); |
| 175 | return -EINVAL; |
| 176 | } |
| 177 | |
| 178 | return delegate_->SetValue(requested); |
| 179 | } |
| 180 | |
| 181 | template <typename T> |
| 182 | std::unique_ptr<Control<T>> Control<T>::NoEffectMenuControl( |
| 183 | int32_t delegate_tag, int32_t options_tag, const std::vector<T>& options) { |
| 184 | HAL_LOG_ENTER(); |
| 185 | |
| 186 | if (options.empty()) { |
| 187 | HAL_LOGE("At least one option must be provided."); |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | return std::make_unique<Control<T>>( |
| 192 | std::make_unique<TaggedControlDelegate<T>>( |
| 193 | delegate_tag, |
| 194 | std::make_unique<NoEffectControlDelegate<T>>(options[0])), |
| 195 | std::make_unique<TaggedControlOptions<T>>( |
| 196 | options_tag, std::make_unique<MenuControlOptions<T>>(options))); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // namespace v4l2_camera_hal |
| 200 | |
| 201 | #endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_ |