blob: 199ecd0413838b3e5ff6cf4e6f61487fd94b7232 [file] [log] [blame]
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -07001/*
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-Cohenfd0ecb72016-08-12 15:45:25 -070025#include "menu_control_options.h"
Ari Hausman-Cohene55f0c72016-08-05 15:49:22 -070026#include "metadata_common.h"
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070027#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-Cohen6c63b502016-08-02 11:28:29 -070031
32namespace v4l2_camera_hal {
33
34// A Control is a PartialMetadata with values that can be gotten/set.
35template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070036class Control : public PartialMetadataInterface {
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070037 public:
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070038 // 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-Cohen6c63b502016-08-02 11:28:29 -070041
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070042 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-Cohen6c63b502016-08-02 11:28:29 -070048 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-Cohenfd0ecb72016-08-12 15:45:25 -070055 // 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-Cohen6c63b502016-08-02 11:28:29 -070060
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070061 protected:
62 std::unique_ptr<TaggedControlDelegate<T>> delegate_;
63 std::unique_ptr<TaggedControlOptions<T>> options_;
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070064
65 DISALLOW_COPY_AND_ASSIGN(Control);
66};
67
68// -----------------------------------------------------------------------------
69
70template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070071Control<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-Cohen6c63b502016-08-02 11:28:29 -070074 HAL_LOG_ENTER();
75}
76
77template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070078std::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
86template <typename T>
87std::vector<int32_t> Control<T>::ControlTags() const {
88 return {delegate_->tag()};
89}
90
91template <typename T>
92std::vector<int32_t> Control<T>::DynamicTags() const {
93 return {delegate_->tag()};
94}
95
96template <typename T>
97int 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
109template <typename T>
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700110int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const {
111 HAL_LOG_ENTER();
112
113 // Populate the current setting.
114 T value;
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700115 int res = delegate_->GetValue(&value);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700116 if (res) {
117 return res;
118 }
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700119 return UpdateMetadata(metadata, delegate_->tag(), value);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700120}
121
122template <typename T>
123bool 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-Cohenfd0ecb72016-08-12 15:45:25 -0700131 // Get the requested setting for this control.
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700132 T requested;
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700133 int res = SingleTagValue(metadata, delegate_->tag(), &requested);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700134 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-Cohenfd0ecb72016-08-12 15:45:25 -0700139 delegate_->tag());
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700140 return false;
141 }
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700142
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-Cohen6c63b502016-08-02 11:28:29 -0700150}
151
152template <typename T>
153int 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-Cohenfd0ecb72016-08-12 15:45:25 -0700162 int res = SingleTagValue(metadata, delegate_->tag(), &requested);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700163 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-Cohenfd0ecb72016-08-12 15:45:25 -0700168 delegate_->tag());
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700169 return res;
170 }
171
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700172 // 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
181template <typename T>
182std::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-Cohen6c63b502016-08-02 11:28:29 -0700197}
198
199} // namespace v4l2_camera_hal
200
201#endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_