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(), &sensor_chars_);
71   if (ret != OK) {
72     ALOGE("%s: Unable to extract sensor characteristics %s (%d)", __FUNCTION__,
73           strerror(-ret), ret);
74     return ret;
75   }
76 
77   stream_coniguration_map_ =
78       std::make_unique<StreamConfigurationMap>(*static_metadata_);
79 
80   return OK;
81 }
82 
GetResourceCost(CameraResourceCost * cost) const83 status_t EmulatedCameraDeviceHwlImpl::GetResourceCost(
84     CameraResourceCost* cost) const {
85   // TODO: remove hardcode
86   cost->resource_cost = 100;
87 
88   return OK;
89 }
90 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics) const91 status_t EmulatedCameraDeviceHwlImpl::GetCameraCharacteristics(
92     std::unique_ptr<HalCameraMetadata>* characteristics) const {
93   if (characteristics == nullptr) {
94     return BAD_VALUE;
95   }
96 
97   *characteristics = HalCameraMetadata::Clone(static_metadata_.get());
98 
99   return OK;
100 }
101 
GetPhysicalCameraCharacteristics(uint32_t physical_camera_id,std::unique_ptr<HalCameraMetadata> * characteristics) const102 status_t EmulatedCameraDeviceHwlImpl::GetPhysicalCameraCharacteristics(
103     uint32_t physical_camera_id,
104     std::unique_ptr<HalCameraMetadata>* characteristics) const {
105   if (characteristics == nullptr) {
106     return BAD_VALUE;
107   }
108 
109   if (physical_device_map_.get() == nullptr) {
110     ALOGE("%s: Camera %d is not a logical device!", __func__, camera_id_);
111     return NO_INIT;
112   }
113 
114   if (physical_device_map_->find(physical_camera_id) ==
115       physical_device_map_->end()) {
116     ALOGE("%s: Physical camera id %d is not part of logical camera %d!",
117           __func__, physical_camera_id, camera_id_);
118     return BAD_VALUE;
119   }
120 
121   *characteristics = HalCameraMetadata::Clone(
122       physical_device_map_->at(physical_camera_id).second.get());
123 
124   return OK;
125 }
126 
SetTorchMode(TorchMode mode)127 status_t EmulatedCameraDeviceHwlImpl::SetTorchMode(TorchMode mode) {
128   if (torch_state_.get() == nullptr) {
129     return INVALID_OPERATION;
130   }
131 
132   return torch_state_->SetTorchMode(mode);
133 }
134 
DumpState(int)135 status_t EmulatedCameraDeviceHwlImpl::DumpState(int /*fd*/) {
136   return OK;
137 }
138 
CreateCameraDeviceSessionHwl(CameraBufferAllocatorHwl *,std::unique_ptr<CameraDeviceSessionHwl> * session)139 status_t EmulatedCameraDeviceHwlImpl::CreateCameraDeviceSessionHwl(
140     CameraBufferAllocatorHwl* /*camera_allocator_hwl*/,
141     std::unique_ptr<CameraDeviceSessionHwl>* session) {
142   if (session == nullptr) {
143     ALOGE("%s: session is nullptr.", __FUNCTION__);
144     return BAD_VALUE;
145   }
146 
147   std::unique_ptr<HalCameraMetadata> meta =
148       HalCameraMetadata::Clone(static_metadata_.get());
149   *session = EmulatedCameraDeviceSessionHwlImpl::Create(
150       camera_id_, std::move(meta), ClonePhysicalDeviceMap(physical_device_map_),
151       torch_state_);
152   if (*session == nullptr) {
153     ALOGE("%s: Cannot create EmulatedCameraDeviceSessionHWlImpl.", __FUNCTION__);
154     return BAD_VALUE;
155   }
156 
157   if (torch_state_.get() != nullptr) {
158     torch_state_->AcquireFlashHw();
159   }
160 
161   return OK;
162 }
163 
IsStreamCombinationSupported(const StreamConfiguration & stream_config)164 bool EmulatedCameraDeviceHwlImpl::IsStreamCombinationSupported(
165     const StreamConfiguration& stream_config) {
166   return EmulatedSensor::IsStreamCombinationSupported(
167       stream_config, *stream_coniguration_map_, sensor_chars_);
168 }
169 
170 }  // namespace android
171