1 /*
2 * Copyright (C) 2022 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 #pragma once
18
19 #include <aidl/android/hardware/media/bufferpool2/BufferStatusMessage.h>
20 #include <bufferpool2/BufferPoolTypes.h>
21
22 #include <map>
23 #include <set>
24
25 namespace aidl::android::hardware::media::bufferpool2::implementation {
26
27 // Helper template methods for handling map of set.
28 template<class T, class U>
insert(std::map<T,std::set<U>> * mapOfSet,T key,U value)29 bool insert(std::map<T, std::set<U>> *mapOfSet, T key, U value) {
30 auto iter = mapOfSet->find(key);
31 if (iter == mapOfSet->end()) {
32 std::set<U> valueSet{value};
33 mapOfSet->insert(std::make_pair(key, valueSet));
34 return true;
35 } else if (iter->second.find(value) == iter->second.end()) {
36 iter->second.insert(value);
37 return true;
38 }
39 return false;
40 }
41
42 // Helper template methods for handling map of set.
43 template<class T, class U>
erase(std::map<T,std::set<U>> * mapOfSet,T key,U value)44 bool erase(std::map<T, std::set<U>> *mapOfSet, T key, U value) {
45 bool ret = false;
46 auto iter = mapOfSet->find(key);
47 if (iter != mapOfSet->end()) {
48 if (iter->second.erase(value) > 0) {
49 ret = true;
50 }
51 if (iter->second.size() == 0) {
52 mapOfSet->erase(iter);
53 }
54 }
55 return ret;
56 }
57
58 // Helper template methods for handling map of set.
59 template<class T, class U>
contains(std::map<T,std::set<U>> * mapOfSet,T key,U value)60 bool contains(std::map<T, std::set<U>> *mapOfSet, T key, U value) {
61 auto iter = mapOfSet->find(key);
62 if (iter != mapOfSet->end()) {
63 auto setIter = iter->second.find(value);
64 return setIter != iter->second.end();
65 }
66 return false;
67 }
68
69 // Buffer data structure for internal BufferPool use.(storage/fetching)
70 struct InternalBuffer {
71 BufferId mId;
72 size_t mOwnerCount;
73 size_t mTransactionCount;
74 const std::shared_ptr<BufferPoolAllocation> mAllocation;
75 const size_t mAllocSize;
76 const std::vector<uint8_t> mConfig;
77 bool mInvalidated;
78
InternalBufferInternalBuffer79 InternalBuffer(
80 BufferId id,
81 const std::shared_ptr<BufferPoolAllocation> &alloc,
82 const size_t allocSize,
83 const std::vector<uint8_t> &allocConfig)
84 : mId(id), mOwnerCount(0), mTransactionCount(0),
85 mAllocation(alloc), mAllocSize(allocSize), mConfig(allocConfig),
86 mInvalidated(false) {}
87
handleInternalBuffer88 const native_handle_t *handle() {
89 return mAllocation->handle();
90 }
91
invalidateInternalBuffer92 void invalidate() {
93 mInvalidated = true;
94 }
95 };
96
97 // Buffer transacion status/message data structure for internal BufferPool use.
98 struct TransactionStatus {
99 TransactionId mId;
100 BufferId mBufferId;
101 ConnectionId mSender;
102 ConnectionId mReceiver;
103 BufferStatus mStatus;
104 int64_t mTimestampMs;
105 bool mSenderValidated;
106
TransactionStatusTransactionStatus107 TransactionStatus(const BufferStatusMessage &message, int64_t timestampMs) {
108 mId = message.transactionId;
109 mBufferId = message.bufferId;
110 mStatus = message.status;
111 mTimestampMs = timestampMs;
112 if (mStatus == BufferStatus::TRANSFER_TO) {
113 mSender = message.connectionId;
114 mReceiver = message.targetConnectionId;
115 mSenderValidated = true;
116 } else {
117 mSender = -1LL;
118 mReceiver = message.connectionId;
119 mSenderValidated = false;
120 }
121 }
122 };
123
124 } // namespace aidl::android::hardware::media::bufferpool2::implementation
125