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>
18 #include <bufferhub/BufferHubService.h>
19 #include <hidl/HidlSupport.h>
20 #include <log/log.h>
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
create(BufferHubService * service,const std::shared_ptr<BufferNode> & node)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
~BufferClient()43 BufferClient::~BufferClient() {
44 {
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
52 close();
53 }
54
close()55 Return<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
duplicate(duplicate_cb _hidl_cb)67 Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) {
68 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
74 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
81 const hidl_handle token = getService()->registerToken(this);
82 _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
83 return Void();
84 }
85
getService()86 sp<BufferHubService> BufferClient::getService() {
87 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
93 return service;
94 }
95
96 } // namespace implementation
97 } // namespace V1_0
98 } // namespace bufferhub
99 } // namespace frameworks
100 } // namespace android