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 <memory>
20 
21 #include <aidl/android/hardware/media/bufferpool2/BnConnection.h>
22 #include <bufferpool2/BufferPoolTypes.h>
23 
24 namespace aidl::android::hardware::media::bufferpool2::implementation {
25 
26 struct Accessor;
27 
28 struct Connection : public BnConnection {
29     // Methods from ::aidl::android::hardware::media::bufferpool2::IConnection.
30     ::ndk::ScopedAStatus fetch(const std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchInfo>& in_fetchInfos, std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchResult>* _aidl_return) override;
31 
32     // Methods from ::aidl::android::hardware::media::bufferpool2::IConnection.
33     ::ndk::ScopedAStatus sync() override;
34 
35     /**
36      * Invalidates all buffers which are active and/or are ready to be recycled.
37      */
38     BufferPoolStatus flush();
39 
40     /**
41      * Allocates a buffer using the specified parameters. Recycles a buffer if
42      * it is possible. The returned buffer can be transferred to other remote
43      * clients(Connection).
44      *
45      * @param params    allocation parameters.
46      * @param bufferId  Id of the allocated buffer.
47      * @param handle    native handle of the allocated buffer.
48      *
49      * @return OK if a buffer is successfully allocated.
50      *         NO_MEMORY when there is no memory.
51      *         CRITICAL_ERROR otherwise.
52      */
53     BufferPoolStatus allocate(const std::vector<uint8_t> &params,
54                           BufferId *bufferId, const native_handle_t **handle);
55 
56     /**
57      * Processes pending buffer status messages and performs periodic cache cleaning
58      * from bufferpool.
59      *
60      * @param clearCache    if clearCache is true, bufferpool frees all buffers
61      *                      waiting to be recycled.
62      */
63     void cleanUp(bool clearCache);
64 
65     /** Destructs a connection. */
66     ~Connection();
67 
68     /** Creates a connection. */
69     Connection();
70 
71     /**
72      * Initializes with the specified buffer pool and the connection id.
73      * The connection id should be unique in the whole system.
74      *
75      * @param accessor      the specified buffer pool.
76      * @param connectionId  Id.
77      */
78     void initialize(const std::shared_ptr<Accessor> &accessor, ConnectionId connectionId);
79 
80     enum : uint32_t {
81         SYNC_BUFFERID = UINT32_MAX,
82     };
83 
84 private:
85     bool mInitialized;
86     std::shared_ptr<Accessor> mAccessor;
87     ConnectionId mConnectionId;
88 
89     bool fetch(
90         uint64_t transactionId,
91         uint32_t bufferId,
92         std::vector<::aidl::android::hardware::media::bufferpool2::IConnection::FetchResult>
93                 *result);
94 };
95 
96 }  // namespace aidl::android::hardware::media::bufferpool2::implementation
97