1 /*
2  * Copyright (C) 2019 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 HARDWARE_GOOGLE_CAMERA_HAL_UTILS_CAMERA_ID_MANAGER_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_UTILS_CAMERA_ID_MANAGER_H_
19 
20 #include <utils/Errors.h>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "hal_types.h"
26 
27 namespace android {
28 namespace google_camera_hal {
29 
30 using ::android::status_t;
31 
32 // Invalid camera ID
33 constexpr uint32_t kInvalidCameraId = std::numeric_limits<uint32_t>::max();
34 
35 // Holds information about a camera's IDs
36 struct CameraIdMap {
37   // An unique camera ID. This should be the internal camera ID and does not
38   // correspond to the public camera ID published to the camera framework
39   uint32_t id = kInvalidCameraId;
40 
41   // Whether this camera ID is visible to camera framework.
42   bool visible_to_framework = false;
43 
44   // physical_camera_ids contains the physical cameras underneath this logical
45   // camera. If this logical camera does not contain multiple physical cameras,
46   // physical_camera_ids should be empty.
47   std::vector<uint32_t> physical_camera_ids;
48 };
49 
50 // CameraIdManager manages public and internal camera IDs.
51 // Internal camera IDs are the camera IDs assigned by CameraProviderHwl.
52 // Public camera IDs are the camera IDs that camera framework sees.
53 class CameraIdManager {
54  public:
55   // Create a CameraIdManager given a list of the internal camera info.
56   // Public camera IDs are assigned to the visible cameras in the id_maps list
57   // first, and then to the non-visible ones, in the same order that the id_maps
58   // list is ordered
59   static std::unique_ptr<CameraIdManager> Create(
60       const std::vector<CameraIdMap>& id_maps);
61 
62   virtual ~CameraIdManager() = default;
63 
64   // Return the camera IDs that are visible to camera framework.
65   std::vector<std::uint32_t> GetVisibleCameraIds() const;
66 
67   // Return camera IDs, including those that are not visible to the framework.
68   std::vector<std::uint32_t> GetCameraIds() const;
69 
70   // Get the list of physical camera IDs for the given logical camera. Returns
71   // an empty vector if the specified ID is a physical camera. The IDs are
72   // public IDs as understood by the camera framework
73   std::vector<std::uint32_t> GetPhysicalCameraIds(
74       uint32_t public_camera_id) const;
75 
76   // Get the public camera ID of an internal camera ID.
77   status_t GetPublicCameraId(uint32_t internal_camera_id,
78                              uint32_t* public_camera_id) const;
79 
80   // Get the internal camera ID of a public camera ID.
81   status_t GetInternalCameraId(uint32_t public_camera_id,
82                                uint32_t* internal_camera_id) const;
83 
84  protected:
85   CameraIdManager() = default;
86 
87  private:
88   status_t Initialize(const std::vector<CameraIdMap>& id_maps);
89 
90   status_t ValidateInput(const std::vector<CameraIdMap>& id_maps);
91 
92   status_t ValidateMappedIds();
93 
94   void PrintCameraIdMapping();
95 
96   // Index is the public camera ID.
97   // Value is the internal camera ID.
98   std::vector<uint32_t> public_camera_internal_ids_;
99 
100   size_t visible_camera_count_ = 0;
101 
102   // Index is public camera ID.
103   // Value is the list of physical camera IDs belonging to the device at this
104   // index. Physical IDs in the list are in the public domain also.
105   //
106   // For a public camera ID:
107   //    physical_camera_ids_[public_logical_id] = { public_physical_id_list }
108   //    physical_camera_ids_[public_physical_id] = {}
109   std::vector<std::vector<uint32_t>> physical_camera_ids_;
110 };
111 
112 }  // namespace google_camera_hal
113 }  // namespace android
114 
115 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_UTILS_CAMERA_ID_MANAGER_H_
116