blob: 7e533045dbfa05fc890f522366f9a7838836ce5c [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
Scott Randolphdcc35342017-01-19 13:33:42 -080017#include "Enumerator.h"
18
19namespace android {
20namespace automotive {
21namespace evs {
22namespace V1_0 {
23namespace implementation {
24
25
26bool Enumerator::init(const char* hardwareServiceName) {
27 ALOGD("init");
28
29 // Connect with the underlying hardware enumerator
30 mHwEnumerator = IEvsEnumerator::getService(hardwareServiceName);
31 bool result = (mHwEnumerator.get() != nullptr);
32
33 return result;
34}
35
36
Scott Randolphf1b55e72017-03-01 20:35:44 -080037// Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
Scott Randolphdcc35342017-01-19 13:33:42 -080038Return<void> Enumerator::getCameraList(getCameraList_cb list_cb) {
39 ALOGD("getCameraList");
40
41 // Simply pass through to hardware layer
42 return mHwEnumerator->getCameraList(list_cb);
43}
44
45
46Return<sp<IEvsCamera>> Enumerator::openCamera(const hidl_string& cameraId) {
47 ALOGD("openCamera");
48
49 // Is the underlying hardware camera already open?
50 sp<HalCamera> hwCamera;
51 for (auto &&cam : mCameras) {
52 bool match = false;
Scott Randolphda289be2017-03-30 14:30:34 -070053 cam->getHwCamera()->getCameraInfo([cameraId, &match](CameraDesc desc) {
54 if (desc.cameraId == cameraId) {
Scott Randolphdcc35342017-01-19 13:33:42 -080055 match = true;
56 }
57 }
58 );
59 if (match) {
60 hwCamera = cam;
61 break;
62 }
63 }
64
65 // Do we need to open a new hardware camera?
66 if (hwCamera == nullptr) {
67 // Is the hardware camera available?
68 sp<IEvsCamera> device = mHwEnumerator->openCamera(cameraId);
69 if (device == nullptr) {
70 ALOGE("Failed to open hardware camera %s", cameraId.c_str());
71 } else {
72 hwCamera = new HalCamera(device);
73 if (hwCamera == nullptr) {
74 ALOGE("Failed to allocate camera wrapper object");
75 mHwEnumerator->closeCamera(device);
76 }
77 }
78 }
79
80 // Construct a virtual camera wrapper for this hardware camera
81 sp<VirtualCamera> clientCamera;
82 if (hwCamera != nullptr) {
83 clientCamera = hwCamera->makeVirtualCamera();
84 }
85
Scott Randolph29803802017-01-25 13:11:51 -080086 // Add the hardware camera to our list, which will keep it alive via ref count
Scott Randolphdcc35342017-01-19 13:33:42 -080087 if (clientCamera != nullptr) {
88 mCameras.push_back(hwCamera);
89 } else {
90 ALOGE("Requested camera %s not found or not available", cameraId.c_str());
91 }
92
Scott Randolph29803802017-01-25 13:11:51 -080093 // Send the virtual camera object back to the client by strong pointer which will keep it alive
Scott Randolphdcc35342017-01-19 13:33:42 -080094 return clientCamera;
95}
96
97
98Return<void> Enumerator::closeCamera(const ::android::sp<IEvsCamera>& clientCamera) {
99 ALOGD("closeCamera");
100
101 if (clientCamera.get() == nullptr) {
102 ALOGE("Ignoring call with null camera pointer.");
103 return Void();
104 }
105
106 // All our client cameras are actually VirtualCamera objects
Scott Randolphdcc35342017-01-19 13:33:42 -0800107 sp<VirtualCamera> virtualCamera = reinterpret_cast<VirtualCamera*>(clientCamera.get());
108
109 // Find the parent camera that backs this virtual camera
110 sp<HalCamera> halCamera = virtualCamera->getHalCamera();
111
112 // Tell the virtual camera's parent to clean it up and drop it
113 // NOTE: The camera objects will only actually destruct when the sp<> ref counts get to
114 // zero, so it is important to break all cyclic references.
115 halCamera->disownVirtualCamera(virtualCamera);
116
117 // Did we just remove the last client of this camera?
118 if (halCamera->getClientCount() == 0) {
Scott Randolphda289be2017-03-30 14:30:34 -0700119 // Take this now unused camera out of our list
Scott Randolphdcc35342017-01-19 13:33:42 -0800120 // NOTE: This should drop our last reference to the camera, resulting in its
121 // destruction.
122 mCameras.remove(halCamera);
123 }
124
125 return Void();
126}
127
128
129Return<sp<IEvsDisplay>> Enumerator::openDisplay() {
130 ALOGD("openDisplay");
131
Scott Randolphda289be2017-03-30 14:30:34 -0700132 // We simply keep track of the most recently opened display instance.
133 // In the underlying layers we expect that a new open will cause the previous
134 // object to be destroyed. This avoids any race conditions associated with
135 // create/destroy order and provides a cleaner restart sequence if the previous owner
136 // is non-responsive for some reason.
137 // Request exclusive access to the EVS display
138 sp<IEvsDisplay> pActiveDisplay = mHwEnumerator->openDisplay();
139 if (pActiveDisplay == nullptr) {
140 ALOGE("EVS Display unavailable");
Scott Randolphdcc35342017-01-19 13:33:42 -0800141 }
Scott Randolphda289be2017-03-30 14:30:34 -0700142
143 // Remember (via weak pointer) who we think the most recently opened display is so that
144 // we can proxy state requests from other callers to it.
145 mActiveDisplay = pActiveDisplay;
146 return pActiveDisplay;
Scott Randolphdcc35342017-01-19 13:33:42 -0800147}
148
149
150Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
151 ALOGD("closeDisplay");
152
Scott Randolph29803802017-01-25 13:11:51 -0800153 // Do we still have a display object we think should be active?
154 sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
155
Scott Randolphdcc35342017-01-19 13:33:42 -0800156 // Drop the active display
Scott Randolph29803802017-01-25 13:11:51 -0800157 if (display.get() != pActiveDisplay.get()) {
158 ALOGW("Ignoring call to closeDisplay with unrecognzied display object.");
159 ALOGI("Got %p while active display is %p.", display.get(), pActiveDisplay.get());
Scott Randolphdcc35342017-01-19 13:33:42 -0800160 } else {
161 // Pass this request through to the hardware layer
162 mHwEnumerator->closeDisplay(display);
163 mActiveDisplay = nullptr;
164 }
165
166 return Void();
167}
168
169
170Return<DisplayState> Enumerator::getDisplayState() {
171 ALOGD("getDisplayState");
172
Scott Randolph29803802017-01-25 13:11:51 -0800173 // Do we have a display object we think should be active?
174 sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
175 if (pActiveDisplay != nullptr) {
176 // Pass this request through to the hardware layer
177 return pActiveDisplay->getDisplayState();
Scott Randolphdcc35342017-01-19 13:33:42 -0800178 } else {
Scott Randolph29803802017-01-25 13:11:51 -0800179 // We don't have a live display right now
180 mActiveDisplay = nullptr;
181 return DisplayState::NOT_OPEN;
Scott Randolphdcc35342017-01-19 13:33:42 -0800182 }
183}
184
185
Scott Randolphdcc35342017-01-19 13:33:42 -0800186} // namespace implementation
187} // namespace V1_0
188} // namespace evs
189} // namespace automotive
190} // namespace android