Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | #define LOG_TAG "SurroundViewService" |
| 17 | |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include "CoreLibSetupHelper.h" |
| 21 | #include "SurroundViewService.h" |
| 22 | |
| 23 | using namespace android_auto::surround_view; |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace automotive { |
| 28 | namespace sv { |
| 29 | namespace V1_0 { |
| 30 | namespace implementation { |
| 31 | |
| 32 | std::mutex SurroundViewService::sLock; |
| 33 | sp<SurroundViewService> SurroundViewService::sService; |
| 34 | sp<SurroundView2dSession> SurroundViewService::sSurroundView2dSession; |
| 35 | sp<SurroundView3dSession> SurroundViewService::sSurroundView3dSession; |
| 36 | |
| 37 | const std::string kCameraIds[] = {"0", "1", "2", "3"}; |
Haoxiang Li | f488c7e | 2020-05-27 09:12:13 -0700 | [diff] [blame] | 38 | static const int kVhalUpdateRate = 10; |
| 39 | |
| 40 | SurroundViewService::SurroundViewService() { |
| 41 | mVhalHandler = new VhalHandler(); |
Haoxiang Li | d564aaf | 2020-06-10 22:26:37 -0700 | [diff] [blame] | 42 | mAnimationModule = new AnimationModule(map<string, CarPart>(), |
| 43 | map<string, CarTexture>(), |
| 44 | vector<AnimationInfo>()); |
Haoxiang Li | f488c7e | 2020-05-27 09:12:13 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | SurroundViewService::~SurroundViewService() { |
| 48 | delete mVhalHandler; |
Haoxiang Li | d564aaf | 2020-06-10 22:26:37 -0700 | [diff] [blame] | 49 | delete mAnimationModule; |
Haoxiang Li | f488c7e | 2020-05-27 09:12:13 -0700 | [diff] [blame] | 50 | } |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 51 | |
| 52 | sp<SurroundViewService> SurroundViewService::getInstance() { |
| 53 | std::scoped_lock<std::mutex> lock(sLock); |
| 54 | if (sService == nullptr) { |
| 55 | sService = new SurroundViewService(); |
Haoxiang Li | 070f17d | 2020-05-28 14:06:25 -0700 | [diff] [blame] | 56 | if (!sService->initialize()) { |
| 57 | LOG(ERROR) << "Cannot initialize the service properly"; |
| 58 | sService = nullptr; |
| 59 | return nullptr; |
| 60 | } |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 61 | } |
| 62 | return sService; |
| 63 | } |
| 64 | |
Haoxiang Li | 070f17d | 2020-05-28 14:06:25 -0700 | [diff] [blame] | 65 | bool SurroundViewService::initialize() { |
| 66 | // Get the EVS manager service |
| 67 | LOG(INFO) << "Acquiring EVS Enumerator"; |
| 68 | mEvs = IEvsEnumerator::getService("default"); |
| 69 | if (mEvs == nullptr) { |
| 70 | LOG(ERROR) << "getService returned NULL. Exiting."; |
| 71 | return false; |
| 72 | } |
| 73 | |
Haoxiang Li | f488c7e | 2020-05-27 09:12:13 -0700 | [diff] [blame] | 74 | // Initialize the VHal Handler with update method and rate. |
| 75 | // TODO(b/157498592): The update rate should align with the EVS camera |
| 76 | // update rate. |
| 77 | if (mVhalHandler->initialize(VhalHandler::GET, kVhalUpdateRate)) { |
| 78 | mVhalHandler->setPropertiesToRead(vector<VehiclePropValue>()); |
| 79 | } else { |
| 80 | LOG(WARNING) << "VhalHandler cannot be initialized properly"; |
| 81 | } |
| 82 | |
Haoxiang Li | 070f17d | 2020-05-28 14:06:25 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 86 | Return<void> SurroundViewService::getCameraIds(getCameraIds_cb _hidl_cb) { |
| 87 | hidl_vec<hidl_string> cameraIds = {kCameraIds[0], kCameraIds[1], |
| 88 | kCameraIds[2], kCameraIds[3]}; |
| 89 | _hidl_cb(cameraIds); |
| 90 | return {}; |
| 91 | } |
| 92 | |
| 93 | Return<void> SurroundViewService::start2dSession(start2dSession_cb _hidl_cb) { |
| 94 | LOG(DEBUG) << __FUNCTION__; |
| 95 | std::scoped_lock<std::mutex> lock(sLock); |
| 96 | |
| 97 | if (sSurroundView2dSession != nullptr) { |
| 98 | LOG(WARNING) << "Only one 2d session is supported at the same time"; |
| 99 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 100 | } else { |
Haoxiang Li | b88cd3e | 2020-06-04 10:27:31 -0700 | [diff] [blame] | 101 | sSurroundView2dSession = new SurroundView2dSession(mEvs); |
| 102 | if (sSurroundView2dSession->initialize()) { |
| 103 | _hidl_cb(sSurroundView2dSession, SvResult::OK); |
| 104 | } else { |
| 105 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 106 | } |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 107 | } |
| 108 | return {}; |
| 109 | } |
| 110 | |
| 111 | Return<SvResult> SurroundViewService::stop2dSession( |
| 112 | const sp<ISurroundView2dSession>& sv2dSession) { |
| 113 | LOG(DEBUG) << __FUNCTION__; |
| 114 | std::scoped_lock<std::mutex> lock(sLock); |
| 115 | |
| 116 | if (sv2dSession != nullptr && sv2dSession == sSurroundView2dSession) { |
| 117 | sSurroundView2dSession = nullptr; |
| 118 | return SvResult::OK; |
| 119 | } else { |
| 120 | LOG(ERROR) << __FUNCTION__ << ": Invalid argument"; |
| 121 | return SvResult::INVALID_ARG; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | Return<void> SurroundViewService::start3dSession(start3dSession_cb _hidl_cb) { |
| 126 | LOG(DEBUG) << __FUNCTION__; |
| 127 | std::scoped_lock<std::mutex> lock(sLock); |
| 128 | |
| 129 | if (sSurroundView3dSession != nullptr) { |
| 130 | LOG(WARNING) << "Only one 3d session is supported at the same time"; |
| 131 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 132 | } else { |
Haoxiang Li | d564aaf | 2020-06-10 22:26:37 -0700 | [diff] [blame] | 133 | sSurroundView3dSession = new SurroundView3dSession(mEvs, |
| 134 | mVhalHandler, |
| 135 | mAnimationModule); |
Haoxiang Li | 070f17d | 2020-05-28 14:06:25 -0700 | [diff] [blame] | 136 | if (sSurroundView3dSession->initialize()) { |
| 137 | _hidl_cb(sSurroundView3dSession, SvResult::OK); |
| 138 | } else { |
| 139 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 140 | } |
Haoxiang Li | 35d2a70 | 2020-04-10 01:19:32 +0000 | [diff] [blame] | 141 | } |
| 142 | return {}; |
| 143 | } |
| 144 | |
| 145 | Return<SvResult> SurroundViewService::stop3dSession( |
| 146 | const sp<ISurroundView3dSession>& sv3dSession) { |
| 147 | LOG(DEBUG) << __FUNCTION__; |
| 148 | std::scoped_lock<std::mutex> lock(sLock); |
| 149 | |
| 150 | if (sv3dSession != nullptr && sv3dSession == sSurroundView3dSession) { |
| 151 | sSurroundView3dSession = nullptr; |
| 152 | return SvResult::OK; |
| 153 | } else { |
| 154 | LOG(ERROR) << __FUNCTION__ << ": Invalid argument"; |
| 155 | return SvResult::INVALID_ARG; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } // namespace implementation |
| 160 | } // namespace V1_0 |
| 161 | } // namespace sv |
| 162 | } // namespace automotive |
| 163 | } // namespace hardware |
| 164 | } // namespace android |
| 165 | |