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 <cutils/native_handle.h>
20 #include <fmq/AidlMessageQueue.h>
21 #include <aidl/android/hardware/media/bufferpool2/BufferStatusMessage.h>
22 #include <aidl/android/hardware/media/bufferpool2/BufferInvalidationMessage.h>
23 #include <aidl/android/hardware/media/bufferpool2/ResultStatus.h>
24
25 namespace aidl::android::hardware::media::bufferpool2 {
26
27 struct BufferPoolData {
28 // For local use, to specify a bufferpool (client connection) for buffers.
29 // Retrieved from returned info of IAccessor#connect(android.hardware.media.bufferpool@2.0).
30 int64_t mConnectionId;
31 // BufferId
32 uint32_t mId;
33
BufferPoolDataBufferPoolData34 BufferPoolData() : mConnectionId(0), mId(0) {}
35
BufferPoolDataBufferPoolData36 BufferPoolData(
37 int64_t connectionId, uint32_t id)
38 : mConnectionId(connectionId), mId(id) {}
39
~BufferPoolDataBufferPoolData40 ~BufferPoolData() {}
41 };
42
43 namespace implementation {
44
45 using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
46 using aidl::android::hardware::common::fmq::UnsynchronizedWrite;
47
48 using aidl::android::hardware::media::bufferpool2::BufferStatusMessage;
49 using aidl::android::hardware::media::bufferpool2::BufferInvalidationMessage;
50
51 typedef uint32_t BufferId;
52 typedef uint64_t TransactionId;
53 typedef int64_t ConnectionId;
54 typedef int32_t BufferPoolStatus;
55
56 // AIDL hal description language does not support unsigned.
ToAidl(BufferId id)57 int32_t static inline ToAidl(BufferId id) {return static_cast<int32_t>(id);}
ToAidl(TransactionId id)58 int64_t static inline ToAidl(TransactionId id) {return static_cast<int64_t>(id);}
59
FromAidl(int32_t id)60 BufferId static inline FromAidl(int32_t id) {return static_cast<BufferId>(id);}
FromAidl(int64_t id)61 TransactionId static inline FromAidl(int64_t id) {return static_cast<TransactionId>(id);}
62
63 enum : ConnectionId {
64 INVALID_CONNECTIONID = 0,
65 };
66
67 typedef ::android::AidlMessageQueue<BufferStatusMessage, SynchronizedReadWrite> BufferStatusQueue;
68 typedef aidl::android::hardware::common::fmq::MQDescriptor<BufferStatusMessage, SynchronizedReadWrite>
69 StatusDescriptor;
70
71 typedef ::android::AidlMessageQueue<BufferInvalidationMessage, UnsynchronizedWrite>
72 BufferInvalidationQueue;
73 typedef aidl::android::hardware::common::fmq::MQDescriptor<BufferInvalidationMessage, UnsynchronizedWrite>
74 InvalidationDescriptor;
75
76 /**
77 * Allocation wrapper class for buffer pool.
78 */
79 struct BufferPoolAllocation {
80 const native_handle_t *mHandle;
81
handleBufferPoolAllocation82 const native_handle_t *handle() {
83 return mHandle;
84 }
85
BufferPoolAllocationBufferPoolAllocation86 BufferPoolAllocation(const native_handle_t *handle) : mHandle(handle) {}
87
~BufferPoolAllocationBufferPoolAllocation88 ~BufferPoolAllocation() {};
89 };
90
91 /**
92 * Allocator wrapper class for buffer pool.
93 */
94 class BufferPoolAllocator {
95 public:
96
97 /**
98 * Allocate an allocation(buffer) for buffer pool.
99 *
100 * @param params allocation parameters
101 * @param alloc created allocation
102 * @param allocSize size of created allocation
103 *
104 * @return OK when an allocation is created successfully.
105 */
106 virtual BufferPoolStatus allocate(
107 const std::vector<uint8_t> ¶ms,
108 std::shared_ptr<BufferPoolAllocation> *alloc,
109 size_t *allocSize) = 0;
110
111 /**
112 * Returns whether allocation parameters of an old allocation are
113 * compatible with new allocation parameters.
114 */
115 virtual bool compatible(const std::vector<uint8_t> &newParams,
116 const std::vector<uint8_t> &oldParams) = 0;
117
118 protected:
119 BufferPoolAllocator() = default;
120
121 virtual ~BufferPoolAllocator() = default;
122 };
123
124 } // namespace implementation
125 } // namespace aidl::android::hareware::media::bufferpool2
126
127