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