blob: b7f6446069d00b4c8f892e2c5303475ec6a7ada0 [file] [log] [blame]
Scott Randolphdcc35342017-01-19 13:33:42 -08001/*
2 * Copyright (C) 2016 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_TAG "EvsManager"
18
19#include "Enumerator.h"
20
21namespace android {
22namespace automotive {
23namespace evs {
24namespace V1_0 {
25namespace implementation {
26
27
28bool Enumerator::init(const char* hardwareServiceName) {
29 ALOGD("init");
30
31 // Connect with the underlying hardware enumerator
32 mHwEnumerator = IEvsEnumerator::getService(hardwareServiceName);
33 bool result = (mHwEnumerator.get() != nullptr);
34
35 return result;
36}
37
38
39// Methods from ::android::hardware::evs::V1_0::IEvsEnumerator follow.
40Return<void> Enumerator::getCameraList(getCameraList_cb list_cb) {
41 ALOGD("getCameraList");
42
43 // Simply pass through to hardware layer
44 return mHwEnumerator->getCameraList(list_cb);
45}
46
47
48Return<sp<IEvsCamera>> Enumerator::openCamera(const hidl_string& cameraId) {
49 ALOGD("openCamera");
50
51 // Is the underlying hardware camera already open?
52 sp<HalCamera> hwCamera;
53 for (auto &&cam : mCameras) {
54 bool match = false;
55 cam->getHwCamera()->getId([cameraId, &match](hidl_string id) {
56 if (id == cameraId) {
57 match = true;
58 }
59 }
60 );
61 if (match) {
62 hwCamera = cam;
63 break;
64 }
65 }
66
67 // Do we need to open a new hardware camera?
68 if (hwCamera == nullptr) {
69 // Is the hardware camera available?
70 sp<IEvsCamera> device = mHwEnumerator->openCamera(cameraId);
71 if (device == nullptr) {
72 ALOGE("Failed to open hardware camera %s", cameraId.c_str());
73 } else {
74 hwCamera = new HalCamera(device);
75 if (hwCamera == nullptr) {
76 ALOGE("Failed to allocate camera wrapper object");
77 mHwEnumerator->closeCamera(device);
78 }
79 }
80 }
81
82 // Construct a virtual camera wrapper for this hardware camera
83 sp<VirtualCamera> clientCamera;
84 if (hwCamera != nullptr) {
85 clientCamera = hwCamera->makeVirtualCamera();
86 }
87
Scott Randolph29803802017-01-25 13:11:51 -080088 // Add the hardware camera to our list, which will keep it alive via ref count
Scott Randolphdcc35342017-01-19 13:33:42 -080089 if (clientCamera != nullptr) {
90 mCameras.push_back(hwCamera);
91 } else {
92 ALOGE("Requested camera %s not found or not available", cameraId.c_str());
93 }
94
Scott Randolph29803802017-01-25 13:11:51 -080095 // Send the virtual camera object back to the client by strong pointer which will keep it alive
Scott Randolphdcc35342017-01-19 13:33:42 -080096 return clientCamera;
97}
98
99
100Return<void> Enumerator::closeCamera(const ::android::sp<IEvsCamera>& clientCamera) {
101 ALOGD("closeCamera");
102
103 if (clientCamera.get() == nullptr) {
104 ALOGE("Ignoring call with null camera pointer.");
105 return Void();
106 }
107
108 // All our client cameras are actually VirtualCamera objects
109 // TODO (b/33492405): This will likely crash until pointers make proper round trips
110 sp<VirtualCamera> virtualCamera = reinterpret_cast<VirtualCamera*>(clientCamera.get());
111
112 // Find the parent camera that backs this virtual camera
113 sp<HalCamera> halCamera = virtualCamera->getHalCamera();
114
115 // Tell the virtual camera's parent to clean it up and drop it
116 // NOTE: The camera objects will only actually destruct when the sp<> ref counts get to
117 // zero, so it is important to break all cyclic references.
118 halCamera->disownVirtualCamera(virtualCamera);
119
120 // Did we just remove the last client of this camera?
121 if (halCamera->getClientCount() == 0) {
122 // Close the hardware camera before we go any further
123 mHwEnumerator->closeCamera(halCamera->getHwCamera());
124
125 // Take this now closed camera out of our list
126 // NOTE: This should drop our last reference to the camera, resulting in its
127 // destruction.
128 mCameras.remove(halCamera);
129 }
130
131 return Void();
132}
133
134
135Return<sp<IEvsDisplay>> Enumerator::openDisplay() {
136 ALOGD("openDisplay");
137
138 // If we already have a display active, then this request must be denied
Scott Randolph29803802017-01-25 13:11:51 -0800139 sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
140 if (pActiveDisplay != nullptr) {
Scott Randolphdcc35342017-01-19 13:33:42 -0800141 ALOGW("Rejecting openDisplay request because the display is already in use.");
142 return nullptr;
143 } else {
144 // Request exclusive access to the EVS display
145 ALOGI("Acquiring EVS Display");
Scott Randolph29803802017-01-25 13:11:51 -0800146 pActiveDisplay = mHwEnumerator->openDisplay();
147 if (pActiveDisplay == nullptr) {
Scott Randolphdcc35342017-01-19 13:33:42 -0800148 ALOGE("EVS Display unavailable");
149 }
150
Scott Randolph29803802017-01-25 13:11:51 -0800151 mActiveDisplay = pActiveDisplay;
152 return pActiveDisplay;
Scott Randolphdcc35342017-01-19 13:33:42 -0800153 }
154}
155
156
157Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
158 ALOGD("closeDisplay");
159
Scott Randolph29803802017-01-25 13:11:51 -0800160 // Do we still have a display object we think should be active?
161 sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
162
Scott Randolphdcc35342017-01-19 13:33:42 -0800163 // Drop the active display
Scott Randolph29803802017-01-25 13:11:51 -0800164 if (display.get() != pActiveDisplay.get()) {
165 ALOGW("Ignoring call to closeDisplay with unrecognzied display object.");
166 ALOGI("Got %p while active display is %p.", display.get(), pActiveDisplay.get());
Scott Randolphdcc35342017-01-19 13:33:42 -0800167 } else {
168 // Pass this request through to the hardware layer
169 mHwEnumerator->closeDisplay(display);
170 mActiveDisplay = nullptr;
171 }
172
173 return Void();
174}
175
176
177Return<DisplayState> Enumerator::getDisplayState() {
178 ALOGD("getDisplayState");
179
Scott Randolph29803802017-01-25 13:11:51 -0800180 // Do we have a display object we think should be active?
181 sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
182 if (pActiveDisplay != nullptr) {
183 // Pass this request through to the hardware layer
184 return pActiveDisplay->getDisplayState();
Scott Randolphdcc35342017-01-19 13:33:42 -0800185 } else {
Scott Randolph29803802017-01-25 13:11:51 -0800186 // We don't have a live display right now
187 mActiveDisplay = nullptr;
188 return DisplayState::NOT_OPEN;
Scott Randolphdcc35342017-01-19 13:33:42 -0800189 }
190}
191
192
193// TODO(b/31632518): Need to get notification when our client dies so we can close the camera.
Scott Randolphdcc35342017-01-19 13:33:42 -0800194
195
196} // namespace implementation
197} // namespace V1_0
198} // namespace evs
199} // namespace automotive
200} // namespace android