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 | e55f0c7 | 2016-08-05 15:49:22 -0700 | [diff] [blame] | 25 | #include "metadata_common.h" |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 26 | #include "partial_metadata_interface.h" |
| 27 | #include "tagged_control_delegate.h" |
| 28 | #include "tagged_control_options.h" |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 29 | |
| 30 | namespace v4l2_camera_hal { |
| 31 | |
| 32 | // A Control is a PartialMetadata with values that can be gotten/set. |
| 33 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 34 | class Control : public PartialMetadataInterface { |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 35 | public: |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 36 | // Options are optional (i.e. nullable), delegate is not. |
| 37 | Control(std::unique_ptr<TaggedControlDelegate<T>> delegate, |
| 38 | std::unique_ptr<TaggedControlOptions<T>> options = nullptr); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 39 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 40 | virtual std::vector<int32_t> StaticTags() const override; |
| 41 | virtual std::vector<int32_t> ControlTags() const override; |
| 42 | virtual std::vector<int32_t> DynamicTags() const override; |
| 43 | |
| 44 | virtual int PopulateStaticFields( |
| 45 | android::CameraMetadata* metadata) const override; |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 46 | virtual int PopulateDynamicFields( |
| 47 | android::CameraMetadata* metadata) const override; |
| 48 | virtual bool SupportsRequestValues( |
| 49 | const android::CameraMetadata& metadata) const override; |
| 50 | virtual int SetRequestValues( |
| 51 | const android::CameraMetadata& metadata) override; |
| 52 | |
Ari Hausman-Cohen | 6cd3fe9 | 2016-08-17 14:00:03 -0700 | [diff] [blame] | 53 | private: |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 54 | std::unique_ptr<TaggedControlDelegate<T>> delegate_; |
| 55 | std::unique_ptr<TaggedControlOptions<T>> options_; |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(Control); |
| 58 | }; |
| 59 | |
| 60 | // ----------------------------------------------------------------------------- |
| 61 | |
| 62 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 63 | Control<T>::Control(std::unique_ptr<TaggedControlDelegate<T>> delegate, |
| 64 | std::unique_ptr<TaggedControlOptions<T>> options) |
| 65 | : delegate_(std::move(delegate)), options_(std::move(options)) { |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 66 | HAL_LOG_ENTER(); |
| 67 | } |
| 68 | |
| 69 | template <typename T> |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 70 | std::vector<int32_t> Control<T>::StaticTags() const { |
| 71 | std::vector<int32_t> result; |
| 72 | if (options_) { |
| 73 | result.push_back(options_->tag()); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | template <typename T> |
| 79 | std::vector<int32_t> Control<T>::ControlTags() const { |
| 80 | return {delegate_->tag()}; |
| 81 | } |
| 82 | |
| 83 | template <typename T> |
| 84 | std::vector<int32_t> Control<T>::DynamicTags() const { |
| 85 | return {delegate_->tag()}; |
| 86 | } |
| 87 | |
| 88 | template <typename T> |
| 89 | int Control<T>::PopulateStaticFields(android::CameraMetadata* metadata) const { |
| 90 | HAL_LOG_ENTER(); |
| 91 | |
| 92 | if (!options_) { |
| 93 | HAL_LOGV("No options for control, nothing to populate."); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | return UpdateMetadata( |
| 98 | metadata, options_->tag(), options_->MetadataRepresentation()); |
| 99 | } |
| 100 | |
| 101 | template <typename T> |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 102 | int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const { |
| 103 | HAL_LOG_ENTER(); |
| 104 | |
| 105 | // Populate the current setting. |
| 106 | T value; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 107 | int res = delegate_->GetValue(&value); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 108 | if (res) { |
| 109 | return res; |
| 110 | } |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 111 | return UpdateMetadata(metadata, delegate_->tag(), value); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | template <typename T> |
| 115 | bool Control<T>::SupportsRequestValues( |
| 116 | const android::CameraMetadata& metadata) const { |
| 117 | HAL_LOG_ENTER(); |
| 118 | if (metadata.isEmpty()) { |
| 119 | // Implicitly supported. |
| 120 | return true; |
| 121 | } |
| 122 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 123 | // Get the requested setting for this control. |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 124 | T requested; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 125 | int res = SingleTagValue(metadata, delegate_->tag(), &requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 126 | if (res == -ENOENT) { |
| 127 | // Nothing requested of this control, that's fine. |
| 128 | return true; |
| 129 | } else if (res) { |
| 130 | HAL_LOGE("Failure while searching for request value for tag %d", |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 131 | delegate_->tag()); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 134 | |
| 135 | // Check that the requested setting is in the supported options. |
| 136 | if (!options_) { |
| 137 | HAL_LOGV("No options for control %d; request implicitly supported.", |
| 138 | delegate_->tag()); |
| 139 | return true; |
| 140 | } |
| 141 | return options_->IsSupported(requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | template <typename T> |
| 145 | int Control<T>::SetRequestValues(const android::CameraMetadata& metadata) { |
| 146 | HAL_LOG_ENTER(); |
| 147 | if (metadata.isEmpty()) { |
| 148 | // No changes necessary. |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | // Get the requested value. |
| 153 | T requested; |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 154 | int res = SingleTagValue(metadata, delegate_->tag(), &requested); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 155 | if (res == -ENOENT) { |
| 156 | // Nothing requested of this control, nothing to do. |
| 157 | return 0; |
| 158 | } else if (res) { |
| 159 | HAL_LOGE("Failure while searching for request value for tag %d", |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 160 | delegate_->tag()); |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 161 | return res; |
| 162 | } |
| 163 | |
Ari Hausman-Cohen | fd0ecb7 | 2016-08-12 15:45:25 -0700 | [diff] [blame] | 164 | // Check that the value is supported. |
| 165 | if (options_ && !options_->IsSupported(requested)) { |
| 166 | HAL_LOGE("Unsupported value requested for control %d.", delegate_->tag()); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
| 170 | return delegate_->SetValue(requested); |
| 171 | } |
| 172 | |
Ari Hausman-Cohen | 6c63b50 | 2016-08-02 11:28:29 -0700 | [diff] [blame] | 173 | } // namespace v4l2_camera_hal |
| 174 | |
| 175 | #endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_ |