| Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 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 | |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 20 | #include <mutex> |
| Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 21 | #include <random> |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 22 | |
| Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 23 | #include <android/frameworks/bufferhub/1.0/IBufferHub.h> |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 24 | #include <bufferhub/BufferClient.h> |
| Fan Xu | 1c16df5 | 2018-11-19 16:27:27 -0800 | [diff] [blame] | 25 | #include <bufferhub/BufferHubIdGenerator.h> |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 26 | #include <utils/Mutex.h> |
| Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace frameworks { |
| 30 | namespace bufferhub { |
| 31 | namespace V1_0 { |
| 32 | namespace implementation { |
| 33 | |
| Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 34 | using hardware::hidl_handle; |
| 35 | using hardware::Return; |
| 36 | using hardware::graphics::common::V1_2::HardwareBufferDescription; |
| Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 37 | |
| 38 | class BufferHubService : public IBufferHub { |
| 39 | public: |
| Fan Xu | ca70b7b | 2018-10-31 13:20:12 -0700 | [diff] [blame] | 40 | Return<void> allocateBuffer(const HardwareBufferDescription& description, |
| 41 | const uint32_t userMetadataSize, |
| 42 | allocateBuffer_cb _hidl_cb) override; |
| Fan Xu | 467e08f | 2018-11-09 15:58:51 -0800 | [diff] [blame] | 43 | Return<void> importBuffer(const hidl_handle& tokenHandle, importBuffer_cb _hidl_cb) override; |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 44 | |
| Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 45 | // Non-binder functions |
| 46 | // Internal help function for IBufferClient::duplicate. |
| Fan Xu | d6cd6ba | 2018-11-15 16:46:55 -0800 | [diff] [blame] | 47 | hidl_handle registerToken(const wp<BufferClient>& client); |
| Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 48 | |
| Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 49 | void onClientClosed(const BufferClient* client); |
| 50 | |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 51 | private: |
| Fan Xu | a7422fe | 2018-11-19 15:21:32 -0800 | [diff] [blame] | 52 | // Helper function to remove all the token belongs to a specific client. |
| 53 | void removeTokenByClient(const BufferClient* client); |
| 54 | |
| Fan Xu | 93c9490 | 2018-11-01 12:22:05 -0700 | [diff] [blame] | 55 | // List of active BufferClient for bookkeeping. |
| Fan Xu | cd74d78 | 2018-11-26 13:51:25 -0800 | [diff] [blame] | 56 | std::mutex mClientSetMutex; |
| 57 | std::set<wp<BufferClient>> mClientSet GUARDED_BY(mClientSetMutex); |
| Fan Xu | 18d90ea | 2018-11-06 15:46:44 -0800 | [diff] [blame] | 58 | |
| 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 Xu | d6cd6ba | 2018-11-15 16:46:55 -0800 | [diff] [blame] | 63 | std::map<uint32_t, const wp<BufferClient>> mTokenMap GUARDED_BY(mTokenMapMutex); |
| Jiwen 'Steve' Cai | d9f2abe | 2018-10-20 17:03:13 -0700 | [diff] [blame] | 64 | }; |
| 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 |