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#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
37// Methods from ::android::hardware::automotive::evs::V1_0::IEvsEnumerator follow.
38Return<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;
53        cam->getHwCamera()->getCameraInfo([cameraId, &match](CameraDesc desc) {
54                                      if (desc.cameraId == cameraId) {
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
86    // Add the hardware camera to our list, which will keep it alive via ref count
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
93    // Send the virtual camera object back to the client by strong pointer which will keep it alive
94    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
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) {
119        // Take this now unused camera out of our list
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
129Return<sp<IEvsDisplay>> Enumerator::openDisplay() {
130    ALOGD("openDisplay");
131
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");
141    }
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;
147}
148
149
150Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
151    ALOGD("closeDisplay");
152
153    // Do we still have a display object we think should be active?
154    sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
155
156    // Drop the active display
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());
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
170Return<DisplayState> Enumerator::getDisplayState()  {
171    ALOGD("getDisplayState");
172
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();
178    } else {
179        // We don't have a live display right now
180        mActiveDisplay = nullptr;
181        return DisplayState::NOT_OPEN;
182    }
183}
184
185
186} // namespace implementation
187} // namespace V1_0
188} // namespace evs
189} // namespace automotive
190} // namespace android
191