• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetTorchMode(TorchMode)92   status_t SetTorchMode(TorchMode /*mode*/) {
93     return OK;
94   }
95 
TurnOnTorchWithStrengthLevel(int32_t torch_strength)96    status_t TurnOnTorchWithStrengthLevel(int32_t torch_strength) {
97     if (torch_strength < 1) {
98       return BAD_VALUE;
99     }
100 
101     torch_strength_ = torch_strength;
102     return OK;
103   }
104 
GetTorchStrengthLevel(int32_t & torch_strength)105   status_t GetTorchStrengthLevel(int32_t& torch_strength) const {
106     torch_strength = torch_strength_;
107     return OK;
108   }
109 
ConstructDefaultRequestSettings(RequestTemplate,std::unique_ptr<HalCameraMetadata> *)110   status_t ConstructDefaultRequestSettings(
111       RequestTemplate /*type*/,
112       std::unique_ptr<HalCameraMetadata>* /*request_settings*/) {
113     return OK;
114   }
115 
116   // Dump the camera device states in fd, using dprintf() or write().
DumpState(int fd)117   status_t DumpState(int fd) {
118     if (fd < 0) {
119       return BAD_VALUE;
120     }
121 
122     dprintf(fd, "%s", dump_string_.c_str());
123 
124     return OK;
125   }
126 
CreateCameraDeviceSessionHwl(CameraBufferAllocatorHwl *,std::unique_ptr<CameraDeviceSessionHwl> * session)127   status_t CreateCameraDeviceSessionHwl(
128       CameraBufferAllocatorHwl* /*camera_allocator_hwl*/,
129       std::unique_ptr<CameraDeviceSessionHwl>* session) {
130     if (session == nullptr) {
131       return BAD_VALUE;
132     }
133 
134     auto session_hwl = std::make_unique<MockDeviceSessionHwl>();
135     if (session_hwl == nullptr) {
136       return NO_MEMORY;
137     }
138     session_hwl->DelegateCallsToFakeSession();
139     *session = std::move(session_hwl);
140 
141     return OK;
142   }
143 
IsStreamCombinationSupported(const StreamConfiguration &,const bool)144   bool IsStreamCombinationSupported(const StreamConfiguration& /*stream_config*/,
145                                     const bool /*check_settings*/) const {
146     return true;
147   }
148 
GetProfiler(uint32_t,int)149   std::unique_ptr<google::camera_common::Profiler> GetProfiler(
150       uint32_t /* camera_id */, int /* option */) {
151     return nullptr;
152   }
153 
154   // Override functions in CameraDeviceHwl end.
155 
156   // The following members are public so the test can change the values easily.
157   uint32_t camera_id_ = 0;
158   CameraResourceCost resource_cost_;
159   std::unique_ptr<HalCameraMetadata> characteristics_;
160 
161   // Map from physical camera ID to physical camera characteristics.
162   std::unordered_map<uint32_t, std::unique_ptr<HalCameraMetadata>>
163       physical_camera_characteristics_;
164 
165   std::string dump_string_;
166   int32_t torch_strength_ = 0;
167 
168  protected:
MockDeviceHwl()169   MockDeviceHwl() {
170     characteristics_ = HalCameraMetadata::Create(
171         /*num_entries=*/0, /*data_bytes=*/0);
172   };
173 };
174 }  // namespace google_camera_hal
175 }  // namespace android
176 
177 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_TESTS_MOCK_DEVICE_HWL_H_
178