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_RANGED_CONVERTER_H_
18 #define V4L2_CAMERA_HAL_METADATA_RANGED_CONVERTER_H_
19 
20 #include <memory>
21 
22 #include <android-base/macros.h>
23 #include "common.h"
24 #include "converter_interface.h"
25 
26 namespace v4l2_camera_hal {
27 
28 // An RangedConverter fits values converted by a wrapped converter
29 // to a stepped range (when going from metadata -> v4l2. The other
30 // direction remains unchanged).
31 template <typename TMetadata, typename TV4L2>
32 class RangedConverter : public ConverterInterface<TMetadata, TV4L2> {
33  public:
34   RangedConverter(
35       std::shared_ptr<ConverterInterface<TMetadata, TV4L2>> wrapped_converter,
36       TV4L2 min,
37       TV4L2 max,
38       TV4L2 step);
39 
40   virtual int MetadataToV4L2(TMetadata value, TV4L2* conversion) override;
41   virtual int V4L2ToMetadata(TV4L2 value, TMetadata* conversion) override;
42 
43  private:
44   std::shared_ptr<ConverterInterface<TMetadata, TV4L2>> wrapped_converter_;
45   const TV4L2 min_;
46   const TV4L2 max_;
47   const TV4L2 step_;
48 
49   DISALLOW_COPY_AND_ASSIGN(RangedConverter);
50 };
51 
52 // -----------------------------------------------------------------------------
53 
54 template <typename TMetadata, typename TV4L2>
RangedConverter(std::shared_ptr<ConverterInterface<TMetadata,TV4L2>> wrapped_converter,TV4L2 min,TV4L2 max,TV4L2 step)55 RangedConverter<TMetadata, TV4L2>::RangedConverter(
56     std::shared_ptr<ConverterInterface<TMetadata, TV4L2>> wrapped_converter,
57     TV4L2 min,
58     TV4L2 max,
59     TV4L2 step)
60     : wrapped_converter_(std::move(wrapped_converter)),
61       min_(min),
62       max_(max),
63       step_(step) {
64   HAL_LOG_ENTER();
65 }
66 
67 template <typename TMetadata, typename TV4L2>
MetadataToV4L2(TMetadata value,TV4L2 * conversion)68 int RangedConverter<TMetadata, TV4L2>::MetadataToV4L2(TMetadata value,
69                                                       TV4L2* conversion) {
70   HAL_LOG_ENTER();
71 
72   TV4L2 raw_conversion = 0;
73   int res = wrapped_converter_->MetadataToV4L2(value, &raw_conversion);
74   if (res) {
75     HAL_LOGE("Failed to perform underlying conversion.");
76     return res;
77   }
78 
79   // Round down to step (steps start at min_).
80   raw_conversion -= (raw_conversion - min_) % step_;
81 
82   // Clamp to range.
83   if (raw_conversion < min_) {
84     raw_conversion = min_;
85   } else if (raw_conversion > max_) {
86     raw_conversion = max_;
87   }
88 
89   *conversion = raw_conversion;
90   return 0;
91 }
92 
93 template <typename TMetadata, typename TV4L2>
V4L2ToMetadata(TV4L2 value,TMetadata * conversion)94 int RangedConverter<TMetadata, TV4L2>::V4L2ToMetadata(TV4L2 value,
95                                                       TMetadata* conversion) {
96   HAL_LOG_ENTER();
97 
98   return wrapped_converter_->V4L2ToMetadata(value, conversion);
99 }
100 
101 }  // namespace v4l2_camera_hal
102 
103 #endif  // V4L2_CAMERA_HAL_METADATA_RANGED_CONVERTER_H_
104