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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "EmulatedCameraDeviceHwlImpl"
19 #include "EmulatedCameraDeviceHWLImpl.h"
20 
21 #include <hardware/camera_common.h>
22 #include <log/log.h>
23 
24 #include "EmulatedCameraDeviceSessionHWLImpl.h"
25 #include "utils/HWLUtils.h"
26 
27 namespace android {
28 
Create(uint32_t camera_id,std::unique_ptr<HalCameraMetadata> static_meta,PhysicalDeviceMapPtr physical_devices,std::shared_ptr<EmulatedTorchState> torch_state)29 std::unique_ptr<CameraDeviceHwl> EmulatedCameraDeviceHwlImpl::Create(
30     uint32_t camera_id, std::unique_ptr<HalCameraMetadata> static_meta,
31     PhysicalDeviceMapPtr physical_devices,
32     std::shared_ptr<EmulatedTorchState> torch_state) {
33   auto device = std::unique_ptr<EmulatedCameraDeviceHwlImpl>(
34       new EmulatedCameraDeviceHwlImpl(camera_id, std::move(static_meta),
35                                       std::move(physical_devices),
36                                       torch_state));
37 
38   if (device == nullptr) {
39     ALOGE("%s: Creating EmulatedCameraDeviceHwlImpl failed.", __FUNCTION__);
40     return nullptr;
41   }
42 
43   status_t res = device->Initialize();
44   if (res != OK) {
45     ALOGE("%s: Initializing EmulatedCameraDeviceHwlImpl failed: %s (%d).",
46           __FUNCTION__, strerror(-res), res);
47     return nullptr;
48   }
49 
50   ALOGI("%s: Created EmulatedCameraDeviceHwlImpl for camera %u", __FUNCTION__,
51         device->camera_id_);
52 
53   return device;
54 }
55 
EmulatedCameraDeviceHwlImpl(uint32_t camera_id,std::unique_ptr<HalCameraMetadata> static_meta,PhysicalDeviceMapPtr physical_devices,std::shared_ptr<EmulatedTorchState> torch_state)56 EmulatedCameraDeviceHwlImpl::EmulatedCameraDeviceHwlImpl(
57     uint32_t camera_id, std::unique_ptr<HalCameraMetadata> static_meta,
58     PhysicalDeviceMapPtr physical_devices,
59     std::shared_ptr<EmulatedTorchState> torch_state)
60     : camera_id_(camera_id),
61       static_metadata_(std::move(static_meta)),
62       physical_device_map_(std::move(physical_devices)),
63       torch_state_(torch_state) {}
64 
GetCameraId() const65 uint32_t EmulatedCameraDeviceHwlImpl::GetCameraId() const {
66   return camera_id_;
67 }
68 
Initialize()69 status_t EmulatedCameraDeviceHwlImpl::Initialize() {
70   auto ret = GetSensorCharacteristics(static_metadata_.get(),
71                                       &sensor_chars_[camera_id_]);
72   if (ret != OK) {
73     ALOGE("%s: Unable to extract sensor characteristics %s (%d)", __FUNCTION__,
74           strerror(-ret), ret);
75     return ret;
76   }
77 
78   stream_configuration_map_ =
79       std::make_unique<StreamConfigurationMap>(*static_metadata_);
80   stream_configuration_map_max_resolution_ =
81       std::make_unique<StreamConfigurationMap>(*static_metadata_,
82                                                /*maxResolution*/ true);
83 
84   for (const auto& it : *physical_device_map_) {
85     uint32_t physical_id = it.first;
86     HalCameraMetadata* physical_hal_metadata = it.second.second.get();
87     physical_stream_configuration_map_.emplace(
88         physical_id,
89         std::make_unique<StreamConfigurationMap>(*physical_hal_metadata));
90     physical_stream_configuration_map_max_resolution_.emplace(
91         physical_id, std::make_unique<StreamConfigurationMap>(
92                          *physical_hal_metadata, /*maxResolution*/ true));
93 
94     ret = GetSensorCharacteristics(physical_hal_metadata,
95                                    &sensor_chars_[physical_id]);
96     if (ret != OK) {
97       ALOGE("%s: Unable to extract camera %d sensor characteristics %s (%d)",
98             __FUNCTION__, physical_id, strerror(-ret), ret);
99       return ret;
100     }
101   }
102   return OK;
103 }
104 
GetResourceCost(CameraResourceCost * cost) const105 status_t EmulatedCameraDeviceHwlImpl::GetResourceCost(
106     CameraResourceCost* cost) const {
107   // TODO: remove hardcode
108   cost->resource_cost = 100;
109 
110   return OK;
111 }
112 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics) const113 status_t EmulatedCameraDeviceHwlImpl::GetCameraCharacteristics(
114     std::unique_ptr<HalCameraMetadata>* characteristics) const {
115   if (characteristics == nullptr) {
116     return BAD_VALUE;
117   }
118 
119   *characteristics = HalCameraMetadata::Clone(static_metadata_.get());
120 
121   return OK;
122 }
123 
GetPhysicalCameraCharacteristics(uint32_t physical_camera_id,std::unique_ptr<HalCameraMetadata> * characteristics) const124 status_t EmulatedCameraDeviceHwlImpl::GetPhysicalCameraCharacteristics(
125     uint32_t physical_camera_id,
126     std::unique_ptr<HalCameraMetadata>* characteristics) const {
127   if (characteristics == nullptr) {
128     return BAD_VALUE;
129   }
130 
131   if (physical_device_map_.get() == nullptr) {
132     ALOGE("%s: Camera %d is not a logical device!", __func__, camera_id_);
133     return NO_INIT;
134   }
135 
136   if (physical_device_map_->find(physical_camera_id) ==
137       physical_device_map_->end()) {
138     ALOGE("%s: Physical camera id %d is not part of logical camera %d!",
139           __func__, physical_camera_id, camera_id_);
140     return BAD_VALUE;
141   }
142 
143   *characteristics = HalCameraMetadata::Clone(
144       physical_device_map_->at(physical_camera_id).second.get());
145 
146   return OK;
147 }
148 
SetTorchMode(TorchMode mode)149 status_t EmulatedCameraDeviceHwlImpl::SetTorchMode(TorchMode mode) {
150   if (torch_state_.get() == nullptr) {
151     return INVALID_OPERATION;
152   }
153 
154   return torch_state_->SetTorchMode(mode);
155 }
156 
DumpState(int)157 status_t EmulatedCameraDeviceHwlImpl::DumpState(int /*fd*/) {
158   return OK;
159 }
160 
CreateCameraDeviceSessionHwl(CameraBufferAllocatorHwl *,std::unique_ptr<CameraDeviceSessionHwl> * session)161 status_t EmulatedCameraDeviceHwlImpl::CreateCameraDeviceSessionHwl(
162     CameraBufferAllocatorHwl* /*camera_allocator_hwl*/,
163     std::unique_ptr<CameraDeviceSessionHwl>* session) {
164   if (session == nullptr) {
165     ALOGE("%s: session is nullptr.", __FUNCTION__);
166     return BAD_VALUE;
167   }
168 
169   std::unique_ptr<HalCameraMetadata> meta =
170       HalCameraMetadata::Clone(static_metadata_.get());
171   *session = EmulatedCameraDeviceSessionHwlImpl::Create(
172       camera_id_, std::move(meta), ClonePhysicalDeviceMap(physical_device_map_),
173       torch_state_);
174   if (*session == nullptr) {
175     ALOGE("%s: Cannot create EmulatedCameraDeviceSessionHWlImpl.", __FUNCTION__);
176     return BAD_VALUE;
177   }
178 
179   if (torch_state_.get() != nullptr) {
180     torch_state_->AcquireFlashHw();
181   }
182 
183   return OK;
184 }
185 
IsStreamCombinationSupported(const StreamConfiguration & stream_config)186 bool EmulatedCameraDeviceHwlImpl::IsStreamCombinationSupported(
187     const StreamConfiguration& stream_config) {
188   return EmulatedSensor::IsStreamCombinationSupported(
189       camera_id_, stream_config, *stream_configuration_map_,
190       *stream_configuration_map_max_resolution_,
191       physical_stream_configuration_map_,
192       physical_stream_configuration_map_max_resolution_, sensor_chars_);
193 }
194 
195 }  // namespace android
196