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