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