blob: ec7e5356992e14ff5c74cf151b4d8f526638c632 [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() {
Fan Xu7da1c052019-01-16 15:31:04 -080044 {
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 Xua7422fe2018-11-19 15:21:32 -080052 close();
53}
54
55Return<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 Xuca70b7b2018-10-31 13:20:12 -070067Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) {
Fan Xua7422fe2018-11-19 15:21:32 -080068 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 Xu18d90ea2018-11-06 15:46:44 -080074 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 Xua7422fe2018-11-19 15:21:32 -080081 const hidl_handle token = getService()->registerToken(this);
82 _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
83 return Void();
84}
85
86sp<BufferHubService> BufferClient::getService() {
Fan Xu18d90ea2018-11-06 15:46:44 -080087 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 Xua7422fe2018-11-19 15:21:32 -080093 return service;
Fan Xuca70b7b2018-10-31 13:20:12 -070094}
95
96} // namespace implementation
97} // namespace V1_0
98} // namespace bufferhub
99} // namespace frameworks
100} // namespace android