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