blob: eff8c2fa768746215bee3f1333a9304b7e8f3cfa [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-Cohen6c63b502016-08-02 11:28:29 -070026#include "tagged_partial_metadata.h"
27
28namespace v4l2_camera_hal {
29
30// A Control is a PartialMetadata with values that can be gotten/set.
31template <typename T>
32class Control : public TaggedPartialMetadata {
33 public:
34 Control(int32_t control_tag, std::vector<int32_t> static_tags = {});
35
36 // Child classes still need to implement PopulateStaticFields.
37 virtual int PopulateDynamicFields(
38 android::CameraMetadata* metadata) const override;
39 virtual bool SupportsRequestValues(
40 const android::CameraMetadata& metadata) const override;
41 virtual int SetRequestValues(
42 const android::CameraMetadata& metadata) override;
43
44 protected:
45 // Simpler access to tag.
46 inline int32_t ControlTag() const { return ControlTags()[0]; }
47
48 // Get/Set the control value. Return non-0 on failure.
Ari Hausman-Cohen3a4c3bb2016-08-01 17:16:01 -070049 // Controls are allowed to be unreliable, so SetValue is best-effort;
50 // GetValue immediately after may not match (SetValue may, for example,
51 // automatically replace invalid values with valid ones,
52 // or have a delay before setting the requested value).
53 // GetValue should always indicate the actual current value of the control.
Ari Hausman-Cohen6c63b502016-08-02 11:28:29 -070054 virtual int GetValue(T* value) const = 0;
55 virtual int SetValue(const T& value) = 0;
56 // Helper to check if val is supported by this control.
57 virtual bool IsSupported(const T& val) const = 0;
58
59 DISALLOW_COPY_AND_ASSIGN(Control);
60};
61
62// -----------------------------------------------------------------------------
63
64template <typename T>
65Control<T>::Control(int32_t control_tag, std::vector<int32_t> static_tags)
66 // Controls use the same tag for setting the control
67 // and getting the dynamic value.
68 : TaggedPartialMetadata(static_tags, {control_tag}, {control_tag}) {
69 HAL_LOG_ENTER();
70}
71
72template <typename T>
73int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const {
74 HAL_LOG_ENTER();
75
76 // Populate the current setting.
77 T value;
78 int res = GetValue(&value);
79 if (res) {
80 return res;
81 }
82 return UpdateMetadata(metadata, ControlTag(), value);
83}
84
85template <typename T>
86bool Control<T>::SupportsRequestValues(
87 const android::CameraMetadata& metadata) const {
88 HAL_LOG_ENTER();
89 if (metadata.isEmpty()) {
90 // Implicitly supported.
91 return true;
92 }
93
94 // Check that the requested setting is in the supported options.
95 T requested;
96 int res = SingleTagValue(metadata, ControlTag(), &requested);
97 if (res == -ENOENT) {
98 // Nothing requested of this control, that's fine.
99 return true;
100 } else if (res) {
101 HAL_LOGE("Failure while searching for request value for tag %d",
102 ControlTag());
103 return false;
104 }
105 return IsSupported(requested);
106}
107
108template <typename T>
109int Control<T>::SetRequestValues(const android::CameraMetadata& metadata) {
110 HAL_LOG_ENTER();
111 if (metadata.isEmpty()) {
112 // No changes necessary.
113 return 0;
114 }
115
116 // Get the requested value.
117 T requested;
118 int res = SingleTagValue(metadata, ControlTag(), &requested);
119 if (res == -ENOENT) {
120 // Nothing requested of this control, nothing to do.
121 return 0;
122 } else if (res) {
123 HAL_LOGE("Failure while searching for request value for tag %d",
124 ControlTag());
125 return res;
126 }
127
128 return SetValue(requested);
129}
130
131} // namespace v4l2_camera_hal
132
133#endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_