blob: 745951745a80315b0ed784c4cb6d97b8cdcdf8aa [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>
20
21namespace android {
22namespace frameworks {
23namespace bufferhub {
24namespace V1_0 {
25namespace implementation {
26
27using hardware::hidl_handle;
28using hardware::Void;
29
Fan Xu18d90ea2018-11-06 15:46:44 -080030BufferClient* 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 Xua7422fe2018-11-19 15:21:32 -080042BufferClient::~BufferClient() {
43 close();
44}
45
46Return<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 Xuca70b7b2018-10-31 13:20:12 -070058Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) {
Fan Xua7422fe2018-11-19 15:21:32 -080059 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 Xu18d90ea2018-11-06 15:46:44 -080065 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 Xua7422fe2018-11-19 15:21:32 -080072 const hidl_handle token = getService()->registerToken(this);
73 _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
74 return Void();
75}
76
77sp<BufferHubService> BufferClient::getService() {
Fan Xu18d90ea2018-11-06 15:46:44 -080078 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 Xua7422fe2018-11-19 15:21:32 -080084 return service;
Fan Xuca70b7b2018-10-31 13:20:12 -070085}
86
87} // namespace implementation
88} // namespace V1_0
89} // namespace bufferhub
90} // namespace frameworks
91} // namespace android