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 
init(const char * hardwareServiceName)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::automotive::evs::V1_0::IEvsEnumerator follow.
getCameraList(getCameraList_cb list_cb)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 
openCamera(const hidl_string & cameraId)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()->getCameraInfo([cameraId, &match](CameraDesc desc) {
56                                       if (desc.cameraId == 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 it 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 back to the client by strong pointer which will keep it alive
96     return clientCamera;
97 }
98 
99 
closeCamera(const::android::sp<IEvsCamera> & clientCamera)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     sp<VirtualCamera> virtualCamera = reinterpret_cast<VirtualCamera*>(clientCamera.get());
110 
111     // Find the parent camera that backs this virtual camera
112     sp<HalCamera> halCamera = virtualCamera->getHalCamera();
113 
114     // Tell the virtual camera's parent to clean it up and drop it
115     // NOTE:  The camera objects will only actually destruct when the sp<> ref counts get to
116     //        zero, so it is important to break all cyclic references.
117     halCamera->disownVirtualCamera(virtualCamera);
118 
119     // Did we just remove the last client of this camera?
120     if (halCamera->getClientCount() == 0) {
121         // Take this now unused camera out of our list
122         // NOTE:  This should drop our last reference to the camera, resulting in its
123         //        destruction.
124         mCameras.remove(halCamera);
125     }
126 
127     return Void();
128 }
129 
130 
openDisplay()131 Return<sp<IEvsDisplay>> Enumerator::openDisplay() {
132     ALOGD("openDisplay");
133 
134     // We simply keep track of the most recently opened display instance.
135     // In the underlying layers we expect that a new open will cause the previous
136     // object to be destroyed.  This avoids any race conditions associated with
137     // create/destroy order and provides a cleaner restart sequence if the previous owner
138     // is non-responsive for some reason.
139     // Request exclusive access to the EVS display
140     sp<IEvsDisplay> pActiveDisplay = mHwEnumerator->openDisplay();
141     if (pActiveDisplay == nullptr) {
142         ALOGE("EVS Display unavailable");
143     }
144 
145     // Remember (via weak pointer) who we think the most recently opened display is so that
146     // we can proxy state requests from other callers to it.
147     mActiveDisplay = pActiveDisplay;
148     return pActiveDisplay;
149 }
150 
151 
closeDisplay(const::android::sp<IEvsDisplay> & display)152 Return<void> Enumerator::closeDisplay(const ::android::sp<IEvsDisplay>& display) {
153     ALOGD("closeDisplay");
154 
155     // Do we still have a display object we think should be active?
156     sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
157 
158     // Drop the active display
159     if (display.get() != pActiveDisplay.get()) {
160         ALOGW("Ignoring call to closeDisplay with unrecognzied display object.");
161         ALOGI("Got %p while active display is %p.", display.get(), pActiveDisplay.get());
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 
getDisplayState()172 Return<DisplayState> Enumerator::getDisplayState()  {
173     ALOGD("getDisplayState");
174 
175     // Do we have a display object we think should be active?
176     sp<IEvsDisplay> pActiveDisplay = mActiveDisplay.promote();
177     if (pActiveDisplay != nullptr) {
178         // Pass this request through to the hardware layer
179         return pActiveDisplay->getDisplayState();
180     } else {
181         // We don't have a live display right now
182         mActiveDisplay = nullptr;
183         return DisplayState::NOT_OPEN;
184     }
185 }
186 
187 
188 } // namespace implementation
189 } // namespace V1_0
190 } // namespace evs
191 } // namespace automotive
192 } // namespace android
193