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_TESTS_MOCK_DEVICE_HWL_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_TESTS_MOCK_DEVICE_HWL_H_
19 
20 #include <unordered_map>
21 
22 #include "camera_device_hwl.h"
23 #include "mock_device_session_hwl.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 class MockDeviceHwl : public CameraDeviceHwl {
29  public:
Create()30   static std::unique_ptr<MockDeviceHwl> Create() {
31     return std::unique_ptr<MockDeviceHwl>(new MockDeviceHwl());
32   }
33 
34   virtual ~MockDeviceHwl() = default;
35 
36   // Override functions in CameraDeviceHwl start.
GetCameraId()37   uint32_t GetCameraId() const {
38     return camera_id_;
39   };
40 
GetResourceCost(CameraResourceCost * cost)41   status_t GetResourceCost(CameraResourceCost* cost) const {
42     if (cost == nullptr) {
43       return BAD_VALUE;
44     }
45 
46     *cost = resource_cost_;
47     return OK;
48   }
49 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics)50   status_t GetCameraCharacteristics(
51       std::unique_ptr<HalCameraMetadata>* characteristics) const {
52     if (characteristics == nullptr) {
53       return BAD_VALUE;
54     }
55 
56     *characteristics = HalCameraMetadata::Clone(characteristics_.get());
57     if (*characteristics == nullptr) {
58       return NO_MEMORY;
59     }
60     return OK;
61   }
62 
GetSessionCharacteristics(const StreamConfiguration &,std::unique_ptr<HalCameraMetadata> & characteristics)63   status_t GetSessionCharacteristics(
64       const StreamConfiguration& /*session_config*/,
65       std::unique_ptr<HalCameraMetadata>& characteristics) const {
66     characteristics = HalCameraMetadata::Clone(characteristics_.get());
67     if (characteristics.get() == nullptr) {
68       return NO_MEMORY;
69     }
70     return OK;
71   }
72 
GetPhysicalCameraCharacteristics(uint32_t physical_camera_id,std::unique_ptr<HalCameraMetadata> * characteristics)73   status_t GetPhysicalCameraCharacteristics(
74       uint32_t physical_camera_id,
75       std::unique_ptr<HalCameraMetadata>* characteristics) const {
76     if (characteristics == nullptr) {
77       return BAD_VALUE;
78     }
79 
80     auto physical_characteristics =
81         physical_camera_characteristics_.find(physical_camera_id);
82     if (physical_characteristics == physical_camera_characteristics_.end()) {
83       return BAD_VALUE;
84     }
85 
86     *characteristics =
87         HalCameraMetadata::Clone(physical_characteristics->second.get());
88 
89     return OK;
90   }
91 
GetMemoryConfig()92   HwlMemoryConfig GetMemoryConfig() const {
93     return HwlMemoryConfig();
94   }
95 
SetTorchMode(TorchMode)96   status_t SetTorchMode(TorchMode /*mode*/) {
97     return OK;
98   }
99 
TurnOnTorchWithStrengthLevel(int32_t torch_strength)100    status_t TurnOnTorchWithStrengthLevel(int32_t torch_strength) {
101     if (torch_strength < 1) {
102       return BAD_VALUE;
103     }
104 
105     torch_strength_ = torch_strength;
106     return OK;
107   }
108 
GetTorchStrengthLevel(int32_t & torch_strength)109   status_t GetTorchStrengthLevel(int32_t& torch_strength) const {
110     torch_strength = torch_strength_;
111     return OK;
112   }
113 
ConstructDefaultRequestSettings(RequestTemplate,std::unique_ptr<HalCameraMetadata> *)114   status_t ConstructDefaultRequestSettings(
115       RequestTemplate /*type*/,
116       std::unique_ptr<HalCameraMetadata>* /*request_settings*/) {
117     return OK;
118   }
119 
120   // Dump the camera device states in fd, using dprintf() or write().
DumpState(int fd)121   status_t DumpState(int fd) {
122     if (fd < 0) {
123       return BAD_VALUE;
124     }
125 
126     dprintf(fd, "%s", dump_string_.c_str());
127 
128     return OK;
129   }
130 
CreateCameraDeviceSessionHwl(CameraBufferAllocatorHwl *,std::unique_ptr<CameraDeviceSessionHwl> * session)131   status_t CreateCameraDeviceSessionHwl(
132       CameraBufferAllocatorHwl* /*camera_allocator_hwl*/,
133       std::unique_ptr<CameraDeviceSessionHwl>* session) {
134     if (session == nullptr) {
135       return BAD_VALUE;
136     }
137 
138     auto session_hwl = std::make_unique<MockDeviceSessionHwl>();
139     if (session_hwl == nullptr) {
140       return NO_MEMORY;
141     }
142     session_hwl->DelegateCallsToFakeSession();
143     *session = std::move(session_hwl);
144 
145     return OK;
146   }
147 
IsStreamCombinationSupported(const StreamConfiguration &,const bool)148   bool IsStreamCombinationSupported(const StreamConfiguration& /*stream_config*/,
149                                     const bool /*check_settings*/) const {
150     return true;
151   }
152 
GetProfiler(uint32_t,int)153   std::unique_ptr<google::camera_common::Profiler> GetProfiler(
154       uint32_t /* camera_id */, int /* option */) {
155     return nullptr;
156   }
157 
158   // Override functions in CameraDeviceHwl end.
159 
160   // The following members are public so the test can change the values easily.
161   uint32_t camera_id_ = 0;
162   CameraResourceCost resource_cost_;
163   std::unique_ptr<HalCameraMetadata> characteristics_;
164 
165   // Map from physical camera ID to physical camera characteristics.
166   std::unordered_map<uint32_t, std::unique_ptr<HalCameraMetadata>>
167       physical_camera_characteristics_;
168 
169   std::string dump_string_;
170   int32_t torch_strength_ = 0;
171 
172  protected:
MockDeviceHwl()173   MockDeviceHwl() {
174     characteristics_ = HalCameraMetadata::Create(
175         /*num_entries=*/0, /*data_bytes=*/0);
176   };
177 };
178 }  // namespace google_camera_hal
179 }  // namespace android
180 
181 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_TESTS_MOCK_DEVICE_HWL_H_
182