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 #ifndef ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_CLIENT_H
18 #define ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_CLIENT_H
19 
20 #include <mutex>
21 
22 #include <android/frameworks/bufferhub/1.0/IBufferClient.h>
23 #include <bufferhub/BufferNode.h>
24 
25 namespace android {
26 namespace frameworks {
27 namespace bufferhub {
28 namespace V1_0 {
29 namespace implementation {
30 
31 using hardware::hidl_handle;
32 using hardware::Return;
33 
34 // Forward declaration to avoid circular dependency
35 class BufferHubService;
36 
37 class BufferClient : public IBufferClient {
38 public:
39     // Creates a server-side buffer client from an existing BufferNode. Note that
40     // this function takes ownership of the shared_ptr.
41     // Returns a raw pointer to the BufferClient on success, nullptr on failure.
42     static BufferClient* create(BufferHubService* service, const std::shared_ptr<BufferNode>& node);
43 
44     // Creates a BufferClient from an existing BufferClient. Will share the same BufferNode.
BufferClient(const BufferClient & other)45     explicit BufferClient(const BufferClient& other)
46           : mService(other.mService), mBufferNode(other.mBufferNode) {}
47     ~BufferClient();
48 
49     Return<BufferHubStatus> close() override;
50     Return<void> duplicate(duplicate_cb _hidl_cb) override;
51 
52     // Non-binder functions
getBufferNode()53     const std::shared_ptr<BufferNode>& getBufferNode() const { return mBufferNode; }
54 
55 private:
BufferClient(wp<BufferHubService> service,const std::shared_ptr<BufferNode> & node)56     BufferClient(wp<BufferHubService> service, const std::shared_ptr<BufferNode>& node)
57           : mService(service), mBufferNode(node) {}
58 
59     sp<BufferHubService> getService();
60 
61     wp<BufferHubService> mService;
62 
63     std::mutex mClosedMutex;
64     bool mClosed GUARDED_BY(mClosedMutex) = false;
65 
66     std::shared_ptr<BufferNode> mBufferNode;
67 };
68 
69 } // namespace implementation
70 } // namespace V1_0
71 } // namespace bufferhub
72 } // namespace frameworks
73 } // namespace android
74 
75 #endif
76