Haoxiang Li | 830834b | 2020-03-05 15:54:34 -0800 | [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"}; |
| 38 | |
| 39 | sp<SurroundViewService> SurroundViewService::getInstance() { |
| 40 | std::scoped_lock<std::mutex> lock(sLock); |
| 41 | if (sService == nullptr) { |
| 42 | sService = new SurroundViewService(); |
| 43 | } |
| 44 | return sService; |
| 45 | } |
| 46 | |
| 47 | Return<void> SurroundViewService::getCameraIds(getCameraIds_cb _hidl_cb) { |
| 48 | hidl_vec<hidl_string> cameraIds = {kCameraIds[0], kCameraIds[1], |
| 49 | kCameraIds[2], kCameraIds[3]}; |
| 50 | _hidl_cb(cameraIds); |
| 51 | return {}; |
| 52 | } |
| 53 | |
| 54 | Return<void> SurroundViewService::start2dSession(start2dSession_cb _hidl_cb) { |
| 55 | LOG(DEBUG) << __FUNCTION__; |
| 56 | std::scoped_lock<std::mutex> lock(sLock); |
| 57 | |
| 58 | if (sSurroundView2dSession != nullptr) { |
| 59 | LOG(WARNING) << "Only one 2d session is supported at the same time"; |
| 60 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 61 | } else { |
| 62 | sSurroundView2dSession = new SurroundView2dSession(); |
| 63 | _hidl_cb(sSurroundView2dSession, SvResult::OK); |
| 64 | } |
| 65 | return {}; |
| 66 | } |
| 67 | |
| 68 | Return<SvResult> SurroundViewService::stop2dSession( |
| 69 | const sp<ISurroundView2dSession>& sv2dSession) { |
| 70 | LOG(DEBUG) << __FUNCTION__; |
| 71 | std::scoped_lock<std::mutex> lock(sLock); |
| 72 | |
| 73 | if (sv2dSession != nullptr && sv2dSession == sSurroundView2dSession) { |
| 74 | sSurroundView2dSession = nullptr; |
| 75 | return SvResult::OK; |
| 76 | } else { |
| 77 | LOG(ERROR) << __FUNCTION__ << ": Invalid argument"; |
| 78 | return SvResult::INVALID_ARG; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | Return<void> SurroundViewService::start3dSession(start3dSession_cb _hidl_cb) { |
| 83 | LOG(DEBUG) << __FUNCTION__; |
| 84 | std::scoped_lock<std::mutex> lock(sLock); |
| 85 | |
| 86 | if (sSurroundView3dSession != nullptr) { |
| 87 | LOG(WARNING) << "Only one 3d session is supported at the same time"; |
| 88 | _hidl_cb(nullptr, SvResult::INTERNAL_ERROR); |
| 89 | } else { |
| 90 | sSurroundView3dSession = new SurroundView3dSession(); |
| 91 | _hidl_cb(sSurroundView3dSession, SvResult::OK); |
| 92 | } |
| 93 | return {}; |
| 94 | } |
| 95 | |
| 96 | Return<SvResult> SurroundViewService::stop3dSession( |
| 97 | const sp<ISurroundView3dSession>& sv3dSession) { |
| 98 | LOG(DEBUG) << __FUNCTION__; |
| 99 | std::scoped_lock<std::mutex> lock(sLock); |
| 100 | |
| 101 | if (sv3dSession != nullptr && sv3dSession == sSurroundView3dSession) { |
| 102 | sSurroundView3dSession = nullptr; |
| 103 | return SvResult::OK; |
| 104 | } else { |
| 105 | LOG(ERROR) << __FUNCTION__ << ": Invalid argument"; |
| 106 | return SvResult::INVALID_ARG; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } // namespace implementation |
| 111 | } // namespace V1_0 |
| 112 | } // namespace sv |
| 113 | } // namespace automotive |
| 114 | } // namespace hardware |
| 115 | } // namespace android |
| 116 | |