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