blob: e312011696588eeee0475b03d9bf0047178bfa81 [file] [log] [blame]
Fan Xuca70b7b2018-10-31 13:20:12 -07001/*
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 Xu18d90ea2018-11-06 15:46:44 -080018#include <bufferhub/BufferHubService.h>
Fan Xuca70b7b2018-10-31 13:20:12 -070019#include <hidl/HidlSupport.h>
Fan Xucfbe0742018-11-21 15:03:32 -080020#include <log/log.h>
Fan Xuca70b7b2018-10-31 13:20:12 -070021
22namespace android {
23namespace frameworks {
24namespace bufferhub {
25namespace V1_0 {
26namespace implementation {
27
28using hardware::hidl_handle;
29using hardware::Void;
30
Fan Xu18d90ea2018-11-06 15:46:44 -080031BufferClient* 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 Xua7422fe2018-11-19 15:21:32 -080043BufferClient::~BufferClient() {
44 close();
45}
46
47Return<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 Xuca70b7b2018-10-31 13:20:12 -070059Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) {
Fan Xua7422fe2018-11-19 15:21:32 -080060 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 Xu18d90ea2018-11-06 15:46:44 -080066 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 Xua7422fe2018-11-19 15:21:32 -080073 const hidl_handle token = getService()->registerToken(this);
74 _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
75 return Void();
76}
77
78sp<BufferHubService> BufferClient::getService() {
Fan Xu18d90ea2018-11-06 15:46:44 -080079 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 Xua7422fe2018-11-19 15:21:32 -080085 return service;
Fan Xuca70b7b2018-10-31 13:20:12 -070086}
87
88} // namespace implementation
89} // namespace V1_0
90} // namespace bufferhub
91} // namespace frameworks
92} // namespace android