blob: 4b4adcf1e3ac91212a48b04b4386f40eb2a13514 [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.
49 virtual int GetValue(T* value) const = 0;
50 virtual int SetValue(const T& value) = 0;
51 // Helper to check if val is supported by this control.
52 virtual bool IsSupported(const T& val) const = 0;
53
54 DISALLOW_COPY_AND_ASSIGN(Control);
55};
56
57// -----------------------------------------------------------------------------
58
59template <typename T>
60Control<T>::Control(int32_t control_tag, std::vector<int32_t> static_tags)
61 // Controls use the same tag for setting the control
62 // and getting the dynamic value.
63 : TaggedPartialMetadata(static_tags, {control_tag}, {control_tag}) {
64 HAL_LOG_ENTER();
65}
66
67template <typename T>
68int Control<T>::PopulateDynamicFields(android::CameraMetadata* metadata) const {
69 HAL_LOG_ENTER();
70
71 // Populate the current setting.
72 T value;
73 int res = GetValue(&value);
74 if (res) {
75 return res;
76 }
77 return UpdateMetadata(metadata, ControlTag(), value);
78}
79
80template <typename T>
81bool Control<T>::SupportsRequestValues(
82 const android::CameraMetadata& metadata) const {
83 HAL_LOG_ENTER();
84 if (metadata.isEmpty()) {
85 // Implicitly supported.
86 return true;
87 }
88
89 // Check that the requested setting is in the supported options.
90 T requested;
91 int res = SingleTagValue(metadata, ControlTag(), &requested);
92 if (res == -ENOENT) {
93 // Nothing requested of this control, that's fine.
94 return true;
95 } else if (res) {
96 HAL_LOGE("Failure while searching for request value for tag %d",
97 ControlTag());
98 return false;
99 }
100 return IsSupported(requested);
101}
102
103template <typename T>
104int Control<T>::SetRequestValues(const android::CameraMetadata& metadata) {
105 HAL_LOG_ENTER();
106 if (metadata.isEmpty()) {
107 // No changes necessary.
108 return 0;
109 }
110
111 // Get the requested value.
112 T requested;
113 int res = SingleTagValue(metadata, ControlTag(), &requested);
114 if (res == -ENOENT) {
115 // Nothing requested of this control, nothing to do.
116 return 0;
117 } else if (res) {
118 HAL_LOGE("Failure while searching for request value for tag %d",
119 ControlTag());
120 return res;
121 }
122
123 return SetValue(requested);
124}
125
126} // namespace v4l2_camera_hal
127
128#endif // V4L2_CAMERA_HAL_METADATA_CONTROL_H_