blob: edad20b19412d4c4fafe4f14084e7cef9816fba0 [file] [log] [blame]
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -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#ifndef ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_HUB_SERVICE_H
18#define ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_HUB_SERVICE_H
19
Fan Xu55b26a62018-12-19 11:03:14 -080020#include <map>
Fan Xu93c94902018-11-01 12:22:05 -070021#include <mutex>
Fan Xu55b26a62018-12-19 11:03:14 -080022#include <set>
23#include <vector>
Fan Xu93c94902018-11-01 12:22:05 -070024
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070025#include <android/frameworks/bufferhub/1.0/IBufferHub.h>
Fan Xu93c94902018-11-01 12:22:05 -070026#include <bufferhub/BufferClient.h>
Fan Xu1c16df52018-11-19 16:27:27 -080027#include <bufferhub/BufferHubIdGenerator.h>
Fan Xu93c94902018-11-01 12:22:05 -070028#include <utils/Mutex.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070029
30namespace android {
31namespace frameworks {
32namespace bufferhub {
33namespace V1_0 {
34namespace implementation {
35
Fan Xu18d90ea2018-11-06 15:46:44 -080036using hardware::hidl_handle;
Fan Xufe097c72018-12-07 15:46:51 -080037using hardware::hidl_string;
38using hardware::hidl_vec;
Fan Xu18d90ea2018-11-06 15:46:44 -080039using hardware::Return;
40using hardware::graphics::common::V1_2::HardwareBufferDescription;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070041
42class BufferHubService : public IBufferHub {
43public:
Fan Xu55b26a62018-12-19 11:03:14 -080044 BufferHubService();
45
Fan Xuca70b7b2018-10-31 13:20:12 -070046 Return<void> allocateBuffer(const HardwareBufferDescription& description,
47 const uint32_t userMetadataSize,
48 allocateBuffer_cb _hidl_cb) override;
Fan Xu467e08f2018-11-09 15:58:51 -080049 Return<void> importBuffer(const hidl_handle& tokenHandle, importBuffer_cb _hidl_cb) override;
Fan Xu93c94902018-11-01 12:22:05 -070050
Fan Xufe097c72018-12-07 15:46:51 -080051 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
52
Fan Xu18d90ea2018-11-06 15:46:44 -080053 // Non-binder functions
54 // Internal help function for IBufferClient::duplicate.
Fan Xud6cd6ba2018-11-15 16:46:55 -080055 hidl_handle registerToken(const wp<BufferClient>& client);
Fan Xu18d90ea2018-11-06 15:46:44 -080056
Fan Xua7422fe2018-11-19 15:21:32 -080057 void onClientClosed(const BufferClient* client);
58
Fan Xu93c94902018-11-01 12:22:05 -070059private:
Fan Xuefce32e2018-12-12 14:34:16 -080060 // Helper function to build BufferTraits.bufferInfo handle
Tianyu Jiang8adb3192019-01-29 10:54:24 -080061 hidl_handle buildBufferInfo(char* bufferInfoStorage, int bufferId, uint32_t clientBitMask,
62 uint32_t userMetadataSize, int metadataFd, int eventFd);
Fan Xuefce32e2018-12-12 14:34:16 -080063
Fan Xua7422fe2018-11-19 15:21:32 -080064 // Helper function to remove all the token belongs to a specific client.
65 void removeTokenByClient(const BufferClient* client);
66
Fan Xu93c94902018-11-01 12:22:05 -070067 // List of active BufferClient for bookkeeping.
Fan Xucd74d782018-11-26 13:51:25 -080068 std::mutex mClientSetMutex;
69 std::set<wp<BufferClient>> mClientSet GUARDED_BY(mClientSetMutex);
Fan Xu18d90ea2018-11-06 15:46:44 -080070
Fan Xu55b26a62018-12-19 11:03:14 -080071 // 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);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070084};
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