blob: ed86d39d7252f035969b7333a6ac33225d87ea97 [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-Cohene55f0c72016-08-05 15:49:22 -070025#include "metadata_common.h"
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070026#include "partial_metadata_interface.h"
27#include "tagged_control_delegate.h"
28#include "tagged_control_options.h"
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070029
30namespace v4l2_camera_hal {
31
32// A Control is a PartialMetadata with values that can be gotten/set.
33template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070034class Control : public PartialMetadataInterface {
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070035 public:
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070036 // 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-Cohen6c63b502016-08-02 11:28:29 -070039
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070040 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-Cohen6c63b502016-08-02 11:28:29 -070046 virtual int PopulateDynamicFields(
47 android::CameraMetadata* metadata) const override;
Ari Hausman-Cohen10264772016-08-22 13:49:43 -070048 virtual int PopulateTemplateRequest(
49 int template_type, android::CameraMetadata* metadata) const override;
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070050 virtual bool SupportsRequestValues(
51 const android::CameraMetadata& metadata) const override;
52 virtual int SetRequestValues(
53 const android::CameraMetadata& metadata) override;
54
Ari Hausman-Cohen6cd3fe92016-08-17 14:00:03 -070055 private:
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070056 std::unique_ptr<TaggedControlDelegate<T>> delegate_;
57 std::unique_ptr<TaggedControlOptions<T>> options_;
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070058
59 DISALLOW_COPY_AND_ASSIGN(Control);
60};
61
62// -----------------------------------------------------------------------------
63
64template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070065Control<T>::Control(std::unique_ptr<TaggedControlDelegate<T>> delegate,
66 std::unique_ptr<TaggedControlOptions<T>> options)
Ari Hausman-Cohenad6fe2b2016-11-16 10:48:07 -080067 : delegate_(std::move(delegate)), options_(std::move(options)) {}
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070068
69template <typename T>
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070070std::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
78template <typename T>
79std::vector<int32_t> Control<T>::ControlTags() const {
80 return {delegate_->tag()};
81}
82
83template <typename T>
84std::vector<int32_t> Control<T>::DynamicTags() const {
85 return {delegate_->tag()};
86}
87
88template <typename T>
89int Control<T>::PopulateStaticFields(android::CameraMetadata* metadata) const {
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -070090 if (!options_) {
91 HAL_LOGV("No options for control, nothing to populate.");
92 return 0;
93 }
94
95 return UpdateMetadata(
96 metadata, options_->tag(), options_->MetadataRepresentation());
97}
98
99template <typename T>
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700100int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const {
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700101 // Populate the current setting.
102 T value;
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700103 int res = delegate_->GetValue(&value);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700104 if (res) {
105 return res;
106 }
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700107 return UpdateMetadata(metadata, delegate_->tag(), value);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700108}
109
110template <typename T>
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700111int Control<T>::PopulateTemplateRequest(
112 int template_type, android::CameraMetadata* metadata) const {
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700113 // Populate with a default.
114 T value;
Ari Hausman-Cohen784bc682016-08-22 18:36:19 -0700115 int res;
116 if (options_) {
117 res = options_->DefaultValueForTemplate(template_type, &value);
118 } else {
119 // If there's no options (and thus no default option),
120 // fall back to whatever the current value is.
121 res = delegate_->GetValue(&value);
122 }
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700123 if (res) {
124 return res;
125 }
Ari Hausman-Cohen784bc682016-08-22 18:36:19 -0700126
Ari Hausman-Cohen10264772016-08-22 13:49:43 -0700127 return UpdateMetadata(metadata, delegate_->tag(), value);
128}
129
130template <typename T>
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700131bool Control<T>::SupportsRequestValues(
132 const android::CameraMetadata& metadata) const {
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700133 if (metadata.isEmpty()) {
134 // Implicitly supported.
135 return true;
136 }
137
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700138 // Get the requested setting for this control.
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700139 T requested;
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700140 int res = SingleTagValue(metadata, delegate_->tag(), &requested);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700141 if (res == -ENOENT) {
142 // Nothing requested of this control, that's fine.
143 return true;
144 } else if (res) {
145 HAL_LOGE("Failure while searching for request value for tag %d",
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700146 delegate_->tag());
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700147 return false;
148 }
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700149
150 // Check that the requested setting is in the supported options.
151 if (!options_) {
152 HAL_LOGV("No options for control %d; request implicitly supported.",
153 delegate_->tag());
154 return true;
155 }
156 return options_->IsSupported(requested);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700157}
158
159template <typename T>
160int Control<T>::SetRequestValues(const android::CameraMetadata& metadata) {
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700161 if (metadata.isEmpty()) {
162 // No changes necessary.
163 return 0;
164 }
165
166 // Get the requested value.
167 T requested;
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700168 int res = SingleTagValue(metadata, delegate_->tag(), &requested);
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700169 if (res == -ENOENT) {
170 // Nothing requested of this control, nothing to do.
171 return 0;
172 } else if (res) {
173 HAL_LOGE("Failure while searching for request value for tag %d",
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700174 delegate_->tag());
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700175 return res;
176 }
177
Ari Hausman-Cohenfd0ecb72016-08-12 15:45:25 -0700178 // Check that the value is supported.
179 if (options_ && !options_->IsSupported(requested)) {
180 HAL_LOGE("Unsupported value requested for control %d.", delegate_->tag());
181 return -EINVAL;
182 }
183
184 return delegate_->SetValue(requested);
185}
186
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -0700187} // namespace v4l2_camera_hal
188
189#endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_