blob: 317d473e33560202e560046b72beffadd5853d87 [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"
Changyeon Jocd5e3fb2019-03-22 15:12:22 -070018#include "HalDisplay.h"
Scott Randolphdcc35342017-01-19 13:33:42 -080019
20namespace android {
21namespace automotive {
22namespace evs {
23namespace V1_0 {
24namespace implementation {
25
26
27bool Enumerator::init(const char* hardwareServiceName) {
28 ALOGD("init");
29
30 // Connect with the underlying hardware enumerator
31 mHwEnumerator = IEvsEnumerator::getService(hardwareServiceName);
32 bool result = (mHwEnumerator.get() != nullptr);
33
34 return result;
35}
36
37
Scott Randolphf1b55e72017-03-01 20:35:44 -080038// Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
Scott Randolphdcc35342017-01-19 13:33:42 -080039Return<void> Enumerator::getCameraList(getCameraList_cb list_cb) {
40 ALOGD("getCameraList");
41
42 // Simply pass through to hardware layer
43 return mHwEnumerator->getCameraList(list_cb);
44}
45
46
47Return<sp<IEvsCamera>> Enumerator::openCamera(const hidl_string& cameraId) {
48 ALOGD("openCamera");
49
50 // Is the underlying hardware camera already open?
51 sp<HalCamera> hwCamera;
52 for (auto &&cam : mCameras) {
53 bool match = false;
Scott Randolphda289be2017-03-30 14:30:34 -070054 cam->getHwCamera()->getCameraInfo([cameraId, &match](CameraDesc desc) {
55 if (desc.cameraId == cameraId) {
Scott Randolphdcc35342017-01-19 13:33:42 -080056 match = true;
57 }
58 }
59 );
60 if (match) {
61 hwCamera = cam;
62 break;
63 }
64 }
65
66 // Do we need to open a new hardware camera?
67 if (hwCamera == nullptr) {
68 // Is the hardware camera available?
69 sp<IEvsCamera> device = mHwEnumerator->openCamera(cameraId);
70 if (device == nullptr) {
71 ALOGE("Failed to open hardware camera %s", cameraId.c_str());
72 } else {
73 hwCamera = new HalCamera(device);
74 if (hwCamera == nullptr) {
75 ALOGE("Failed to allocate camera wrapper object");
76 mHwEnumerator->closeCamera(device);
77 }
78 }
79 }
80
81 // Construct a virtual camera wrapper for this hardware camera
82 sp<VirtualCamera> clientCamera;
83 if (hwCamera != nullptr) {
84 clientCamera = hwCamera->makeVirtualCamera();
85 }
86
Scott Randolph29803802017-01-25 13:11:51 -080087 // Add the hardware camera to our list, which will keep it alive via ref count
Scott Randolphdcc35342017-01-19 13:33:42 -080088 if (clientCamera != nullptr) {
89 mCameras.push_back(hwCamera);
90 } else {
91 ALOGE("Requested camera %s not found or not available", cameraId.c_str());
92 }
93
Scott Randolph29803802017-01-25 13:11:51 -080094 // Send the virtual camera object back to the client by strong pointer which will keep it alive
Scott Randolphdcc35342017-01-19 13:33:42 -080095 return clientCamera;
96}
97
98
99Return<void> Enumerator::closeCamera(const ::android::sp<IEvsCamera>& clientCamera) {
100 ALOGD("closeCamera");
101
102 if (clientCamera.get() == nullptr) {
103 ALOGE("Ignoring call with null camera pointer.");
104 return Void();
105 }
106
107 // All our client cameras are actually VirtualCamera objects
Scott Randolphdcc35342017-01-19 13:33:42 -0800108 sp<VirtualCamera> virtualCamera = reinterpret_cast<VirtualCamera*>(clientCamera.get());
109
110 // Find the parent camera that backs this virtual camera
111 sp<HalCamera> halCamera = virtualCamera->getHalCamera();
112
113 // Tell the virtual camera's parent to clean it up and drop it
114 // NOTE: The camera objects will only actually destruct when the sp<> ref counts get to
115 // zero, so it is important to break all cyclic references.
116 halCamera->disownVirtualCamera(virtualCamera);
117
118 // Did we just remove the last client of this camera?
119 if (halCamera->getClientCount() == 0) {
Scott Randolphda289be2017-03-30 14:30:34 -0700120 // Take this now unused camera out of our list
Scott Randolphdcc35342017-01-19 13:33:42 -0800121 // NOTE: This should drop our last reference to the camera, resulting in its
122 // destruction.
123 mCameras.remove(halCamera);
124 }
125
126 return Void();
127}
128
129
130Return<sp<IEvsDisplay>> Enumerator::openDisplay() {
131 ALOGD("openDisplay");
132
Scott Randolphda289be2017-03-30 14:30:34 -0700133 // We simply keep track of the most recently opened display instance.
134 // In the underlying layers we expect that a new open will cause the previous
135 // object to be destroyed. This avoids any race conditions associated with
136 // create/destroy order and provides a cleaner restart sequence if the previous owner
137 // is non-responsive for some reason.
138 // Request exclusive access to the EVS display
139 sp<IEvsDisplay> pActiveDisplay = mHwEnumerator->openDisplay();
140 if (pActiveDisplay == nullptr) {
141 ALOGE("EVS Display unavailable");
Changyeon Jocd5e3fb2019-03-22 15:12:22 -0700142
143 return nullptr;
Scott Randolphdcc35342017-01-19 13:33:42 -0800144 }
Scott Randolphda289be2017-03-30 14:30:34 -0700145
146 // Remember (via weak pointer) who we think the most recently opened display is so that
147 // we can proxy state requests from other callers to it.
Changyeon Jocd5e3fb2019-03-22 15:12:22 -0700148 // TODO: Because of b/129284474, an additional class, HalDisplay, has been defined and
149 // wraps the IEvsDisplay object the driver returns. We may want to remove this
150 // additional class when it is fixed properly.
151 sp<IEvsDisplay> pHalDisplay = new HalDisplay(pActiveDisplay);
152 mActiveDisplay = pHalDisplay;
153
154 return pHalDisplay;
Scott Randolphdcc35342017-01-19 13:33:42 -0800155}
156
157
158Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
159 ALOGD("closeDisplay");
160
Scott Randolph29803802017-01-25 13:11:51 -0800161 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()) {
Changyeon Jocd5e3fb2019-03-22 15:12:22 -0700165 ALOGW("Ignoring call to closeDisplay with unrecognized display object.");
Scott Randolphdcc35342017-01-19 13:33:42 -0800166 } else {
167 // Pass this request through to the hardware layer
Changyeon Jocd5e3fb2019-03-22 15:12:22 -0700168 sp<HalDisplay> halDisplay = reinterpret_cast<HalDisplay *>(pActiveDisplay.get());
169 mHwEnumerator->closeDisplay(halDisplay->getHwDisplay());
Scott Randolphdcc35342017-01-19 13:33:42 -0800170 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
Scott Randolphdcc35342017-01-19 13:33:42 -0800193} // namespace implementation
194} // namespace V1_0
195} // namespace evs
196} // namespace automotive
197} // namespace android