Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 21 | namespace android { |
| 22 | namespace automotive { |
| 23 | namespace evs { |
| 24 | namespace V1_0 { |
| 25 | namespace implementation { |
| 26 | |
| 27 | |
| 28 | bool 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. |
| 40 | Return<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 | |
| 48 | Return<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 | |
| 88 | // Add the hardware camera to our list, which will keep the object alive via ref count |
| 89 | 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 | |
| 95 | // Send the virtual camera object (also owned by our hwCamera) back to the client |
| 96 | return clientCamera; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | Return<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 | |
| 135 | Return<sp<IEvsDisplay>> Enumerator::openDisplay() { |
| 136 | ALOGD("openDisplay"); |
| 137 | |
| 138 | // If we already have a display active, then this request must be denied |
| 139 | if (mActiveDisplay != nullptr) { |
| 140 | ALOGW("Rejecting openDisplay request because the display is already in use."); |
| 141 | return nullptr; |
| 142 | } else { |
| 143 | // Request exclusive access to the EVS display |
| 144 | ALOGI("Acquiring EVS Display"); |
| 145 | |
| 146 | mActiveDisplay = mHwEnumerator->openDisplay(); |
| 147 | if (mActiveDisplay.get() == nullptr) { |
| 148 | ALOGE("EVS Display unavailable"); |
| 149 | } |
| 150 | |
| 151 | return mActiveDisplay; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) { |
| 157 | ALOGD("closeDisplay"); |
| 158 | |
| 159 | // Drop the active display |
| 160 | if (display != mActiveDisplay) { |
| 161 | ALOGW("Ignoring call to closeDisplay with a display object we don't recognize."); |
| 162 | } else { |
| 163 | // Pass this request through to the hardware layer |
| 164 | mHwEnumerator->closeDisplay(display); |
| 165 | mActiveDisplay = nullptr; |
| 166 | } |
| 167 | |
| 168 | return Void(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | Return<DisplayState> Enumerator::getDisplayState() { |
| 173 | ALOGD("getDisplayState"); |
| 174 | |
| 175 | // Pass this request through to the hardware layer |
| 176 | if (mHwEnumerator == nullptr) { |
| 177 | // If we haven't even been initialized, this should not be called |
| 178 | ALOGE("getDisplayState called on incompletely initialized EvsEnumerator"); |
| 179 | return DisplayState::NOT_OPEN; |
| 180 | } else if (mActiveDisplay == nullptr) { |
| 181 | // We actually DO hold the underlying hardware display open (in order to reserve it) |
| 182 | // but the application hasn't opened it, so we know it isn't actually doing anything. |
| 183 | return DisplayState::NOT_OPEN; |
| 184 | } else { |
| 185 | return mActiveDisplay->getDisplayState(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | // TODO(b/31632518): Need to get notification when our client dies so we can close the camera. |
| 191 | // As possible work around would be to give the client a HIDL object to exclusively hold |
| 192 | // and use it's destructor to perform some work in the server side. |
| 193 | |
| 194 | |
| 195 | } // namespace implementation |
| 196 | } // namespace V1_0 |
| 197 | } // namespace evs |
| 198 | } // namespace automotive |
| 199 | } // namespace android |