1 /*
2 * Copyright (C) 2019 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 "ResourceManager.h"
18
19 namespace android {
20 namespace automotive {
21 namespace evs {
22 namespace support {
23
24 using ::std::lock_guard;
25
26 const string ResourceManager::kDefaultServiceName = "default";
27 sp<ResourceManager> ResourceManager::sInstance;
28 mutex ResourceManager::sLockSingleton;
29 mutex ResourceManager::sLockEvs;
30 sp<IEvsEnumerator> ResourceManager::sEvs;
31
getEvsEnumerator(string serviceName)32 sp<IEvsEnumerator> ResourceManager::getEvsEnumerator(string serviceName) {
33 lock_guard<mutex> lock(sLockEvs);
34 if (sEvs.get() == nullptr) {
35 sEvs = IEvsEnumerator::getService(serviceName);
36 }
37 return sEvs;
38 }
39
getInstance()40 sp<ResourceManager> ResourceManager::getInstance() {
41 lock_guard<mutex> lock(sLockSingleton);
42 if (sInstance == nullptr) {
43 ALOGD("Creating new ResourceManager instance");
44 sInstance = new ResourceManager();
45 }
46 return sInstance;
47 }
48
obtainStreamHandler(string pCameraId)49 sp<StreamHandler> ResourceManager::obtainStreamHandler(string pCameraId) {
50 ALOGD("ResourceManager::obtainStreamHandler");
51
52 // Lock for stream handler related methods.
53 lock_guard<mutex> lock(mLockStreamHandler);
54
55 auto result = mCameraInstances.find(pCameraId);
56 if (result == mCameraInstances.end()) {
57 sp<CameraInstance> instance = new CameraInstance();
58
59 // CameraInstance::useCaseCount
60 instance->useCaseCount++;
61
62 // CameraInstance::cameraId
63 instance->cameraId = pCameraId;
64
65 // CameraInstance::camera
66 instance->camera = getEvsEnumerator()->openCamera(pCameraId);
67 if (instance->camera.get() == nullptr) {
68 ALOGE("Failed to allocate new EVS Camera interface for %s", pCameraId.c_str());
69 return nullptr;
70 }
71
72 // CameraInstance::handler
73 instance->handler = new StreamHandler(instance->camera);
74 if (instance->handler == nullptr) {
75 ALOGE("Failed to create stream handler for %s", pCameraId.c_str());
76 }
77
78 // Move the newly-created instance into vector, and the vector takes
79 // ownership of the instance.
80 mCameraInstances.emplace(pCameraId, instance);
81
82 return instance->handler;
83 } else {
84 auto instance = result->second;
85 instance->useCaseCount++;
86
87 return instance->handler;
88 }
89 }
90
releaseStreamHandler(string pCameraId)91 void ResourceManager::releaseStreamHandler(string pCameraId) {
92 ALOGD("ResourceManager::releaseStreamHandler");
93
94 // Lock for stream handler related methods.
95 lock_guard<mutex> lock(mLockStreamHandler);
96
97 auto result = mCameraInstances.find(pCameraId);
98 if (result == mCameraInstances.end()) {
99 ALOGW("No stream handler is active with camera id %s", pCameraId.c_str());
100 } else {
101 auto instance = result->second;
102 instance->useCaseCount--;
103
104 if (instance->useCaseCount <= 0) {
105 // The vector keeps the only strong reference to the camera
106 // instance. Once the instance is erased from the vector, the
107 // override onLastStrongRef method for CameraInstance class will
108 // be called and clean up the resources.
109 mCameraInstances.erase(result);
110 }
111 }
112 }
113
114 // TODO(b/130246434): have further discussion about how the display resource
115 // should be managed.
openDisplay()116 sp<IEvsDisplay> ResourceManager::openDisplay() {
117 // Lock for display related methods.
118 lock_guard<mutex> lock(mLockDisplay);
119
120 if (mDisplay.get() == nullptr) {
121 mDisplay = getEvsEnumerator()->openDisplay();
122 if (mDisplay.get() != nullptr) {
123 ALOGD("Evs display is opened");
124 } else {
125 ALOGE("Failed to open evs display.");
126 }
127 }
128
129 return mDisplay;
130 }
131
closeDisplay(sp<IEvsDisplay> pDisplay)132 void ResourceManager::closeDisplay(sp<IEvsDisplay> pDisplay) {
133 // Lock for display related methods.
134 lock_guard<mutex> lock(mLockDisplay);
135
136 // Even though there are logics in evs manager to prevent errors from
137 // unrecognized IEvsDisplay object, we still want to check whether the
138 // incoming pDisplay is the one we opened earlier in resource manager. So
139 // when developer make mistakes by passing in incorrect IEvsDisplay object,
140 // we know that we should not proceed and the active display is still
141 // opened.
142 if (mDisplay.get() == pDisplay.get()) {
143 getEvsEnumerator()->closeDisplay(mDisplay);
144 mDisplay = nullptr;
145 ALOGD("Evs display is closed");
146 } else {
147 ALOGW("Ignored! Unrecognized display object for closeDisplay method");
148 }
149 }
150
isDisplayOpened()151 bool ResourceManager::isDisplayOpened() {
152 // Lock for display related methods.
153 lock_guard<mutex> lock(mLockDisplay);
154
155 return mDisplay.get() != nullptr;
156 }
157
158 } // namespace support
159 } // namespace evs
160 } // namespace automotive
161 } // namespace android
162