blob: 38d05246cda6a3e5b840c1cc5185097359819c4e [file] [log] [blame]
Haoxiang Li35d2a702020-04-10 01:19:32 +00001/*
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
23using namespace android_auto::surround_view;
24
25namespace android {
26namespace hardware {
27namespace automotive {
28namespace sv {
29namespace V1_0 {
30namespace implementation {
31
32std::mutex SurroundViewService::sLock;
33sp<SurroundViewService> SurroundViewService::sService;
34sp<SurroundView2dSession> SurroundViewService::sSurroundView2dSession;
35sp<SurroundView3dSession> SurroundViewService::sSurroundView3dSession;
36
37const std::string kCameraIds[] = {"0", "1", "2", "3"};
Haoxiang Lif488c7e2020-05-27 09:12:13 -070038static const int kVhalUpdateRate = 10;
39
40SurroundViewService::SurroundViewService() {
41 mVhalHandler = new VhalHandler();
Haoxiang Lid564aaf2020-06-10 22:26:37 -070042 mAnimationModule = new AnimationModule(map<string, CarPart>(),
43 map<string, CarTexture>(),
44 vector<AnimationInfo>());
Haoxiang Lif488c7e2020-05-27 09:12:13 -070045}
46
47SurroundViewService::~SurroundViewService() {
48 delete mVhalHandler;
Haoxiang Lid564aaf2020-06-10 22:26:37 -070049 delete mAnimationModule;
Haoxiang Lif488c7e2020-05-27 09:12:13 -070050}
Haoxiang Li35d2a702020-04-10 01:19:32 +000051
52sp<SurroundViewService> SurroundViewService::getInstance() {
53 std::scoped_lock<std::mutex> lock(sLock);
54 if (sService == nullptr) {
55 sService = new SurroundViewService();
Haoxiang Li070f17d2020-05-28 14:06:25 -070056 if (!sService->initialize()) {
57 LOG(ERROR) << "Cannot initialize the service properly";
58 sService = nullptr;
59 return nullptr;
60 }
Haoxiang Li35d2a702020-04-10 01:19:32 +000061 }
62 return sService;
63}
64
Haoxiang Li070f17d2020-05-28 14:06:25 -070065bool 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 Lif488c7e2020-05-27 09:12:13 -070074 // 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 Li070f17d2020-05-28 14:06:25 -070083 return true;
84}
85
Haoxiang Li35d2a702020-04-10 01:19:32 +000086Return<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
93Return<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 Lib88cd3e2020-06-04 10:27:31 -0700101 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 Li35d2a702020-04-10 01:19:32 +0000107 }
108 return {};
109}
110
111Return<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
125Return<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 Lid564aaf2020-06-10 22:26:37 -0700133 sSurroundView3dSession = new SurroundView3dSession(mEvs,
134 mVhalHandler,
135 mAnimationModule);
Haoxiang Li070f17d2020-05-28 14:06:25 -0700136 if (sSurroundView3dSession->initialize()) {
137 _hidl_cb(sSurroundView3dSession, SvResult::OK);
138 } else {
139 _hidl_cb(nullptr, SvResult::INTERNAL_ERROR);
140 }
Haoxiang Li35d2a702020-04-10 01:19:32 +0000141 }
142 return {};
143}
144
145Return<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