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_DEFAULT_OPTION_DELEGATE_H_
18 #define V4L2_CAMERA_HAL_METADATA_DEFAULT_OPTION_DELEGATE_H_
19 
20 #include <map>
21 
22 #include <hardware/camera3.h>
23 
24 namespace v4l2_camera_hal {
25 
26 // A constant that can be used to identify an overall default.
27 static constexpr int OTHER_TEMPLATES = CAMERA3_TEMPLATE_COUNT;
28 
29 // DefaultingOptionDelegate provides an interface to get default options from.
30 template <typename T>
31 class DefaultOptionDelegate {
32  public:
33   // |defaults| maps template types to default values
DefaultOptionDelegate(std::map<int,T> defaults)34   DefaultOptionDelegate(std::map<int, T> defaults)
35       : defaults_(std::move(defaults)){};
~DefaultOptionDelegate()36   virtual ~DefaultOptionDelegate(){};
37 
38   // Get a default value for a template type. Returns false if no default
39   // provided.
DefaultValueForTemplate(int template_type,T * default_value)40   virtual bool DefaultValueForTemplate(int template_type, T* default_value) {
41     if (defaults_.count(template_type) > 0) {
42       // Best option is template-specific.
43       *default_value = defaults_[template_type];
44       return true;
45     } else if (defaults_.count(OTHER_TEMPLATES)) {
46       // Fall back to a general default.
47       *default_value = defaults_[OTHER_TEMPLATES];
48       return true;
49     }
50 
51     return false;
52   };
53 
54  private:
55   std::map<int, T> defaults_;
56 };
57 
58 }  // namespace v4l2_camera_hal
59 
60 #endif  // V4L2_CAMERA_HAL_METADATA_DEFAULT_OPTION_DELEGATE_H_
61