blob: f2c8ef89656d4acfbe2d9feb4c2d1b1a688e60c6 [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 Xu93c94902018-11-01 12:22:05 -070020#include <mutex>
Fan Xu18d90ea2018-11-06 15:46:44 -080021#include <random>
Fan Xu93c94902018-11-01 12:22:05 -070022
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070023#include <android/frameworks/bufferhub/1.0/IBufferHub.h>
Fan Xu93c94902018-11-01 12:22:05 -070024#include <bufferhub/BufferClient.h>
Fan Xu1c16df52018-11-19 16:27:27 -080025#include <bufferhub/BufferHubIdGenerator.h>
Fan Xu93c94902018-11-01 12:22:05 -070026#include <utils/Mutex.h>
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070027
28namespace android {
29namespace frameworks {
30namespace bufferhub {
31namespace V1_0 {
32namespace implementation {
33
Fan Xu18d90ea2018-11-06 15:46:44 -080034using hardware::hidl_handle;
35using hardware::Return;
36using hardware::graphics::common::V1_2::HardwareBufferDescription;
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070037
38class BufferHubService : public IBufferHub {
39public:
Fan Xuca70b7b2018-10-31 13:20:12 -070040 Return<void> allocateBuffer(const HardwareBufferDescription& description,
41 const uint32_t userMetadataSize,
42 allocateBuffer_cb _hidl_cb) override;
Fan Xu467e08f2018-11-09 15:58:51 -080043 Return<void> importBuffer(const hidl_handle& tokenHandle, importBuffer_cb _hidl_cb) override;
Fan Xu93c94902018-11-01 12:22:05 -070044
Fan Xu18d90ea2018-11-06 15:46:44 -080045 // Non-binder functions
46 // Internal help function for IBufferClient::duplicate.
Fan Xud6cd6ba2018-11-15 16:46:55 -080047 hidl_handle registerToken(const wp<BufferClient>& client);
Fan Xu18d90ea2018-11-06 15:46:44 -080048
Fan Xua7422fe2018-11-19 15:21:32 -080049 void onClientClosed(const BufferClient* client);
50
Fan Xu93c94902018-11-01 12:22:05 -070051private:
Fan Xua7422fe2018-11-19 15:21:32 -080052 // Helper function to remove all the token belongs to a specific client.
53 void removeTokenByClient(const BufferClient* client);
54
Fan Xu93c94902018-11-01 12:22:05 -070055 // List of active BufferClient for bookkeeping.
Fan Xucd74d782018-11-26 13:51:25 -080056 std::mutex mClientSetMutex;
57 std::set<wp<BufferClient>> mClientSet GUARDED_BY(mClientSetMutex);
Fan Xu18d90ea2018-11-06 15:46:44 -080058
59 // TODO(b/118180214): use a more secure implementation
60 std::mt19937 mTokenEngine;
61 // The mapping from token to the client creates it.
62 std::mutex mTokenMapMutex;
Fan Xud6cd6ba2018-11-15 16:46:55 -080063 std::map<uint32_t, const wp<BufferClient>> mTokenMap GUARDED_BY(mTokenMapMutex);
Jiwen 'Steve' Caid9f2abe2018-10-20 17:03:13 -070064};
65
66} // namespace implementation
67} // namespace V1_0
68} // namespace bufferhub
69} // namespace frameworks
70} // namespace android
71
72#endif // ANDROID_FRAMEWORKS_BUFFERHUB_V1_0_BUFFER_HUB_SERVICE_H