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 | |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 17 | #include "Enumerator.h" |
| 18 | |
| 19 | namespace android { |
| 20 | namespace automotive { |
| 21 | namespace evs { |
| 22 | namespace V1_0 { |
| 23 | namespace implementation { |
| 24 | |
| 25 | |
| 26 | bool 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 Randolph | f1b55e7 | 2017-03-01 20:35:44 -0800 | [diff] [blame] | 37 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow. |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 38 | Return<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 | |
| 46 | Return<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 Randolph | da289be | 2017-03-30 14:30:34 -0700 | [diff] [blame] | 53 | cam->getHwCamera()->getCameraInfo([cameraId, &match](CameraDesc desc) { |
| 54 | if (desc.cameraId == cameraId) { |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 55 | 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 Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 86 | // Add the hardware camera to our list, which will keep it alive via ref count |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 87 | 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 Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 93 | // Send the virtual camera object back to the client by strong pointer which will keep it alive |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 94 | return clientCamera; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | Return<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 Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 107 | 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 Randolph | da289be | 2017-03-30 14:30:34 -0700 | [diff] [blame] | 119 | // Take this now unused camera out of our list |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 120 | // 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 | |
| 129 | Return<sp<IEvsDisplay>> Enumerator::openDisplay() { |
| 130 | ALOGD("openDisplay"); |
| 131 | |
Scott Randolph | da289be | 2017-03-30 14:30:34 -0700 | [diff] [blame] | 132 | // 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 Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 141 | } |
Scott Randolph | da289be | 2017-03-30 14:30:34 -0700 | [diff] [blame] | 142 | |
| 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 Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) { |
| 151 | ALOGD("closeDisplay"); |
| 152 | |
Scott Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 153 | // Do we still have a display object we think should be active? |
| 154 | sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote(); |
| 155 | |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 156 | // Drop the active display |
Scott Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 157 | 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 Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 160 | } 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 | |
| 170 | Return<DisplayState> Enumerator::getDisplayState() { |
| 171 | ALOGD("getDisplayState"); |
| 172 | |
Scott Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 173 | // 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 Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 178 | } else { |
Scott Randolph | 2980380 | 2017-01-25 13:11:51 -0800 | [diff] [blame] | 179 | // We don't have a live display right now |
| 180 | mActiveDisplay = nullptr; |
| 181 | return DisplayState::NOT_OPEN; |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | |
Scott Randolph | dcc3534 | 2017-01-19 13:33:42 -0800 | [diff] [blame] | 186 | } // namespace implementation |
| 187 | } // namespace V1_0 |
| 188 | } // namespace evs |
| 189 | } // namespace automotive |
| 190 | } // namespace android |