• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_HUB_SERVICE_H
18 #define ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_HUB_SERVICE_H
19 
20 #include <map>
21 #include <mutex>
22 #include <set>
23 #include <vector>
24 
25 #include <android/frameworks/bufferhub/1.0/IBufferHub.h>
26 #include <bufferhub/BufferClient.h>
27 #include <bufferhub/BufferHubIdGenerator.h>
28 #include <utils/Mutex.h>
29 
30 namespace android {
31 namespace frameworks {
32 namespace bufferhub {
33 namespace V1_0 {
34 namespace implementation {
35 
36 using hardware::hidl_handle;
37 using hardware::hidl_string;
38 using hardware::hidl_vec;
39 using hardware::Return;
40 using hardware::graphics::common::V1_2::HardwareBufferDescription;
41 
42 class BufferHubService : public IBufferHub {
43 public:
44     BufferHubService();
45 
46     Return<void> allocateBuffer(const HardwareBufferDescription& description,
47                                 const uint32_t userMetadataSize,
48                                 allocateBuffer_cb _hidl_cb) override;
49     Return<void> importBuffer(const hidl_handle& tokenHandle, importBuffer_cb _hidl_cb) override;
50 
51     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
52 
53     // Non-binder functions
54     // Internal help function for IBufferClient::duplicate.
55     hidl_handle registerToken(const wp<BufferClient>& client);
56 
57     void onClientClosed(const BufferClient* client);
58 
59 private:
60     // Helper function to build BufferTraits.bufferInfo handle
61     hidl_handle buildBufferInfo(char* bufferInfoStorage, int bufferId, uint32_t clientBitMask,
62                                 uint32_t userMetadataSize, int metadataFd, int eventFd);
63 
64     // Helper function to remove all the token belongs to a specific client.
65     void removeTokenByClient(const BufferClient* client);
66 
67     // List of active BufferClient for bookkeeping.
68     std::mutex mClientSetMutex;
69     std::set<wp<BufferClient>> mClientSet GUARDED_BY(mClientSetMutex);
70 
71     // Token generation related
72     // A random number used as private key for HMAC
73     uint64_t mKey;
74     static constexpr size_t kKeyLen = sizeof(uint64_t);
75 
76     std::mutex mTokenMutex;
77     // The first TokenId will be 1. TokenId could be negative.
78     int mLastTokenId GUARDED_BY(mTokenMutex) = 0;
79     static constexpr size_t mTokenIdSize = sizeof(int);
80     // A map from token id to the token-buffer_client pair. Using token id as the key to reduce
81     // looking up time
82     std::map<int, std::pair<std::vector<uint8_t>, const wp<BufferClient>>> mTokenMap
83             GUARDED_BY(mTokenMutex);
84 };
85 
86 } // namespace implementation
87 } // namespace V1_0
88 } // namespace bufferhub
89 } // namespace frameworks
90 } // namespace android
91 
92 #endif // ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_HUB_SERVICE_H
93