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() { |
| 44 | close(); |
| 45 | } |
| 46 | |
| 47 | Return<BufferHubStatus> BufferClient::close() { |
| 48 | std::lock_guard<std::mutex> lock(mClosedMutex); |
| 49 | if (mClosed) { |
| 50 | return BufferHubStatus::CLIENT_CLOSED; |
| 51 | } |
| 52 | |
| 53 | getService()->onClientClosed(this); |
| 54 | mBufferNode.reset(); |
| 55 | mClosed = true; |
| 56 | return BufferHubStatus::NO_ERROR; |
| 57 | } |
| 58 | |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 59 | Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) { |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 60 | std::lock_guard<std::mutex> lock(mClosedMutex); |
| 61 | if (mClosed) { |
| 62 | _hidl_cb(/*token=*/hidl_handle(), /*status=*/BufferHubStatus::CLIENT_CLOSED); |
| 63 | return Void(); |
| 64 | } |
| 65 | |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 66 | if (!mBufferNode) { |
| 67 | // Should never happen |
| 68 | ALOGE("%s: node is missing.", __FUNCTION__); |
| 69 | _hidl_cb(/*token=*/hidl_handle(), /*status=*/BufferHubStatus::BUFFER_FREED); |
| 70 | return Void(); |
| 71 | } |
| 72 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 73 | const hidl_handle token = getService()->registerToken(this); |
| 74 | _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR); |
| 75 | return Void(); |
| 76 | } |
| 77 | |
| 78 | sp<BufferHubService> BufferClient::getService() { |
Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 79 | sp<BufferHubService> service = mService.promote(); |
| 80 | if (service == nullptr) { |
| 81 | // Should never happen. Kill the process. |
| 82 | LOG_FATAL("%s: service died.", __FUNCTION__); |
| 83 | } |
| 84 | |
Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 85 | return service; |
Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } // namespace implementation |
| 89 | } // namespace V1_0 |
| 90 | } // namespace bufferhub |
| 91 | } // namespace frameworks |
| 92 | } // namespace android |