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 <camera_device_hwl.h>
21 #include <unordered_map>
22 
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 
GetPhysicalCameraCharacteristics(uint32_t physical_camera_id,std::unique_ptr<HalCameraMetadata> * characteristics)63   status_t GetPhysicalCameraCharacteristics(
64       uint32_t physical_camera_id,
65       std::unique_ptr<HalCameraMetadata>* characteristics) const {
66     if (characteristics == nullptr) {
67       return BAD_VALUE;
68     }
69 
70     auto physical_characteristics =
71         physical_camera_characteristics_.find(physical_camera_id);
72     if (physical_characteristics == physical_camera_characteristics_.end()) {
73       return BAD_VALUE;
74     }
75 
76     *characteristics =
77         HalCameraMetadata::Clone(physical_characteristics->second.get());
78 
79     return OK;
80   }
81 
SetTorchMode(TorchMode)82   status_t SetTorchMode(TorchMode /*mode*/) {
83     return OK;
84   }
85 
86   // Dump the camera device states in fd, using dprintf() or write().
DumpState(int fd)87   status_t DumpState(int fd) {
88     if (fd < 0) {
89       return BAD_VALUE;
90     }
91 
92     dprintf(fd, "%s", dump_string_.c_str());
93 
94     return OK;
95   }
96 
CreateCameraDeviceSessionHwl(CameraBufferAllocatorHwl *,std::unique_ptr<CameraDeviceSessionHwl> * session)97   status_t CreateCameraDeviceSessionHwl(
98       CameraBufferAllocatorHwl* /*camera_allocator_hwl*/,
99       std::unique_ptr<CameraDeviceSessionHwl>* session) {
100     if (session == nullptr) {
101       return BAD_VALUE;
102     }
103 
104     auto session_hwl = std::make_unique<MockDeviceSessionHwl>();
105     if (session_hwl == nullptr) {
106       return NO_MEMORY;
107     }
108     session_hwl->DelegateCallsToFakeSession();
109     *session = std::move(session_hwl);
110 
111     return OK;
112   }
113 
IsStreamCombinationSupported(const StreamConfiguration &)114   bool IsStreamCombinationSupported(const StreamConfiguration& /*stream_config*/) {
115     return true;
116   }
117   // Override functions in CameraDeviceHwl end.
118 
119   // The following members are public so the test can change the values easily.
120   uint32_t camera_id_ = 0;
121   CameraResourceCost resource_cost_;
122   std::unique_ptr<HalCameraMetadata> characteristics_;
123 
124   // Map from physical camera ID to physical camera characteristics.
125   std::unordered_map<uint32_t, std::unique_ptr<HalCameraMetadata>>
126       physical_camera_characteristics_;
127 
128   std::string dump_string_;
129 
130  protected:
MockDeviceHwl()131   MockDeviceHwl() {
132     characteristics_ = HalCameraMetadata::Create(
133         /*num_entries=*/0, /*data_bytes=*/0);
134   };
135 };
136 }  // namespace google_camera_hal
137 }  // namespace android
138 
139 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_TESTS_MOCK_DEVICE_HWL_H_
140