Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <bufferhub/BufferClient.h> |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 18 | #include <bufferhub/BufferHubService.h> |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 19 | #include <hidl/HidlSupport.h> |
Fan Xu | cfbe074 | 2018-11-21 15:03:32 -0800 | [diff] [blame] | 20 | #include <log/log.h> |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace frameworks { |
| 24 | namespace bufferhub { |
| 25 | namespace V1_0 { |
| 26 | namespace implementation { |
| 27 | |
| 28 | using hardware::hidl_handle; |
| 29 | using hardware::Void; |
| 30 | |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 31 | BufferClient* BufferClient::create(BufferHubService* service, |
| 32 | const std::shared_ptr<BufferNode>& node) { |
| 33 | if (!service) { |
| 34 | ALOGE("%s: service cannot be nullptr.", __FUNCTION__); |
| 35 | return nullptr; |
| 36 | } else if (!node) { |
| 37 | ALOGE("%s: node cannot be nullptr.", __FUNCTION__); |
| 38 | return nullptr; |
| 39 | } |
| 40 | return new BufferClient(service, node); |
| 41 | } |
| 42 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 43 | BufferClient::~BufferClient() { |
Fan Xu | 7da1c05 | 2019-01-16 15:31:04 -0800 | [diff] [blame] | 44 | { |
| 45 | std::lock_guard<std::mutex> lock(mClosedMutex); |
| 46 | if (!mClosed) { |
| 47 | ALOGW("%s: client of buffer #%d destroyed without close. Closing it now.", __FUNCTION__, |
| 48 | mBufferNode->id()); |
| 49 | } |
| 50 | } |
| 51 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 52 | close(); |
| 53 | } |
| 54 | |
| 55 | Return<BufferHubStatus> BufferClient::close() { |
| 56 | std::lock_guard<std::mutex> lock(mClosedMutex); |
| 57 | if (mClosed) { |
| 58 | return BufferHubStatus::CLIENT_CLOSED; |
| 59 | } |
| 60 | |
| 61 | getService()->onClientClosed(this); |
| 62 | mBufferNode.reset(); |
| 63 | mClosed = true; |
| 64 | return BufferHubStatus::NO_ERROR; |
| 65 | } |
| 66 | |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 67 | Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) { |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 68 | std::lock_guard<std::mutex> lock(mClosedMutex); |
| 69 | if (mClosed) { |
| 70 | _hidl_cb(/*token=*/hidl_handle(), /*status=*/BufferHubStatus::CLIENT_CLOSED); |
| 71 | return Void(); |
| 72 | } |
| 73 | |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 74 | if (!mBufferNode) { |
| 75 | // Should never happen |
| 76 | ALOGE("%s: node is missing.", __FUNCTION__); |
| 77 | _hidl_cb(/*token=*/hidl_handle(), /*status=*/BufferHubStatus::BUFFER_FREED); |
| 78 | return Void(); |
| 79 | } |
| 80 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 81 | const hidl_handle token = getService()->registerToken(this); |
| 82 | _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR); |
| 83 | return Void(); |
| 84 | } |
| 85 | |
| 86 | sp<BufferHubService> BufferClient::getService() { |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 87 | sp<BufferHubService> service = mService.promote(); |
| 88 | if (service == nullptr) { |
| 89 | // Should never happen. Kill the process. |
| 90 | LOG_FATAL("%s: service died.", __FUNCTION__); |
| 91 | } |
| 92 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 93 | return service; |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | } // namespace implementation |
| 97 | } // namespace V1_0 |
| 98 | } // namespace bufferhub |
| 99 | } // namespace frameworks |
| 100 | } // namespace android |