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_SLIDER_CONTROL_OPTIONS_H_ 18 #define V4L2_CAMERA_HAL_METADATA_SLIDER_CONTROL_OPTIONS_H_ 19 20 #include <errno.h> 21 22 #include <vector> 23 24 #include "../common.h" 25 #include "control_options_interface.h" 26 #include "default_option_delegate.h" 27 28 namespace v4l2_camera_hal { 29 30 // SliderControlOptions offer a range of acceptable values, inclusive. 31 template <typename T> 32 class SliderControlOptions : public ControlOptionsInterface<T> { 33 public: 34 // |min| must be <= |max|. 35 SliderControlOptions(const T& min, 36 const T& max, 37 std::shared_ptr<DefaultOptionDelegate<T>> defaults) 38 : min_(min), max_(max), defaults_(defaults){}; 39 SliderControlOptions(const T& min, const T& max, std::map<int, T> defaults) 40 : min_(min), 41 max_(max), 42 defaults_(std::make_shared<DefaultOptionDelegate<T>>(defaults)){}; 43 44 virtual std::vector<T> MetadataRepresentation() override { 45 return {min_, max_}; 46 }; 47 virtual bool IsSupported(const T& option) override { 48 return option >= min_ && option <= max_; 49 }; 50 virtual int DefaultValueForTemplate(int template_type, 51 T* default_value) override { 52 if (min_ > max_) { 53 HAL_LOGE("No valid default slider option, min is greater than max."); 54 return -ENODEV; 55 } 56 57 if (defaults_->DefaultValueForTemplate(template_type, default_value)) { 58 // Get as close as we can to the desired value. 59 if (*default_value < min_) { 60 *default_value = min_; 61 } else if (*default_value > max_) { 62 *default_value = max_; 63 } 64 return 0; 65 } 66 67 // No default given, just fall back to the min of the range. 68 *default_value = min_; 69 return 0; 70 }; 71 72 private: 73 T min_; 74 T max_; 75 std::shared_ptr<DefaultOptionDelegate<T>> defaults_; 76 }; 77 78 } // namespace v4l2_camera_hal 79 80 #endif // V4L2_CAMERA_HAL_METADATA_SLIDER_CONTROL_OPTIONS_H_ 81