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 "GCH_CameraDevice"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include "camera_device.h"
24 #include "vendor_tags.h"
25 
26 namespace android {
27 namespace google_camera_hal {
28 
Create(std::unique_ptr<CameraDeviceHwl> camera_device_hwl,CameraBufferAllocatorHwl * camera_allocator_hwl)29 std::unique_ptr<CameraDevice> CameraDevice::Create(
30     std::unique_ptr<CameraDeviceHwl> camera_device_hwl,
31     CameraBufferAllocatorHwl* camera_allocator_hwl) {
32   ATRACE_CALL();
33   auto device = std::unique_ptr<CameraDevice>(new CameraDevice());
34 
35   if (device == nullptr) {
36     ALOGE("%s: Creating CameraDevice failed.", __FUNCTION__);
37     return nullptr;
38   }
39 
40   status_t res =
41       device->Initialize(std::move(camera_device_hwl), camera_allocator_hwl);
42   if (res != OK) {
43     ALOGE("%s: Initializing CameraDevice failed: %s (%d).", __FUNCTION__,
44           strerror(-res), res);
45     return nullptr;
46   }
47 
48   ALOGI("%s: Created a camera device for public(%u)", __FUNCTION__,
49         device->GetPublicCameraId());
50 
51   return device;
52 }
53 
Initialize(std::unique_ptr<CameraDeviceHwl> camera_device_hwl,CameraBufferAllocatorHwl * camera_allocator_hwl)54 status_t CameraDevice::Initialize(
55     std::unique_ptr<CameraDeviceHwl> camera_device_hwl,
56     CameraBufferAllocatorHwl* camera_allocator_hwl) {
57   ATRACE_CALL();
58   if (camera_device_hwl == nullptr) {
59     ALOGE("%s: camera_device_hwl cannot be nullptr.", __FUNCTION__);
60     return BAD_VALUE;
61   }
62 
63   public_camera_id_ = camera_device_hwl->GetCameraId();
64   camera_device_hwl_ = std::move(camera_device_hwl);
65   camera_allocator_hwl_ = camera_allocator_hwl;
66 
67   return OK;
68 }
69 
GetResourceCost(CameraResourceCost * cost)70 status_t CameraDevice::GetResourceCost(CameraResourceCost* cost) {
71   ATRACE_CALL();
72   return camera_device_hwl_->GetResourceCost(cost);
73 }
74 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics)75 status_t CameraDevice::GetCameraCharacteristics(
76     std::unique_ptr<HalCameraMetadata>* characteristics) {
77   ATRACE_CALL();
78   status_t res = camera_device_hwl_->GetCameraCharacteristics(characteristics);
79   if (res != OK) {
80     ALOGE("%s: GetCameraCharacteristics() failed: %s (%d).", __FUNCTION__,
81           strerror(-res), res);
82     return res;
83   }
84 
85   return hal_vendor_tag_utils::ModifyCharacteristicsKeys(characteristics->get());
86 }
87 
GetPhysicalCameraCharacteristics(uint32_t physical_camera_id,std::unique_ptr<HalCameraMetadata> * characteristics)88 status_t CameraDevice::GetPhysicalCameraCharacteristics(
89     uint32_t physical_camera_id,
90     std::unique_ptr<HalCameraMetadata>* characteristics) {
91   ATRACE_CALL();
92   status_t res = camera_device_hwl_->GetPhysicalCameraCharacteristics(
93       physical_camera_id, characteristics);
94   if (res != OK) {
95     ALOGE("%s: GetPhysicalCameraCharacteristics() failed: %s (%d).",
96           __FUNCTION__, strerror(-res), res);
97     return res;
98   }
99 
100   return hal_vendor_tag_utils::ModifyCharacteristicsKeys(characteristics->get());
101 }
102 
SetTorchMode(TorchMode mode)103 status_t CameraDevice::SetTorchMode(TorchMode mode) {
104   ATRACE_CALL();
105   return camera_device_hwl_->SetTorchMode(mode);
106 }
107 
DumpState(int fd)108 status_t CameraDevice::DumpState(int fd) {
109   ATRACE_CALL();
110   return camera_device_hwl_->DumpState(fd);
111 }
112 
CreateCameraDeviceSession(std::unique_ptr<CameraDeviceSession> * session)113 status_t CameraDevice::CreateCameraDeviceSession(
114     std::unique_ptr<CameraDeviceSession>* session) {
115   ATRACE_CALL();
116   if (session == nullptr) {
117     ALOGE("%s: session is nullptr.", __FUNCTION__);
118     return BAD_VALUE;
119   }
120 
121   std::unique_ptr<CameraDeviceSessionHwl> session_hwl;
122   status_t res = camera_device_hwl_->CreateCameraDeviceSessionHwl(
123       camera_allocator_hwl_, &session_hwl);
124   if (res != OK) {
125     ALOGE("%s: Creating a CameraDeviceSessionHwl failed: %s(%d)", __FUNCTION__,
126           strerror(-res), res);
127     return res;
128   }
129 
130   *session = CameraDeviceSession::Create(std::move(session_hwl),
131                                          camera_allocator_hwl_);
132   if (*session == nullptr) {
133     ALOGE("%s: Creating a CameraDeviceSession failed: %s(%d)", __FUNCTION__,
134           strerror(-res), res);
135     return UNKNOWN_ERROR;
136   }
137 
138   return OK;
139 }
140 
IsStreamCombinationSupported(const StreamConfiguration & stream_config)141 bool CameraDevice::IsStreamCombinationSupported(
142     const StreamConfiguration& stream_config) {
143   bool supported =
144       camera_device_hwl_->IsStreamCombinationSupported(stream_config);
145   if (!supported) {
146     ALOGD("%s: stream config is not supported.", __FUNCTION__);
147   }
148 
149   return supported;
150 }
151 
152 }  // namespace google_camera_hal
153 }  // namespace android
154